find and xargs

Some working example on how to use find together with xargs

find syntax:

find [options] <where> <what>

find -L . -name *17a69*

find in local directory (.) and subdirectories (also follows symbolic links due to option -L) all files that contains 17a69 in the name

find . -cmin -30

find all files in current directory that changed status in the last 30 minutes. Use -amin or -mmin to find those that where accessed or modified in the last 30 minutes. Use -ctime -atime -mtime to specify days.

find . -cmin +30

same as above, but looks for files created/accessed/modified MORE than 30 minutes ago


xargs let you apply a comand to a list of arguments found by, for instance, find

find . -name interm -type d | xargs rm -r

find all directory that match “interm” and delete them

find . -mmin +100 -type f | xargs -I xxx mv xxx ~/tmp1/

finds all files (-type f) modified more than 100 minutes ago and then move them to ~/tmp1
-I xxx tell xargs to put the arguments where the next occurrence of xxx is

Some links

Unix Find Command Tutorial
Some examples of using UNIX find command

One Comment

  1. Hi Stefano,
    When using find in combination with xargs, there it may be some errors occurring when there are files containing spaces or newlines.
    To avoid this, you may want to use the -print0 option of find, and the -0 option of xargs:

    find . -inmae “adsad” -print0 | xargs -0 echo

Leave a Reply

Your email address will not be published. Required fields are marked *