Friday, 11 April 2014

Basic - File Handling

file

The file command is a used for recognizing the type of data contained in a file.
 # file mon.tar
mon.tar: POSIX tar archive (GNU)

# file sanu/
sanu/: directory

# file file1
file1: ASCII text


- To check the types of all files in a directory.
# file /home/subrat/*
/home/subrat/file1:      ASCII text
/home/subrat/mon.tar:    POSIX tar archive (GNU)
/home/subrat/my.pkt:     ASCII text
/home/subrat/sanu:       directory
/home/subrat/sar.txt:    ASCII text


stat

The stat command is used to display status information of files and file systems.

- To check the status of a file.
# stat file1
  File: `file1'
  Size: 73740           Blocks: 152        IO Block: 4096   regular file
Device: caa1h/51873d    Inode: 5317        Links: 1
Access: (0600/-rw-------)  Uid: (  512/  subrat)   Gid: (  512/  subrat)
Access: 2014-04-11 09:40:37.095008865 +0530
Modify: 2014-04-09 06:42:21.635010321
+0530 
Change: 2014-04-09 06:42:21.635010321 +0530

- To check the status of a directory.
# stat /home/subrat/
  File: `/home/subrat/'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: caa1h/51873d    Inode: 8196        Links: 5
Access: (0700/drwx------)  Uid: (  512/  subrat)   Gid: (  512/  subrat)
Access: 2014-04-11 08:40:32.213008990
+0530
Modify: 2014-04-11 08:40:21.753009285
+0530
Change: 2014-04-11 08:40:21.753009285
+0530

- To check the status of a file system.
# stat -f  /dev/xvdk1
  File: "/dev/xvdk1"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 69065      Free: 69025      Available: 69025
Inodes: Total: 69065      Free: 68440


locate

The locate command is often the simplest and quickest way to find the locations of files and directories. 

- To search a particular file.
# locate sar.txt
/home/subrat/sar.txt
/opt/apps/sar/sar.txt


- To locate the file irrepsective of its name.
# locate -i new.txt
/tmp/NEW.txt
/tmp/new.txt
/usr/share/doc/samba-common/WHATSNEW.txt.gz 


- To get the count of number of matching entry.
# locate -c sar.txt


Note: One limitation of the ‘locate’ command is its dependency on the database which can be updated by another utility ‘updatedb’. Hence, in order to get the latest and reliable results from ‘locate’ command the database on which it works should be updated at regular intervals.

updatedb

The updatedb command is used to refresh mlocate Database.
# locate out.mp

I have created some files with the name 'out.mp'. But I am not able to  find. So, once run 'updatedb', then tried searching.
 

# updatedb
# locate out.mp
/home/subrat/out.mp
/home/subrat/sanu/out.mp
/home/subrat/sanu/out.mp.bak


which

The which command is used to locate executable in the system. It returns the absolute path of the executable that is called. It allows user to pass several command names as arguments to get their paths in the system. “which” commands searches the path of executable in system paths set in $PATH environment variable.

Syntax: which command | program

# which java
/usr/bin/java

You can search for multiple commands or programs also as below.
# which echo telnet chmod java
/bin/echo
/usr/bin/telnet
/bin/chmod
/usr/bin/java


whereis

The whereis command is used to find the source, binary, and manuals sections for specified commands or files.

# whereis find
find: /bin/find /usr/bin/find /usr/share/man/man1p/find.1p.gz /usr/share/man/man1/find.1.gz


- To search only binary files.
# whereis -b find
find: /bin/find /usr/bin/find


- To search only mannual sections files.
# whereis -m find
find: /usr/share/man/man1p/find.1p.gz /usr/share/man/man1/find.1.gz


- To search only source code files.
# whereis -s date
find:


- To search for multiple commands.
# whereis date grep
date: /bin/date /usr/share/man/man1p/date.1p.gz /usr/share/man/man1/date.1.gz
grep: /bin/grep /usr/share/man/man1p/grep.1p.gz /usr/share/man/man1/grep.1.gz


whatis

The whatis command is used to display a single line information about a command. It accomplishes this by searching the short descriptions in the whatis database for each keyword provided to it as an argument.

# whatis ls
ls                   (1)  - list directory contents
ls                   (1p)  - list directory contents

# whatis find
find                 (1p)  - find files
find                 (1)  - search for files in a directory hierarchy



No comments:

Post a Comment