Find Cheatsheet

The find command in Linux is immensely useful. Here are some invocations of this command I find useful:

  • List all files and directories under a specified directory:
$ find /usr/include
$ find ../foobar/lib
$ find .

Unlike commands like ls, note that the find command is recursive by default. It will list everything under the specified directory.

  • List only normal files (symbolic links and directories will not be shown):
$ find /usr/include -type f
  • List only symbolic links:
$ find /usr/foobar -type l
  • List only directories:
$ find /usr/foobar -type d
  • Combining two or more types can be done. For example, to list normal files and symbolic links:
$ find /usr/foobar -type f -or -type l
  • List file or directory paths with a wildcard pattern:
$ find /usr/foobar -name "exactly_match_this"
$ find /usr/foobar -name "*.this_extension"
$ find /usr/foobar -name "*some_substring*"
  • List matching a wildcard pattern, but case insensitive:
$ find /usr/foobar -iname "*someblah*"
  • List files whose size is exactly or greater than a specific size:
$ find /usr/foobar -size 10k
$ find /usr/foobar -size +10k
$ find /usr/foobar -size +10M
$ find /usr/foobar -size +10G
  • List empty files:
$ find /usr/foobar -size 0
  • List files and directories newer than the specified file or directory:
$ find /usr/foobar -newer stdio.cpp
  • Do a ls -l on specific files and directories:
$ find /usr/foobar -size +20k -ls
  • Delete files and directories that match:
$ find /usr/foobar -size +20k -delete
  • Note that many of the above operations can be performed by piping the results of find to the immensely useful xargs command. For example, to ls on the results of find:
$ find /usr/foobar -size +20k | xargs ls -l

Reference:

  • man find
  • The Linux Command Line: A Complete Introduction by William Shotts

Tried with: Ubuntu 16.04

Missing argument to exec

Problem

I run a find command at the Fish shell and get this error:

$ find . -type f -exec sed -i 's/foo/bar/g' {} +
find: missing argument to `-exec`

Solution

This command has nothing wrong with it, it works under Bash. Fish expands the curly braces by default. So, for this to work, just enclose the curly braces in single quotes, so that it is not expanded. More details here.

Tried with: Fish 2.2.0 and Ubuntu 14.04

How to list files or directories only

There are many instances where I have wanted to list only the files or only the directories. The find tool seems to be the most convenient way to do this.

To list only the files in current directory:

$ find . -maxdepth 1 -type f | sort

To list only the directories in current directory:

$ find . -maxdepth 1 -type d | sort

The -maxdepth says it should only look in the directory and not recurse into subdirectories. The -type indicates that only files (or directories) should be found. The results of find are not in sorted order, so we pipe them to sort.

I recommend making an aliases out of this, so that they can be called easily like say lsf or lsd.

Tried with: FindUtils 4.4.2-7 and Ubuntu 14.04