?
Solved

shell script to remove redundant files

Posted on 1997-12-19
1
Medium Priority
?
362 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 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

Technology Partners: 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: 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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

801 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