Conquer The Command Line: The find Command
By: Jim Homme
Tags: Beginner, Tutorial, Command Line, Linux, Programming

How to use the Linux find command plus essential options

In this installment of “Conquer The Command Line” we talk about how to use the Linux find command.

When ever you want to locate files or folders, use the find command.

Memory Aid: Think “fetch”

Confession: find is self-explanitory, so I made up the memory aid for this post to be silly.

You use find when you want to find files whose names match certain patterns, and you don’t necessarily know where they are on your system. Find searches recursively, which means it starts at the current directory and looks inside the directories a level down from that one. Then it searches the subdirectories of that directory, and the subdirectories of those, and on and on.

Essential Options

The find command consists mostly of all kinds of options that make it do processing on files it finds. You’ll see lots of examples below. Now that you know it searches recursively, I won’t repeat that it does that.

Find all files with the .js extension and print their relative path: find . -name '*.js'

The -name option makes the find command search for case-sensitive file names.

The -iname option makes the find command search for names that contain either upper or lower case letters. It is case-insensitive. In other words, and this is somethinng you wouldn't commonly do, you could search for js , Js , or jS .

To keep the shell, the program that interprets and runs commands, from processing the content you want the find command to process, put it inside single quotes.

. means the current directory.

the * character means 0 or more of any character.

the .js part after the * means litterally find those characters in file names.

Find directories matching the name “src”: find . -type d -name src

-type d means directory. This makes find only display directories and not the files in them.

-type f searches for files.

-type l searches for symbolic links. Windows people, think shortcuts.

You can also make find search starting with more than one directory. Let’s say that you have a directory named docs and another one named logs in the current directory. You want to search both the docs and the logs directories and their subdirectories for the file called history.txt. All you have to do is include them both on the command line like this: find docs logs -name history.txt .

Related Commands

To print the working directory, the one you are currently pointing to, use the pwd command.

To change to directories, use the cd command.

To list the contents of a directory, use the ls command.

To make directories, use the mkdir command.

To remove directories, use the rmdir command.

To create files or change their time stamps, use the touch command.

To move or rename files and directories, use the mv command.

To copy files and directories, use the cp command.

Remember to use the man command to find all the options for any command on your system. And remember that for a less technical explanation of commands to check out tldr pages.

What Now?

Hopefully this post has helped you take one more step in conquering the command line. I welcome any feedback.

This post was originally written at https://www.jimhomme.com