Home > Support > HOWTO List > Using find and xargs

Using find and xargs

find and xargs are two separate commands that you will often seen used together.

find figures out a set of files matching criteria that you pass to it (e.g. filenames, directories vs. files, programs vs. data files, files older than/newer than a certain age).

xargs takes a list of files and lets you run a command on that file.  e.g. grep or rm.

Some examples...

Delete old files

You can cleanup 'old' files (e.g. files that have not been modified in say, 5 days) using the find command:

find /path/to/dir -mtime +5 -delete

Please BE CAREFUL when you run this command, it will not prompt you for permission to delete.

The following code achieves the same result:

find /path/to/dir -mtime +5 | xargs /bin/rm --force

Grep for some text in all files under /etc

e.g. find under /etc searching down up to two subdirectories deep for files (not directories) then grep for the text 'postfix' and list the matching filenames:

 find /etc/ -maxdepth 2 -type f | grep --files-with-matches postfix

Find options

The *time options are in days.  -atime +5 finds anything last accessed 5 or more days ago, -atime -5 find anything accessed in 5 or less days, -atime 5 finds anything accessed 5 days ago.

See also man find.