Polecenia Linuksa

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

How to Use the “find” command in Linux to Search Files?
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:

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

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

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 . -type d

To search for files, use:

$ find . -type f

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 . -name *.txt

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 \;

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

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

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

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

$ find . -type f -size +1M

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 . -perm 664

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' \;

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

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

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.

Gry How to Show FPS Counter in Linux Games
How to Show FPS Counter in Linux Games
Linux gaming got a major push when Valve announced Linux support for Steam client and their games in 2012. Since then, many AAA and indie games have m...
Gry How to download and Play Sid Meier's Civilization VI on Linux
How to download and Play Sid Meier's Civilization VI on Linux
Introduction to the game Civilization 6 is a modern take on the classic concept introduced in the series of the Age of Empires games. The idea was fai...
Gry How to Install and Play Doom on Linux
How to Install and Play Doom on Linux
Introduction to Doom The Doom Series originated in the 90s after the release of the original Doom. It was an instant hit and from that time onwards th...