Linux find command cheatsheet

Generic Syntax:

find [path] -[option1] [argument to option1] -[option2] [argument to option2]

etc

Multiple options can be combined by specifying them one after the other

Find by filename

find . -iname [filespec]

-iname: Case insensitive name search

-name: Case sensitive

Find and delete file:

find . -iname [filespec] -delete

There are three ways to delete found files:

  •  -delete option
    • find /path/to/search -delete[/code]
      
  • Using xargs:
    • find /path/to/search | xargs rm[/code]
      
  • With exec
    • find /path/to/search -exec {} \;[/code]
      
Find only in current directory (Specify level of search): find . -iname [filespec] -maxdepth [level] level1: Only in current dir  Find only files/directories: By default, both files and directories are searched. You can specify one or the other with: -type f : Only files -type d: Only directories Find files more than a specific file size, and display its size:
find . -size +200M -exec du -h {} \;

where I have asked it to find all files more than 200MB in size and display sizes. I get a result like this:

351M    ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 4/01.14.13- Gender Bender (21-01-1994)/01.14.13- Gender Bender (21-01-1994).avi
351M    ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 4/01.16.15- Young at Heart (11-02-1994)/01.16.15- Young at Heart (11-02-1994).avi
351M    ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 5/01.19.18- Shapes (01-04-1994)/01.19.18- Shapes (01-04-1994).avi
351M    ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 5/01.18.17- Miracle Man (18-03-1994)/01.18.17- Miracle Man (18-03-1994).avi
351M    ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 5/01.17.16- E.B.E. (18-02-1994)/01.17.16- E.B.E. (18-02-1994).avi
351M    ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 5/01.20.19- Darkness Falls (15-04-1994)/01.20.19- Darkness Falls (15-04-1994).avi

Here, note that I have actually passed on the file name to du, which is responsible for the resulting listing.

Note notes from man on time related options:

-mmin n
File's data was last modified n minutes ago.

-mtime n
File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification
times.

Notes on the exec option:

 -exec command ;

All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell.

Note that there is something similiar to exec: the -ok option

It works just like -exec, except that -ok asks user confirmation before running the command. See below:

[[email protected]] /var/www/virtual/joel.co.in/htdocs/downloaded #find . -size +200M -ok du -h {} \;
< du ... ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 4/01.15.14- Lazarus (04-02-1994)/01.15.14- Lazarus (04-02-1994).avi > ? n
< du ... ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 4/01.13.12- Beyond the Sea (07-01-1994)/01.13.12- Beyond the Sea (07-01-1994).avi > ? y
5.4M    ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 4/01.13.12- Beyond the Sea (07-01-1994)/01.13.12- Beyond the Sea (07-01-1994).avi
< du ... ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 4/01.14.13- Gender Bender (21-01-1994)/01.14.13- Gender Bender (21-01-1994).avi > ? y
351M    ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 4/01.14.13- Gender Bender (21-01-1994)/01.14.13- Gender Bender (21-01-1994).avi
< du ... ./X-Files-Season1-HQ-DVDRIP-624x464/DISC 4/01.16.15- Young at Heart (11-02-1994)/01.16.15- Young at Heart (11-02-1994).avi > ?

File details, with printf:

The printf option accepts a multitude of options and can provide lots of useful info.

Eg: I'm printing the filename and the size in bytes:

$find . -size +200M -printf "%f %s\n"
01.15.14- Lazarus (04-02-1994).avi 367159296
01.13.12- Beyond the Sea (07-01-1994).avi 361488384
01.14.13- Gender Bender (21-01-1994).avi 367157248
01.16.15- Young at Heart (11-02-1994).avi 367153152
01.19.18- Shapes (01-04-1994).avi 367165440
01.18.17- Miracle Man (18-03-1994).avi 367159296
01.17.16- E.B.E. (18-02-1994).avi 367167488
01.20.19- Darkness Falls (15-04-1994).avi 367171584
01.02.01- Deep Throat (17-09-1993).avi 367187968
01.01.79- Pilot (10-09-1993).avi 367173632
01.24.23- The Erlenmeyer Flask (13-05-1994).avi 367163392
01.23.22- Roland (06-05-1994).avi 367169536
01.22.21- Born Again (29-04-1994).avi 367222784
01.21.20- Tooms (22-04-1994).avi 367163392

Note that all arguments to go with print need to be quoted.