how to use the find command to search for files and directories in Linux:
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.
1. 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.
2. 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.
You can control this by passing the -maxdepth option to the find command, which instructs it to search the specified number of directories down.
3. Find files by extension
Searching for files by extension is similar to searching for files by name, with the exception that you will use the * wildcard meta character.
Searching for files by extension is similar to searching for files by name, with the exception that you will use the * wildcard meta character.
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.
5. 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.
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
• 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)
• s - a socket
• D - door (Solaris)
6. 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
You can also use the find command to look for files that are larger or smaller than a certain size.
In the following example, we look for files that are less than 600k in the effective user's home directory.
In the following example, we look for files that are less than 600k in the effective user's home directory.
The above command will match all the files with execute permissions set for either user, group, or others.
If minus - is used as the prefix, then for the file to match, at least the specified bits must be set.
9. Find files by modification date
The find command can also look for files based on when they were last modified, accessed, or changed.
You can also use the plus and minus symbols to indicate "greater than" or "less than" when searching by modification data.
The find command can also look for files based on when they were last modified, accessed, or changed.
You can also use the plus and minus symbols to indicate "greater than" or "less than" when searching by modification data.
This will allow you to find files that were modified within a specified range of time.
Assume you modified one of your markdown files a few days ago but have forgotten which one.
Assume you modified one of your markdown files a few days ago but have forgotten which one.
10. 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.
⚠️ Caution
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.
12. 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.
Here is an example of listing the permissions and other metadata of every file that the find command finds:
$ find ~/ -type f -name "*.sh" -exec ls -lah {} +
$ $ find ~/ -type f -name "*.sh" -exec ls -lah {} \;
$ find ~/ -type f -name "*.sh" -exec ls -lah {} +
$ $ find ~/ -type f -name "*.sh" -exec ls -lah {} \;
That it for today's thread! Thank you for reading!
If you enjoyed this thread and found it useful, toss us a follow (@linuxopsys) for more Linux, sysadmin, and devops content!.
If you enjoyed this thread and found it useful, toss us a follow (@linuxopsys) for more Linux, sysadmin, and devops content!.
Loading suggestions...