Monday, March 15, 2010

Find command in detail

Find command
Commonly used operators or options:

-atime n

Find files that were accessed n days ago. +n finds files accessed greater than n days ago and -n will find files accessed less than n days ago.

eg: We all have old files on our systems and in our home directories that have not been accessed in a long time. To find files in a home directory that have not been accessed in the last 200 days, you would issue the following command:

# find . -atime +200 -print

-ctime n

Find files in which the inode was modified n days ago. +n finds files in which the inode was modified greater than n days ago and -n will find files in which the inode modified less than n days ago.

-exec command

Execute command.

-group name

Find files belonging to the given group name where name can also be the group ID number.

-mount

Do not descend directories on other file systems (not available on all UNIX variants.)

-mtime n

Find files that were modified n days ago. +n finds files modified greater than n days ago and -n will find files modified less than n days ago.

-newer file

File was modified more recently than file.

eg: to find and remove all newer modified files
#find . ! -newer "almdmst001_30062009234031.htm" -exec rm -rf {} \;

-name pattern

Look for file name of pattern.

-ok command

Check to see that it is okay to execute command before doing so. This is similar to -exec except that you are prompted for permission.

-perm mode

Find files with the specified access mode. You would supply the access mode in octal.

-print

Print the current file name to standard output.

-type t

File has a type of t, such as d for directory and f for file.

eg:You may want to perform a find operation to produce a list of files only and not include directories in the operation. The following find is similar to what we performed earlier, but this time it produces a list of files only. This is achieved by specifying that we are looking for type f for files:

# find /home -type f -print

-size n

Find files with a size of n blocks. A block is usually 512 bytes. Using +n will find files greater than n blocks, nc will find files n characters in size and +nc will find files greater than n characters in size.

eg:we can search for all files on the system greater than 500,000 characters in size with the find command below:

# find / -size +500000c -print

-user uname

File is owned by uname.

Multiple Criteria:

-a to and two operators (operator1 -a operator2)

-o to or two operators (operator1 -o operator2)

! to specify that the operator not be matched (!operator)

\( expression )\ to specify that this expression be evaluated before others to specify preference.

No comments:

Post a Comment