The easiest way to locate files by name, partial name, or date at the command line

If you're looking for a file on your Linux system, the find command makes it easy. You can use find to search for files by name, partial name, date, modification time, size, and more. If you know which directory the file is in, you can specify that directory in your find command. If not, you can search your entire Linux system starting with the root (/) directory. This wikiHow article will teach you how to use the find command in Linux to find any file, from files you downloaded to configuration files.

Things You Should Know

  • The basic syntax of find is find <starting directory> <options> <search terms>
  • You can use asterisks as wildcards if you don't know the exact name of the file.
  • Use the -iname option to ignore case sensitivity when using find.
Section 1 of 8:

Using Find: The Basics

  1. 1
    You'll typically use the find command with the syntax find /path -type f -iname filename. You can use a variation of this command to find any file or directory on your Linux machine. We'll break down the command into simple parts.
  2. 2
    /path/to/file is the directory in which you want to search for the file. For example, to search the current directory, use . as the path. To search your entire Linux file system, use / as the path.
    Advertisement
  3. 3
    -type indicates the type of file or directory you're searching for. You'll follow -type with a flag. In our example, we're using the f flag. When searching for files, you'll typically use any of these three flags:
    • f: This means "regular file," which can be a text file, image, program, configuration file, executable, and basically any type of file (including hidden files).
      • Tip: -type f is the default for the find command. This means that if you're looking for a file (not a directory or symbolic link), you can actually leave -type f out of the file command.
    • d: Searches for directories (folders).
    • l: Searches for symbolic links to other files.
    • You can search for multiple types by separating the letters with commas. For example, to find all files, directories, and symbolic links called "etc," you'd use find / -type f,d,l -iname etc
  4. 4
    -iname tells find to ignore case-sensitivity. This is important if you're not 100% sure of the file's name or case. However, if you want find to specifically match the case you type, replace -iname with -name, which is case-sensitive.[1]
  5. 5
    filename is the name of the file you're looking for. If you know the exact name of the file, you'll type it completely. If not, you can use wildcards anywhere in your search term.
    • For example, to find all configuration files on your computer, you might use find / -type f -iname "*.conf". This returns the names of files ending with .conf.
  6. Advertisement
Section 2 of 8:

Finding by Name or Partial Name

  1. 1
    Use find /path -iname filename to search for a file by exact name. If you know the exact name and directory of the file, you'd use this command to find it.
  2. 2
    Use the wildcard character * to search for anything that matches the part of the query. The wildcard * character is useful for finding files when you don't know the full name. This can help you find files with specific file extensions (e.g., .pl or .c). Some helpful examples:
    • find /home/pat -iname "*.conf"
      • This will return all of the .conf files in Pat's user directory and subdirectories.
    • find / -type d -iname "*lib*"
      • This command finds all directories on the Linux filesystem containing the string "lib."
  3. 3
    Make your search results easier to manage with the less command. If you're getting lots of results, it can be difficult to sift through them. By piping the results to the less command, you can scroll through them easily. For example:
    • find /home/pat -iname "*.conf" | less
  4. Advertisement
Section 3 of 8:

Finding by Time and Date

  1. 1
    Use the -mtime option to find files by modification date (in days). Use this option when you want to find files last modified a certain number of days ago (or between two day ranges). Some examples:
    • find /home/pat -iname "*.txt " -mtime -2
      • This command will find all files ending with .txt in the directory /home/pat modified in the last two days.
    • Place a + before the number of days to indicate "longer than x days ago, or a - to indicate fewer than x days ago .[2] For example:
      • find . -mtime +90 : This command will display all files in the current directory that were modified more than 90 days ago.
      • find /home/pat -iname "*test*" -mtime -90 : This command will list all files in /home/pat with "test" in the name edited in the past 90 days.
    • If you want to find files modified by minutes instead of days, use -mmin instead. For example, to find all files in the current directory modified in the last 10 minutes, you'd use find . -type f -mmin -10.
  2. 2
    Use -atime and -ctime to find files by the date last accessed or date created. Replace -mtime with -atime to search by the last date accessed (opened), or -ctime to search by the day the file was created (e.g., 15 days ago, or more than 90 days ago.
    • If you'd rather search by minutes instead of days, replace -atime with -amin and -ctime with -cmin.
  3. 3
    Find files between two timestamps. To search for files between two specific dates and times, use the -newermt option. You'll need to use this option twice in your command—one for the start date of your search, and another for the end date. Here's how it will look:
    • find / -type f -newermt "2022-12-02 11:00:00" ! -newermt "2023-2-08 12:00:00"
      • This command will find all files on the Linux system with timestamps between 12/02/2022 at 11:00 AM and 2/08/2023 at 12PM.
  4. Advertisement
Section 4 of 8:

Finding by Size

  1. 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 results by size.
    • find / -size +50M -iname filename
      • This example 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).
Section 5 of 8:

Finding by Owner or Permissions

  1. Use the -user, -group, and -perm options to find 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.
    • Examples:
      • find / -user pat -iname filename searches for files called filename owned by the user pat.
      • find / -group users -iname filename searches for files called filename in the users group.
      • find / -perm 777 -iname filename searches for files called filename with 777 permissions (no restrictions).
  2. Advertisement
Section 6 of 8:

Combining Find Commands

  1. Use boolean operators to combine search filters. You can use the -and, -or, and -not operators to combine different types of searches into one. For example:
    • find /travelphotos -type f -size +200k -not -iname "*2015*"
    • The command will find files in the "travelphotos" directory that are greater than 200 kb in size but do not have "2015" anywhere in the file name.
Section 7 of 8:

Performing Actions on Found Files

  1. Combine commands to perform actions when files are located. You can combine find with other commands so that you can execute them on the files that are returned by the query. You can also use this feature to run the files that appear in find results. Separate the find command and the second command with the -exec flag, and then end the line with {} \;. For example:
    • 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.
  2. Advertisement
Section 8 of 8:

Searching for Text in Files

  1. 1
    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. Here's an example of a basic grep command:
    • grep -r -i "search query" /path/to/directory/
    • The -r flag 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 flag indicates that the query is not case-sensitive. If you want to force the search to pay attention to case, omit the -i flag.
  2. 2
    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/
  3. 3
    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.
    • grep -r -i "search query" /path/to/directory/ 2>/dev/null
  4. Advertisement

Community Q&A

  • Question
    What does the 'ls' command do?
    Community Answer
    Community Answer
    The 'ls' command lists all files in the current directory you are working in. To find out what directory you are working in, type 'pwd' (stands for "print working directory").
  • Question
    Which command will display the last ten lines of a file?
    Community Answer
    Community Answer
    You have to used tail command to display the last lines of a file, the command used is, "tail -n filename." To display last 10 lines the command will be: tail -10 filename.
  • Question
    How can I find a file in the root directory if I've forgotten the name and the directory in which it was placed?
    Living Concrete
    Living Concrete
    Top Answerer
    You can try searching for a file extension, or checking places where files are commonly saved, such as your home directory. If it's a text file, you can try using a recursive grep search ('grep -R') with text that you remember from the file as search criteria.
Advertisement

About This Article

Matt Ham
Co-authored by:
Computer Repair Specialist
This article was co-authored by Matt Ham and by wikiHow staff writer, Nicole Levine, MFA. Matt Ham is a Computer Repair Specialist and the CEO and President of Computer Repair Doctor. With over a decade of experience, Matt specializes in Mac, PC, iPhone, iPad, and Smartphone repairs and upgrades. Matt holds a BS in Mechanical Engineering from North Carolina State University and an MS in Mechanical Engineering from Columbia University. Matt has expanded Computer Repair Doctor to seven different locations. He is also a Co-Owner of Repair Life, a full-scale marketing agency specializing in driving both online and offline leads to cell phone and computer repair shops and device retailers. This article has been viewed 1,064,107 times.
How helpful is this?
Co-authors: 20
Updated: March 6, 2023
Views: 1,064,107
Categories: Linux
Article SummaryX

1. Open a command prompt.
2. Type / file -iname ″filename″
3. Replace ″filename″ with the file’s name.
4. Press Enter or Return.

Did this summary help you?
Advertisement