Beset Scripting langauge to delete old directories and contents?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Beset Scripting langauge to delete old directories and contents?
# 1  
Old 04-24-2008
Beset Scripting langauge to delete old directories and contents?

What would the best scripting language be for selecting all the directories in the current directory that are over a week old and deleting there contents?

Perl? bash? groovy? ruby? something else? How would you do it? I got a start with perl in the beginners@perl.org and I started to think: hmmm... what about the bash find command -- would that be easier? What about other scripting languages?

Thanks,
siegfried
# 2  
Old 04-24-2008
the find utility (runs under any shell, not just bash) is what you want
do NOT run your find on / or /usr or /opt - just data directories.
# 3  
Old 04-25-2008
help with find utiltity

I looked at the man page for find but I'm having trouble understanding how to use find to filter out recent files.

Could someone kindly show me how to use find to select only child (but not grandchild) directories that are over a week old?

I guess this would include some bash statements to do the arithmetic to compute the datetime of 7 days ago. I know how to do that in perl but I

(1) don't know how to do that in bash
(2) don't know how to pass the results of perl back to a bash variable (is that possible?)

Thanks,
Siegfried
# 4  
Old 04-25-2008
Just run find and post-filter it to discard anything with the wrong number of slashes.

The number of slashes depends on what you mean by "child" and "grandchild". find usually prints one slash for the current directory (like ./fileincurrentdir) so children would have two slashes and grandchildren three, and grand-grandchildren of course one more, etc. So we run egrep to discard anything with at least three slashes, and anything with only one:

Code:
find -mtime +7 -type d | egrep -v '/.*/.*/|^[^/]*/[^/]*$'

I'll answer your other questions, but the real answer was on the previous line.

date -d "week ago" will get you the date of 7 days ago, for any reasonably POSIX version of date. (Excludes some dinosaur architectures, unfortunately. If you're among the unfortunate, get your sysadmin to install the GNU utilities or at least a POSIX add-on. You will congratulate yourself many times over.)

The standard way to pass anything from a subprocess is to run something which prints its result in backticks. I'm sure you are familiar with backticks from Perl, too.

Code:
perloutput=`perl -e 'print "foobar" x 1024'`

Unlike in Perl, the shell does some sanitization of the printed value, so there will be no trailing newlines, for example. This is mostly a feature.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Clear contents of specified directories, then return exit status

Hello, this is my first post here. I'm attempting to write a bash shell script to rm the contents of a directory without deleting the directory, specifically in OS X 10.10 . Here's what I have: function clear() { USER="$USER" DIR=$1 rm -rfv /Users/"$USER"/library/$DIR/* } clear... (6 Replies)
Discussion started by: YouNicks
6 Replies

2. UNIX for Advanced & Expert Users

Appending a files contents to the end of a specific file name in several directories

Here is my dir structure: /tmp/dave/myappend.txt /tmp/dave/dir1/test.txt /tmp/dave/dir2/test.txt /tmp/dave/dir3/test.txt /tmp/dave/dir4/test.txt I want to append the contents of myappend.txt to the end of each file with the name "test.txt" in all dirs in /tmp/dave/ I have tried this:... (2 Replies)
Discussion started by: bigd213
2 Replies

3. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

4. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

5. Shell Programming and Scripting

compare the contents of two directories

i have been asked to write a bash shell script comparing two directories and sed or awk should not be used in this assignment. compdir will compare filenames in two directories, and list information about filenames that are in one directory but not the other. The information listed will be a long... (1 Reply)
Discussion started by: soccerball
1 Replies

6. UNIX for Dummies Questions & Answers

delete recursively contents of folders

hi, I've a folder structure like : /home/project/LIBNAMEA/FILE1 /home/project/LIBNAMED/FILE2 /home/project/LIBNAMEC/FILE3 /home/project/LIBNAMED/FILE4 /home/project/LIBNAMEX/FILE5 (there is no relation in the letters after the project/ ) and i need to delete the files keeping... (5 Replies)
Discussion started by: jtmartins
5 Replies

7. UNIX for Dummies Questions & Answers

Help displaying contents of Directories - Urgent

I am new to shell programming and have an assignment question which requires me to list the contents of the present working directory in 4 column format and highlight any subdirectories. It then requires me to develop the shell script to accept a directory name as a positional parameter (if no... (1 Reply)
Discussion started by: cjnd1988
1 Replies

8. UNIX for Dummies Questions & Answers

Delete Directories and Contents

How to delete all subdirectories and contents? oh, and if they are hard linked? (1 Reply)
Discussion started by: t4st33@mac.com
1 Replies

9. UNIX for Dummies Questions & Answers

delete contents from command line

How might I delete the contents of a file from the command line? (not delete the file) (4 Replies)
Discussion started by: juanbro
4 Replies
Login or Register to Ask a Question