Linux find Command Tutorial

Linux provides the find command in order to find files and directories. The find command is executed via the command line interface and it is also provided by the Unix and other related Unix-based operating systems. The find command can be used to search files and folders according to their names, creation date, modification date, ownership, permission, path, etc. Also, it provides useful features like executing commands for the match results.

find Command Syntax

The find command has the following syntax.

find PATH -name SEARCH_TERM OPTIONS
  • PATH is the search path where the specified SEARCH_TERM will be searched.
  • SEARCH_TERM is searched in the specified PATH.
  • OPTIONS are used for different search types.

find Command Help

The find command provides different help options where the --help option is one of them. The –help option list very basic help and usage information about the find command.

$ find --help

To get more details about the find command the man page can be used. The man page of the find command provides a lot of information about the options, usage examples, etc.

$ man find

Search File with Name

The files can be searched according to their names by using the -name option. The complete or partial name is provided as a parameter with the -name option. In the following example, we search for the file named “database” in the home directory of the user “ismail”.

$ find /home/ismail -name database

Alternatively, we can search in the current working directory and all child directories by using bash “.” operator.

$ find . -name database

Search File with Pattern

The find command provides the ability to search files with their names for specific patterns. The “*” glob operator can be used to search files with partial names. We can search files those names that contain “data” with the following command.

$ find / -name "*data*"

Search File with Extension

The find command can be used to search files according to their extensions. The glob operator is used to express the name of the file and the extension is specified explicitly. In the following example, we search files with the “*.txt” extension.

$ find / -name "*.txt*"

Find and Delete File

One of the useful features of the find command is the ability to delete founded files. The -exec option is used to delete files by executing the “rm” command. In the following example, we delete all text or “*.txt” extension files.

$ find / -name "*.txt*" -exec rm {} \;

Search Empty Files and Directory

The Linux operating system may contain empty files and directories. The find command can be used to find empty files and directories easily by using the -empty option. There is no need to provide a file or directory name.

$ find / -empty

Search Empty Files with Specific Extension

The -empty option can be used to search empty files with a specific extension. In the following example we search empty *.txt files by using the -name option.

$ find / -name "*.txt" -empty

Search with Permission

Another useful feature of the find command is the ability to search files and directories according to their permissions. The Linux system use permission consisting of 3 numbers like “000”, “777” etc. The -perm the option is used to specify the permission value where all matched files and directories will be listed.

$ find / -perm 777

Search with User Name

In Linux, every file and directory has an owner. The find command can be used to search according to the owner information of the file or directory. The -user option is used to specify the owner name of the file or directory. In the following example, we search files and directories owned by “ismail”.

$ find / -user "ismail"
See also  Linux zgrep Command Tutorial

Leave a Comment