Find files (not dirs) but skip /proc, /dev and so on

find / -type f -mount
(the -mount command is the key - it prevents descending down into other mounted filesystems. Otherwise, I can't find a way to filter out /proc files)

Find files < 1 day old, skipping certain directories, skipping some kinds of files

find / -path /var/spool -prune -o -path /var/cache -prune -o \( -mount -type f -mtime -1 -not -iregex \.\*rpm -not -iregex \.\*hdr -print \)

I think you read this like so: the -path ./var/spool -prune is one clause, the rest is another. We or them together. Also note the spaces around the perens.

Take files found by find and tar them up

find tmp/ -type f | cpio -H ustar -o -O blah.tar
-H tar tells it to use the 'old' tar format

or, to output as a BZ2, do ... | cpio -H ustar -o | bzip2 > blah.tar.bz2

see this for more on cpio

Sort all files by change time in all directories under this one

find . -type f -printf "%C@ - %h/%f\n" | sort

Find all files ending with multiple, case-insensitive extensions

du -c `find . -iregex '.*\.\(gif\|jpg\|jpeg\|bmp\|png\),v'`

Find all files with SUID and SGID bits set

find / \( -perm -4000 -o -perm -2000 \) -type f

( -o for 'or', () to force precedence - not actually needed in this example.)

Here's a case where we allow the sticky bit using -not

find / -type d \( -perm -g+w -o -perm -o+w \) -not -perm -a+t -exec ls -lad {} \;

Find cpu_khz in all header files

find . -name \*.h -exec grep -Hn cpu_khz {} \;

Find all .c files modified more recently than Makefile

find . -name '*.c' -newer Makefile

Find all .java files that haven't changed in the last week

find . -name '*.java' -mtime +7

Find all files that have changed more recently than 10 minutes ago

find . -mmin -10

How many image files do I have?

find . -iregex '.*\.\(gif\|jpg\|jpeg\|bmp\|png\),v' | wc -l

Find image files that are larger than 500k

find . -iregex '.*\.\(gif\|jpg\|jpeg\|bmp\|png\)'  -size +500k -exec ls -s {} \;

Find all image files except those that have thumbs in them (like a thumbs folder) and compute the total.

find . -iregex '.*\.\(gif\|jpg\|jpeg\|bmp\|png\)' -exec ls -s {} \; | grep -v thumbs | awk '{total = total + $1} END {print total}

How do I search for files, then do something with the result?

  • Find all .conf files, then search through them to see if they contain any IP addresses, then print the filename, line number and the line containing the address

    find /etc -name *.conf -exec grep -Hn '[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*' {} \;

  • Find all files modified 10 minutes ago or less, then show a long directory listing (trick: not showing the directories using -type f to only show files)

    find . -mmin -10 -type f -exec ls -lt {} \;

  • Find all .mp3 files and play them with mpg123 (note, -@ - means look for a playlist from stdin, and -z means shuffle)

    find . -name "*.mp3" -type f | mpg123 -z -@ -

Dealing with spaces

find . -type f -name "*.txt" -print0 | xargs -0 (command)

-- MattWalsh - 02 Aug 2004

Topic revision: r6 - 17 Jan 2007 - 21:56:54 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback