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 examples to help you understand how to use the find command.
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
Related:
20 Advanced Linux Find Command Examples
How to use Find Command in Linux