The best way to find files by name in Linux is using the find command with the “-name” option.
This command will search through the directories for files that have the specific word in their name.
This can be very useful when you need to find a specific file and don’t know where it is located.
In this blog post, we will discuss 6 ways to use the find command to search for files by name.
We will also show you how to pipe the output of the find command so that you can easily find the information that you need. Let’s get started!
The following Linux commands can be used to search files by name.
- find /path -name *.txt
- find /path -type f -name test.txt
- find /path -name failed*.* -type f
- find /path -type f -not -name “*.html”
- find / -name “file.txt” -size +4M
- find /dev/ -type b -name “sda*”
If you’re new to the world of Linux, you can refer to “Mastering the Linux Command: A Beginner’s Guide.” This comprehensive article serves as your gateway to gaining essential knowledge and honing the necessary skills for the Linux command line interface (CLI) with confidence.
Table of Contents
Find command in Linux
Linux find command is a powerful tool that can be used to locate and manage files and directories based on a wide range of search criteria. This post will cover how to find file by name in Linux.
When using find, we would follow the syntax below.
find [path] [expression]
- path: This is the directory we want to search.
- expression: This is where we place our search criteria for what we want to find whether by name, or file size etc.
To find files with a specific name in Linux, you can use the find command with the -name option. Here’s the basic syntax:
find [path] -name [filename]
Where path is the directory to search, and filename is the name of the file you want to find.
Understanding -name option in find command
The -name option in the find command is used to search for files or directories based on their names. It allows you to specify a pattern or exact name to match when searching for files.
The syntax for using the -name option in the find command is as follows:
find [path] -name "pattern"
- [path]: Specifies the starting directory for the search. It can be an absolute path or a relative path. If not provided, the search starts from the current directory.
- -name “pattern”: Specifies the pattern to match the file or directory names. The pattern can be a complete filename or include wildcard characters for partial matching. Common wildcard characters are * (matches any sequence of characters) and ? (matches any single character).
Here are a few examples to illustrate the usage:
- -name myfile.txt: Select files or directories with the exact name “myfile.txt”.
- -name “*.txt”: Select files or directories with names ending in “.txt”.
- -name “file*”: Select files or directories with names starting with “file”.
- -iname “NAME*”: Select files or directories with names starting with “name” or “NAME” (case-insensitive).
You can combine the -name option with other find options, such as specifying the starting directory, specifying the type of files to search (-type f for regular files), and using the -exec option to perform actions on the matched files or directories.
Find All Files With A specific Name in Linux
We can use the find command to search for all files with a specific name. In this example, we will search for all files with the name “test.txt”.
To do this, we will use the following command:
find / -name "test.txt"
This command will search through all of the directories on your system for a file named “test.txt“. The output of this command will look something like this:
/home/user/test.txt
/tmp/test.txt
As you can see, this command was able to find two files with the name “test.txt”. One file was in the user’s home directory and the other file was in the /tmp directory.
To list all files in the current directory, we can use the following command. ./ means current directory.
find ./
If we need to list the file under the current directory and its name is ‘test’, we can use this command.
find ./ -name 'test'
Absolute path is a complete path from the start of the actual filesystem from / directory.
find /opt -name .profile -print
This command searches the /opt directory and prints the complete path names of all files named .profile.
The /opt (slash) instructs the find command to search the /opt directory and all of its subdirectories.
In order not to waste time, it is best to limit the search by specifying the directories where we think the files might be.
Check this post to learn more about file path.
Find Files with a name pattern in Linux
We can use basic shell wildcard characters to broaden our search. For instance, the asterisk (*) represents any number of characters:
$ find ~ -iname "foo*"
/home/tux/Documents/examples/foo
/home/tux/Documents/examples/Foo
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt
A question mark (?) represents a single character:
$ find ~ -iname "foo*.???"
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt
This isn’t regular expression syntax, so the dot (.) represents a literal dot in this example.
If there is more than one file which name has ‘test’ in it like test1, test2, we can use this command.
find ./ -name '*test*'
If we want to search and list all files with a given name in multiple directories, we can either start the search at root directory, or if we know the directories, we can specify them.
Example:
find ./test ./logs -name failed*.* -type f
Sample output:
./test/failed_tests.txt
./logs/failed_tests.log
*
An asterisk is replaced by any number of characters in a filename. For example, ae* would match aegis, aerie, aeon, etc. if those files were in the same directory. You can use this to save typing for a single filename (for example, al* for alphabet.txt) or to name many files at once (as in ae*).
?
A question mark is replaced by any single character (so h?p matches hop and hip, but not help).
For example, if you want to find all of the files that have the word “file” in their name, you can run the following command:
find . -name '*file*'
This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name.
If you want to find all of the files that have the word “file” at the beginning of their name, you can use the following command:
find . -name 'file*'
This command will search through the current directory and all of its subdirectories for files that have the word “file” at the beginning of their name.
Find Multiple Files by name in Linux
Here is a little complex example. This command will remove all files named a.out or *.o that are not accessed for a week and that are not mounted by using nfs.
find / \( -name a.out -o -name '*.o' \) -atime +7 ! -fstype nfs -exec rm {} \;
Note: The number that is used within the -atime expression is +7. It is the correct entry if we want the command to act on files that are not accessed for more than a week (seven 24-hour periods).
Find Files not matching a name pattern in Linux
This Linux find command using the “not” operator creates a list of all files not ending with the .html file extension (filename pattern).
find . -type f -not -name "*.html"
We can also use the following command to get this. find . -type f ! -name “*.html”
Excluding Files and Directories in Find command
You can use the “find” command with the “-exclude” option to exclude certain files and directories from your search.
For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the PDF files, you can run the following command:
find . -name 'file*' -exclude *.pdf
This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the PDF files.
You can also use the “-exclude-dir” option to exclude certain directories from your search. For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the files in the “tmp” directory, you can run the following command:
"find . -name 'file*' -exclude-dir tmp
This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the files in the “tmp” directory.
Advanced options in Find command
The find command also allows you to use advanced search options to filter results. For example, you can use the find command with the -type option to search for files of a specific type.
You can use the find command with the -mtime option to search for files that have been modified in a certain amount of time.
You can also use the find command with the –maxdepth option to specify how deep you want to search into directories.
If you want to find all of the PDF files that are older than one week, you can run the following command:
find . -type f -mtime +7
This command will search through the current directory and all of its subdirectories for PDF files that have been modified more than seven days ago.
Find command and other options in Linux
- find / -name “file.txt” -size +4M
- find /dev/ -type b -name “sda*”
- find / -type d -name “a.txt”
- find /opt -type f -name ‘howtouselinux’ -mtime +1
Related:
- 20 Advanced Linux Find Command Examples
- How to use Find Command in Linux
- 10 Linux Find Exec examples – Advanced Part
FAQs
How do you search for files by name in Linux? ›
Finding files by name is probably the most common use of the find command. To find a file by its name, use the -name option followed by the name of the file you are searching for. The command above will match “Document. pdf”, “DOCUMENT.
How to find path by using file name in Linux? ›- Method 1: The “readlink” Command.
- Method 2: The “realpath” Command.
- Method 3: The “dirname” Command.
- Method 4: The combination of “basename” and “dirname” commands.
- Method 5: The “find” Command.
- Method 6: The “ls” Command.
The locate command finds files in Linux using the file name. locate is used for obtaining instantaneous results, and it is an essential utility when speed is a priority. The command performs the search using a database containing bits of files with the corresponding paths in the system.
How to find all files names containing string in Linux? ›grep command in Linux that is used to search for files containing a specific text or string. By default, it shows us the lines in the files that contain the particular text. If we append the -l option to it, the command will show us all the files that contain the particular text.
How do you search a file name in Unix? ›The Unix find command is a powerful utility to search for files or directories. The search can be based on different criteria, and the matching files can be run through defined actions. This command recursively descends the file hierarchy for each specified pathname.
How do I search for a file by name in command prompt? ›In Command Prompt, type dir "search term*" /s , but replace the words "search term" with the file name or a part of the name you remember.
How do I find the path of a file name? ›Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).
How do I find a file in Unix without path? ›Replace "pattern" with a filename or matching expression, such as "*. txt" . (Leave the double quotes in.)
How do I find the path location in Linux? ›The command pwd stands for print working directory. When you typed pwd, you asked your Linux system to display your current location. Your system responded by printing the full path of the current directory in the shell prompt window.
How do I find the path of a directory in Linux? ›To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd. This tells you that you are in the user sam's directory, which is in the /home directory. The command pwd stands for print working directory.
How do I find the current path in Linux? ›
To know your current directory, you can use the pwd command which stands for Print Working Directory. The name of the current working directory is the last directory in the absolute path.
How do I find the path of a specific folder in Linux? ›How to Find a Directory in Linux. Searching for a directory is possible by passing the d to the -type parameter in the find command. In the above screenshot, we're finding a directory named zip from our current directory. Similarly the -type option accepts other parameter options to simplify our finding process.