Find and

Find and compress

Find all .java files and compress it into java.tar

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

Find all .csv files and compress it into quickref.zip

$ find . -type f -name "\*.csv" | xargs zip quickref.zip

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 sort

Find and sort in ascending

$ find . -type f | sort

find and sort descending

$ find . -type f | sort -r

Find and concatenate

Merge all csv files in the download directory into merged.csv

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

Merge all sorted csv files in the download directory into merged.csv

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

Find and copy

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

Find matching files and copy to a specific directory (/tmp/backup)

Find and move

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

Find and move it to a specific directory (/tmp/music)

Find and rename

Find and suffix (added .bak)

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

Find and rename extension (.html => .gohtml)

$ find ./ -depth -name "\*.html" -exec sh -c 'mv "$1" "${1%.html}.gohtml"' \_ {} \;

Find and replace

Find all files and modify the content const to let

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

Find readable and writable files and modify the content old to new

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

See also: sed cheatsheet

Find and delete

Find and remove multiple files

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

Find and remove single file

$ find . -type f -name "tecmint.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 {} \;
Comments