Linux basics - how to use the find command to search for files and directories ๐งตโ
The Linux find command is a powerful tool that allows system administrators to search for and manage files and directories that match a set of criteria in the filesystem.
You can not only use the find command to search for files on your system, but you can also use external commands to perform actions on those files, such as deleting them with the rm command, checking their permissions with the ls command, copying them with the cp command etc.
๐ง Basic syntax of the find command:
The find command syntax if very simple to understand, here is the basic find command syntax:
$ find [path] [options] [action]
The find command syntax if very simple to understand, here is the basic find command syntax:
$ find [path] [options] [action]
โข [path]: It defines the directory where to begin searching.
โข [options]: It defines the criteria of filtering e.g. searching a file/folder by its name, permission, time, or date.
โข [action]: It defines what actions to perform with the file.
โข [options]: It defines the criteria of filtering e.g. searching a file/folder by its name, permission, time, or date.
โข [action]: It defines what actions to perform with the file.
๐ง Find files by name:
Finding files by name is most likely the most common and basic application of the find command. To find a specific file by name, use the -name option followed by the file name you want to find.
Finding files by name is most likely the most common and basic application of the find command. To find a specific file by name, use the -name option followed by the file name you want to find.
๐ง Find files on in the parent directory
Find searches for files in specified directories (parent directory) and sub-directories within the parent directory by default.
Find searches for files in specified directories (parent directory) and sub-directories within the parent directory by default.
It's important to note that if you don't want the shell to interpret the asterisk * symbol , you escape it with a backslash or place it in quotaions.
Notice that I'm still using the -maxdepth option to only search in the parent directory.
๐ง Find files by type
You may need to search for specific file types such as regular files, directories, or symlinks on occasion. Everything in Linux is a file. To search for files based on their type, use the -type option and one of the file descriptors listed below:
You may need to search for specific file types such as regular files, directories, or symlinks on occasion. Everything in Linux is a file. To search for files based on their type, use the -type option and one of the file descriptors listed below:
โข b - block (buffered) special
โข c - block (buffered) special
โข d - a directory `f`: a regular file
โข l - a symbolic link
โข p - a named pipe (FIFO)
โข s - a socket
โข D - door (Solaris)
โข c - block (buffered) special
โข d - a directory `f`: a regular file
โข l - a symbolic link
โข p - a named pipe (FIFO)
โข s - a socket
โข D - door (Solaris)
๐ง Find files by size
Use the -size parameter along with the size criteria to find files based on file size. To specify the file size, use the following suffixes:
โข b - 512-byte blocks
โข c - bytes
โข k - Kilobytes
โข G - Gigabyte
โข M - Megabytes
โข w - two-byte words
Use the -size parameter along with the size criteria to find files based on file size. To specify the file size, use the following suffixes:
โข b - 512-byte blocks
โข c - bytes
โข k - Kilobytes
โข G - Gigabyte
โข M - Megabytes
โข w - two-byte words
The above command will match all the files with execute permissions set for either user, group, or others.
๐ง Find files by modification date
The find command can also look for files based on when they were last modified, accessed, or changed.
When searching by size, use the plus and minus symbols to indicate "greater than" or "less than."
The find command can also look for files based on when they were last modified, accessed, or changed.
When searching by size, use the plus and minus symbols to indicate "greater than" or "less than."
This will allow you to find files that were modified within a specified range of time.
๐ง Find and delete files
You can use the -delete option to delete every file that match a certain criteria. It is important to note that the -delete option will not delete not-empty directories.
You can use the -delete option to delete every file that match a certain criteria. It is important to note that the -delete option will not delete not-empty directories.
โ ๏ธ Warning
Use this option only when you are certain that the result matches the files you want to delete. Before using the -delete option, it is always a good idea to print the matched files.
Use this option only when you are certain that the result matches the files you want to delete. Before using the -delete option, it is always a good idea to print the matched files.
๐ง Perform actions on each matched result
The `find` command has a useful option for calling external programs to perform specific actions on the returned files that matched a certain criteria.
The `find` command has a useful option for calling external programs to perform specific actions on the returned files that matched a certain criteria.
A breakdown of the -exec option:
โข exec ls - this tells find to execute the ls command on every filename that matches the search string.
โข -lah - displays all files, including hidden files, their permissions, and other file metadata, such as sizes, in human readable format.
โข exec ls - this tells find to execute the ls command on every filename that matches the search string.
โข -lah - displays all files, including hidden files, their permissions, and other file metadata, such as sizes, in human readable format.
โข {} - The โ{}โ placeholder represents each filename and must be the last item in the parameter list.
โข ; - To indicate the end of the parameter list, a semicolon ";" is used. It must be escaped with a backslash "\" otherwise the shell will interpret it.
You can also use the + instead of the ; to indicate the end of the parameter list.
You can also use the + instead of the ; to indicate the end of the parameter list.
That concludes the find command. If you take the time to experiment with the find command, you will discover that there are numerous possibilities. You'll save a lot of time if you get good at commands like this.
If you get stuck, feel free to consult the find man pages.
If you get stuck, feel free to consult the find man pages.
That it for today's thread! Thank you for reading!
If you enjoyed this thread and found it useful, please follow us
(@linuxopsys) for more Linux, sysadmin, and devops content!.
If you enjoyed this thread and found it useful, please follow us
(@linuxopsys) for more Linux, sysadmin, and devops content!.
Loading suggestions...