How to Find a File in Linux
Finding a file in a Linux system can be difficult if you don't know how. The best way to find files is to utilize several different terminal commands. Mastering these commands can give you complete control over your files, and they are...
Method 1 of 3:
Using "find"
- Search for a file by its file name. This is the most basic search you can perform using the find command. The command below will search for the query in the current directory and any subdirectories.[1]
find -iname "filename"
- Using -iname instead of -name ignores the case of your query. The -name command 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 tell find to 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 -not operators 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 / -perm 777 -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 777 will return all of the files with the 777 (no restrictions) permissions.
- Combine commands to perform actions when files are found. You can combine the find command with other commands so that you can execute them on the files that are returned by the query. Separate the find command and the second command with the -exec flag, and then end the line with {} ;
find . -type f -perm 777 -exec chmod 755 {} ;
- This will search the current directory (and all subdirectories) for files that have 777 permissions. It will then use the chmod command to change the permissions to 755.
Method 2 of 3:
Using "locate"
- Install the .locate functionality. The locate command generally works much faster than find, because it works off a database of your file structure. Not all Linux distributions come with the locate functionality installed, so enter the following commands to attempt to install it:
- Type sudo apt-get update and press ↵ Enter.
- You can install it in Debian and Ubuntu like this: Type sudo apt-get install mlocate and press ↵ Enter. If locate is already installed, you'll see the message mlocate 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. The locate command 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 using locate immediately.[3]
- Type sudo updatedb and press ↵ Enter.
- Use .locate to perform simple searches. The locate command is fast, but it doesn't have as many options as the find command. You can perform a basic file search in much the same way as the find command.
locate -i "*.jpg"
- This command will search you entire system for files with the .jpg extension. The wildcard character * functions the same way it does with the find command.
- Like the find command, the -i ignores 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 -n option, followed by the number of results you want displayed.
locate -n 20 -i "*.jpg"
- Only the first 20 results that match the query will be displayed.
- You can also use the | pipe to send the results to less for easy scrolling.
Method 3 of 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 the grep command. A basic grep command is formatted as follows:
grep -r -i "search query" /path/to/directory/
- The -r sets the search to "recursive", so it will search the current directory and all subdirectories for any file that contains the query string.
- The -i indicates that the query is not case-sensitive. If you want to force the search to pay attention to case, omit the -i operator.
- Cut out the extra text. When you perform a grep search 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. The grep command 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
4.5 ★ | 2 Vote
You should read it
- The reg query command in Windows
- The bootcfg query command in Windows
- Query command in Windows
- Qappsrv (query termserver) command in Windows
- What do you know about data queries?
- The LIKE command in SQL
- Google improves Search to provide more in-depth results for questions
- Qprocess (query process) command in Windows
- Qwinsta (query session) command in Windows
- The quser (query user) command in Windows
- The multiple-choice question set has an answer to Query P1
- How to turn off search suggestions in Mozilla Firefox
Maybe you are interested
Windows 11 is about to support content search in local video and audio files
Research shows that gaming is beneficial for mental health
How to remove Bing from Chrome and reset default search engine
How to search for similar photos using Google Lens on your computer
Google is sentenced to a search monopoly, with the possibility of being split up
AWS will discontinue Cloud9, CodeCommit, CloudSearch, and several other services