Last updated on September 23rd, 2024 at 09:07 am
Many times, you may need to search a file in Unix or to find a file in directory in Linux. You may need to do this as a standalone command, or from within shell script. There are several ways to locate one or more files in Linux. In this article, we will learn how to search a file in Linux. You can use it to search a file in Ubuntu, CentOS, Redhat, Fedora, and other Linux systems.
How to Search a File in Linux
There are many commands such as locate, find, etc. to search directories for files. Here are the steps to search a file in Linux. We will use the find command to search a file in Linux. It is one of the most powerful commands to search for files and directories. It provides very powerful options that are easy to use and understand. Here’s the syntax of find command.
$ find starting_path options search_expression
In the above command, you need to specify the starting path where find command needs to start searching file, options as per your requirement, and the search expression to look for in file attributes.
If you don’t specify the starting path, find command will start searching in your present working directory.
For example, here’s the command to find “document.txt” file in /home/ubuntu folder
$ find /home/ubuntu/ -name document.txt
Please note, find command is case-sensitive and does an exact match by default. So it won’t match files like “DOCUMENT.TXT” or “word-document.txt”
Bonus Read : How to Create Zip File in Linux
We will look at some commonly used examples for find command.
Find Files by Name in Linux
Here’s the command to find files by name, with a case-sensitive exact match, using -name option. The following command searches exactly for linux.txt file at /home folder
$ find /home -name linux.txt
The above command will match only linux.txt and no other filename
Here’s the command to find files by name, with a case-insensitive exact match, using -iname option. The following command searches for unix.txt file at /home folder
$ find /home -iname unix.txt
The above command will match unix.txt, UNIX.TXT but not 123unix.txt.
Here’s the command to find files by name, using wildcard characters. The following command searches for any file that contains the string unix in its filename. You can use wildcard characters like * to specify search patterns.
$ find /home -name "*unix*"
The above command will do a case sensitive match for “unix” pattern. So it will match 123unix.txt, unix.pdf but not UNIX.TXT
If we use -iname option instead of -name option, it will do a case insensitive match.
$ find /home -iname "*unix*"
The above command will do a case sensitive match for “unix” pattern. So it will match 123unix.txt, unix.pdf and also UNIX.TXT.
We can use the above-mentioned pattern matching to find files by extension. For example, here’s the find command to search all pdf files in /home directory
$ find /home -name *.pdf
System administrators need to commonly search for log files. Since they end with .log extension, we can easily use the above method to search for log files on your system.
$ find . -name *.log
Bonus Read : How to Install Zip File in Linux
How to Search for Hidden Files
In Linux, hidden files start with dot(.) in their filename. Find command also allows you to search specifically for hidden files. You can easily do this using the following command.
$ find . -type f -name ".*"
The above command is similar to searching for files using wildcard patterns in the previous section. Here we specify that our filename starts with dot(.)
Find Files by Type in Linux
Since there are different file types in Linux, you can use -type option to find files by their type. Here’s the command to find regular files in /home directory
$ find /home -type f
Here’s the command to list all empty files in /home directory using -empty option
$ find /home -type f -empty
Here’s the command to list all symbolic links in /home folder
$ find /home -type l
Search For Directories
Based on the above example, you can also search for directories in Linux by mentioning d after -type option. Here’s the command to find all directories in /home
$ find /home -type d
Find File by Size in Linux
You can easily search file by size in Linux using -size option. Here’s the command to find file greater than 10Mb in /home folder
$ find /home -size +10MB
You can use c, KB, MB, GB to describe file sizes in bytes, kilobytes, megabytes and gigabytes respectively.
Search Empty Files
On the other hand, you may need to sometimes look for empty files in your system. You can do this by mentioning empty after -type option.
$ find . -type empty
Bonus Read : How to List all files in Directory
Find File by Modified Date in Linux
You can easily search file by modified time in Linux using -mtime option. Here’s the command to find file modified in last 7 days
$ find /home -mtime 7
You need to input number of days after -mtime option above.
Look for Files based on Content
Find command can also be used in conjunction with other commands such as grep, thanks to -exec option. Grep is a tool that allows you to easily search files and directories for specific content. Here is a simple example to recursively look for all files and directories under current directory, for ‘hello world’.
$ find . type f -exec grep "hello world" '{}' \;-print
In the above command, find looks for all files (“type f”) in present directory (“.”) and executes (-exec) grep command ‘grep “hello world”‘. We also provide curly braces within single quotes are placeholder for storing results of find command. We use semi colon to terminate the find command and escape it with backslash to prevent shell from interpreting it. Lastly, we print result using -print command.
Conclusion
In this article, we have learnt several different ways to easily search the file you need, using find command. Although there are several commands to search files and directories, nothing comes close to find, with its options and utility. We have also covered several common use cases system administrators and Linux developers face while searching files and directories. Hopefully, now you can easily search a file in Linux.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.