Solved

shell script to remove redundant files

Posted on 1997-12-19
1
357 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
withoutTen challenge 14 100
noX challenge 17 92
mapBully challenge 6 115
Detect file exist or not 3 133
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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

11 Experts available now in Live!

Get 1:1 Help Now