Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
The find command is a command-line utility used for finding files and directories and performing subsequent operations on them. In this article we discuss the find command and present common use cases.
Table of contents.
- Introduction.
- Syntax.
- Find files based on names.
- Find files based on file extensions.
- Find files based on type.
- Find files based on permissions.
- Find files based on owners and groups.
- Find files based on modification.
- Find files based on size.
- Summary.
- References.
Introduction.
The find command searches for files and directories based on permissions, type, modification date, ownership, size etc and once a file is matched operations such as deleting, renaming, changing permissions can be performed. These functionalities make it very useful especially for linux system administrators as such tedious work can be done by placing the discussed commands in a shell script and executing it.
Note that these commands can also be executed in a git bash terminal installed in a windows operating system.
Syntax.
find [options] [path] [expression]
Options: control the treatment of the symbolic links, debugging options and optimizations.
Path: defines the directory in which we shall begin a search.
Expression: this is made up of options, search patterns and actions all separated by operators.
Option expressions constrain the find operation and always return true these may be -depth, -maxdepth or -mindepth
Test expressions evaluate file properties and return true or false, these include, -atime, -size, -type, -name, -iname and more.
Action expressions define actions with side effects, they may return true or false, when unspecified, the -print action is performed for matching files, these include -exec, -delete, -ok etc.
Expressions are evaluated from left to right and put together by operators such as ! for negating an expression, -a for ANDing two expressions, o which is the result of an or of two expressions.
Find files based on names.
To find files by name with the find command we write,
find ~/Downloads/ -type f -name file.pdf
where ~/Downloads is the directory to search and file.pdf is the file we are searching for.
Assuming file.pdf was File.pdf, the above find command would come up empty because of case sensitivity, To find it while ignoring the case we write,
find ~/Downloads/ -type f -iname file.pdf
We can also opt to find a file by name and delete by using -exec expression as follows,
find ~/Desktop/ -type f -iname test.txt -exec rm -i {} \;
The '\' is used to terminate the command being executed, the '{}' substitutes the actual file name.
The command will find the file and prompt the user before deleting it.
Find files based on extensions.
Similarly like searching for a file by its name, we can also search for a file by its extension.
An example
To search for all pdf files in the downloads folder we could write the following,
find ~/Downloads/ -type f -name '*.pdf'
Find files based on type.
Files can be of the following type,
f: a regular file.
d: a directory.
l: a symbolic link.
b: block devices.
c: character devices.
b: block devices.
p: named pipe.
s: socket.
An example.
To find all directories in desktop we write,
find ~/Desktop/ -type d
To find all files in a downloads folder we write,
find ~/Downloads/ -type f
Find files based in permissions.
Permissions can be of three types, read, write or execute and each permission has a numeric value, i.e 4, 2, 1 respectively.
To find a file based on permission we use the -perm option.
An example
777 permission indicates that anyone can read, write or execute this kind of file. To find such files we write,
curl ~/Downloads/ -type f -perm 777
Find files based on owners and groups.
We can use the -user and -group options with find to find files owned by either of the two.
An example
To find files owner by an user joe we could write,
find -user joe
Find files based on modification.
We can also use the find command to search for files based on their modification or access dates and time.
To search for files based on their modification dates we use the -mtime option.
An example
To find a file or files modified in the last 7 days we write,
find ~/Desktop/ -mtime -7
To find files accessed in the last 7 days, we write,
find -atime 7
To find files accessed in the last hour, we write,
find -amin -60
And to find files modified in the last hour we write,
find -mmin -60
We can also find the time range in which a file was modified, for example to find files that were modified 7 days back and less than 10 days we can write,
find -mtime +7 -mtime -10
Find files based on size.
We can also search for a file based on it size.
The size can be,
b: 512-byte block, this is the default size.
c: represent bytes.
w: represent two bytes.
k: kilobytes.
M: megabytes.
G: gigabytes.
An example
For finding a file which is 1024 bytes in the current directory we write,
find -type f -size 1024c
or, finding files between 1MB and 10MB we write,
find -type f -size +1M -size -10M
Summary.
The find command will return all files in the current working directory and can specify an action to be performed once a match is found.
Most unix, linux command-line utilities become very useful when used with other commands, that is, you can use find command together with sed, awk or grep command for example, getting the output of find command and using it as the input of any of the former commands.
References.
- man find - (linux)
- The grep command
- The sed command
- The awk command