- How to Use Find and Locate Commands In Linux: A Beginner’s Guide
- Why Use Find and Locate Commands in Linux?
- Using the Find Command in Linux
- The Basic Syntax
- Ways to Utilize find Command
- Searching by Name
- Searching by Type
- Searching by Time
- Searching by Size
- Searching by Ownership
- Searching by Permissions
- Other Useful Options
- Using Locate Command in Linux
- How to Install locate Package
- The Basic Syntax
- How to Use Linux Locate Command
- Search Exact File Name
- Count the Number of Files
- Ignore Case Sensitive
- Show Existing Files
- Disables Errors While Searching
- Limit the Number of Search Results
- Conclusion
- 35 Practical Examples of Linux Find Command
- 1. Find Files Using Name in Current Directory
- 2. Find Files Under Home Directory
- 3. Find Files Using Name and Ignoring Case
- 4. Find Directories Using Name
- 5. Find PHP Files Using Name
- 6. Find all PHP Files in the Directory
- 7. Find Files With 777 Permissions
- 8. Find Files Without 777 Permissions
- 9. Find SGID Files with 644 Permissions
- 10. Find Sticky Bit Files with 551 Permissions
- 11. Find SUID Files
- 12. Find SGID Files
- 13. Find Read-Only Files
- 14. Find Executable Files
- 15. Find Files with 777 Permissions and Chmod to 644
- 16. Find Directories with 777 Permissions and Chmod to 755
- 17. Find and remove single File
- 18. Find and remove Multiple File
- 19. Find all Empty Files
- 20. Find all Empty Directories
- 21. File all Hidden Files
- 22. Find Single File Based on User
- 23. Find all Files Based on User
- 24. Find all Files Based on Group
- 25. Find Particular Files of User
- 26. Find Last 50 Days Modified Files
- 27. Find Last 50 Days Accessed Files
- 28. Find Last 50-100 Days Modified Files
- 29. Find Changed Files in Last 1 Hour
- 30. Find Modified Files in Last 1 Hour
- 31. Find Accessed Files in Last 1 Hour
- 32. Find 50MB Files
- 33. Find Size between 50MB – 100MB
- 34. Find and Delete 100MB Files
- 35. Find Specific Files and Delete
- If You Appreciate What We Do Here On TecMint, You Should Consider:
How to Use Find and Locate Commands In Linux: A Beginner’s Guide
Do you want to know how to perform searches on a Linux computer or server? Then you’ve come to the right place!
In this article, we’ll talk about the Linux find and locate commands, which will help you to look for any file on your machine.
Why Use Find and Locate Commands in Linux?
New Linux users often claim that they get confused about the location of their files on a server. This might be because most people are used to operating Windows or macOS, which have more clear-cut and user-friendly directory layouts.
While there is some truth to this, Linux gives users more options on how to search for files using certain commands. Besides searching based on common filters, you are also able to find files by user permissions, size, timestamps, and so on.
What’s great, once you understand the commands, searching for files on your Linux platform is strikingly easy.
To do that, we’ll utilize the find and locate commands in Linux.
An important thing to note is that we will be using Ubuntu VPS in this guide. That being said, the steps should also work for Debian, CentOS, or any other distribution of Linux. If you don’t know how to connect to VPS, you can follow this guide before proceeding further.
Using the Find Command in Linux
First, let us explain the find command and how to use it.
The Basic Syntax
The most popular command to find and filter files on Linux is find. The basic syntax is as follows:
It starts with the keyword find, which alerts Linux that whatever follows after will be used to find your file. The argument is the origin point of where you want to start the search. It can be replaced with several arguments, including:
- / (slash) — search the whole system.
- . (dot) — search from the folder you’re currently working on (current directory).
(tilde) — to search from your home folder.
In order to find the current directory you are in, use the pwd command.
The second argument is dedicated to your file. This could be the file’s name, type, date of creation, etc. The third argument is where you will specify the relevant search term.
Ways to Utilize find Command
Let’s take a look at various options Linux provides its users:
Searching by Name
Of course, the most common method to look for a file is using its name. To run a simple search query using the name of the file, use the find command like this:
We used the -name option, and searched for a file called my-file. Note that we started the search in our current directory by using the . (dot) argument.
Keep in mind that the -name argument looks for case-sensitive terms in Linux. If you know the name of the file, but are not sure about its case-sensitivity, use the following find command:
You can also search for all files without a certain keyword in their name. There are two ways to do this. The first method involves using the –not keyword in the following manner:
Second, we can use the exclamation symbol (!). However, it has to be preceded by the escape identifier (\) to let Linux know that this is the part of the find command.
You can look for multiple files with a common format like .txt as well:
This will list down all the text files starting with the current folder.
Lastly, if you want to find a certain file by name and remove it, use the -delete argument after the file name:
Searching by Type
Linux allows users to list all information based on their types. There are several filters that you can use:
- d – directory or folder
- f – normal file
- l – symbolic link
- c – character devices
- b – block devices
A simple example of using a file type can be seen below:
This will list all of the current directories in your system since we searched from our root directory with the / (slash) symbol.
You can also combine the –type and -name options to narrow down your searches further:
This will look for files named my-file, excluding directories or links.
Searching by Time
If you want to search for files based on when they were accessed and modification time footprints. Linux keeps track of the files using these three timestamps.
- Access Time (-atime) – when the file was either read or written into.
- Modification Time (-mtime) – when the file was modified.
- Change Time (-ctime) – when the file’s meta-data was updated.
This option has to be used with a number that specifies how many days passed since the file was accessed, modified or changed:
This command will show all files that were accessed a day ago starting from your current time.
We can narrow down our queries even more by adding plus (+) and minus (–) signs preceding the number of days. For instance:
It lists down all the files that have a modification time of more than two days ago.
To find all files whose meta-data was updated less than a day ago, run the following:
While not often used, there are some additional arguments that are also related to timed-searches. The -mmin argument looks for modified files on a minute basis. It can be used like this:
Also, we have the -newer argument, which can be used to compare the age of two or more files and display the newer one.
What you’ll get are all of the files that are more recently modified than your file.
Searching by Size
Linux lets you search for files based on their sizes. The syntax for searching files by size is:
You can specify the following size units:
- c – bytes
- k – kilobytes
- M – megabytes
- G – gigabytes
- b – 512-byte chunks
A simple example of how to use the find command for file sizes is as follows:
Here we search for all of the files in your system that are exactly 10 megabytes. Just like when searching based on time, you can filter your searches further using the plus and minus signs:
It will display all the files that are more than five gigabytes in size.
Searching by Ownership
Linux gives you the ability to narrow down your searches based on file ownership. To find files of a certain owner, the following command should be executed:
The script will return a list of all files that the user named john owns. Similar to usernames, we can also find files through group names:
Searching by Permissions
Users can search for files based on file permissions using -perm option. For example:
In Linux, 644 corresponds to read and write permission. That means this command will look for all the files that have only read and write permissions. You can play around with this option further:
With an addition of a dash symbol, it will return with all the files that have at least 644 permission.
Feel free to read more on permissions and various codes corresponding to other Linux permissions.
Other Useful Options
There are other useful options that you should remember.
For example, to look for empty files and folders on your system, use the following:
Similarly, to look for all the executables saved on your drive, utilize the -exec option:
To look for readable files, you can run the following command:
As you can see, there is a ton of options at hand for users to tailor their queries as they wish. Let us look at the other command which can be used to locate files in Linux.
Using Locate Command in Linux
The locate command is a useful alternative, as it is faster than the find command when performing searches. That’s because the former only scans your Linux database instead of the whole system. Furthermore, the syntax is relatively easier to write.
How to Install locate Package
By default, Linux does not come with the locate command pre-installed. To get the package, run the following commands one after another:
The Basic Syntax
You can now use the command to search for files using this syntax:
The vanilla locate command can sometimes return files that have been deleted, if the database wasn’t updated. The best solution is to manually update the database by running the following:
How to Use Linux Locate Command
We’ll share with you the most common applications of Linux locate command.
Search Exact File Name
The basic syntax only allows you to search for files that contain the search term. If you want to get the file with the exact name, you can use the -r option and add dollar symbol ($) at the end of your search term, for example:
Count the Number of Files
In order to tell how many files appear on your search result, insert -c after the locate command.
Instead of listing all the files, it will give you the total number of them.
Ignore Case Sensitive
Use -i on your linux locate command to ignore case sensitive files. For instance:
All of the files with this name will be shown, regardless of any uppercase or lowercase symbols found.
Show Existing Files
Just like we’ve mentioned, Linux locate command can even show you a deleted file if you haven’t updated the database. Thankfully, you can get around this problem by using -e option, like this:
By doing this, you will only get files that exist at the time you perform the locate command.
Disables Errors While Searching
-q option will prevent any errors from showing up when the search is being processed. To do this, simply enter:
Limit the Number of Search Results
If you want to limit the number of search results, -n will do the trick. However, remember that you need to put the option at the end of the command line. Take a look at this example:
The script will only display the first 10 files it discovers even when there are more.
Conclusion
You can search for files on your server using the find and locate commands in Linux. These two powerful tools have their own advantages. Therefore, we encourage you to give both of them a go and see which one is more suitable for you. Here’s a short summary of what we talked about:
- Use find to search for files based on name, type, time, size, ownership and permissions, in addition to some other useful options
- Install and use Linux locate command to perform faster system-wide searches for files. It also allows you to filter out by name, case-sensitive, folder, and so on.
If you have any questions, feel free to ask in the comments!
Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.
Источник
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.
- Part I: Basic Find Commands for Finding Files with Names
- Part II: Find Files Based on their Permissions
- Part III: Search Files Based On Owners and Groups
- Part IV: Find Files and Directories Based on Date and Time
- Part V: Find Files and Directories Based on Size
- Part VI: Find Multiple Filenames in Linux
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.
Источник