find (Find File and Directory)
The find
command is used to locate files or directories under the specified path.
Find with File or Directory Name
Using a file or directory name is one of the frequently used approaches in the find
command. Specify the file or directory name after the -name
option.
For example, to find the ssh key file named authorized_keys, you can run the command below.
find ~ -name authorized_keys
The command returns the file path of the file like the one below.
/home/ubuntu/.ssh/authorized_keys
Find with Other Options
There are many other approaches to finding files or directories. Here are the selected options for the find command.
-type option
You can specify what type of files or directories you are searching. To use the option, add the document type after the -type
option. These are examples of the document type.
f
: normal filed
: directoryl
: symbolic links
: hard link
For example, to find a symbolic link whose name starts with “file” run the command below.
find file* -type l
-size option
You can also search files with the document data size. Specify the size that you are looking for.
-size +10M
: size is 10M or more than 10M-size 10M
: size is 10M-size -10M
: size is 10M or less than 10M
-empty option
With this option, you can find empty documents only.
-o option
When you have two search criteria, you can connect the search name with the -o
option. It works as the OR function.
If you want to find two different file names, you need to add the -name
option both before and after the -o
option. The following is an example of the command used to search documents named test or test_sub.
find ~ -name test -o -name test_sub
Wildcard
Wildcard is useful when you forget the exact name of a file or directory. Please see the next section on how to use the wildcard in the find command.