How to find file in linux
How to find file in linux
Classic SysAdmin: How to Search for Files from the Linux Command Line
This is a classic article written by Jack Wallen from the Linux.com archives. For more great SysAdmin tips and techniques check out our free intro to Linux course.
It goes without saying that every good Linux desktop environment offers the ability to search your file system for files and folders. If your default desktop doesn’t — because this is Linux — you can always install an app to make searching your directory hierarchy a breeze.
But what about the command line? If you happen to frequently work in the command line or you administer GUI-less Linux servers, where do you turn when you need to locate a file? Fortunately, Linux has exactly what you need to locate the files in question, built right into the system.
The command in question is find. To make the understanding of this command even more enticing, once you know it, you can start working it into your Bash scripts. That’s not only convenience, that’s power.
Let’s get up to speed with the find command so you can take control of locating files on your Linux servers and desktops, without the need of a GUI.
How to use the find command
When I first glimpsed Linux, back in 1997, I didn’t quite understand how the find command worked; therefore, it never seemed to function as I expected. It seemed simple; issue the command find FILENAME (where FILENAME is the name of the file) and the command was supposed to locate the file and report back. Little did I know there was more to the command than that. Much more.
If you issue the command man find, you’ll see the syntax of the find command is:
Naturally, if you’re unfamiliar with how man works, you might be confused about or overwhelmed by that syntax. For ease of understanding, let’s simplify that. The most basic syntax of a basic find command would look like this:
Now we’ll see it at work.
Find by name
Let’s break down that basic command to make it as clear as possible. The most simplistic structure of the find command should include a path for the file, an option, and the filename itself. You may be thinking, “If I know the path to the file, I’d already know where to find it!”. Well, the path for the file could be the root of your drive; so / would be a legitimate path. Entering that as your path would take find longer to process — because it has to start from scratch — but if you have no idea where the file is, you can start from there. In the name of efficiency, it is always best to have at least an idea where to start searching.
The next bit of the command is the option. As with most Linux commands, you have a number of available options. However, we are starting from the beginning, so let’s make it easy. Because we are attempting to find a file by name, we’ll use one of two options:
name – case sensitive
iname – case insensitive
Remember, Linux is very particular about case, so if you’re looking for a file named Linux.odt, the following command will return no results.
Find by type
What if you’re not so concerned with locating a file by name but would rather locate all files of a certain type? Some of the more common file descriptors are:
f – regular file
l – symbolic link
c – character devices
b – block devices
Now, suppose you want to locate all block devices (a file that refers to a device) on your system. With the help of the -type option, we can do that like so:
The above command would result in quite a lot of output (much of it indicating permission denied), but would include output similar to:
Voilà! Block devices.
Figure 1: Locating all of your configuration files in /etc.
Outputting results to a file
One really handy trick is to output the results of the search into a file. When you know the output might be extensive, or if you want to comb through the results later, this can be incredibly helpful. For this, we’ll use the same example as above and pipe the results into a file called conf_search. This new command would look like:
You will now have a file (conf_search) that contains all of the results from the find command issued.
Finding files by size
Now we get to a moment where the find command becomes incredibly helpful. I’ve had instances where desktops or servers have found their drives mysteriously filled. To quickly make space (or help locate the problem), you can use the find command to locate files of a certain size. Say, for instance, you want to go large and locate files that are over 1000MB. The find command can be issued, with the help of the -size option, like so:
You might be surprised at how many files turn up. With the output from the command, you can comb through the directory structure and free up space or troubleshoot to find out what is mysteriously filling up your drive.
You can search with the following size descriptions:
b – 512-byte blocks
Keep learning
We’ve only scratched the surface of the find command, but you now have a fundamental understanding of how to locate files on your Linux systems. Make sure to issue the command man find to get a deeper, more complete, knowledge of how to make this powerful tool work for you.
How to Find a File in Linux Using the Find Command
The Linux find command is one of the most important and handy commands in Linux systems. It can, as the name suggests, find files on your Linux PC based on pretty much whatever conditions and variables you set. You can find files by permissions, users, groups, file type, date, size and other possible criteria using the find command. Here we show you how to find a file in Linux using the find command.
The find command is available on most Linux distro by default, so you do not have to install a package for it.
Find Files by Name in Current Directories
The most obvious way of searching for files is by name. To find a file by name in the current directory, run:
If you want to find a file by name that contains both capital and small letters, run:
Find Files Under Specific Directory
If you want to find files under a specific directory like “/home,” run:
If you want to find files with the “.txt” extension under the “/home” directory, run:
To find files whose name is “test.txt” under multiple directories like “/home” and “/opt”, run:
To find hidden files in the “/home” directory, run:
To find a single file called “test.txt” and remove it, run:
To find all empty files under the “/opt” directory, run:
Find Directories Using Name
If you want to find all directories whose name is “testdir” under the “/home” directory, run:
To file all empty directories under “/home,” run:
Find Files with Certain Permissions
The find command can be used to find files with a specific permission using the perm option.
To find all files whose permissions are “777” in the “/home” directory, run:
To find all the files without permission “777,” run:
To find all read-only files, run:
To find all executable files, run:
To find all the sticky bit set files whose permissions are “553,” run:
To find all SUID set files, run:
To find all files whose permissions are “777” and change their permissions to “700,” run:
Find Files and Directories Based on Date and Time
To find all the files under “/opt” which were modified 20 days earlier, run:
To find all the files under “/opt” which were accessed twenty days earlier, run:
To find all the files under “/opt” which were modified more than 30 days earlier and less than 50 days after:
To find all the files under “/opt” which were changed in the last two hours, run:
Find Files and Directories Based on Size
To find all 10MB files under the “/home” directory, run:
To find all the files under the “/home” directory which are greater than 10MB and less than 50MB, run:
To find all “.mp4” files under the “/home” directory with more than 10MB and delete them using a single command, run:
As you can see, the find command is incredibly useful for administering a system, looking through directories to find files, and generally pruning the virtual directory tree in Linux. If you enjoyed this Linux article, make sure you check out some of our other Linux content, like how to use the scp command to securely transfer files, how to use nnn as a file manager in the terminal, and how to fix broken packages.
Our latest tutorials delivered straight to your inbox
Find Command in Linux with Examples [How to Find File in Linux]
Are you looking for a file in the Linux platform and are not able to find that then the Find command in Linux with examples blog post will help you to Find Files in Linux.
List of content you will read in this article:
Being a Linux administrator or beginner, many times we forgot any file which we have download or kept somewhere else in the system but unfortunately, we were not able to find that file. So to increase your productivity and lesser the work efficiency, You can use some basic Linux commands shortcuts to solve your problem. This article will help you to find file in Linux or how to search file in Linux, before moving forward or dive directly into the file command we should have a brief understanding of SSH.
What is SSH?
SSH stands for Secure Shell. It is a protocol that connects you to a remote server or system. The established connection between the host and the client is extremely secure and safe due to encryption.
How to find file in Linux
There are two prominent ways to Search File and Directory in Linux, carry out this task:
Find command in Linux
Find command eases your process of find file in Linux (CentOS, Debian, Ubuntu)using various arguments. You can search for the desired file through date, time, or even through modified time.
1. Suppose, you wish to search for a file a.txt, follow the mentioned command:
2. Now, if you do not remember the exact file name, try this method:
Here, the system will scan all the files and present you with the file that matches your argument the best.
3. Moving on, if you want to view a list of all the directories, simply use the following command:
4. Next, to view files modified in the last 3 days, use
Additionally, if you want to search a specific part of a text in a file, use the Grep command.
Grep command
This command searches for the string, “warehouse” in the file config.php that you mentioned.
1. Now, in case you forget the file name, simply follow this command:
2. To search for a specific text in a file, use the below-mentioned command:
Grep “warehouse” config.php
4. Additionally, if you only want a list of all the files that contain your mentioned string and not the entire line, use this command:
4. Moving on to our last segment, there can be situations when these commands don’t work. Follow these two commands in such cases.
whereis warehouse
which warehouse
Conclusion
This all about find commands in Linux that will help you how to find file in Linux/How to search files in Linux. In this article, we have listed many find commands for your help and to improve your productivity. We hope that using the commands listed above is sufficient. If still, you face any problems and do you have any kind of suggestion or you know more efficient command then you can comment them via comment box list below.
How to find file in Linux
If you need to search for one or more particular files, Linux systems have a few powerful methods for locating them, such as the find and locate commands. Searching for a file with a specific name can be done, but you can also search for files that follow certain naming patterns. This can be broadened all the way to finding files based on file size, file extension, or a lot of other options.
It’s also possible to find a particular directory or search for files based on their contents, such as finding all files containing a specific text, but we cover those topics in separate guides.
In this tutorial, you’ll learn how to find a file in Linux by using the command line and GUI. Let’s get started.
In this tutorial you will learn:
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux disto |
Software | find, locate, GUI file explorer |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user |
Find a file with via command line
Most of this section will revolve around the find command. When it comes to finding a particular file or set of files, the find command is your best friend on Linux. So all you really need to know is how to use the command effectively. Through the following examples and explanations, you’ll learn how to use it.
Search your present working directory and its subdirectories for a particular file:
The locate command
The command syntax is very simple. Just specify the file you’d like to find.
Find a file with GUI
All GUIs look a little different but they all surely have a file explorer. We’re using GNOME on our test machine, but searching for files on any GUI is pretty much the same, regardless of the desktop environment you’re using.
On GNOME, we just need to open the file browser, click the magnifying glass (maybe it says ‘search’ on your GUI), and type the name of the file we wish to find.
Conclusion
Finding a file on Linux is very easy, but it’s surprising how complex the search query can be when you need to find something very specific. The find and locate commands, as well as the GUI method, are all easy to learn when performing a basic search. Once you get comfortable with some of the find command’s more complex capabilities, it becomes an extremely viable tool for finding and manipulating files in one go.
35 Practical Examples of Linux Find Command
The Linux find command is one of the most important and frequently used command command-line utility in Unix-like operating systems. The find command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.
find command can be used in a variety of conditions like you can find files by permissions, users, groups, file types, date, size, and other possible criteria.
Through this article, we are sharing our day-to-day Linux find command experience and its usage in the form of examples.
In this article, we will show you the most used 35 Find Commands Examples in Linux. We have divided the section into Five parts from basic to advance usage of the find command.
1. Find Files Using Name in Current Directory
Find all the files whose name is tecmint.txt in a current working directory.
2. Find Files Under Home Directory
Find all the files under /home directory with the name tecmint.txt.
3. Find Files Using Name and Ignoring Case
Find all the files whose name is tecmint.txt and contains both capital and small letters in /home directory.
4. Find Directories Using Name
Find all directories whose name is Tecmint in / directory.
5. Find PHP Files Using Name
Find all php files whose name is tecmint.php in a current working directory.
6. Find all PHP Files in the Directory
Find all php files in a directory.
7. Find Files With 777 Permissions
Find all the files whose permissions are 777.
8. Find Files Without 777 Permissions
Find all the files without permission 777.
9. Find SGID Files with 644 Permissions
Find all the SGID bit files whose permissions are set to 644.
10. Find Sticky Bit Files with 551 Permissions
Find all the Sticky Bit set files whose permission is 551.
11. Find SUID Files
Find all SUID set files.
12. Find SGID Files
Find all SGID set files.
13. Find Read-Only Files
Find all Read-Only files.
14. Find Executable Files
Find all Executable files.
15. Find Files with 777 Permissions and Chmod to 644
Find all 777 permission files and use the chmod command to set permissions to 644.
16. Find Directories with 777 Permissions and Chmod to 755
Find all 777 permission directories and use the chmod command to set permissions to 755.
17. Find and remove single File
To find a single file called tecmint.txt and remove it.
18. Find and remove Multiple File
To find and remove multiple files such as .mp3 or .txt, then use.
19. Find all Empty Files
To find all empty files under a certain path.
20. Find all Empty Directories
To file all empty directories under a certain path.
21. File all Hidden Files
To find all hidden files, use the below command.
22. Find Single File Based on User
To find all or single files called tecmint.txt under / root directory of owner root.
23. Find all Files Based on User
To find all files that belong to user Tecmint under /home directory.
24. Find all Files Based on Group
To find all files that belong to the group Developer under /home directory.
25. Find Particular Files of User
To find all .txt files of user Tecmint under /home directory.
26. Find Last 50 Days Modified Files
To find all the files which are modified 50 days back.
27. Find Last 50 Days Accessed Files
To find all the files which are accessed 50 days back.
28. Find Last 50-100 Days Modified Files
To find all the files which are modified more than 50 days back and less than 100 days.
29. Find Changed Files in Last 1 Hour
To find all the files which are changed in the last 1 hour.
30. Find Modified Files in Last 1 Hour
To find all the files which are modified in the last 1 hour.
31. Find Accessed Files in Last 1 Hour
To find all the files which are accessed in the last 1 hour.
32. Find 50MB Files
To find all 50MB files, use.
33. Find Size between 50MB – 100MB
To find all the files which are greater than 50MB and less than 100MB.
34. Find and Delete 100MB Files
To find all 100MB files and delete them using one single command.
35. Find Specific Files and Delete
Find all .mp3 files with more than 10MB and delete them using one single command.
That’s it, We are ending this post here, In our next article, we will discuss more other Linux commands in-depth with practical examples. Let us know your opinions on this article using our comment section.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.