LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-02-2012, 03:40 AM   #1
AntBla
LQ Newbie
 
Registered: Oct 2012
Posts: 16

Rep: Reputation: Disabled
Script to delete folders (whose names are in format XXXmmdd) but leave latest folder


Hi Everyone.

I am a real newbie with Linux and this is my first post. I have been googling my way through and have worked out some shell scripts and crontab stuff.

The problem I am stuck with is this.
Each day I download a zip file and extract to a foldername like /usr/local/files/XYZmmdd

mmdd = month date

eg today the folder name is XYZ1002

When I download the file tomorrow the folder XYZ1003 will be created.

What I need to do is remove all folders prior to the latest folder that fit the profile XYZmmdd.

So basically I want to remove all folders that have the last 4 digits less than the current 4 digits.

Hope that explains it without being to confusing.

Thanks in advance for any tips.

Ant.

Last edited by AntBla; 10-02-2012 at 03:41 AM.
 
Old 10-02-2012, 04:22 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,122

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
ls -1t <dir> will list the entries (of a dir) sorted by time, so the first one is the latest.
ls -1t <dir> | awk '(NR>1)' will remove the first line. You can redirect the output to xargs to delete those dirs, and you need to specify <dir> to match your criteria
 
Old 10-02-2012, 04:43 AM   #3
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 273Reputation: 273Reputation: 273
Very simple script.
It deleted all XYZmmdd dir, but not that with current date.

Code:
#!/bin/bash
STRING=XYZ
MONTHDAY=$(date +%m%d)
FOLDER=${STRING}${MONTHDAY}

ls -1rt | grep $STRING'[0-9]\{4\}' | grep -v $FOLDER | xargs rm -r
 
Old 10-04-2012, 04:24 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Actually, it's not a good idea to use ls for things like this (although I don't fault it as an easy, quick&dirty solution that works in most cases).

parsing ls

Assuming that the name format is zero-padded and consistent, the shell should sort them properly with a regular globbing match. In which case I'd just use a simple array (also assuming bash or a similar array-supporting shell):

Code:
dnames=( XYZ*/ )
unset dnames[$(( ${#dnames[@]} - 1 ))]
rm -rf "${dnames[@]}"
In fact, it could even be reduced to two lines:

Code:
dnames=( XYZ*/ )
rm -rf "${dnames[@]:0:$(( ${#dnames[@]} - 1 ))}"
In the shell, I'd follow it with a final "unset dnames", but that's not usually necessary in a script.

See here for more:

How can I get the newest (or oldest) file from a directory?
http://mywiki.wooledge.org/BashFAQ/099

How can I use array variables?
http://mywiki.wooledge.org/BashFAQ/005

Last edited by David the H.; 10-04-2012 at 04:31 AM. Reason: fixed minor coding error + added link
 
Old 10-04-2012, 04:41 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Here's another quick solution that doesn't require an array, and so should be completely portable:

Code:
for dname in XYZ*/; do : ; done	#$dname will end up with the final entry in the list
mv "$dname" "x$dname"		#temporarily rename the directory
rm -rf XYZ*/			#remove all the other directories
mv "x$dname" "$dname"		#return the remaining one back to its original value
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find folders in a folder and delete ibbofez Linux - Newbie 3 10-26-2011 05:20 PM
Bash Script to categorize folders on similar names GnuBoi Linux - General 1 10-10-2010 08:25 AM
A script within a folder to delete the folder, script, and the folder's contents Cyberman Programming 15 10-17-2007 08:32 AM
Shell Script to Get Folder Names firefoxlinux Programming 6 09-27-2007 03:56 PM
Delete old files and folders Script? AsteX Linux - General 4 11-11-2004 07:26 PM


All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration