This post will cover topic related to find command particularly about ‘Find Files Based on Last Access Time: Using -amin in find Command’ with multiple find command examples and different find scenerios. So this find commandd article will help you to understand find command and options available in find command. End of the post, you will have better understanding of find command and how to use find command in better way.
The find
command is a powerful tool for searching files and directories in Unix-like operating systems. When it comes to locating files based on their last access time, the -amin N
option is particularly useful. This option allows users to find files that were last accessed exactly N
minutes ago. This can be useful for managing files that have been recently used, checking for stale files, or performing system maintenance tasks. Below are several examples demonstrating how to use the -amin
option with the find
command.
Example 1: find /path/to/dir -amin 30
This command searches for files in the directory /path/to/dir
that were last accessed exactly 30 minutes ago. If a file was accessed precisely 30 minutes prior, it will be listed in the output.
Example 2: find /var/log -amin -60
Here, the -amin -60
option finds files in the /var/log
directory that have been accessed within the last 60 minutes. The minus sign before 60 indicates “less than,” so it retrieves files accessed within the last hour.
Example 3: find /home/user -amin +120
This command looks for files in the /home/user
directory that were accessed more than 120 minutes ago. The plus sign before 120 indicates “more than,” so it lists files that haven’t been accessed in the last 2 hours.
Example 4: find /tmp -amin 15 -type f
With this command, only regular files (not directories or other types) in the /tmp
directory that were accessed exactly 15 minutes ago are listed. The -type f
option ensures only files are considered.
Example 5: find . -amin 5 -exec ls -l {} \;
This command finds files in the current directory (.
) accessed exactly 5 minutes ago and then executes the ls -l
command on each of those files. This provides a detailed listing of each file found.
Example 6: find /etc -amin -30 -print
This command locates files in the /etc
directory that were accessed within the last 30 minutes and prints their paths. The -print
option explicitly specifies that the file paths should be printed to the standard output.
Example 7: find /usr/local -amin +10 -size +1M
Here, files in the /usr/local
directory accessed more than 10 minutes ago and larger than 1 megabyte are found. The -size +1M
option specifies that only files larger than 1 MB are considered.
Example 8: find /var/cache -amin 45 -not -name "*.log"
This command searches for files in the /var/cache
directory that were accessed exactly 45 minutes ago but excludes files with the “.log” extension. The -not -name "*.log"
combination filters out log files.
Example 9: find /home/user/docs -amin -120 -print | grep "report"
In this example, the command finds files accessed within the last 120 minutes in the /home/user/docs
directory and then filters the results to include only those whose paths contain the word “report”.
Example 10: find /mnt -amin +30 -exec du -h {} \;
This command finds files in the /mnt
directory accessed more than 30 minutes ago and executes the du -h
command on each, which reports the disk usage of each file in human-readable format.
Verification Steps:
1. Run the command in your terminal.
2. Check the output to see if the listed files match your criteria based on access time.
3. Verify file access times using the stat
command to confirm they meet the expected times. For example, stat filename
will display file attributes including the last access time.
Also check similar articles.
How -noignore_readdir_race Can Affect Your find Command Results
Avoid Race Conditions with -ignore_readdir_race in find Command
Improve find Accuracy with -noleaf to Avoid Issues with Hard Links
Limit Your Search to a Single Filesystem with -mount Option in find Command
How to Use -mindepth in find to Skip Initial Levels in Directory Searches
Discussion about this post