Linux find Command: A Guide
When you’re starting to use the Linux command line, you’ll encounter the question: How do I find files using Linux? It’s not as if there is a search bar you can use in your command line to find a file on your operating system. That’s where the Linux find command comes in handy.
In this article, we’re going to discuss how to find files in Linux using the find command. We will walk through a couple of examples of the find command in action to help you search for files based on different attributes. Let’s get started.
Linux find Command Syntax
Here is the syntax for the find command in Linux:
Find Your Bootcamp Match
- Career Karma matches you with top tech bootcamps
- Access exclusive scholarships and prep courses
find [options] [file path] [expressions]
- The options attribute controls what type of files you will search for and the debugging options you specify.
- file path is the directory at which your search for files will begin.
- expressions refer to all the options and search patterns that will be used to find a file or set of files.
How to Find Files by Name
Perhaps the most popular usage of the find command is to find a file by name. This allows you to search through a folder for a file based on its filename.
You can use the -name option with the find command to search for a file, followed by the name of the file for which you are searching.
Suppose we want to search for a file called find_tutorial.pdf in the directory /home/careerkarma/tutorials. We could do so using this command:
find /home/careerkarma/tutorials -name find_tutorial.pdf
Let’s break down this command:
- /home/careerkarma/tutorials is the name of the folder in which we are searching for our files (the file path).
- -name find_tutorial.pdf specifies the name of the file for which we want to search (the expression).
The find command is not case sensitive. As a result, the above command would return any file which has the name “find_tutorial.pdf”, even if the matching filename contained capital letters.
How to Find Files by Extension
You can also search for files based on their extensions. To do so, you can use the same syntax you would use for finding a file by name.
Suppose we want to find all files in the /var/log folder that ended in “.log”. We could do so using the following command:
find /var/log -name '*.log'
The “*.log” name tells the command line to search for any file whose name ends in .log;
is known as the wildcard character which can be used as a substitution for any text.
Find Files by Modified Date
When you’re searching for a file, you may want to refine your search to only files that were modified between a specific date.
Suppose you modified a file in /etc/nginx last week but you’re not sure which one. You could find which file you modified using this command:
find /etc/nginx -name * -mtime 7
This command uses
to denote any file name, and the
flag to instruct find to only return files that have been modified in the past seven days.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
How to Find and Delete Files
You may want to delete the files returned by a find search. You can do so using the -delete option at the end of your command.
Before you use the find command to delete files, you should ensure the files returned by the find command are the ones you want to delete. It’s best to run the find command without the -delete option first, so you can see what files will be deleted.
Remember, there is no trash can on the Linux command line; when a file is gone, it’s gone.
Suppose we want to delete all “.log” files in the folder /home/careerkarma/logs. We could do so using this command:
find /home/careerkarma/logs -name "*.log" -delete
Find Files by Different Types
So far, we’ve used the find command to search for any file or directory that matches a certain criteria on your file system. But what if you only want to search for files, folders or symbolic links?
option allows you to specify the file type for which you want to search. The most common values used with this option are:
- f: Search for files
- d: Search for folder
- l: Search for symbolic link
Suppose you want to find a list of all folders in the /home/careerkarma folder. You could accomplish this task using the following command:
find /home/careerkarma -type d
In this command, the
flag tells the find command to only search for folders.
The Linux find command gives you a number of options to find files and directories on your system. While the command may be less intuitive than a search bar, it gives you a lot of control over file types for your search.
Interested in learning more about the find command? Type
into your terminal to read more about the options offered by this command from the Linux manual.