User Tools

Site Tools


linux:find

This is an old revision of the document!


find examples

Find and extract all rar files in subfolders into the current folder:

find . -name *.rar -exec unrar x {} \;

Find duplicate files:

find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD

Find duplicate filenames:

find . -mindepth 1 -printf '%h %f\n' | sort -t ' ' -k 2,2 | uniq -f 1 --all-repeated=separate | tr ' ' '/'

Find files older than 7 years:

find . -mtime +2555 -print

Find directories containing files older than 30 days:

find . -type f -mtime +30 -printf '%h\n' | sort | uniq > old.txt

Find directories containing ONLY files older than 30 days (not working with subdirs properly):

find . -type f -mtime +30 -printf '%h\n' | sort | uniq > old.txt
find . -type f -mtime -30 -printf '%h\n' | sort | uniq > new.txt
grep -vf new.txt old.txt

Remove all old temporary MS Office lock files:

find . -type f -mtime +10 -name "~*" -print0 | xargs -r0 rm --

Show top level folders and full file count and file count>7years:

find . -maxdepth 1 -type d -print0 -exec echo -n " " \; -exec sh -c "find '{}' -type f | wc -l | tr -d '\n'" \; -exec echo -n " " \; -exec sh -c "find '{}' -type f -mtime +2555 | wc -l" \;

Convert tabs to spaces at the beginning of lines in source code:

find . -iname '*.php' -type f -exec bash -c 'expand -i -t 4 "$0" > /tmp/e && mv /tmp/e "$0"' {} \;

Better with 'sponge' from the moreutils package as it retains permissions:

find . -iname '*.php' -type f -exec bash -c 'expand -i -t 4 "$0" | sponge "$0"' {} \;

Fixing permissions of directories / files of a path:

find /path/to/base/dir -type d -print0 | xargs -0 chmod 755 
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

Recursive file renaming (e.g. from “something [xxxxp].ext” to “something.xxxxp.ext”) :

#NOTE: use -n in rename parameter to test!
find . -type f -name "*\[*" -print0 | xargs -r0 rename 's/(.*) \[(\d\d\d\d?p)\](.*)/$1.$2$3/' --
linux/find.1593302877.txt.gz · Last modified: 2023/05/29 11:53 (external edit)