23 March 2012

find Command in Linux/Unix

3 comments


find Command in Linux/Unix
find command recursively examines a directory to look for files that matches some criteria and takes some action on the the selected files.


Syntax


find path_list selection_criteria action


1. path_list : It comprises one or more sub-directories separated by white-space.
2. selection_criteria : Use to match a file.
3. action : appropriate action.

Example

Let's find all files named new.java

$ find / -name new.java -print

Here
 /   path_list
-name selection_criteria
-print  action

Major Options Used by find command


Selction CriteriaMeaning
-inum n Having inode number n

Example


find  /  -inum  23456  -print (select file having inode number 23456)
-type xselect files depending upon their type , x can be f (ordinary file) d(directory) etc.

Example

find . -type d -print (select all directories of home folder)
-perm nnn If octal permission match nnn completely

Example


find $HOME -perm 777 -type d -print (select all directories having permission 777 in your home directory)
-mtime -x If modified in less than x days

Example


find $HOME -mtime -2 -print (select all files that have been modified in less than 2 days)
-mmin -x If modified in less than x minutes

Example


find $HOME -mmin -2 -print (select all files that have been modified in less than 2 minutes)
-amin +x If accessed in more than x minutes

Example


find $HOME -amin +2 -print (select all files that have been accessed in more than 2 minutes)
-atime +x If acessed in more than x days

Example


find $HOME -atime +365 -print (select all files that have been accessed in more than a year)
-name fname select files having fname as its name

Example


find $HOME -name list -print (select all list files in home directory)
-iname fname same as above but match is case-insensitive

Example


find $HOME -iname list -print (select all list,LIST , List , list etc files in home directory)

Operators Used by find Command

1. ! : Negation

Example

find all files except java files in your home directory

$ find $HOME ! -name "*.java" -print

2. -a : AND condition

Example

find all files java and shell script files

$ find $HOME \(-name "*.sh" -a -name "*.java" \) -print

3. -o : OR condition

Example

find all files java or shell script files

$ find $HOME \(-name "*.sh" -o -name "*.java" \) -print


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

3 comments:

  1. $ find $HOME \(-name "*.sh" -a -name "*.java" \) -print

    and

    $ find $HOME \(-name "*.sh" -o -name "*.java" \) -print

    not working in kubuntu

    I am getting error for 'or' operation:

    find: invalid expression; you have used a binary operator '-o' with nothing before it.

    ReplyDelete
  2. the && and || replacement would not work in this case - the problem is that a space needs to go between the \( and the -name.
    find $HOME \( -name "*.sh" -o -name "*.java" \) -print
    not
    find $HOME \(-name "*.sh" -o -name "*.java" \) -print

    ReplyDelete