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"

  1. Picture 1 of How to Find a File in Linux
    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" 
    1. Using -iname instead of -name ignores the case of your query. The -name command is case-sensitive.
  2. Picture 2 of How to Find a File in Linux
    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" 
    1. You can start the search in a specific directory by replacing the / with a directory path, such as /home/pat.
    2. You can use a . instead of / to force the search to only be performed on the current directory and subdirectories.
  3. Picture 3 of How to Find a File in Linux
    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" 
    1. This will return all of the .conf files in Pat's user folder (and subdirectories).
    2. 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*".
  4. Picture 4 of How to Find a File in Linux
    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 
  5. Picture 5 of How to Find a File in Linux
    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" 
  6. Picture 6 of How to Find a File in Linux
    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" 
    1. 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.
    2. 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.
  7. Picture 7 of How to Find a File in Linux
    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*" 
    1. 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.
  8. Picture 8 of How to Find a File in Linux
    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" 
    1. 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.
  9. Picture 9 of How to Find a File in Linux
    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 {} ; 
    1. 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"

  1. Picture 10 of How to Find a File in Linux
    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:
    1. Type sudo apt-get update and press Enter.
    2. 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.
    3. In Arch Linux, use the pacman package manager: pacman -S mlocate
    4. For Gentoo, use emerge: emerge mlocate
  2. Picture 11 of How to Find a File in Linux
    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]
    1. Type sudo updatedb and press Enter.
  3. Picture 12 of How to Find a File in Linux
    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" 
    1. 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.
    2. Like the find command, the -i ignores the case of your query.
  4. Picture 13 of How to Find a File in Linux
    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" 
    1. Only the first 20 results that match the query will be displayed.
    2. You can also use the | pipe to send the results to less for easy scrolling.
Method 3 of 3:

Searching for Text in Files

  1. Picture 14 of How to Find a File in Linux
    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/ 
    1. 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.
    2. 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.
  2. Picture 15 of How to Find a File in Linux
    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 
  3. Picture 16 of How to Find a File in Linux
    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 
Update 04 March 2020
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile