Hello there, In this guide, you will learn how to find files by name in Linux using the
find command. If you have ever used a windows OS or macOS then you should know how easy is that to find a file in those operating systems. But if you are a beginner to Linux then it may be a bit tricky to perform that same operation on Linux. It is because Linux is a command-based operating system so you have to use the terminal for the file operations like finding a file in Linux, editing files, or renaming files.
In this article, we will see how to use
find Linux commands with examples.
[You may also like to read – How to create files in Linux]
Who will get the benefits from this article?
The simple answer to this question is anyone who is using any Linux-based operating system will get benefits from this article. We are answering this question because there are many Linux distributions available nowadays so you may think whether will these commands shared in this article work on my Linux distribution or not.
Table of Contents
Find File in Linux using Find Command
The find command is a really powerful tool to find files in Linux. There are many options that you can use with the
find command for finding the exact file you are looking for. Let’s see how you can use the find command with its options.
Find the file in Linux using the file name
- Open your Linux Terminal
In order to find files in Linux using the find command, open the terminal of your Linux machine. If you are using a Linux cloud server then you can connect to it using SSH command or Putty application.
- Login as Root (If needed)
You will have to log in as root if the file you are looking for is part of the root directory. Otherwise, if the file you want to find is part of your home directory then you do not need to have root access.
- Use Find Command to Find File By Name
Now use the find command to find the file you are looking for. Please go through this complete article as we have shared a complete guide.
Find Command Syntax
The syntax of the find command is very simple but some users might find it difficult to use with options.
find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]
Let’s make it a little bit simple and understandable for beginners-
find [Path] [Expression]
Find Command Syntax for Finding a File By File Name in Linux
We do not want to make you confused so we will keep it simple as it could be. Let’s see how you can find a file in Linux using the file name in a very simple way.
find . -name "filename"
There is one interesting thing about the path that we would like to share-
Note: If you want to find a file that is available in your home/current directory then you would use a dot (.) as the path or if you want to find a file that is available anywhere in the root file system then you would use the forward slash (/) as the path. If you don’t do so then you would get errors.
For Example, if there is a “testfile” file in your home directory then you can use the following command-
find . -name "testfile"
Well, now how can you find a file which is available in the root file system?
As we already have mentioned that you can find it using a forward slash (/) as the path.
sudo find / -name "filename"
let’s suppose, you want to find grub.cfg which is available in the root file system but you do not know the exact file path. So, you will have to run the following code-
sudo find / -name "grub.cfg"
Note- In order to find a file that is available in the root file system you need to have either root access or you can use Sudo.
Find Files Containing Any Specific Characters
In some cases, you may want to find a file that contains any specific Characters. So, in such a case, you will have to use the star “*” as the prefix and suffix of the characters.
Let’s suppose, you want to find all the files that contain the “Report” character in it then you will have to run the following command-
find . -name "*Report*"
Let me test you, how would you find all the PNG files in a directory?
Well, It is so simple. you can use *.png. Well if you are unable to understand then please have a look at the example-
find . -name *.png
Find Command Useful Options
Find command has many options you can use them also as per requirement. Like, you can find files owned by a group or user using the find command with the option.
Find files owned by the user and group in Linux
Use the following command to find files owned by any particular user-
find [Path] -user [YourUserName] -name ["FileName"]
For example, if you want to find all the “Reports” files created by user “John” then you will have to run the following command-
find . -user john -name "*Reports*"
If you still want to learn more about the find command in Linux then please run the
man find command on your Linux terminal.
If you want to find file owned by any particular group then use -group option instead of -user option.
find . -group [GroupName] -name ["FileName"]
In this guide post, we learned how to find file files in Linux using the find command. We covered some useful options also. After going through this complete article you will be able to understand how to find directories or files in Linux. If you have any confusion then please let us know through the comment.