Find

From LQWiki
Jump to navigation Jump to search

find is a command which allows primarily for the searching of files in a directory hierarchy. It allows for versatile search criteria, for formatted output, and for custom commands to be run on search-results

Examples

List files fully qualified

List all files fully qualified (e.g. /etc/services instead of services)

tweedleburg:/ # find
.
./.vmware
./srv
./srv/ftp
...

Disregard upper/lower case

To disregard upper/lower cases in filenames use iname instead of name (i like case insensitive). For example, to find all files named myClass.h, MyClass.h and so on:

find -iname myClass.h

Delete files when found

If one wants to find and delete all old backup files from your vi or emacs edit sessions, but save directories that happen to end in a tilde

As root:

find / -type f -name "*~" -exec rm {} \;

Search files by content

If you wanted to perform a simple recursive grep:

find . -exec grep -H "searchtext" {} \;

Find all non-empty files:

find -not -empty

Using xargs

Find also works well with xargs. Using xargs minimizes the number of times that grep or rm is run, speeding things up. The last commands could have been:

find / -type f -name "*~" | xargs rm
find | xargs grep "searchtext"

From the current dir down

find . -type f -ctime -3 -exec ls -l {} \;

for the last 72 hours

find . -daystart -type f -ctime -3 -exec ls -l {} \;

Count the rows in all files

To count the rows of all your files in the current directory use

find | xargs cat  | wc -l

External links