With the Unix commands find and xargs you have the ability to find any files on your hard drive while being able to manipulate them individually. Here are few examples:

find ./ -type f -name "*.php"  | xargs -r rename "s/php/html/"

Find all files (-type f) with extension .php -name "*.php" and replace php by html in their names (rename "s/php/html/"). The option -r for xargs prevents xargs to execute the following command if the ouput provided by find is empty.

find ./ -type f -name *".html" | xargs sed -i "s/php/html/g"

Find all file (-type f) with extension .html (-name "*.html") and replace all occurences of php by html inside these files.

find ./work/ -type f -name "*.sh" -mtime -20 | xargs -r ls -l

Find all files (-type f) in the ./work/ directory with extension .sh (-name *.sh) and modified less than 20 days ago and print details of these files (ls -l).

find ./ -size +5M -type f | xargs -r ls -Ssh

Find all files (-type f) with size bigger than 5 Mb (-size +5M) and sort them by size (ls -Ssh)

find ./work/ -type f -name "*.pdf" -mtime +5 -size +2M  | xargs -r cp -t ./backup/

Find all .pdf files (-type f -name "*.pdf") from the ./work/ directory with size bigger then 2 Mb (-size +2M) and modified more than 5 days ago and copy them into the ./backup/ directory.


Comments

comments powered by Disqus