A refresher on find: find files on your Linux distro

Land

Searching for files and directories is relatively easy with the current search engines integrated into the file managers, but sometimes we may like to use the terminal more, or we may have no choice but to use the terminal as we do not have a graphical environment. So, I found it interesting to review find, a well-known command, but something forgotten by the massive use of graphical environments.

Well, find, as its name suggests, is useless for anything other than to locate files, although it has other alternatives such as locate, etc., we will focus on find, since it is quite powerful and allows us many interesting options to carry out our search successfully. If we use it as is, without any option, what it will do is launch a list similar to the one we would obtain with an ls, of the contents of an entire directory (and subdirectories).

But this is not what interests us, what we want is to customize and refine a little more the search to be more precise and help us find what we are really looking for. To understand how it works, I think there is no better way than to show practical examples of find:

  • To search by name, we can use the option or search criteria «-name». For example, the first example looks for files / directories with a name that starts with "musi", the second one that ends with "eon", and the last one that contains the word "found" inside the root / directory:
find / -name "musi*"

find / -name "*eon"

find / -name "fundar"

  • There is an option called -type, which can serve as a filter and can be used together with -name. In this case it serves to specify the type of file to search. With B we look for special files in block mode, with C special files in character mode, D for directories, F for ordinary files, L for symbolic links, P for named pipe and S for Socket or network connection. For example, suppose you want to search for a directory that has "hello" at the end of its name and in the Home / user directory:
find /Home/usuario -name "*hola" -type D

  • We can also search by user or group to which it belongs a directory or files on the system. For that, we can use the -user and -group criteria. Now imagine that you want to find a file that contains «.mp3» of the user «Rosa» and the group «Cats» in the Downloads directory:
find /Descargas -name ".mp3" -user Rosa -group Gatos

  • Size can help us search by size. In this case we have different options to apply. A B would indicate the block, if the size is not indicated, by default it will be 512 bytes. C for 1-byte ASCII characters, W for 2-byte (old) words, and K for KiloBytes or 1024 bytes. For example, if we want to search in / for a file of 2560 bytes (5 blocks · 512), another of 10 ASCII characters, another of 100KBs, one of less than 5MB and another of more than 30KB:
find / -size 5

find / -size 10c

find / -size 100K

find / -size -5000K

find / -size +30K

Of course, -size would be combinable with all search criteria anterior and posterior, thus we will obtain even more precise results ...

  • You can even search by temporal criteria. With -atime you can search by the date of the last access. -mtime by date of content modification and -ctime by date of last modification of the inode. For example, we want to search in / Home, a directory called "hello", belonging to the user "Zaca" and that has been modified less than 3 days ago:
find /Home -name "hola" -user Zaca -mtime -3

  • There are more criteria search such as -perm to search for access authorizations or permissions, -links to search for hard links, -inum for inode number. Let's go with our last example, in this case, we will look for a directory in the current directory whose permissions are total for the owner user and group, and execution for the rest:
find -type d -perm 771

Sometimes we are unaware of the potential of the terminal and we use other tools that may not allow the flexibility of what is already innate. So I hope I have helped something with this humble article. Have fun and leave your comments...


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

A comment, leave yours

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   names said

    I am not amused, but it has helped me a little