Command line tips - Finding files; Part 1: By Name

There is always a time when working with the CLI that you will want to find a set of files by name, such as all JPEG images. The `find` command lets you do this and more. Let's jump into the basic structure of a find command:

find starting-folder find-files action

The first word, find, is obviously the command itself. Starting-folder is the directory that you want to start looking in, such as /home. Find-files tells find what you are looking for. This could be composed of a pattern of names, file modification dates, permissions, you name it. Action is what will happen when the files are found. "-print" is the most common action.

To start, here is an example of a simple find command:

find /home -name '*.jpg' -print

Let's split the command into pieces:

  1. The find command itself.
  2. /home, where we want to search in (recursive).
  3. -name '*.jpg' tells find to look for all files that end in .jpg.
  4. -print tells find to simply output what it finds in the terminal.

Before we get into any actions on what find can do, let's start with some available options for finding files. You can find these and a lot more in the command man find.

  • -name pattern - Like above, pattern is a shell pattern detailing what you want to find in the name.
  • -iname pattern - Case-insensitive version of -name.
  • -regex pattern - Like -name, but supports regular expressions.
  • -size n - The file size is exactly n. n can be something like "20M" for 20 megabytes, or "12k" for 12 kilobytes. See the manual page for more info.
  • -type c - The file is of type c, which can be:
    • d - directory
    • f - regular file
    • l - symbolic link
    More on the manual page.
  • -user name - the file is owned by user name.
  • -group name - file is owned by group name.
There are much more options, a lot dealing with file times and dates, in the manual page. You can combine any number of them to match your files.

Now, what if you want to act upon a certain file when it is found? You can do it with these actions (again, more are in man find).

  • -print - output the files found
  • -delete - delete the files when found
  • -exec command \; - executes command each time a file is found. Note the \; is required.
  • -exec command '{}' \; - executes command using '{}' as parameters. '{}' is never actually ran as a parameter in command, it is replaced by the file found. Explained in the example below.
Here is another example:
find /home -name '*.sh' -exec chmod +x '{}' \;

  1. find /home -name '*.sh' finds all files ending in .sh in /home recursively.
  2. -exec starts the action.
  3. chmod +x is the command we want to run, in this case, add execute permission.
  4. '{}' is where the filename will go when find finds a file.
  5. \; is the required ending sequence of -exec.

When ran, every time a .sh file is found, chmod +x will be run on the file. For example, if /home/jacob/ is found, the following command will be automatically run:
chmod +x '/home/jacob/'

The -exec command can be invaluable when dealing with large amounts of files.

find is incapable of searching inside files however. We'll cover that in the next part of this tutorial using a command called grep.

Avatar for jacob Jacob Peddicord -

Jacob is a web developer, student, and programmer from Ohio. He is a staff member at the Ubuntu Forums and is most likely a fanboy of the distribution. He loves to write in code and words, play video games, and rant about topics most would have abandoned long ago. Jacob uses GNOME and is never seen running stable software, much to the demise of his laptop.

Home » Articles »

Discussion: Command line tips - Finding files; Part 1: By Name

  1. Marcus (guest)

    # Posted on 22 August 2007 at 04:51 PM

    How about including even some basics like how to recursively search some dirs while excluding some subdirs? Well, that should be pretty basic, but find somehow makes this an almost impossible task. You have to be extremely careful in which order you give the parameters and I don't think I've ever even managed to exclude more than one subdir at a time. shudder

Home » Articles » Command line tips - Finding files; Part 1: By Name