Linux Find command tips

Find is a most important command for an admin for his/her daily system administration.

Find command will search recursively search and display the results .

I would like to share/document some of the find command examples that I am using in my day to day use.

Find a file name

# find . -name “admin.txt”
Search for the file admin.txt in present working directory

# find /usr -name “admin.txt”
this command will search for the exact name “admin.txt” in /usr

# find /usr -iname “admin.txt”
this will search for the name admin.txt and ignore the case.
For example this will display Admin.txt and admin.txt

Find a file owned by a specific user

# find  /  -user  admin

Find everything under /var ending with “.log”

# find /var -name \*.log

Find files with respect to the date of creation

# find /backup -mtime +60

Find files with specific permission and owenrship

# find /home -perm 777  -user nobody

Find a files with specified size

# find /home -size 1024k

Find files with zero size  ( empty files )

# find /home -empty

Find files with file type ( ” f  ” for files and ” d”  for directory types )

# find /home -type f -name index
# find /home -type d -name  public

Find files specified with in the dept

touch /passwd

# find /  -maxdepth 3 -name password
it will display the following ( root and two levels down )

Find and write

a) Find and write the result to new file

# find . -iname test -type f > /tmp.txt
# find . -iname test -type f -fprint /tmp.txt

Find and remove

# find /backup -mtime +4  -type d \( -iname “*-2011*” \) -exec rm -rf {} \;

The above will find and delete for all the directories which are older than 4 days and its name contains the string “-2011”

# find . -name ‘*.class’ | xargs /bin/rm -f

This command will remove all the files which are having extenstion .class

Find the 5 biggest files  in /home

find /home -type f -exec ls -s {} \; | sort -n -r | head -5

Find and zip the files

find /var -name \*.log -exec bzip {} \;

Find and change the ownership of files

find /home/admin -user nobody -exec chown admin {} \;

Above command will search for all the files which are under nobody user ownership and change the user ownership to admin.

you can suggest your own and I am happy to include your suggestions to this doc