Find command in Linux

If we work with many files it is normal that at some point we forget the location of the file. This can be aggravated if our computer does not have a suitable tool or a desktop environment that allows us to do so graphically. But the best thing about Linux is the terminal so there is the find command that will help you find files or folders quickly and efficiently.

The Find in Linux command

The most common command used to find and filter files on Linux is the find command.

This command performs a search of one or more directories on the system along with search criteria. In summary, the basic syntax used by the find command is as follows

find [directory] [options] [search term]

Where we first run the find command, then we indicate a directory where the search will start, then the options, and finally a search term that can be a name or something similar.

By default, the find command will search the current directory of the prompt. This can be changed to an explicitly stated directory or by using a reference such as

/ Searches the entire system
~ You will only search in the home directory
. Search the current directory.

Using the find command – Searching by name

As you might expect, the most important and well-known way of searching is by name. The find command requires the -name option

For example

find . -name file

In this case, we are looking for files in the current directory (.) that are called file. Remember that you can specify a search directory:

find /home/user/Documents -name file

You can also specify a file extension:

find /home/user/Documents -name file.txt

Something especially useful is to make the find command case insensitive. To do this, use -iname

find /home/user/Documents -iname file.txt

In this case, results will appear as File.txt or fiLe.txt

We can also search for files that are different from the search criteria. For example:

find . -not -name file

In this case, all files not called file will be searched.

Searching by a directory and other types

The command type also supports other file types and even directories. To specify that we are looking for some of them just use the filters and the desired options:

  • d – directory or folder
  • f – normal archive
  • l – symbolic links
  • c – character files
  • b – block files

For example, to search for any of these files you need to combine it with the -name option explained in the previous section. For example:

find / -type d -name example

This command will search the entire system for folders called example.

The type f is quite useful if we want to exclude folders and symbolic links from the search.

find / -type f -name example

In this case, the search will only be for files with an extension excluding folders and symbolic links.

Combining these two options will make the results much more precise.

Using the find command to search by date or size

The find command also allows you to search by date. This is possible thanks to other options we can use. These are:

  • -atime Most recent date the file was read or written
  • -mtime – The most recent date the file was modified.
  • -ctime Latest date the file’s metadata was updated.

Together with these new options, we can improve the quality of the search. However, to use it, it is necessary to add the + or – signs preceded by a number representing the days. For example:

find / -atime +1

This will search for files that were accessed more than one day ago.

Or also:

find / -ctime -1

This means that the files that have changed their metadata less than a day ago will be searched.

These options are really useful especially for documents that we know have been modified but we don’t remember where they are.

We can also make searches keeping in mind the size of the file we want to find. To do this, we have to follow this syntax

find [directory] -size [magnitude] [size-unit]

The size units that are supported are as follows:

  • c – bytes
  • k – kilobytes
  • M – megabytes
  • G – gigabytes

Also, by using the + or signs we can further specify the search by size.

To search for all files on the system that weigh more than 1G the syntax would be as follows

find / -size +1G

Or all files less than 2 Gigabyte

find / -size -2G

You can also combine this option with one of the date options and improve the results.

Search files by property and permissions

A more advanced type of search is to search for files or folders belonging to a system user. This situation can be very useful in the case of servers that have many users created. Or on family computers that have several accounts created.

To perform the search, it is necessary to add the -user option to the command and specify a parameter. For example:

find / -user atechtown

In this case, all system files belonging to the atechtown user will be searched. Of course, you can combine it with the -name option and thus filter the search better

find / -user atechtown -name file

Finally, you can search for files that do not belong to any user with the -nouser option

But you can not only search by users but also by a group. Just change the -user parameter to -group

find / -group atechtown

You can also combine it with the -name parameter. To search for files that do not belong to any group there is the option -nogroup

It is also necessary to search for files by permissions. This is possible thanks to the -perm option and we have to indicate some permissions.

The way to use this option is as follows:

find / -perm 755

This will search for files with permissions of 755 whose value you can adjust to improve the search criteria.

Other useful options

Other very useful options can serve us from one moment to the next.

First of all, we can search for empty files and folders with the -empty

find / -empty

Quite useful if we want to get rid of these useless files and have a clearer view of the system.

We can also search for executables or files that have this permission. In this case, the option to use is -exec

find / -executable

Or you can also search for archivists who have reading permission

find / -readable

This will search for files accessible by the user who executes the command.

It is also possible to check the files that can be edited by the current user. In this case, we would have to use the -writable

find / -writable

By default, the find command performs recursive searches at the maximum possible level. This behavior can be controlled with the -maxdepth option.

Example:

find / -maxdepth 1

This will search files only up to the first level of directories. In this case, it will only search for folders that are in / but not beyond.

Conclusion

Searching for files or folders in the terminal might be thought to be something very complex, but the reality is that it is not. Thanks to the find command we can do it without almost any problems and it’s a practical way to do it because in the terminal almost no resources are consumed.

Have you used the find command? Do you use it frequently? Leave us a comment

Find command page

Share This: