Javatpoint Logo
Javatpoint Logo

Linux find Command

The find command helps us to find a particular file within a directory. It is used to find the list of files for the various conditions like permission, user ownership, modification, date/time, size, and more.

The find utility comes by default with most of the Linux distros, so we don't need to install any additional package. It is one of the most essential and used commands of the Linux system.

The following symbols are used to specify the directory:

(.) : For current directory name

(/) : For the root directory

Examples of the find Command

Let's see the following examples of the find command:

Find files by name

We can search all the files ending with the extension '.txt.' To do so, execute the below command:

The above command will list all the text files from the current working directory. Consider the below output:

Linux Find

From the above output, all the files having '.txt' extension are listed with the find command.

Finding files by type

The '-type' parameter is used to specify the file type.

Some of the file types are as follows:

  • f: regular file
  • d: directory
  • l: symbolic links
  • c: character devices
  • b: block devices

Consider the below command:

The above command will list all the directories having '.bak' extension. Consider the below output:

Linux Find

From the above output, the command "find . -type d -name "*.bak" is displaying all the directories ending with '.bak'.

Find newer files

The '-newer' parameter helps in searching the files which are newer than the mentioned file. Consider the below command:

The above command will display all the files which are newer than ' msg.txt' from the current working directory. Consider the below output:

Linux Find

From the above output, all the files displayed are newer than the 'msg.txt' file.

Find and delete a file

The '-delete' option is used to delete a specific file. We need to be very careful while using this command because there is no undo option if it is once executed. Consider the below command:

The above command will delete the file 'Demo.txt' from the current working directory. Consider the below output:

Linux Find

Find a directory

The ' type -d' option is used to find a directory. Consider the below command:

The above command will find the 'Newdirectory' location. Consider the below output:

Linux Find

Find files by modification time

The '-mtime' option, followed by the number of days, is used to find the files by modification. The number of days can be positive or negative. The negative value will be used for less than like -1 is used for the last day, and similarly, +1 will find the file for the more than one day ago. Consider the below command:

The above command will find the files which are modified within the last day. Consider the below output:

Linux Find

Find files by permission

The '-perm' option is used to find files by permission. Execute the find command with the '-perm' option and pass the required value. Consider the below command:

The above command will list the files from the specified directory that everyone could read, write, and execute.

Find and replace files

To find and replace files, we have to combine find command with the sed command. To operate on files, use the '-exec' option with the find command. Consider the below command:

from the above command, the specified occurrence will be replaced. Consider the below output:

Linux Find

Find text within multiple files

We can make another combination of the find command with the grep command to find the text from the various files. Consider the below command:

The above command will find the lines containing the text 'demo' from all the text files within the directory 'Newdirectory.' Consider the below output:

Linux Find

From the above output, we can see the lines having text 'demo' has been displayed.


Next TopicLinux Locate




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA