Download Article
The easiest way to locate files by name, partial name, or date at the command line
Download Article
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.
Steps
Section 2 of 8:
Finding by Name or Partial Name
-
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.
-
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."
- find /home/pat -iname "*.conf"
-
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
Advertisement
Section 3 of 8:
Finding by Time and Date
-
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]
X
Research source
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 .
- find /home/pat -iname "*.txt " -mtime -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 .
-
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.
- find / -type f -newermt "2022-12-02 11:00:00" ! -newermt "2023-2-08 12:00:00"
Advertisement
Section 8 of 8:
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. 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.
-
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/
-
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
Advertisement
Community Q&A
Search
-
QuestionWhat does the 'ls' command do?Community AnswerThe '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").
-
QuestionWhich command will display the last ten lines of a file?Community AnswerYou 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.
-
QuestionHow can I find a file in the root directory if I've forgotten the name and the directory in which it was placed?Living ConcreteTop AnswererYou 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.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Video
Tips
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
References
About This Article
Article Summary
X
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?
Thanks to all authors for creating a page that has been read 1,229,573 times.
Reader Success Stories
- "I needed a quick tutorial on how to search for files, given a piece of a name, using the terminal. This tutorial is helpful as it is straight to the point with gradual complexity, which is great!" ..." more
Advertisement