The find
command is a versatile tool in Linux for searching and locating files and directories. It allows you to explore the file system and identify specific items based on various criteria.
Basic Structure:
The general structure of the find
command is:
find <start_directory> <options>
<start_directory>
: This specifies the location where the search begins. By default, it starts from the current working directory (.
).<options>
: These are flags that define howfind
searches and what information it displays.
Common Use Cases:
- Finding Files by Name:
Use the -name
option followed by a filename pattern (including wildcards like *
).
Example: Find all files with the name “*.txt” in the current directory:
find . -name "*.txt"
- Finding Files by Type:
Use the -type
option with f
for files and d
for directories.
Example: Locate all directories within the /home
directory:
find /home -type d
- Searching by Permissions:
Use the -perm
option with a specific permission code (e.g., -u+w
for user write permission).
Example: Find all files writable by the user in the /tmp
directory:
find /tmp -perm -u+w
- Finding Files by Date:
Use the -mtime
, -atime
, or -ctime
options followed by a number and optional modifiers (+
for days after, -
for days before) to search based on modification time, access time, or change time, respectively.
Example: Locate files modified in the last 7 days within /var/log
:
find /var/log -mtime -7
- Taking Actions on Found Files:
The find
command can be combined with the -exec
or -ok
option to perform actions on the found files. However, be cautious, especially with deletion.
Example: Delete all empty files in the current directory (use with caution!):
find . -type f -empty -exec rm -f {} ;
Remember:
The official source for the Linux find
command documentation is the man page. You can access it directly from your terminal by typing:
man find