Solved

shell script to remove redundant files

Posted on 1997-12-19
1
356 Views
Last Modified: 2013-12-26
How can I create a shell script to run from a specific directory, search that directory and it's descendants for all files older than a given date and remove them to a 'dustbin'?
0
Comment
Question by:copernicas
1 Comment
 
LVL 1

Accepted Solution

by:
hno earned 100 total points
Comment Utility
Use find.

To move all files older than 7 days to /tmp/dustbin:

mkdir /tmp/dustbin
find /directory/name -mtime +7 -type f -exec mv \{\} /tmp/dustbin \;

To move all files older than 1 Sep (1/9 00:00)
touch -t 09010000 /tmp/marker
mkdir /tmp/dustbin
find /directory/name \! -newer /tmp/marker -exec mv \{\} /tmp/dustbin \;

(all find lines end with \;, watch out for wrapped lines)


If you want to fully remove the files, you can of course use rm instead of mv, and skip the /tmp/dustbin part ;-).

find ... -exec rm -f \{\} \;
(-f can be left out, if you'd like to be asked on "strange" permisions)

If you know that no names contain spaces, then you can optionally write it in a faster way:

find ... -print | xargs rm -f
(-f is required here, to avoid "are you sure" questions from eating file names)

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

12 Experts available now in Live!

Get 1:1 Help Now