Advanced Find exec xargs Examples

Updated: Feb 23

Find command is a very useful command for Linux admins. We can use it to handle multiple tasks with the combination of other Linux commands like rm sed awk tar etc. We collect the following commands to see how powerful find command is.


Find and remove multiple files

$ find . -type f -name "*.mp3" -exec rm -f {} \;


Find and remove single file

$ find . -type f -name "howtouselinux.txt" -exec rm -f {} \;


Find and delete 100mb files

$ find / -type f -size +100m -exec rm -f {} \;


Find specific files and delete

$ find / -type f -name *.mp3 -size +10m -exec rm {} \;



Find and replace

$ find ./ -type f -exec sed -i 's/find/replace/g' {} \;

$ find ./ -type f -readable -writable -exec sed -i "s/old/new/g" {} \;


Find and rename

$ find . -type f -name 'file*' -exec mv {} {}_renamed \;

$ find . -type f -name 'file*' -exec sh -c 'x="{}"; mv "$x" "${x}.bak"' \;


Find and move

find . -name '*.mp3' -exec mv {} /tmp/music \;

Find and move it to a specific directory


Find and copy

$ find . -name '*2020*.xml' -exec cp -r "{}" /tmp/backup \;


Find and concatenate

$ find download -type f -iname '*.csv' | xargs cat > merged.csv

$ find download -type f -name '*.gz' -exec cat {} \; > output


Find and sort

$ find . -printf "%T+\t%p\n" | sort

$ find . -printf "%T+\t%p\n" | sort -r


Find and chmod

Find files and set permissions to 644.

$ find / -type f -perm 0777 -print -exec chmod 644 {} \;


Find directories and set permissions to 755.

$ find / -type d -perm 777 -print -exec chmod 755 {} \;


Find and tar

$ find . -type f -name "*.java" | xargs tar cvf myfile.tar

$ find . -type f -name "*.java" | xargs tar rvf myfile.tar



40 views

Join our mail group. Get a free Linux account on Cloud.

Never miss a post!

Want a free Linux account?  This account can be used to login to our cloud server and practice Linux commands.

 
kamateravps.gif
topbackgroud.png

✔ Linux Commands   ✔ Linux Skills    ✔ LinuxPerformance   ✔ Linux Interview

Some articles are from the public internet. If you find your article misused or undesired here and you don't want us to display it, please let us know and we'll remove it immediately.