find

The find command in Unix/Linux is used to search for files and directories in a directory hierarchy based on various criteria. It is a powerful and flexible tool that allows you to locate files and perform actions on them

find [path] [expression]
Here are some common options:
    -name pattern: Search for files with a specific name pattern.
    -type type: Search for files of a specific type (e.g., f for regular files, d for directories).
    -mtime n: Search for files modified n days ago.
    -size n[c]: Search for files of a specific size (optionally followed by a unit, such as k for kilobytes).
    -exec command {} +: Execute a command on the found files.

Examples

Find files by name
find /path/to/search -name "filename.txt"
Find directories by name
find /path/to/search -type d -name "directory_name"
Find files modified in the last 24 hours
find /path/to/search -mtime -1
Find files larger than 1gb
find /path/to/search -size +1G
Find files by extension
find /path/to/search -name "*.log"
Find files and execute a command on each
find /path/to/search -type f -exec chmod 644 {} \;
Find files by inode number
find /path/to/search -inum 12345
Find files and exclude a directory
find /path/to/search -type f -not -path "/path/to/exclude/*"
Find files by permission mode
find /path/to/search -type f -perm 644
Find empty directories
find /path/to/search -type d -empty
Find files and delete them
find /path/to/search -type f -name "*.tmp" -delete
Find files and archive them
find /path/to/search -type f -name "*.txt" -exec tar czf archive.tar.gz {} +
Find files based on modification time
find /path/to/search -newermt "2023-01-01"
Find files and list only their names
find /path/to/search -type f -exec basename {} \;
Find files and count them
find /path/to/search -type f | wc -l
Find files and display their size
find /path/to/search -type f -exec du -h {} \;
Find files and display their permissions
find /path/to/search -type f -exec ls -l {} \;
Find files and sort them by modification time
find /path/to/search -type f -exec ls -lt {} +
Find files modified in the last 7 days
find /path/to/search -mtime -7
Find files and display their owners
find /path/to/search -type f -exec ls -l {} \; | awk '{print $3}'
Find directories and list contents recursively
find /path/to/search -type d -exec ls -R {} \;
Find files and move them to another directory
find /path/to/search -type f -name "*.jpg" -exec mv {} /path/to/destination/ \;
Find files and compress them
find /path/to/search -type f -name "*.log" -exec gzip {} \;
Find files and display their last access time
find /path/to/search -type f -exec stat --format="%x" {} \;
Find files and set their permissions
find /path/to/search -type f -exec chmod 644 {} \;
Find files modified in the last 30 minutes
find /path/to/search -mmin -30
Find files and delete them interactively
find /path/to/search -type f -name "*.bak" -exec rm -i {} \;
Find files and display their inode numbers
find /path/to/search -type f -exec ls -i {} \;
Find files and rename them
find /path/to/search -type f -name "*.txt" -exec rename 's/\.txt$/\.bak/' {} \;
Find files and archive them with timestamp in the filename
find /path/to/search -type f -name "*.log" -exec tar czf archive_$(date +"%Y%m%d%H%M%S").tar.gz {} +
Find files and display their selinux context
find /path/to/search -type f -exec ls -Z {} \;
Find files and display their modification time in iso format
find /path/to/search -type f -exec stat --format="%y" {} \;
Find files and display their size in megabytes
find /path/to/search -type f -exec du -h --apparent-size {} \;
Find files and copy them to another directory
find /path/to/search -type f -name "*.jpg" -exec cp {} /path/to/destination/ \;
Find directories and remove them
find /path/to/search -type d -name "temp" -exec rm -r {} \;
Find files and display their modification time in epoch format
find /path/to/search -type f -exec stat --format="%Y" {} \;
Find files and archive them with timestamp and custom prefix
find /path/to/search -type f -name "*.log" -exec tar czf archive_$(date +"%Y%m%d")_{} \;
Find files modified more than 30 days ago
find /path/to/search -mtime +30
Find files and move them to a directory structure based on modification year and month
find /path/to/search -type f -exec bash -c 'year=$(date -r "{}" +%Y); month=$(date -r "{}" +%m); mkdir -p "/path/to/destination/$year/$month" && mv "{}" "/path/to/destination/$year/$month/"' \;
Find files and display their permissions in octal format
find /path/to/search -type f -exec stat --format="%a" {} \;
Delete files matching a pattern
find /path/to/directory -type f -name "*.txt" -exec rm {} \;
Copy files to another directory
find /path/to/source -type f -exec cp {} /path/to/destination/ \;
Move files to a backup directory
find /path/to/files -type f -exec mv {} /path/to/backup/ \;
Change file permissions recursively
find /path/to/files -type f -exec chmod 644 {} \;
Search for and print text in files
find /path/to/files -type f -exec grep "search_text" {} \;
Count lines in files and summarize
find /path/to/files -type f -exec wc -l {} \; | awk '{ total += $1 } END { print "Total lines: " total }'
Execute a custom script on files
find /path/to/files -type f -name "*.sh" -exec ./custom_script.sh {} \;
Display file information
find /path/to/files -type f -exec stat {} \;
Find and archive files
find /path/to/files -type f -name "*.log" -exec tar -cvzf logs.tar.gz {} +
Delete empty directories
find /path/to/directory -type d -empty -exec rmdir {} \;
Search for files modified in the last 7 days
find /path/to/files -type f -mtime -7 -exec ls -l {} \;
Search for and delete temporary files
find /path/to/files -type f -name "*.tmp" -exec rm {} \;
Search for large files
find /path/to/files -type f -size +1G -exec ls -lh {} \;
Search for and archive files by extension
find /path/to/files -type f -name "*.jpg" -or -name "*.png" -exec tar -cvzf images.tar.gz {} +
Search for and move files to another directory
find /path/to/files -type f -name "*.pdf" -exec mv {} /path/to/pdf_files/ \;
Search for files owned by a specific user
find /path/to/files -type f -user username -exec ls -l {} \;
Search for files with specific permissions
find /path/to/files -type f -perm 644 -exec ls -l {} \;
Search for files with specific extension and delete
find /path/to/files -type f -name "*.bak" -exec rm {} \;
Search for and display file metadata
find /path/to/files -type f -exec exiftool {} \;
Search for and archive files with specific modification time
find /path/to/files -type f -mtime -30 -exec tar -cvzf recent_files.tar.gz {} +