find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be used to search for files based on various criteria such as name, type, size, and date modified.

The basic syntax of the find command is:

find [path] [options] [expression]

The "path" is the directory or directories where the search should begin. If no path is specified, the search will begin in the current directory. The "options" are used to specify the criteria for the search, and the "expression" is used to specify the name of the file or files to be searched for.

The most basic use of the find command is to search for a specific file by name. For example, to search for a file called "fruits.txt" in the current directory, you would use the following command:

find . -name "fruits.txt"

This command will search the current directory and all its subdirectories for a file called "fruits.txt" and will display the path to the file if it is found.

The find command also has the ability to search for files based on various criteria such as type, size, and date modified. For example, the following command will search for all files in the current directory that are larger than 100KB:

find . -size +100k

The command will search the current directory and all its subdirectories for files larger than 100KB and will display the path to the files that are found.

The find command also has several options that can be used to modify its behavior. Some of the most commonly used options include:

  • -type: specifies the type of file to be searched for, such as f for regular files or d for directories.
  • -name: specifies the name of the file or files to be searched for.
  • -size: specifies the size of the file or files to be searched for.
  • -mtime: specifies the number of days ago the file was last modified.
  • -exec: allows you to execute a command on the files that are found.
  • -print: will print the files that match the search criteria.
  • -delete: will delete the files that match the search criteria.

The find command can also be used in combination with other commands to perform more complex tasks. For example, you can use the find command in combination with the grep command to search for a specific pattern in all files in a directory and its subdirectories. For example, the following command will search for the word "apple" in all text files in the "fruits" directory and its subdirectories:

find fruits -type f -name "*.txt" -exec grep "apple" {} +

The find command can also be used to perform actions on the files that are found. For example, the following command will search for all files in the current directory that are larger than 100KB and will delete them:

find . -size +100k -delete

Another useful feature of the find command is the ability to use regular expressions to search for files. This can be done by using the -regex option. For example, the following command will search for all files in the current directory that have a .txt or .md extension:

find . -regex ".*\.\(txt\|md\)"

The find command also has the ability to search for files that have specific permissions. This can be done by using the -perm option. For example, the following command will search for all files in the current directory that have read and write permissions for the owner:

find . -perm -600

The find command can also be used to search for files that were modified within a specific time frame. This can be done by using the -mtime option. For example, the following command will search for all files in the current directory that were modified within the last 24 hours:

find . -mtime 0

Additionally, the find command also has the ability to search for files based on their inode number. This can be done by using the -inum option. For example, the following command will search for all files in the current directory that have the inode number 12345:

find . -inum 12345

The find command also has the ability to search for files based on the user or group ownership. This can be done by using the -user or -group option respectively. For example, the following command will search for all files in the current directory that are owned by the user "john":

find . -user john

Another useful feature of the find command is the ability to search for files based on their access time. This can be done by using the -atime option. For example, the following command will search for all files in the current directory that were accessed within the last 24 hours:

find . -atime 0

The find command also has the ability to search for files based on their status change time. This can be done by using the -ctime option. For example, the following command will search for all files in the current directory that had their status changed within the last 24 hours:

find . -ctime 0

The find command can also be used to search for files based on their type, such as block special files, character special files, etc. This can be done by using the -type option along with the specific file type. For example, the following command will search for all block special files in the current directory:

find . -type b

In summary, the find command in Linux is a powerful command-line utility that is used to search for files and directories in a file system. It is versatile, it can be used to search for files based on various criteria such as name, type, size, date modified, user or group ownership, access time, change time and many more. The find command has several options that can be used to modify its behavior and it can be used in combination with other commands to perform more complex tasks. It is an essential command for anyone who works with files in Linux, it can help you quickly find the files you need, even in the most complex of file systems.