How to search for a file in Linux

The fastest way to find a file in Linux is the find command, which searches your entire system or a specific folder. Open your terminal and type find /home -name "filename" to search for a file by its exact name, or find /home -name "*filename*" to find files that contain those letters anywhere in the name. The locate command is faster but searches a database that updates once a day, so it misses very new files. For searching inside file contents rather than just names, use grep.

Which command you use depends on what you're looking for: a file you know the name of, a file you half-remember, or text inside files. This guide walks you through each one and shows you how to avoid the most common mistakes — like searching the wrong folder or getting too many results to read through.

Key Takeaways

  • The find command searches by filename and works anywhere on your system, but can be slow on large drives.
  • The locate command is much faster but only searches a database updated once daily, so it will not find files created today.
  • Use wildcards like *filename* when you only remember part of the name, and put the search path first to avoid searching your entire system.
  • The grep command searches inside files for text, not for filenames, and works best when you know which folder to search.
  • Redirect results to a file with > results.txt when a search returns hundreds of matches you cannot read in one screen.

Using find to search by filename

The find command is the most reliable way to search for a file when you know its name or part of it. Open your terminal and navigate to the folder where you think the file might be, or start from your home directory. Type find . -name "filename" to search the current folder and all subfolders. The period means "start here"; if you want to search your entire home directory instead, use find ~ -name "filename".

If you only remember part of the filename, use a wildcard. Type find . -name "*document*" to find any file with "document" anywhere in its name. The asterisk matches any characters before or after. You can also search by file type: find . -name "*.pdf" finds all PDF files in the current folder and below. This is much faster than searching by name alone because the system stops checking files once it matches the pattern.

One common mistake is forgetting to put the search path first. If you type just find -name "filename" without a path, the system searches from your current location, which might not be where you think. Always start with a path: the current folder (.), your home folder (~), or a specific path like /home/username/Documents.

Using locate for quick searches

The locate command searches a database of filenames instead of scanning your actual folders, so it returns results in under a second. Type locate filename and the system shows every file with that name on your computer. This is much faster than find when you are searching a large drive or the entire system.

The catch is that the database updates only once a day, usually at night. If you created a file an hour ago, locate will not find it yet. You can force an update by typing sudo updatedb, but this takes a minute or two and requires your password. For files you created today, use find instead.

Like find, you can use wildcards with locate. Type locate *filename* to find files with that text anywhere in the path. One difference: locate searches the entire system by default, so you do not need to specify a starting folder. If you get too many results, pipe them to grep to filter: locate filename | grep Documents shows only results in your Documents folder.

Searching inside files with grep

grep searches for text inside files, not for filenames. If you remember a sentence or phrase from inside a document but not the filename, grep is the right tool. Open your terminal, navigate to the folder where the file might be, and type grep -r "search text" . to search all files in the current folder and subfolders. The -r flag means "recursive" — it goes into every subfolder.

By default, grep shows the filename and the line where it found your text. If you only want the filenames, add the -l flag: grep -r -l "search text" . This is useful when you have hundreds of matches and just need to know which files contain the text. You can also make the search case-insensitive with -i: grep -r -i "Search Text" . will find "search text", "SEARCH TEXT", and "Search Text".

A common mistake is running grep on your entire system without specifying a folder. This searches every file on your computer and takes a very long time. Always navigate to the folder you think the file is in first, or specify the path: grep -r "text" /home/username/Documents.

Combining commands to narrow results

When a search returns hundreds of results, you can filter them using pipes and additional commands. A pipe (|) sends the output of one command into another. For example, find . -name "*.txt" | grep report finds all text files, then shows only the ones with "report" in the name. This is faster than running two separate searches.

You can also count results with wc -l: find . -name "*.pdf" | wc -l tells you how many PDF files exist in the current folder without listing them all. To save results to a file so you can read them later, use >: find . -name "*.jpg" > image_list.txt writes all JPG files to a new file called image_list.txt in your current folder.

Another useful filter is head, which shows only the first 10 results: find . -name "*document*" | head. This helps you check if your search is working before it finishes scanning thousands of files. If the first 10 results look right, you know the command is correct.

Searching by file size, date, or permissions

The find command can search by more than just the filename. To find files larger than 100 megabytes, type find . -size +100M. Use -size -100M for files smaller than 100 megabytes. You can also search by when a file was last changed: find . -mtime -7 finds files modified in the last 7 days, and find . -mtime +30 finds files not touched in over 30 days.

To find files by permissions, use find . -perm 644 to search for files with specific permission settings. This is useful when you are looking for executable files or files that might have unusual access restrictions. You can combine these searches: find . -name "*.log" -mtime +30 finds log files that have not been modified in over a month.

Troubleshooting common search problems

If a search returns "Permission denied" errors, the system is trying to search folders you do not have access to. This is normal and does not mean the file is not there. You can hide these errors by redirecting them to nowhere: find / -name "filename" 2>/dev/null. The 2>/dev/null part throws away error messages so you only see actual results.

If a search takes too long, you probably started from the root folder (/) or a very large directory. Press Ctrl+C to stop the search, navigate to a more specific folder, and try again. For example, search /home instead of /, or search a specific project folder instead of your entire home directory. This saves time and gives you fewer irrelevant results.

If locate says "command not found", the database tool is not installed on your system. Use find instead, or ask your system administrator to install the mlocate package. On Ubuntu or Debian, type sudo apt install mlocate and then sudo updatedb to build the database.

Frequently Asked Questions

What is the difference between find and locate?

find scans your actual folders and is slower but always current, while locate searches a database and is much faster but misses files created since the last update. Use locate for quick searches of files you created days ago, and find for recent files or when you need to search by size or date.

How do I search for a file with spaces in the name?

Put the filename in quotes: find . -name "my document.txt". Without quotes, the system treats each word as a separate argument and the search fails. Wildcards work inside quotes too: find . -name "my *" finds all files starting with "my ".

Can I search multiple folders at once?

Yes. Type find /path1 /path2 /path3 -name "filename" to search several folders in one command. You can also use find / -name "filename" to search your entire system, though this takes longer and may return permission errors.

How do I make a search case-insensitive?

Add the -iname flag to find: find . -iname "filename" finds "Filename", "FILENAME", and "filename". With grep, use -i: grep -i "text" file.txt. With locate, use -i as well: locate -i filename.

What should I do if a search returns too many results?

Narrow the search by adding more details: search by file type with *.pdf, by date with -mtime, or by size with -size. You can also pipe results through grep to filter by a word in the filename, or save them to a file with > results.txt and open it in a text editor.