The power of find command in Linux – advanced.

2010-05-24 350 words 2 mins read

Generally whoever uses Linux, would know about the find command. Find the man page <a href="http://amit.themafia.info/phpMan.php?parameter=find&mode=man" target="_blank">here.

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

–depth β€” Process each directory&#8217;s contents before the directory itself.
–maxdepth β€” Descend at most <span style="text-decoration: underline;">levels (a non-negative integer) levels of directories below the command line arguments.
–xdev β€” Don&#8217;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&#8217;s numeric group ID.
–nouser β€” No user corresponds to file&#8217;s numeric user ID.
–fls <span style="text-decoration: underline;">file β€” True; like -ls but write to <span style="text-decoration: underline;">file like -fprint.
–ok <span style="text-decoration: underline;">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 <span style="text-decoration: underline;">format β€” True; print <span style="text-decoration: underline;">format on the standard output, interpreting &#8217;&#8217; escapes and &#8217;%&#8217; 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 &#8221;Name : %20f Size : %s\n&#8221;

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

find . -name &#8221;name*&#8221; -print0 |xargs -0 rm -f

Updated 28th May: Realinged for issues with post going into the sidebar πŸ™


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it