Linux delete file 5 days old and size greater than 5MB

Linux

To delete files that are older than 5 days and have a size greater than 5MB in Linux, you can use the find command with the -mtime and -size options.

For example, to delete all files in the /path/to/directory directory that are older than 5 days and have a size greater than 5MB, you can use the following command:

find /path/to/directory -mtime +5 -size +5M -delete
 

This will search for all files in the /path/to/directory directory that are older than 5 days (-mtime +5) and have a size greater than 5MB (-size +5M), and delete them (-delete).

Note: Be careful when using the find command with the -delete option, as it can permanently delete files and cannot be undone. It is always a good idea to test the command first by removing the -delete option and adding the -print option to see a list of the files that would be deleted.

find /path/to/directory -mtime +5 -size +5M -print
 

You can also use the -exec option to run a command on the matching files, such as moving them to a different location or archiving them. For example:

find /path/to/directory -mtime +5 -size +5M -exec mv {} /path/to/archive ;
 

This will move all matching files to the /path/to/archive directory.

For more information on the find command and its options, you can refer to the find man page or the GNU Findutils documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *