How to Search a File in Linux

How to Search a File in Linux

There are hundreds and thousands of files on your computer. It is impractical to search any file manually. This is where the find command comes in. Find is a very handy command for sysadmins and mastering it is necessary.

In today’s write-up, we are teaching you the method to search files in your Linux system with 7 examples. We have implemented the commands on Red Hat Enterprise Linux 9.

Syntax

You can search a file in Linux by using the find command. The general syntax of the command is as follows:

find [PATH] [OPTIONS]

Examples

Search a file by name

This is one of the simplest examples of find command. Invoke the find command by providing the file path followed by its name Suppose you want to search a file foo.txt in your home directory. You would run:

find /home -name “foo.txt”

When you are unsure about the exact name of the file and you have just a hint in your mind say foo text file. You can run:

find /home -iname “*foo*txt”

-i option is used for incase sensitive. Suppose foo and FOO would be same.

Search a file by type

You can search any file based on its type. Following options are available:

-f – regular file

-d – directory

-l – symbolic link

-c – character devices

-b – block devices

-p – named pipe

-s – socket

Suppose you want to search a directory sample_dir in the home folder, you would type:

find /home/ type -d -name sample_dir

Search files by extension

You can search or list files by using their extension. Suppose we want to search all text files located in the home directory, we should run:

find /home/ -type f -name “*.txt”

You must enclose the file extension in inverted commas otherwise you will get an error.

Search files by sizes

You can search files based on their sizes. Invoke the find command with -size option. Following parameters are available:

b – 512 byte block

c – bytes

w – two byte words

k – kilobytes

M – Megabytes

G- Gigabytes

Suppose you ran out of memory and you want to find all those files that are in 5 MByte size. You would run:

find /home/ -type f -size 5M

You can use the – and + symbols to search the file greater than or less than respectively. Suppose you want to find all files less than 1 KB, you would type:

find /home/ -type f -size -1K

Similarly, for files greater than 1MB, you would execute:

find /home/ -type f -size +1M

You can also specify the range as well. Suppose, you want to search files between 1MB and 5 MB, you would type:

find /home -type f -size +1M -size -5M

Search files by user name

You can search the files owned by a specific user or group by using -user or -group options respectively.

Suppose you want to search all text files of user “karim”, you would invoke the find command as follows:

find /home/karim -type f -user karim -name “*.txt”

Search files and delete them

You can search the specific files and delete them using -delete option in the following fashion. Suppose you want to search and delete all text files in the home directory /home/karim, you would run:

find /home/karim -name “*.txt” -delete

When deleting the files using the above method, make sure you are deleting the correct files.

Search empty files

This is one other useful example. Suppose you want to search all regular empty files in your root directory. You would run:

find /home/karim -type f -empty

Conclusion

I hope this article is useful. Please like and share on Linkedin, Facebook, and other social media pages. Keep visiting Linux world.