Bash script for ubuntu to remove files older than 30 days

Posted on 2012-09-16
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
    LVL 68

    Accepted Solution

    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.
    LVL 2

    Author Comment

    Excellent thank you. I will try tomorrow morning when i get in the office. I will report results.
    LVL 2

    Author Closing Comment

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

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now