Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

shell script to remove redundant files

Posted on 1997-12-19
1
Medium Priority
?
365 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 400 total points
ID: 1295829
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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 lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

824 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