35 C
Jaipur
Saturday, May 8, 2021

How to Use the “find” command in Linux to Search Files? – Linux Hint

Must read

If you are a Linux user, then you can’t just rely on GUI to perform various tasks, therefore, a solid grasp of terminal commands is really essential. All distributions based on Linux run the commands to perform different administrative tasks.

Although the Linux terminal is a text interface that seems complex, it is actually very flexible, easy to use, and quite a useful tool. Commands can easily be copied from online sources and pasted into the terminal to perform various operations. There are tons of commands but this post will focus on the “find” command.

The “find” command is used to find, filter, or search files and folders in your system according to user-specified conditions and perform several operations on them.

Let’s discuss how to use the “find” command, its syntax, and various operations performed by this command in detail.

Syntax of “find” Command in Linux

The “find” command syntax is shown below:

find [path] [options] [expression]

Three attributes go with the “find” command:

  • [path]: It defines the directory where to begin searching.
  • [options]: It defines the criteria of filtering e.g. searching a file/folder by its name, permission, time, or date.
  • [expression]: It defines what actions to perform with the file.

All of the above attributes are optional as they can be used according to the requirement.

For demonstration, I have created different directories and some text files, see the image below:

Finding a File by Name

To search the file by name, use the below-given command:

$ find . -name MyTextFile1.txt

find/2%20copy.png

The dot after “find” in the above command indicates the current directory.

If you don’t remember the exact file name, the search can further be refined and make it case-insensitive by using the “-iname” in the place of “name”:

$ find . -iname mytextfile1.txt

find/3%20copy.png

Finding a File by Type

To find a file by its type, use the “-type” option with letters that are also known as descriptors such as “f” for files, “d” for directories, “l” for the symbolic link, and “s” for sockets.

To search all directories use:

find/4%20copy.png

To search for files, use:

c%20copy.png

Finding a File by the File Extension

To search the file by pattern, e.g., file extension, such as displaying all the files with “.txt”, use the following command:

find/5%20copy.png

All the files with “.txt” will be displayed along with their corresponding directories.

Finding and Deleting a File

To search and delete a file, use the command below:

$ find . -iname mytextfile1.txt -exec rm {} ;

find/6%20copy.png

The above command first searches the file and then delete it. The image is demonstrating that “MyTextFile1” has been deleted.

To delete all files with extension “.txt”, you can use the appended command:

$ find . -name *.txt -delete

find/7%20copy.png

Finding a File by Size

The “find” command can also search a file by size. Simply use “-size” option along with its descriptors such as “b” for 512 Kb blocks, “c” for bytes, “k” for kilobytes, “M” and “G” for megabytes and gigabytes respectively:

$ find . -type f -size -1024c

find/8%20copy.png

The command mentioned above searches all files with a size less than 1024 bytes. The search can further be refined, for instance, if we want to find all the files that are less than 1Mb, then we use the command below:

$ find . -type f -size 1M

ab/a%20copy.png

For all the files that are greater than 1Mb, use the command below:

$ find . -type f -size +1M

ab/b%20copy.png

A range of size can also be defined, using the appended command:

$ find . –type f -size +1M -size 10M

Finding Files by Permission

To search a file by permission, we will use the “-perm” option, then permission code, as demonstrated below:

find/10%20copy.png

Find a Text Within Text Files

To find text in multiple text files in your system,s use the command given below:

$ find . -type f -name *.txt -exec grep ‘Hello’ {} ;

find/12%20copy.png

The command is searching the “Hello” word in the text files. The output is text strings from the text files containing “Hello”.

Finding a File by Modification Date and Time

To access a file by its last modification, use the command below:

$find . -type f -iname *.txt -mmin +10

find/13%20copy.png

The above command is searching for a file last modified four minutes ago, and “m” signifies the “Modification”.

$find . –type f -iname *.txt -amin -10

find/14%20copy.png

The above command is searching for a file last accessed 4 mins ago, and the “a” in “amin” is signifying “Access”. To access a file that wasmodifiedfour days ago, use “-mtime +4” in the place of “mmin +4”.

Conclusion

The “find” command in Linux is a very useful command that lets you search a file or directory using different criteria, and even allows you to modify the files from the terminal. In this guide, we observed the syntax of the “find” command in Linux and learned how to use the “find” command to perform various functions.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article