Skip to main content

Find Tricks

Excluding stuff (say, the .snapshot directory of a find)

$ find $dir -name .snapshot -prune -o -type f

Dealing with names with spaces in them:

$ IFS='
'; for f in $(find . -name "*mp3"); do echo command-mp3-2-ogg "$f"; done
command-mp3-2-ogg ./How Long.mp3
command-mp3-2-ogg ./Do Something Beautiful.mp3
command-mp3-2-ogg ./There is a hope so sure.mp3
command-mp3-2-ogg ./Oh I was made for this.mp3
command-mp3-2-ogg ./Blessed are the humble.mp3
command-mp3-2-ogg ./Earth Lies Spellbound.mp3
$

Or how about:

$ find . -print0 | xargs -0 grep -H term

This approach uses null characters instead of newlines to separate the results. This means things won't go screwy if there are files with white space or newlines in the file names, e.g. files that have come from Windows or are on a Windows partition.

Simple exec:

$ find . -type f -exec md5sum {} \; > inventory.restored
$ find -L / -samefile /path/to/your/file

Find all files owned by $USER in the current tree on the current file system, ignoring the .snapshot subtree(s); print out file sizes and names:

$ find * -xdev -name .snapshot -prune -o -type f -user $USER -printf '%s %p\n'

Sort results of find by age (because, say, you want to delete the oldest file):

find $DIR -type f -printf '%Tst%pn' | sort -nr | cut -f2

List all directories in current directory, exclude simbolic links:

find . -maxdepth 1 -type d

(same as dir -d .)