| Type: |
Code 
|
| Level: |
Expert
|
| Date: |
2010-Jan-30
|
| Visited: |
904 times
|
| Rating: |

|
| Published: |
Tony Potter |
|
|
This tutorial is dedicated to the
‘find’ command used in Linux/Unix operating systems.
It is really powerful and commonly used command which helps in troubleshooting, finding stuff and as a part of server scripts.
The very common usage, just to find a file or folder on your server is:
find . -name "*STRING*" -print
This will find all files or folders on a particular server with STRING included in the name located into the current folder and all the subfolder.
The next one will search all the files into the current folder and all the subfolder including certain string.
find . -name "*.*" -exec grep 'STRING' {} \; -print
or
find ./ -type f -print | xargs grep -H "STRING" /dev/null
egrep -r STRING *
Why I am giving you two commands?
Take in mind that the find command may be different on a different unix based operating systems, and the syntax may differ!
Let’s see if we want to find all files bigger than 5MB ..
find . -size +5000000c -print
.. or files bigger than 10MB using different find syntax :
find ./ -size +10000k -print0 | xargs -0 ls -lh
To find user files you may search by ‘User ID’ into the systems (this may take time!)
To find all Unix/Linux files modified in the last 5 minutes…
... and if you want them all deleted:
find ./* -mtime +5 -exec rm -f {} \;
Let’s search some directories with certain permissions:
find / -perm -0002 -type d -print
Here is the same for files " find files with same permissions:
find / -perm -0002 -type f -print
or
find / -perm -2 ! -type l -ls
Files without associated user
find / -nouser -o -nogroup -print
Files modified and created last two days:
find / -mtime 2 -o -ctime 2
The next command will show you a list of SUID/SGID of programs in your system.
find / -type f \( -perm -04000 -o -perm -02000 \) \-exec ls -l {} \;
The next ’find’ string will show you how many directories, files and links has a particular folder.
for t in files links directories; do echo `find . -type ${t:0:1} -follow | wc -l` $t; done 2> /dev/null
Note that if you remove the "-follow" ‘find’ parameter it wont search/ trace symlinks (Symbolic links).
With the following command ‘find’ will search in all the files from the current folder recursively for a string (in my example ‘mamma mia!’) and will replace it with another which will be ‘daddy dog’.
(For the special characters you have to use so-called “escape” character )
for file in `grep -Rl " mamma mia!" *`; do sed "s/ mamma mia\!/ daddy dog /g" $file > /tmp/123; cat /tmp/123 > $file; rm /tmp/123; done
It is very, very useful, but be careful how you are using it. It is a must to have backup, before messing all around.
With the next string one you can do a change on every ‘.php’ file " in this case we are going to change the permissions of all files to 644 found inside the current and all of the subfolders.
for z in `find . -type f -name "*.php"`; do chmod 644 $z; done
Hope you liked the 'find' command examples. I am using them very often and even that I am referring to this tutorial for fresh ideas.
Think twice cut ones!