[Linux] Command to Delete Old Files by Specifying Date and Time

Tadashi Shigeoka ·  Thu, November 14, 2019

I’ll introduce a Linux command to delete old files by specifying date and time.

Linux

Background: Want to Bulk Delete Unused Files on Linux

The background is the same as [Linux] Command to Check Directories with High Disk Usage.

After identifying directories consuming large amounts of space for disk full response, I researched Linux commands to bulk delete files.

Bulk Delete Files on Linux by Specifying Last Modified Time and Last Access Time

Bulk Delete Files Modified More Than 30 Days Ago

find ./ -mtime +30 | xargs rm -f

# -mtime    Last modification time of file data (days)

find command date/time specification options and overview

  • -mmin Last modification time of file data (minutes)
  • -mtime Last modification time of file data (days)
  • -amin Last access time to file (minutes)
  • -atime Last access time to file (days)
  • -cmin Last modification time of file data and status (minutes)
  • -ctime Last modification time of file data and status (days)

Bulk Delete by Specifying File Extension

The Linux command to add file extension specification option to the above command for bulk deletion is as follows:

find ./ -mtime +30 -name "*.jpg" | xargs rm -f

Bulk Delete Files Not Accessed for More Than 30 Days

find ./ -atime +30 | xargs rm -f

# -atime  Last access time to file (days)

That’s all from the Gemba.

Reference Information