When it comes to finding files and folders efficiently on a Linux system, there are two main commands available: find and locate. Both have their own strengths and use cases. In this guide, you will learn how to use these commands to quickly and precisely find the data you need, whether in the home directory or across the entire hard drive.
Key Takeaways
- locate is faster because it uses a database but may not be up to date.
- find searches directly on the hard drive and is always up to date but can be slower.
- Both commands offer various options to refine your search queries.
Step-by-Step Guide
1. An Overview: locate
Start your search with the locate command. This is particularly easy if you know the exact name or part of the name of the file. Let's say you want to find the file "Sonne". Switch to your root directory and enter the following command:
locate Sonne
This command will search the database and list any files that contain the search term "Sonne".

2. Ignore Case Sensitivity
Do you want to ignore case sensitivity in your search? Then you can use the -i option:
locate -i sonne
This will give you a list that includes both "Sonne" and "sonne".

3. Precision with Regular Expressions
If you want to conduct a more precise search, you can also use regular expressions with locate. For example, to search exactly for "ls", you can use the -b option in combination with single quotes:
locate -b '/ls'
This will ensure that you only get results that exactly match "ls".

4. Update Database
If you've created a new file and want to ensure that locate finds it, you need to update the database. You do this with the command:
sudo updatedb
This command updates the database and brings it up to date.

5. Using find
The find command is especially useful when you want to define more precise criteria for your search. For example, to search for a file named "xyz" in the home directory, you can use the following command:
find ~/ -name 'xyz'
This will only search the home directory for "xyz".

6. Filter by File Type
To search only for specific file types, you can use the -type option. For example, to find only directories:
find /var/log -type d
This command will list all directories in the /var/log directory.

7. Search Files by Size
If you want to search for files based on their size, you can use the -size option. For example, to find files larger than 1 MB:
find / -type f -size +1M
This command searches the entire system for files that are larger than 1 megabyte.

8. Find Files Changed Within a Specific Timeframe
You can also search for files that have been changed within a certain time frame. For example, if you want to search for files that were changed in the last 48 hours, you use:
find / -type f -mtime -2
Here, "-mtime -2" stands for files that were changed in the last two days.

9. Combinations of Search Criteria
The combination of multiple search criteria enhances the precision of your queries. You can filter simultaneously by file type and last modified date:
find /opt -type f -mtime -1
This will find all files in the /opt directory that were changed in the last 24 hours.

10. Conclusion on File Searching
With the commands find and locate, you are well-equipped to efficiently find files and folders on your Linux system. You can choose between speed and up-to-date data, depending on your needs.
Summary – Searching for Files and Folders in Linux – find and locate in Detail
Finding files and folders in Linux is a breeze with find and locate. Both commands offer you different options to customize your search individually.
Frequently Asked Questions
What is the difference between find and locate?find searches in real-time on the hard drive, while locate uses a database, making it faster but potentially delivering outdated results.
How do I update the locate database?With the command sudo updatedb, you can update the locate database.
Can I search by file size using find?Yes, you can do this by using the -size option.
How can I narrow my search by file or directory name?Use the -name or -type options to specifically search for certain names or file types.
What is an example of a combination of search criteria?You can use find to search for files of type -type f while specifying that the files were changed in the last 24 hours using -mtime -1.