Fun with Find

Find is a really useful tool. I must use it many times every day.

Here are some useful examples:

Finding all files matching

find all directories in current folder

$ find . -type d

find all files:

$ find . -type f

Find all .php files

$ find . -type f -name "*.php"

Find all .php files updated in the last 10 minutes

$ find . -type f -name "*.php" -cmin -10

Find all files that were updated more than 10 minutes ago

$ find . -type f -name "*.php" -cmin +10

What did I change ?

If you are about to update a number of files, and don’t want to have to log all the files that you have changed:

$ touch TIMESTAMP

[ make various changes to files]

List changed files

$ find . -newer TIMESTAMP

Create a tar file of changed files

$ tar cf myfile.tar `find . -newer TIMESTAMP`

Removing files

Sometimes you can get a folder that has too many files to delete with rm normally. This often seems to happen for session files.

Remove all files in the current folder:

$ find . -type f -print0 | xargs -r -0 rm -f

Purge files not changed for over 30 minutes from this folder: (good for session files)

$ find . -type f -cmin +30 -print0 | xargs -r -0 rm -f

Changing permissions

Make all directories searchable by everyone

$ find . -type d -exec chmod a+x {}

Find files containing

Find all files containing  foobar

$ find . -type f -exec grep -l foobar {} \;

Show matched lines from all files containing foobar

$ find . -type f -exec grep foobar {} /dev/null \;

 

Date: March 5, 2015

Category: Linux Admin Tips

Author: John Taylor

Respond To This Post: