search file in linux

How to Search a File in Linux

Many times, you may need to search a file in Unix or to find a file in directory in Linux. Here’s 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

Here are the steps to search a file in Linux.

We will use the find command to search a file in Linux. 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

 

Bonus Read : How to Install Zip File in Linux

 

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

 

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.

 

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.

 

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!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!