Search for files

The find command is used to search for files in the directory tree starting from the specified location. It is usually used to search for files by their filenames, but you can also search for files of specific sizes, permissions, group ownership, etc.

The syntax of the command is simple:

find PATH EXPRESSION

The PATH parameter specifies the directory in which you would like to start the search. The EXPRESSION parameter specifies whether to search for files by filename, size, permission, etc.

Here is an example. To search for files in the /home/pi/files/ directory that begin with the string new, we can use the following command: find /home/pi/files/* -name “new*”

find command example

Let’s break down the command:

  • find – invokes the find command.
  • /home/pi/files/* – specifies the directory that will be searched for the files. The * character specifies that all files inside the /home/pi/files directory will be included in the search.
  • -name – specifies that the files will be searched by filename.
  • “new*” – the files whose names begin with new will be matched. The expression has to be enclosed in double quotes because of the * character.

Here is how we can find files bigger then 60 bytes:

find command size example

In the picture above you can see that only one file is bigger than 40 bytestextfile.txt.

When using the -size expression, use c for bytes, k for Kilobytes, M for Megabytes and G for Gigabytes.

 

To find files owned by the user pi, use the following command:

find command owner example

In the picture above you can that the find command listed all files owned by the pi user.

Geek University 2022