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:
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:
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”:
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:
To search for files, use:
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:
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:
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:
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:
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:
For all the files that are greater than 1Mb, use the command below:
A range of size can also be defined, using the appended command:
Finding Files by Permission
To search a file by permission, we will use the “-perm” option, then permission code, as demonstrated below:
Find a Text Within Text Files
To find text in multiple text files in your system,s use the command given below:
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:
The above command is searching for a file last modified four minutes ago, and “m” signifies the “Modification”.
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”.
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.