Useful Linux find Commands


General

Find all files and directories starting from the current directory (including all subdirectories).

find .

Find all files starting from the current diretory (including all subdirectories).

find . -type f

Find all directories starting from the current directory (including all subdirectories).

find . -type d

Find all hidden files starting from the current directory (including all subdirectories).

find . -type f -name ".*"

Find all hidden directories starting from the current directory (including all subdirectories).

find . -type d -name ".*"

Find all files and directories starting from the current directory (including all subdirectories), and exclude hidden files.

$ find . -name '[!.]*'
find . \( ! -regex '.*/\..*' \) | sed 's/^..//'

Find all files and directories starting from the current directory (including all subdirectories) that have the name linkin in it (case insensitive).

  • -iname pattern
    • Like -name, but the match is case insensitive.
find . -iname *linkin*

Find all files and directories starting from the current directory (including all subdirectories) that have the name “linkin park” in it (case insensitive).

find . -iname "*linkin park*"

Find all files and directories starting from the current directory (including all subdirectories) that START with the words “Linkin Park” (case sensitive).

find . -name "Linkin Park*"

Find all files and directories starting from the current directory (including all subdirectories) that END with the words “Linkin Park” (case sensitive).

find . -name "*Linkin Park"

Find all .mp3 files starting from the current directory (including all subdirectories) (case insensitive).

find . -type f -iname *.mp3

Find all .mp3 files starting from the current directory (including all subdirectories) and delete them (case insensitive).

find . -type f -iname *.mp3 -delete

Find all files starting from the current directory (including all subdirectories) ending with .zip which are owned by the user tommye (case insensitive).

find . -type f -user tommye -iname "*.zip"

Find all files starting from the current directory (including all subdirectories) ending with .bak and delete them (case insensitive).

find . -iname "*.bak" -type f -print | xargs /bin/rm -f

Find all .jpg files within the current directory and archive them using tar to a file called jpeg.tgz (case insensitive).

find . -maxdepth 1 -iname "*.jpg" | xargs tar -czvf jpeg.tgz

Find all the files within the current directory with the words “linkin park” in it (case insensitive).

find . -maxdepth 1 -iname "*linkin park*"

Find all files starting from the current directory (including all subdirectories) which are owned by the user tommye.

find . -user tommye

Find all symbolic links starting from the current directory (including all subdirectories) and list the permissions.

find . -type l -ls

Find all files starting from root / that have permissions 777.

find / -type f -perm 0777

Find all directories starting from root / that have permissions 777.

find / -type d -perm 0777

Find a string inside a filename starting from the current directory (including all subdirectories).

Note: xargs can be used to process input from another command.

find . -type f -print | xargs grep -ni "STRING"
  • -n, –line-number
  • -i, –ignore-case

Find Files by Size

Find all files starting from the current directory (including all subdirectories) which are larger than the given size.

find . -size +100M

Find all files starting from the current directory (including all subdirectories) which are smaller than the given size.

find . -size -100M

Find all files starting from the current directory (including all subdirectories) which match the given size.

find . -size 100M

Empty Files & Directories

Find all empty files starting from the current directory (including all subdirectories).

find . -type f -empty

Find all empty directories starting from the current directory (including all subdirectories).

find . -type d -empty

Find all empty files starting from the current directory (including all subdirectories) and delete them.

$ find . -type f -empty -delete
find . -type f -empty -print0 | xargs -0 -I {} /bin/rm "{}"

Find all empty directories starting from the current directory (including all subdirectories) and delete them.

$ find . -type d -empty -delete
find . -type d -empty -print0 | xargs -0 -I {} /bin/rmdir "{}"

Find all empty files within the current directory and delete them.

$ find . -maxdepth 1 -type f -empty -delete
find . -type f -maxdepth 1 -empty -print0 | xargs -0 -I {} /bin/rm "{}"

Find all empty directories within the current directory and delete them.

$ find . -type d -maxdepth 1 -empty -delete<
find . -type d -maxdepth 1 -empty -print0 | xargs -0 -I {} /bin/rmdir "{}"
  • -empty : Only find empty files and make sure it is a regular file or a directory.
  • -type d : Only match directories.
  • -type f : Only match files.
  • -delete : Delete files. Always put -delete option at the end of find command as find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.

mtime, ctime, atime and amin

“Unix keeps 3 timestamps for each file: mtime, ctime, and atime. Most people seem to understand atime (access time), as when the file was last read. There does seem to be some confusion between mtime and ctime though. ctime is the inode change time while mtime is the file modification time. “Change” and “modification” are pretty much synonymous. There is no clue to be had by pondering those words. Instead you need to focus on what is being changed. mtime changes when you write to the file. It is the age of the data in the file. Whenever mtime changes, so does ctime. But ctime changes a few extra times. For example, it will change if you change the owner or the permissions on the file.” —Source

  • -mtime n //modified n days ago
  • -ctime n //change n days ago
  • -atime n //accessed n days ago
  • -amin n //accessed n minuties ago
  • +n //More than ‘n’ days/minuties ago
  • -n //Less than ‘n’ days/minuties ago
  • n //Exactly ‘n’ days/minuties ago
$ find . type -f -mtime +2
find . type -f -mtime -2
find . type -f -mtime 2
find . type -f -ctime +2
find . type -f -ctime -2
find . type -f -ctime 2
find . type -f -atime +2
find . type -f -atime -2
find . type -f -atime 2
find . type -f -amin +2
find . type -f -amin -2
find . type -f -amin 2

Note: +0 is supposed to work and would mean files more than 0 days old.

IMPORTANT: find’s concept of a “day” has NOTHING TO DO WITH MIDNIGHT. It works in 24 hour increments. 1 = 24 hours.

Find all files and directories that were modified within the last 48 hours.

find . -mtime -2

Delete all files older than X days starting from the current directory (including all subdirectories).

find . -type f -mtime +X -delete

Delete all directories older than X days starting from the current directory (including all subdirectories).

find . -type d -mtime +X -delete

Delete all files newer than X days starting from the current directory (including all subdirectories).

find . -type f -mtime -X -delete

Delete all directories newer than X days starting from the current directory (including all subdirectories).

find . -type d -mtime +X -delete

Find & Copy

Find all files starting from the current directory with the name “linkin park” in it and copy them over to /Users/tommye/Desktop/LP (case insensitive).

  • Does NOT preserve path hierarchy
find . -type f -iname "*linkin park*" -exec cp -r {} /Users/tommye/Desktop/LP \;

Find all files within the current directory with the name “linkin park” in it and copy the files over to /Users/tommye/Desktop/LP (case insensitive).

  • Preserves path hierarchy
find . -type f -iname "*linkin park*" | cpio -pvdmu /Users/tommye/Desktop/LP
  • -p (pass) cpio -p reads the standard input to obtain a list of path names of files.
  • -v Verbose. Print a list of file names.
  • -d Creates directories as needed
  • -m Retain previous file modification time. This option is ineffective on directories that are being copied (mutually exclusive with -a).
  • -u Copy unconditionally (normally, an older file will not replace a newer file with the same name)

Find all directories within the current directory with the words “linkin park” in it and copy them to /Users/tommye/Desktop/LP (case insensitive).

find . -maxdepth 1 -type d -iname "*linkin park*" -exec cp -r {} /Users/tommye/Desktop/LP \;

Find all the files and directories starting from the current directory with the name “linkin park” in it and copy them over to /Users/tommye/Desktop/LP (case insensitive).

  • Does NOT preserve path hierarchy

Note: Keep in mind that Directories with the name “linkin park” in it will be copied over, but will not include its contents (it’s kind of useless in my opinion).

find . -iname "*linkin park*" -exec cp -r {} /Users/tommye/Desktop/LP \;

Find all the files and directories starting from the current directory with the name “linkin park” in it and copy them over to /Users/tommye/Desktop/LP (case insensitive).

  • Preserves path hierarchy
find . -iname "*linkin park*" > temp;rsync -arhv --files-from=temp . /Users/tommye/Desktop/LP;rm temp
  • -a, –archive archive mode
  • -r, –recursive # recurse into directories (meaning, include all sub-folders)
  • -h, –human-readable output numbers in a human-readable format
  • -v, –verbose # increase verbosity
,

One response to “Useful Linux find Commands”

  1. Hi Tommy,

    thanks for this list.

    Do you know how to write a shell script which will move folder and subfolders and files which are older than X days to a another folder