delete some days old files and folder linux command

Linux systems are always used as server. Many times we need to cleanup the server. I found nice solution for deleting older files and folders from server without any issue.

For deleting files or folder you can use following command:

rm -rf `find /home/purab/* -type d -mtime +90`

Above command will find all files and folders which are created 90 days ago and delete those folders and files.

Before running above command be careful about directory path. please directory path. rm -rf command is always dangerous.

Above We used find command for searching all files in folder.

-mtime is used to calculate time.

If you want to keep only 5 latest folders on server than you can use following command.

ls -dt /home/purab/*/ | tail -n +6 | xargs rm -rf

Above command has three parts.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.