The power of find command in Linux – advanced.

Generally whoever uses Linux, would know about the find command. Find the man page here.

There are also lots of blogs, tutorials and other articles on find command on the web, so why write another one. Because it\’s worth every word spent on it 🙂
find is a very powerful command, let\’s see how (options for find command from man page and usage):

depth — Process each directory\’s contents before the directory itself.
maxdepth — Descend at most levels (a non-negative integer) levels of directories below the command line arguments.
xdev — Don\’t descend directories on other filesystems.
executable — Matches files which are executable and directories which are searchable (in a file name resolution sense).
This takes into account access control lists and other permissions artefacts which the -perm test ignores.
iname — Like -name, but the match is case insensitive.
nogroup — No group corresponds to file\’s numeric group ID.
nouser — No user corresponds to file\’s numeric user ID.
fls file — True; like -ls but write to file like -fprint.
ok command — Like -exec but ask the user first (on the standard input);
print0 — True; print the full file name on the standard output, followed by a null character
(instead of the newline character that -print uses).
printf format — True; print format on the standard output, interpreting \’\’ escapes and \’%\’ directives.

There are lot of other options but these are worth mention not used on regular basis. The printf option requires
special mention as the number of options that it supports is quite large and can reallyl help you in the formatting
the data that you get, as well as help you get the data that you want.

Example:
1) Get names of files bigger than 5M

find . +5M -printf \”Name : %20f Size : %s\\n\”

2) delete files (filenames contain spaces or quotes and so on)

find . -name \”name*\” -print0 |xargs -0 rm -f

Updated 28th May: Realinged for issues with post going into the sidebar 🙁

Tip: Using find Command in Linux

Ok this I was planning for quite sometime.. but found this here. Worth reading for people who want to start off using find command.

Doing command-line stuff in Linux is fun. It may be intimidating for some at first, now that we are in the age where GUI is no longer an option. But with CLI, we can do so many things that can be accomplished faster if we know how to utilize the features of a certain command.

One command that is very flexible is find. With find, you can search not only based on filenames, you can also use other identifiers like GUI and UID, timestamps and file types.

Here are some examples of find commands:

This command will find all files in /home directory with .doc as extension and was modified 24 hours ago:

1
find /home -name *.doc -mtime 1

This one will find the same files, but not directories, and delete them using -exec option (great for disk usage maintenance, but BACKUP first!):

1
find /home -name *.doc -type f -mtime 1 -exec rm ’{}’ \\;

You can also find files owned by a certain UID:

1
find /tmp -user johndoe find /tmp -uid 502

Or by GID:

1
find /home/development -gid 1000

You can also search for files and directories with certain permissions:

1
 find . -perm -777

And from those examples, you can build your own command to find what you are looking for.