The find
command in Linux is a powerful utility used to search for files and directories based on various criteria like name, type, size, date, permissions, and more.
find [path] [expression]
path
: The directory where the search begins. If omitted, it defaults to the current directory (.
).expression
: Criteria or filters for finding files.find
Find Files by Name:
find /path/to/search -name "filename"
Example: Find all files named “example.txt” in /home/user/
:
find /home/user/ -name "example.txt"
Find Files by Extension:
find /path/to/search -name "*.txt"
Example: Find all .txt
files:
find . -name "*.txt"
Find Directories:
find /path/to/search -type d -name "directory_name"
Example: Find all directories named “backup”:
find . -type d -name "backup"
Find Files by Size:
find /path/to/search -size [+/-]size
Example: Find files larger than 100MB:
find . -size +100M
Find Files Modified in the Last X Days:
find /path/to/search -mtime -X
Example: Find files modified in the last 7 days:
find . -mtime -7
Find Files by Permission:
find /path/to/search -perm 755
Example: Find files with 755
permissions:
find . -perm 755
Find and Delete Files:
find /path/to/search -name "filename" -delete
Example: Find and delete all .tmp
files:
find . -name "*.tmp" -delete
Execute a Command on Found Files:
find /path/to/search -name "filename" -exec command {} \;
Example: Find .log
files and remove them:
find . -name "*.log" -exec rm {} \;
List all files created today:
To list all files created today using the shell, you can use the find
command with the -type f
option to search for files only and the -ctime 0
option to search for files created within the last 24 hours.
Here’s the command you can use:
find /path/to/directory -type f -ctime 0
Replace /path/to/directory
with the path to the directory where you want to search for files. If you want to search for files in the current directory, you can use .
instead of the path.
This command will list all files that were created within the last 24 hours in the specified directory.