Wednesday, September 17, 2008

Find Command : A Swiss Army Knife

Find is a very useful and powerful utility. It is often used by system administration and in shell scripts. Here are 6 commands that might be useful:

$ find / -perm -4000 -print

This command will find every file on the system that is suid. This means that when you run it you will be running it as an other user. For example, traceroute is a utility that needs to be run as root. To allow users to run it, systems administrators will set it suid root so it will be run as root even if a user starts it. This can be useful, but can also be a big security risk if the utility has a security hole in it.

Here is another interesting command:

$ find / -atime +10 -print

This command will find all the files accessed more than 10 days ago. Commands like this one can be useful to find old files that need to be backuped or erased.

Find links that point to nothing

$ find / -type l -print | perl -nle '-e || print';

List zero-length files

$ find . -empty -exec ls {} \;

Finding files which contains a given pattern:

$ find . -type f -exec grep "pattern" /dev/null {} \;

Finding large files (>= 100 MB) in your home directory:

$find ~/ -size +100M

No comments: