Table of Contents
This updated guide examines Search: How to Find a File in Linux and organizes the essential facts, background, and practical takeaways in clear American English.
Method 1of 3:
Using "find"
Search for a file by its file name. This is the most basic search you can perform using thefindcommand. The command below will search for the query in the current directory and any subdirectories.[1]
find -iname"filename"
- Using-inameinstead of-nameignores the case of your query. The-namecommand is case-sensitive.
Set the search to start in the root directory. If you want to search your whole system, you can add the/modifier to the query. This will tellfindto search all directories starting from the root directory.
find / -iname"filename"
- You can start the search in a specific directory by replacing the/with a directory path, such as/home/pat.
- You can use a.instead of/to force the search to only be performed on the current directory and subdirectories.
Use the wildcard character. * to search for anything that matches the part of the query. The wildcard*character can be useful for finding something if you don't know the full name, or if you want to find everything with a specific extension.
find /home/pat -iname"*.conf"
- This will return all of the.conf files in Pat's user folder (and subdirectories).
- You can also use it to find everything that matches part of the file name. For example, if you have a lot of documents related to wikiHow, you could find them all by typing"*wiki*".
Make your search results easier to manage. If you're getting lots of search results, it can be difficult to sift through them. Use the|character and send the search results to the "less" filtering program. This can allow you to scroll through and filter the results much easier.
find /home/pat -iname"*.conf"|less
Find specific types of results. You can use modifiers to only return specific types of results. You can search for regular files (f), directories (d), symbolic links (l), character devices (c), and block devices (b) by using the right modifier.
find / -type f -iname"filename"
Filter your search results by size. If you have lots of files with similar names, but know the size you are looking for, you can filter our results by size.
find / -size +50M -iname"filename"
- This will return results that are 50 megabytes or larger. You can use+or-to search for greater or lesser sizes. Omitting the+or-will search for files exactly the specified size.
- You can filter by bytes (c), kilobytes (k), megabytes (M), gigabytes (G), or 512-byte blocks (b). Note that the size flag is case-sensitive.
Use boolean operators to combine search filters. You can use the-and,-or, and-notoperators to combine different types of searches into one.[2]
find /travelphotos -type f -size +200k -not -iname"*2015*"
- The command will find files in the "travelphotos" directory that are greater than 200 kilobytes in size but do not have "2015" anywhere in the file name.
Search for files by owner or permissions. If you are trying to find a specific file owned by a user, or files with certain permissions, you can narrow the search.
find / -user pat -iname"filename"find / -group users -iname"filename"find / -perm777-iname"filename"
- The above examples will search the specified users, groups, or permissions for the query. You can also omit the filename query to return all of the files that match that type. For example,find / -perm 777will return all of the files with the 777 (no restrictions) permissions.
Combine commands to perform actions when files are found. You can combine thefindcommand with other commands so that you can execute them on the files that are returned by the query. Separate thefindcommand and the second command with the-execflag, and then end the line with{};
find. -type f -perm777-exec chmod755{};- This will search the current directory (and all subdirectories) for files that have 777 permissions. It will then use thechmodcommand to change the permissions to 755.
Method 2of 3:
Using "locate"
Install the. locate functionality. Thelocatecommand generally works much faster thanfind, because it works off a database of your file structure. Not all Linux distributions come with thelocatefunctionality installed, so enter the following commands to attempt to install it:
- Typesudo apt-get updateand press?Enter.
- You can install it in Debian and Ubuntu like this: Typesudo apt-get install mlocateand press?Enter. Iflocateis already installed, you'll see the messagemlocate is already the newest version.
- In Arch Linux, use the pacman package manager:pacman -S mlocate
- For Gentoo, use emerge:emerge mlocate
Update your. locate database. Thelocatecommand will not be able to find anything until its database has been built and updated. This happens automatically daily, but you can manually update it too. You'll need to do this if you want to start usinglocateimmediately.[3]
- Typesudo updatedband press?Enter.
Use. locate to perform simple searches. Thelocatecommand is fast, but it doesn't have as many options as thefindcommand. You can perform a basic file search in much the same way as thefindcommand.
locate -i"*.jpg"
- This command will search you entire system for files with the.jpgextension. The wildcard character*functions the same way it does with thefindcommand.
- Like thefindcommand, the-iignores the case of your query.
Limit your search results. If your searches are returning too many results to handle, you can trim them down using the-noption, followed by the number of results you want displayed.
locate -n20-i"*.jpg"
- Only the first 20 results that match the query will be displayed.
- You can also use the|pipe to send the results tolessfor easy scrolling.
Method 3of 3:
Searching for Text in Files
Use the. grep command to search for strings of text within files. If you are looking for a file that contains a certain phrase or string of characters, you can use thegrepcommand. A basicgrepcommand is formatted as follows:
grep -r -i"search query"/path/to/directory/
- The-rsets the search to "recursive", so it will search the current directory and all subdirectories for any file that contains the query string.
- The-iindicates that the query is not case-sensitive. If you want to force the search to pay attention to case, omit the-ioperator.
Cut out the extra text. When you perform agrepsearch as above, you'll see the file name along with the text with the matching query highlighted. You can hide the matching text and just display the file names and paths by including the following:
grep -r -i"search query"/path/to/directory/|cut -d: -f1
Hide error messages. Thegrepcommand will return an error when it tries to access folders without the correct permissions or runs into empty folders. You can send the error messages to /dev/null, which will hide them from the output.[4]
grep -r -i"search query"/path/to/directory/2>/dev/null
FAQ
What is Search: How to Find a File in Linux about?
It provides a structured overview of search, explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
Reader Comments 0
Sign in with email or Google to join the discussion.