Solved

shell script to remove redundant files

Posted on 1997-12-19
1
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

688 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