Raspberry Pi

Delete Files Older Than X Days on Raspberry Pi

Deleting the old files from Raspberry Pi system can create more storage as well as increase the overall efficiency of a system. The removal of older files, caches, cookies, and residue files of any program free up the space inside the storage along with RAM / swap memory, which ultimately enhance the performance of a system.

If you need help deleting old files from Raspberry Pi system, follow this article for a detailed guidance.

Delete Files Older than X Days on Raspberry Pi

If you want to delete specific files from Raspberry Pi that reside inside the directory for many days, you can get help from this guidance. However, before deleting the files, you should aware of two commands, which are “rm” and “find”.

The “rm” command let remove the files or directories and move them to wastebasket and “find” command help to find a specific file, directory, or the specific extension that are associated with the specific file. So, both commands together will lead user to find and delete files very easily.

Delete Files from a Specific Day

The first command that is mentioned below is utilized to delete specific files from the past day. In the command, “-exec” command search for all files that matches the specific day within given directory using “find” command and removes them using the “rm” command as shown below.

$ find <directory>* -mtime +<passed-days> -exec rm {} \;

The above executed command finds and delete all files inside the Pictures folder that were placed more than 2-days.

Delete Files from Minutes or Hours

You can also find and delete files that are placed inside the directory for more than minutes or hours using the following syntax.

$ find <directory> -mmin +<minutes> -exec rm {} \;

Delete Files from Scheduled Time

Files can also be deleted within the scheduled time that set by the users. in order to evaluate this operation, you have to utilize the “Crontab” editor command. To open “Crontab” file use following syntax in the command terminal/

$ crontab -e<img src="https://linuxhint.com/wp-

Upon opening, you need to add following syntax inside the “crontab” file at the end. Then save the file by pressing CTRL + X and Y then run every day this file to delete files of the specific directory.

@daily find <directory>* -<mtime> or <mmin> +<day> or <time> -exec rm {} \;

Once you add and save the above command line in the crontab, then you will see the following message inside the terminal command.

Now the crontab will attempt to find and remove files that were placed some minutes. You can change the time according to yourself since and it’s better to increase this time to your desired level.

Conclusion

Deleting old files from a specific directory helps reduce the storage on system and increase the system’s performance. You can find and delete files from Raspberry Pi system that are placed more than a specific day or minutes through the commands provided in the above guidelines. However, time can be increased according to the user’s choice.

About the author

Muhammad Nasir

Hi! I am an engineer and technical blogger by profession and I enjoy getting things done with electronics. I am using this platform to share my knowledge in the field of Raspberry Pi, Linux, and Embedded Systems with others.