Javatpoint Logo
Javatpoint Logo

How to Find a File in Linux from Command Line?

It goes without saying that all good Linux desktop environments provide the ability to find our file system for folders and files. If our default desktop does not- because it is Linux- we can always get an app to search our directory hierarchy a breath.

But what about the terminal or command line? If we happen to work in the terminal window frequently or we conduct GUI-less Linux servers, where do we turn when we need to find a file? Linux, fortunately, has exactly what we need to find the files in question, established right into the computer.

The command is find. Once we know it, we can start implementing this command into our Bash script to understand it even more appealing. Let's get started with the command, i.e., find, so we can take control of finding the file on our Linux desktops and service without the requirement of a GUI.

Usage of the find command in Linux

If we are Linux users, then we can't only rely on GUI to implement several tasks. Hence, a solid grip on terminal commands is very important. Every distribution, based on Linux, executes the commands to implement distinct administrative tasks.

Even if the Linux terminal can be defined as a text interface that looks complex, but it's actually very easy to use, flexible, and quite a helpful tool. Commands can be easily copied from any online source and pasted onto the terminal to implement several operations. There are hundreds of commands, but we will focus on the command, i.e., find, in this article. The find command is used to search, filter, or find folders and files in our system according to the user-specified conditions and implement many operations on them.

Let's see how to use the command, i.e., find, its syntax, and many operations implemented by this command.

Find Command Syntax in Linux

The syntax of the find command is mentioned as follows:

Three attributes of the above syntax are explained below:

  • [path]: It specifies the directory in which to start searching.
  • [options]: It specifies the filtering criteria. For example, searching a folder/file by its date, time, permission, or name.
  • [expression]: It specifies what actions to implement with the file.

Every above attribute is optional because it can be applied according to the need.

Finding File by Its Name

We can use the following command to find the file by its name:


How to Find a File in Linux from Command Line?

After find, the dot in the above command represents the current directory. If we do not remember the same file name, the searching process can further be prcised and case-insensitive by utilizing the "iname" instead of "name".

Finding File by Its Type

We can use the "-type" flag along with letters that are also called descriptors, like "s" for sockets, "I" for symbolic links, "d" for directories, and "f" for files to find the file by type.

To find every directory, we can use the following command:


How to Find a File in Linux from Command Line?

To find files, we can use the following command:


How to Find a File in Linux from Command Line?

Finding File by Its File Extension

To find the file by its pattern, e.g., file extension, like showing every file using .txt, we can use the below command:


How to Find a File in Linux from Command Line?

With .txt, every file will be shown with its related directories.

Finding and Removing File

To find and remove the file, we can use the following command:


How to Find a File in Linux from Command Line?

First, the above command finds the file and then removes it.

To delete every file with the .txt extension, we can use the following appended command:

Finding File by Its Size

Also, the find command can find a file by its size. We can simply use the option, i.e., "-size" along with the descriptors, like "G" for gigabytes, "M" for megabytes, "k" for kilobytes, "c" for bytes, and "b" for 512 kb blocks.


How to Find a File in Linux from Command Line?

The above-mentioned command finds any file with less than 1024 byte size. This search process can be refined further. For example, if we wish to find every file that is less than 1 MB, we can use the following command:


How to Find a File in Linux from Command Line?

For every file that is greater than 1


How to Find a File in Linux from Command Line?

Also, a size range can be defined with the help of the following appended command:

Finding File by Its Permission

We can use "-perm" and permission code to find a file by its permission. The command is mentioned below:


How to Find a File in Linux from Command Line?

Finding Text in Text Files

We can use the following command to search for text in two or more text files on our system:


How to Find a File in Linux from Command Line?

The above command is finding "Hello" in the text files. The result is text strings through the text files having "Hello".

Finding File by Its Modification Time and Date

We can use the following command to find a file by the last modification:


How to Find a File in Linux from Command Line?

The command is finding a file last changed 4 minutes ago, where "m" in "-mmin" represents the "Modification".


How to Find a File in Linux from Command Line?

The command is finding a file last accessed 4 minutes ago, where "a" represents the "Access". We can use the "-mtime +4" instead of "mmin +4" to use a file that was changed 4 days ago.

How to find files in Linux?

The file system is a primary thing in any operating system as it holds every data in the folders and files in distinct formats. In this article, we will explain some commands to search the files.

Technique 1: Locate

Let's get started with the most common instruction, i.e., locate, to search for a file by its name from our system. The "-c" flag of the "locate" instruction is mainly designed for getting the count number for a particular file in our system. Let's start with the first "-c" option under using it inside the instruction with the "new.sh" file name. It will return 5 files with the name.

Here is the option, i.e., "-n" of "locate" instruction to show the records for a particular file on our terminal window as per the choice of the user. We need to specify the number in the command to show the file records. While we ran the instruction with the option, i.e., "-n" and 1 value to show one record for the search output of the "new.sh" file at the home directory. It will return one record for the file path. This path will be attached to our root directory or home directory.

It will return a total of 3 records on our shell window when we have utilized this instruction along with the updated number "3" for the "-n" option to show the search output for the "new.sh" file. It also shows that the file is positioned in the Trash folder.

We have received 5 search records for the "new.sh" file on executing the "locate" command along with the option, i.e., "-n" of value 5. There is also another file with the "new.sh.swp" name in our directories. There is the exact file name in the directory, i.e., "var", of the filesystem of Ubuntu.

In the "locate" instruction, the "-b" option can do our search for the same file name from our directories. So, we need to run with it with that file name in one inverted comma. It provides 3 records because the exact file name has been positioned at just 3 locations.

Also, the Linux "locate" instruction can be used to bring information about our current database. We need to use the "-S" flag with it, and it will give us the database path, the total files and directories on our system, the bytes to save our records, and bytes within the file names.

Technique 2: find

The "find" instruction can be used to find any particular file in our Linux system. Also, it came up with several options to show different results. The e initial option is the "-name" to find a file in double inverted commas using its name. We can use this option with the path to a few directories to find a file. If we do not wish to use the path, we can leave it with the "." symbol. It will show 3 records with the exact file name.

Also, we can utilize an explicit path to check the file inside it. For example,

Let's say we wish to find a symbolic link file for the file's ".sh" extensions; we need to use the "-L" flag, "-name" option, and the path in our command. In the name specification, the "*" symbol is used to find "all" bash files along with the ".sh" extensions. It will return 4 records on the screen.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA