The find
command in Linux is a powerful tool for searching files and directories. You can filter results based on name, size, modification time, and permissions. Below are practical examples to help you use the find
command effectively.
Example 1: Find all files in the current directory
Explanation: The find .
command lists all files and directories in the current directory.
Command:
find .
Run the command and check the output.
Example 2: Find all .txt
files
Explanation: The find . -name '*.txt'
command searches for .txt
files in the current directory and subdirectories.
Command:
find . -name '*.txt'
Run the command and check the output.
Example 3: Find all files larger than 1MB
Explanation: The find . -size +1M
command locates files larger than 1MB.
Command:
find . -size +1M
Run the command and check the output.
Example 4: Find files modified in the last 7 days
Explanation: The find . -mtime -7
command lists files modified in the last 7 days.
Command:
find . -mtime -7
Run the command and check the output.
Example 5: Find all directories
Explanation: The find . -type d
command lists directories.
Command:
find . -type d
Run the command and check the output.
Example 6: Find all symbolic links
Explanation: The find . -type l
command lists symbolic links.
Command:
find . -type l
Run the command and check the output.
Example 7: Find files by permission (executable by owner)
Explanation: The find . -perm /u=x
command finds files that are executable by the owner.
Command:
find . -perm /u=x
Run the command and check the output.
Example 8: Find files and exclude specific directories
Explanation: The find . -path './demo_files/dir1' -prune -o -print
command excludes ./demo_files/dir1
from the search.
Command:
find . -path './demo_files/dir1' -prune -o -print
Run the command and check the output.
Example 9: Find files and execute ls
on them
Explanation: The find . -type f -exec ls -lh {} \;
command displays details of each regular file.
Command:
find . -type f -exec ls -lh {} \;
Run the command and check the output.
Example 10: Find empty files
Explanation: The find . -type f -empty
command searches for empty files.
Command:
find . -type f -empty
Run the command and check the output.
Example 11: Find Files Accessed in the Last 2 Days
Explanation:
The command below lists files that were accessed within the last 2 days.
Command:
find . -atime -2
Try running this command in your terminal and see how it works!
Example 12: Find Files Accessed More Than 10 Days Ago
Explanation:
This command searches for files that were accessed more than 10 days ago.
Command:
find . -atime +10
Give it a try and check the output in your terminal.
Example 13: Find Files with a Specific Size Range (1MB to 5MB)
Explanation:
The command below searches for files whose sizes range between 1MB and 5MB.
Command:
find . -size +1M -size -5M
Run this command and observe which files fall within the specified size range.
Example 14: Find Files with a Specific Extension (Case-Insensitive)
Explanation:
This command finds files ending with the .TXT
extension, ignoring case sensitivity.
Command:
find . -iname '*.TXT'
Test it in your terminal to see the results regardless of case.
Example 15: Find Files Newer Than a Reference File
Explanation:
The command below lists files that are newer than a reference file (in this case, demo_files/text1.txt
).
Command:
find . -newer demo_files/text1.txt
Try running this command to check which files have been modified more recently than the reference file.
Example 16: Find and Delete All .log
Files (Be Cautious)
Explanation:
This command deletes all files ending with the .log
extension. Use with caution to avoid accidental data loss.
Command:
find . -name '*.log' -delete
Run this command carefully, ensuring you have backups or are working in a safe environment.
Example 17: Find Files and Display Their Depth in the Directory Structure
Explanation:
The command below displays each file’s depth in the directory tree, with %d
representing the depth and %p
the file path.
Command:
find . -printf '%d %p\n'
Execute the command and observe the directory structure’s depth for each file.
Example 18: Find and Sort Files by Modification Time
Explanation:
This command finds regular files, prints their modification time along with their path, and sorts the output by modification time.
Command:
find . -type f -printf '%T+ %p\n' | sort
Run this command to see a sorted list of files based on when they were last modified.
These advanced examples build upon our previous tutorial, helping you master more practical and powerful aspects of the Linux find
command. Experiment with these commands to enhance your file management and search efficiency on Linux.
Happy exploring, and be sure to share your experiences in the comments below!