Tip: Using find Command in Linux

Author Rai    Category Linux     Tags , , , , ,

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:

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!):

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

You can also find files owned by a certain UID:

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

Or by GID:

find /home/development -gid 1000

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

find . -perm -777

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

Related Posts

7 Comments to “Tip: Using find Command in Linux”

  • anomie November 19, 2008 at 12:10 pm

    It’d be a good idea to single quote your wildcard so that your shell does not expand it before the search.

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

  • a guest November 19, 2008 at 3:49 pm

    as regards deleting files, i’d suggest to pipe find’s results to xargs for efficiency reasons, e.g.:

    find … -print0 | xargs -0 rm

    this results in a single call to rm, instead of one call per file found.

  • jklowden November 20, 2008 at 12:32 am

    Quoting {} is unnecessary:

    $ touch "a b"
    $ find . -name a\*b
    ./a b
    $ find . -name a\*b -exec ls {} \;
    ./a b
    $ find . -name a\*b -exec rm {} \;
    $ find . -name a\*b

  • Peter Whittaker November 20, 2008 at 2:04 am

    For finding files older than a certain time, you might want to use relative time, e.g.,

    $ find /home -mtime +1

    The commands provided only find files modified 24 hours ago; this example finds files modified 24 hours or more ago. Likewise,

    $ find /home -mtime 1

    finds files modified about 24 hours ago, while

    $ find /home -mtime 0


    $ find /home -mtime -1

    both find files modified less than 24 hours ago, but don’t find quite the same files, due to slight rounding differences when find calculates the time period.

    One of the first changes I used to make any Unix system I inherited was to replace the “rm -rf /tmp/*” command found in startup scripts with

    find /tmp -mtime +7 -exec /bin/rm -rf {} \;

    which deletes all files/directories unmmodified in more than 7 days. Why? Because sometimes /tmp contains core and log files useful to figuring out why your system crashed – and having “rm -rf /tmp/*” in startup scripts cleaned them out.

  • Amit Agarwal November 21, 2008 at 12:52 am

    find . -empty -delete

    I like this one and other thing I like is that mostly all -exec rm -rf {} \; can be replaced with -delete

  • maldito November 21, 2008 at 3:51 pm

    i prefer “-iname” over “-name” to make it case insensitive.

  • Dennis quek December 2, 2008 at 6:45 pm

    I kept having problems with the stupid error msg saying find: paths must precede expressions.

Post comment

CommentLuv Enabled

Powered by 1and1.comDomain Registrations starting at $9.98* Earn with Your BlogAdvertise @ PinoyTux

Search PinoyTux

Subscribe to Email Feeds

Enter Email Address:

Blog Lounge

Popular Posts

Recent Posts

Recent Comments

Site Stats