Bash script for ubuntu to remove files older than 30 days

Posted on 2012-09-16
Medium Priority
Last Modified: 2012-09-17
I'm not a programmer but one of our dept's has an SFTP server that they would like files older than 30 days to be deleted. The folders are in /home/clientname

Can anyone point me in the direction of a script that could cycle through the /home folder and certain sub directories for files older than 30 days and delete them? I could put the script in a cron job so it could run daily, weekly or monthly.

We are using ubuntu 64bit server version 10.10

thank you.
Question by:msidnam
  • 2
LVL 68

Accepted Solution

woolmilkporc earned 1200 total points
ID: 38403521
find /home/clientname -type f -mtime +30 | xargs rm

If there are several clients put their names into a textfile called "clients.txt" (one name per line) and run

while read clientname
    find /home/$clientname -type f -mtime +30 | xargs rm
 done < clients.txt

For a "dry run" replace

xargs rm


xargs echo rm

This will display the rm commands, but not execute them.

To be really sure that the command is going to do the right things you can also run:

find /home/clientname -type f -mtime +30 -ls

This will produce a detailed listing of the selected files, but not run any further commands.

Author Comment

ID: 38403589
Excellent thank you. I will try tomorrow morning when i get in the office. I will report results.

Author Closing Comment

ID: 38405259
This is great. thank you. I'm going to post something in exchange forum if you know power shell too :)

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question