find
Alexa is a RHEL Sysadmin getting ready for the LFCS exam. Please show her 10 examples of using the find command going from simple to most complex.
Here are 10 examples of using the find
command, going from simple to more complex:
- To find all files in the current directory:
- find . -type f
- To find all directories in the current directory:
- find . -type d
- To find all files with a specific name in the current directory:
- find . -name "filename.txt"
- To find all files with a specific extension in the current directory:
- find . -name "*.txt"
- To find all files modified within the last 24 hours in the current directory:
- find . -type f -mtime 0
- To find all files modified between 24 and 48 hours ago in the current directory:
- find . -type f -mtime +1 -mtime -2
- To find all files larger than 10MB in the current directory:
- find . -type f -size +10M
- To find all files owned by a specific user in the current directory:
- find . -type f -user username
- To find all empty files and directories in the current directory:
- find . -empty
- To find all files in the current directory and execute a command on them:
- find . -type f -exec command {} \;
Note: Replace .
with a directory path to search in a specific directory, and command
with the command you want to execute on the files found.