For When You Can't Have The Real Thing
[ start | index | login ]
start > find


Created by dave. Last edited by dave, 5 years and 178 days ago. Viewed 4,126 times. #10
[diff] [history] [edit] [rdf]

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 all links that point to the same file:

$ 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
no comments | post comment
This is a collection of techical information, much of it learned the hard way. Consider it a lab book or a /info directory. I doubt much of it will be of use to anyone else.

Useful: | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt