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. How to Find a File in Linux Picture 1How to Find a File in Linux Picture 1
    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. How to Find a File in Linux Picture 2How to Find a File in Linux Picture 2
    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. How to Find a File in Linux Picture 3How to Find a File in Linux Picture 3
    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. How to Find a File in Linux Picture 4How to Find a File in Linux Picture 4
    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. How to Find a File in Linux Picture 5How to Find a File in Linux Picture 5
    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. How to Find a File in Linux Picture 6How to Find a File in Linux Picture 6
    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. How to Find a File in Linux Picture 7How to Find a File in Linux Picture 7
    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. How to Find a File in Linux Picture 8How to Find a File in Linux Picture 8
    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. How to Find a File in Linux Picture 9How to Find a File in Linux Picture 9
    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. How to Find a File in Linux Picture 10How to Find a File in Linux Picture 10
    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. How to Find a File in Linux Picture 11How to Find a File in Linux Picture 11
    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. How to Find a File in Linux Picture 12How to Find a File in Linux Picture 12
    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. How to Find a File in Linux Picture 13How to Find a File in Linux Picture 13
    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. How to Find a File in Linux Picture 14How to Find a File in Linux Picture 14
    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. How to Find a File in Linux Picture 15How to Find a File in Linux Picture 15
    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. How to Find a File in Linux Picture 16How to Find a File in Linux Picture 16
    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