find command

In the find tutorial, you will learn how to use the find command to search for files and directories on a Linux system based on various criteria, such as name, size, modification time, and permissions. You will learn how to specify the directory to search, and how to use different options to customize the search criteria and the actions to be performed on the found files and directories. You will also learn how to combine multiple options and criteria to create more complex searches.

The find command is a powerful Linux command that is used to search for files and directories based on various criteria, such as name, size, modification time, and permissions. It is an essential tool for locating and organising files and directories on the file system.

Here is a tutorial on using the find command:

Basic usage

To search for files and directories, simply type find followed by the path to the directory where you want to start the search, and the criteria for the search, and press enter:

$ find path -criteria

For example, to search for all files and directories in the /home/user directory, you would use the following command:

$ find /home/user

Searching for files and directories with a specific name

To search for files and directories with a specific name, you can use the -name option, followed by the name you want to search for. For example, to search for all files and directories with the name file.txt in the /home/user directory, you would use the following command:

$ find /home/user -name file.txt

Searching for files and directories with a specific file type

To search for files and directories with a specific file type, you can use the -type option, followed by the type you want to search for. The possible file types are f for regular files, d for directories, l for symbolic links, and b for block devices. For example, to search for all directories in the /home/user directory, you would use the following command:

$ find /home/user -type d

Searching for files and directories based on size

To search for files and directories based on their size, you can use the -size option, followed by the size you want to search for. The size can be specified in bytes, kilobytes, or megabytes, using the c (bytes), k (kilobytes), or M (megabytes) suffixes, respectively. You can also use the + or - signs to specify files that are larger or smaller than the specified size. For example, to search for all files larger than 1 megabyte in the /home/user directory, you would use the following command:

$ find /home/user -size +1M

Searching for files and directories based on modification time

To search for files and directories based on their modification time, you can use the -mtime option, followed by the number of days since the file was last modified. You can also use the + or - signs to specify files that were modified more or less recently than the specified number of days. For example, to search for all files that were modified in the past 7 days in the /home/user directory, you would use the following command:

$ find /home/user -mtime -7

Other options

The find command also has several other options that can be used to customize the search criteria and the actions to be performed on the found files and directories. Some of the most useful options include:

  • -exec: This option allows you to execute a command on each file and directory that is found.
  • -ok: This option is similar to -exec, but it prompts the user for
  • -print: This option prints the names of the found files and directories to the standard output.
  • -print0: This option is similar to -print, but it separates the names with a null character, which is useful for dealing with filenames that contain spaces or other special characters.
  • -prune: This option prevents the find command from descending into directories that match the specified criteria.
  • -perm: This option searches for files and directories with specific permissions.

To use these options, simply include them as arguments to the find command, like this:

$ find path -exec command {} \;
$ find path -ok command {} \;
$ find path -print
$ find path -print0
$ find path -prune
$ find path -perm mode

Combining options and criteria

You can also combine multiple options and criteria to create more complex searches. For example, to search for all regular files in the /home/user directory that were modified in the past 7 days and are larger than 1 megabyte, and delete them, you would use the following command:

$ find /home/user -type f -mtime -7 -size +1M -exec rm {} \;

I hope this tutorial on the find command has been helpful!