in

LinuxLinux

How To Find Files Using “Find” Command in Linux Using Terminal Command?

Using FIND command to find files in Linux and it’s distros!

Using File Command in Linux

F

ind Command in Linux: “FIND” is a command line utility method which will allow any user to search for files in any directories based on the user given expression. This is the most powerful command given to the sudo user in Linux command. You can use this Find commad to search the files based on Type, Extension, Format, Date, Size & Permission levels. This Find command has the ability to work with many other commands such as GREP & SED. In this article, we explained some important puposes of “Find” command in Linux PC.

Jump To:

Find Command Basic Syntax in Linux:

The basic syntax for using the Find command in Linux is:

$find [options] [path-to-directory] [user-expression]

The explanation of the above syntax is:

  • [Options] is the attribute of the “find” command, which controls the treatment of the debugging option, links & optimization method used to find the file.
  • [path-to-directory], which defines the path or address of the directories that the file need to be searched.
  • [user-expression], is the area where the user can define the functions or activities that need to be performed with the find command. These expressions are made up of options such as patterns and actions separated by operators.
Example Syntax for Find Command:
find -L /home/apps/ -filename “*.css” -exec chmod 644 {} \;

The above command is the sample syntax for using the “Find” command. This command includes a parameter -L (options) which allows the find command to follow symbolic links, searches the entire directory tree beneath /home/apps/ (path-to-directory) for all files ending with .css (expression) and set the permissions of all matched files to 644.

 Find Files by Name in any directories:

You can find the files by using the file name in any directories. This is the most common command used to find the files in Linux. Use the file name along with the find command will help you to do the purpose. We provided the sample command to use this function efficiently.

$sudo find /home/tecrobust -type f -name main.txt

The above command is the sample for using the find command by its file name!. To search for a file called main.txt in the /home/tecrobust directory you would use the above command.

Check this too  How to Delete Line in Vim Editor on Linux Using Terminal Command!

If you are not sure about the file name having the case letter or not. Then you can run the find command with iname option. Follow the below command:

$sudo find /home/tecrobust -type f -iname main.txt

The above command will look for the filename with no case search. If it matches the file name and it will bring you the file. If you enter the file name as “MAIN.txt” it will also look for “main.txt” with “iname” option.

Find Files by Size in any directories:

You can use the “Find” command by the size of the file. You just need to use or add the -size attribute with the size value of the file.

Before getting into the syntax and example for these options, we suggest you learn the attributes that used below to define the size value of the file.

    • b: 512-byte blocks (default)
    • c: bytes
    • w: two-byte words
    • k: Kilobytes
    • M: Megabytes
    • G: Gigabytes

Now, we will show you the perfect example for using the find command with file size.

$find /dir -type f -size 2048c

The above command will help you find the file in the “/dir” directory having the file size greater than 2048 bytes.

If you want to find the file having the file size less than 2MB in the working or current directory. Then use the below command.

$find . -type f -size -2M

You may able to notice the “-” minus symbol used, which do the find action of the files having the file size less than 2MB.

If you want to find the files having the size greater than 2MB then you just need to follow the below command:

$find . -type f -size +2M

You may able to notice the “+” plus symbol used, which do the find action of the files having the file size greater than 2MB.

Check this too  Wget Command in Linux: Wget Command Syntax, Options & Examples!

You can even look for a file to have the size range between 1MB to 2MB and then use the following command:

$find . -type f -size +1M -size 21M

Find Files by Type in any directories:

If you want to find the file with its type and then you just need to use the -type attribute with the find command.

You can use the following descriptors:

  • f: a regular file
  • d: directory
  • l: symbolic link
  • c: character devices
  • b: block devices
  • p: named pipe (FIFO)
  • s: socket
$find . -type d

The above command can be used to find the file in the current working directory.

$find /var/www/app -type d -exec chmod 0755 {} \;

The above command will find the file inside the current working directory and will set the file to “chmod 0755” value.

Find Files by Date in any directories:

In this section, we will tell you about finding a file based on the last modification date or change time. This command works similarly to the “size” command.

If you want to find the file which is modified less than 3 days before and then you just need to follow the below command:

$find /etc/apache2/conf.d -name "*.conf" -mtime 3

The above command will find the file under the directory which has been edited or modified less than 3 days before.

You can also find the file which has been modified very long time ago and then we suggest you follow the below command:

$find /tecrobust -mtime +45 -daystart
The above command will help you to find the file which has been modified 45 days before.

Find Files by Extension in any directories:

This command will help you find the file based on the file’s extension. You need to follow the below command:

$find /var/log/apache2 -type f -name '*.main.php'

The above command will help you to find the “main.php” in the apache2 directory. The above command will help you to find the file based on the extension.

If you want to find all the files except some file extensions. You need to find all files in the apache2 directory except the files having .php extension. Then you need to use the following command.

$find /var/log/apache2 -type f -not -name '*.main.php'

You may able to notice “-not” attribute with the find command. This will find all the files except some files having .php extension.

Check this too  Bash Case Statement Command Syntax & Examples in Linux {Latest}

Find Files by Root User in any directories:

This section explains that using the find command to find the files based on the owner or user. Use the below command to execute the command:

$find / -user tecrobust

The above command explains that finding the file owned by the definite user “tecrobust“.

$find / -user www-data -type f -exec chown apache {} \;

Find all files owned by the user www-data and change the ownership of the matched files from www-data to apache.

Find Files by Permissions in any directories

This command is mainly used to find the files based on its extension. Use the -perm option.

$find /home/s1/html/main/ -perm 644

The above command will let you find all files with permissions of exactly 775 inside the /home/s1/html/main/ directory.

$find /home/s1/html/main/ -perm 644

If you want to find the file having the permission to all users and then use the following command:

$find . -perm -664

Find the files & Delete the files:

To delete a file which matching with the given expression and then you need to use the -delete attribute with the find command.

The below command will help you to find the file and delete the file with the find command.

$find /var/apache/log/ -filename `*.temp` -delete

The above command will find the file having the file extension “.temp” & inside the directory /var/apache/log/ and deleting the file.

Important
You need to use the -delete command with very careful. The command will delete everything below the starting points you specified.

Verdict:

Yes!!, I hope you guys learned about using Find command in Linux. If you have any queries in implementing Find command and then comment us below about your query, We will get you the proper solution to your query.

Article Review
  • Content Explanation
  • Commands Listed
  • Commands Working
  • Alternate Usage of Commands
4.8

Summary

For more article, Kindly visit our Homepage.

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by Julian

Hello Folks! I'm Julian, Promoted to the Content writer for the category of Commands & Web Apps in Tec Robust. Enjoy my content & Learn more Linux Commands!

Contact me:
tecrobust [@] gmail.com
Sub: Message to Julian

Opera for Ubuntu 18.04

How to Install Opera Browser on Ubuntu 18.04 LTS & 19.04 Disco Dingo

Random Password Generator

Random Password Generator- Generate Random Password Online {Official}