Linux Tip: Listing Files by Their Age

Introduction

Ever wondered how to find files in a directory that haven’t been touched in ages? Whether you’re cleaning up old files, archiving data, or checking for stale files, Linux’s find command is your go-to solution. In this tip, we’ll show you how to list files by their age in Linux using a simple yet powerful command.


The Command

find /path/to/files -type f -name '*.jpg' -mtime +30 -exec ls -l {} \;

This command lets you locate files based on their last modification date, filter them by type or name, and display detailed information about them.


Breaking It Down

1. /path/to/files

This specifies the directory to search in. Replace it with the actual folder path you want to explore.

  • Example: If you want to search your photo library, use ~/Pictures.

2. -type f

The -type option specifies the type of items to search for.

  • f: Refers to files only (ignores directories).

3. -name '*.jpg'

This filters results based on file names or extensions.

  • Example: *.jpg finds files ending with .jpg.
  • You can replace it with other patterns, like *.txt for text files or *.log for log files.

4. -mtime +30

This is the key to filtering files based on their age.

  • +30: Finds files last modified more than 30 days ago.
  • Replace 30 with any number to suit your needs:
    • 1 year: Use -mtime +365.
    • 1 week: Use -mtime +7.

5. -exec ls -l {} \;

This tells Linux what to do with the files it finds.

  • -exec: Executes a command on each file.
  • ls -l: Lists detailed information about each file (e.g., size, permissions, owner, and last modified date).
  • {}: Placeholder for each file found.
  • \;: Signals the end of the command.

Pro Tip

Want to test the command without running ls -l? Omit the -exec part:

find /path/to/files -type f -name '*.jpg' -mtime +30

This will simply list the file paths without additional details.


Why Use This Command?

The find command is incredibly versatile and useful for system maintenance tasks:

  • Free up disk space: Identify and delete old files.
  • Archive older data: Move files untouched for months or years to backup storage.
  • Monitor stale files: Check for outdated files in critical directories.

Practical Examples

Example 1: Find Old Documents

Search for .docx files in your Documents folder that haven’t been modified in over 90 days:

find ~/Documents -type f -name '*.docx' -mtime +90 -exec ls -l {} \;

Example 2: Locate Large Files Older Than 6 Months

Find files larger than 100MB in /home/user that haven’t been modified in over 6 months:

find /home/user -type f -size +100M -mtime +180 -exec ls -lh {} \;
  • -size +100M: Filters files larger than 100MB.
  • ls -lh: Displays file sizes in human-readable format (e.g., MB, GB).

Example 3: Search for Old PDFs

Find .pdf files in /home/user/Downloads that haven’t been modified in the last year:

find ~/Downloads -type f -name '*.pdf' -mtime +365 -exec ls -l {} \;

Example 4: Find Recently Modified Files

Search for .txt files modified in the last 7 days in /var/www:

find /var/www -type f -name '*.txt' -mtime -7 -exec ls -l {} \;
  • -mtime -7: Finds files modified within the last 7 days.

Example 5: Remove Old Temporary Files

Delete files in /tmp that haven’t been modified in over 30 days:

find /tmp -type f -mtime +30 -exec rm {} \;
  • rm: Deletes the files.
  • Warning: Use with caution as this permanently removes files.

Example 6: Archive Old Media Files

Move .mp4 files older than 60 days from ~/Videos to an archive folder:

find ~/Videos -type f -name '*.mp4' -mtime +60 -exec mv {} ~/Archives/ \;
  • mv: Moves files to the specified folder.

Example 7: Find Files Created in a Specific Time Range

Search for .log files created between 30 and 60 days ago in /var/log:

find /var/log -type f -name '*.log' -mtime +30 -mtime -60 -exec ls -l {} \;
  • -mtime +30: Files older than 30 days.
  • -mtime -60: Files modified within the last 60 days.

Example 8: Find Empty Files Older Than 1 Week

Locate empty files in /home/user that haven’t been modified in the last week:

find /home/user -type f -empty -mtime +7 -exec ls -l {} \;
  • -empty: Filters files with zero size.

Example 9: Search for Files by Access Time

Find .png files in /var/www/html that haven’t been accessed in the last 90 days:

find /var/www/html -type f -name '*.png' -atime +90 -exec ls -l {} \;
  • -atime: Filters files by access time instead of modification time.

Example 10: Find Recently Created Files

Locate files in /home/user created within the last 3 days:

find /home/user -type f -ctime -3 -exec ls -l {} \;
  • -ctime: Filters files by creation time.

Example 11: Find Files by Permissions

Search for files in /etc with specific permissions (e.g., writable by others):

find /etc -type f -perm /o+w -exec ls -l {} \;
  • -perm /o+w: Finds files writable by others.

Example 12: Locate Files Modified in the Last Hour

Find files modified in the last hour in /home/user/Documents:

find ~/Documents -type f -mmin -60 -exec ls -l {} \;
  • -mmin -60: Filters files modified within the last 60 minutes.

Example 13: Find Files with Specific Ownership

Search for files owned by the user jose in /var/log that haven’t been modified in over 90 days:

find /var/log -type f -user jose -mtime +90 -exec ls -l {} \;
  • -user jose: Filters files owned by the specified user.

Example 14: Exclude Specific Directories

Find .jpg files in /home/user older than 30 days, excluding the Downloads folder:

find /home/user -type f -name '*.jpg' -mtime +30 -not -path "~/Downloads/*" -exec ls -l {} \;
  • -not -path: Excludes files in the specified directory.

Example 15: Find Files Modified on a Specific Date

Search for files modified exactly 10 days ago in /home/user:

find /home/user -type f -mtime 10 -exec ls -l {} \;
  • -mtime 10: Finds files modified exactly 10 days ago.

Example 16: Locate Files with Specific Extensions

Search for .zip and .tar.gz files older than 60 days in /backups:

find /backups -type f \( -name '*.zip' -o -name '*.tar.gz' \) -mtime +60 -exec ls -l {} \;
  • \( -name '*.zip' -o -name '*.tar.gz' \): Searches for multiple file extensions.

Conclusion

Linux’s find command is a powerful tool for managing files based on their age, type, and name. Whether you’re cleaning up disk space, archiving old data, or monitoring stale files, this command gives you complete control over your directories.

Try it out today, and take your Linux skills to the next level!

Leave a Comment