Skip to content

Various example of find commands in Linux

The find command in Linux is used to search for files and directories in a specified location. All UNIX Admins and DBAs use it very extensively. find command has many options and it can be used in a lot of different ways. Below I have listed some of examples of using find commands which can prove useful. One of the commands should fit your use case. You can also combine it with cron to complete many maintenance works easily.

Find all files in the current directory:

find . -type f

Find all files with a specific extension in the current directory:

find . -name "*.txt"

Find all files modified within the last 24 hours:

find . -mtime -1

Find all files larger than 100MB:

find . -size +100M

Find all files with a specific name and execute a command on them:

find . -name "example.txt" -exec cat {} \;

Find all files with a specific permission:

find . -perm 777

Find all files that are empty:

find . -empty

Find all files that are owned by a specific user:

find / -user myusername

Find all files that are in a specific group:

find / -group mygroupname

Find all files that are older than a certain date:

find / -type f -newermt "2022-01-01"

Find all directories in the current directory:

find . -type d

Find all files with a specific name and delete them:

find . -name "temp*" -delete

Find all files with a specific name and move them to a different directory:

find . -name "*.log" -exec mv {} /var/log/ \;

Find all files that are larger than a certain size and print their size:

find . -size +1G -exec ls -lh {} \;

Find all files that are older than a certain number of days and print their names:

find . -mtime +30 -exec ls {} \;

Find all files that have been accessed within the last 7 days and print their names:

find . -atime -7 -exec ls {} \;

Find all files that match a specific pattern and copy them to a different directory:

find . -name "*.txt" -exec cp {} /backup/ \;

Find all files that are symbolic links and print their target:

find . -type l -exec ls -l {} \;

Find all files that have been modified within the last hour and print their names:

find . -cmin -60 -exec ls {} \;

Find all files that have been modified within the last week and compress them:

find . -mtime -7 -exec gzip {} \;

Find all files that have been modified within the last 30 days and have a specific string in their content:

find . -mtime -30 -exec grep -l "string" {} \;

Find all files that have been modified within the last 30 days and have a specific string in their content and delete them:

find . -mtime -30 -exec grep -l "string" {} \; -delete

Find all files that are larger than a certain size and have a specific string in their content and move them to a different directory:

find . -size +1G -exec grep -l "string" {} \; -exec mv {} /new_folder/ \;

Find all files that are smaller than a certain size and have a specific string in their content and print their name and size:

find . -size -100M -exec grep -l "string" {} \; -exec ls -lh {} \;

Find all files that have been modified within the last year and have a specific string in their content and compress them with tar:

find . -mtime -365 -exec grep -l "string" {} \; -exec tar -czf {}.tar.gz {} \;

Find all files that have been accessed within the last month and have a specific string in their content and print their name and last access time:

find . -atime -30 -exec grep -l "string" {} \; -exec stat -c "%n last accessed on %x" {} \;

Find all files that have been modified within the last week and have a specific string in their content and mail them to a specific email address:

find . -mtime -7 -exec grep -l "string" {} \; -exec mail -s "Files with specific string" 
email@example.com < {} \;

Find all files that have been modified within the last 30 days and have a specific string in their content and run a specific command on them:

find . -mtime -30 -exec grep -l "string" {} \; -exec your_command {} \;

Find all files that have been modified within the last year and have a specific string in their content and rename them:

find . -mtime -365 -exec grep -l "string" {} \; -exec mv {} {}_newname \;

Find all files that have a specific string in their content and have been modified within the last year, and change their permission:

find . -mtime -365 -exec grep -l "string" {} \; -exec chmod 755 {} \;

Find all files that have been modified within the last year and have a specific string in their content and also have a specific file extension and print their name and path:

find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; -exec stat -c "%n path: %h" {} \;

Find all files that have a specific string in their content and have been modified within the last year and also have a specific file extension and change their owner:

find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; -exec chown new_owner {} \;

Find all files that have a specific string in their content and have been modified within the last year and also have a specific file extension and change their group:

find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; -exec chgrp new_group {} \;

Find all files that have been modified within the last year and have a specific string in their content and also have a specific file extension and print their name and last access time:

find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; -exec stat -c "%n last accessed on %x" {} \;

Find all files that have been modified within the last year and have a specific string in their content and also have a specific file extension and move them to a different directory:

find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; -exec mv {} /new_folder/ \;

Find all files that have been modified within the last year and have a specific string in their content and also have a specific file extension and compress them with tar:

find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; -exec tar -czf {}.tar.gz {} \;

Find all files that have been modified within the last year and have a specific string in their content and also have a specific file extension and mail them to a specific email address:

find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; -exec mail -s "Files with specific string" email@example.com < {} \;

Find all files that have a specific string in their content and have been modified within the last year and also have a specific file extension and rename them:

find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; -exec mv {} {}_newname \;

Find all files that have been modified within the last year and have a specific string in their content and also have a specific file extension and change their permission:

find . -mtime -365 -name "*.txt" -exec grep -l "string" {} \; -exec chmod 755 {} \;

 

Brijesh Gogia
Leave a Reply