Linux 101 : Finding files - Find and Locate tools -



Using the "locate" command:

If the "locate" is not installed on our system, we could use the below command (debian) to install it: 


This will install the locate tool and upgrade its database.

After the installation we could for example try to find all the python files using:


We could also use it with the "-i" option so it ignores the case:


So it would look for "python" or "Python" for example.

The "find" utility:

We could look for the file name "python" (in the root directory) with random characters on each side using the below:


If we want the find a file taking into account the case of the letters in the search (upper or lower case) we use the below:


Finding file according to their size:

The below command find all the files bigger than 20 megabytes:


The below command finds all the files smaller than one megabyte:


The below command finds all the files which size is between 200 megabytes and one gigabyte, it then executes a "du" command to list the results (files and their sizes) of the previous command "find":


Finding files by username:.

We can search for files that have a specific owner (-user) or a specific group (-group).
In the example below, we find all the files in the "/home" directory that have the "debian" user as the owner.


We use the below command to look for the files belonging to the "debian" group:


We use the below command to look for files not belonging to the "debian" group:


Remark:

The "ls" command uses as argument the results of the "find" command.

Finding files that have certain permissions:

In the below example, we are looking for the files that have the read, write and execute rights for the owner and read and execute rights for the rest (regardless of the other permissions):


We could fine tune the search and only look for "folders" and not "files" using the "type" option:


Remark:

We could also use "-perm /420", that would look for files that have the "read" permission set for the owner or the "write" permission set for the group, regardless of how the other permission are set.

Changes in the files:

To display the files in the "/etc" folder that have changed in the last 10 minutes (counting from now) we use:



To display the files of which the status (ownership or permissions) have changed in the last week (counting from now), we use:




To display the files that haven't been accessed seven or more days ago, we use:


  • atime : access time
  • ctime : "change" time
When we don't use a "+" or "-" sign, it means exactly the number of days or minutes ago, for example "-atime 7", means exactly seven days ago .

Finding files by user and size:

To display a file by "user_name" and "size" ( more than one megabyte in size in the below example), we use:


The exec command:

The exec instruction executes a command in the bash.
The {} stands for the result of the previous command "find".

The example below finds all the files that are bigger than one megabyte in size and displays their sizes using the "du" command:


Comments

Leave as a comment:

Archive