I'm looking to write a script that will do a find of directories and delete them if they are older than x days but keep the last x # of folders even if they are older than x days.
The usage is for a deployment location, so we want to keep the location clean but retain maybe the last 2 builds that have been deployed from a given folder. If there have been multiple builds in the last couple of days then we might want to keep more than 2 builds.
So the first criteria is keep a minimum of x builds.
The 2nd criteria is if they are less than 2 days old, keep more builds.
So I'm thinking I have to write something that does an ls -al, dumps to a temp file, removes the last 2 lines in that file or more lines if they are less than 2 days old and then uses that file to delete the folders.
I know the logic to do the find by date and delete, but I need to add the logic of keeping the last x builds to it. I'm just wondering about the best way to do this. Has anyone done something similar or have some suggestions?
So the ideal would be to keep the last 2 build#'s so you have the last successful deploy and the current one which may have multiple iterations but as the folder names are constantly changing that would be too difficult, this is an attempt to do the same thing with different logic.
Example 1 - would want to keep the last 2 folders from Dec 22/23
Example 2 - in this case would keep the last 3 builds because they are within the 2 day criteria (would probably change this to something like a 7 day criteria so would actually be saving the last 4 builds)
Even better would be to come up with logic that compared the first part of the folder name and if it is the same keep all those plus 1 more. ie. 2.0.0.020 - keep all that matched this, plus the last 2.0.0.019.
So you would want to save the most recent folder, any folders that match the same first 10 characters of that folder, and then the most recent folder before all of those folders. All other folders would be deleted.
---------- Post updated at 10:48 AM ---------- Previous update was at 09:20 AM ----------
I'm getting closer. This will give me that last build and any associated revisions of it. Now I just have to figure out how to add the last previous folder to this list, and then use this as an exclude list while I delete the other folders.
Last edited by Scott; 01-14-2010 at 07:14 PM..
Reason: Code tags
That's great and clearly cleaner than the round about way that I was trying to do it. I guess it's time to stop putting off learning awk and sed.
I get that awk is splitting the name using the period with -F and grabbing the first 4 sections separated by a period ($1$2$3$4). Then you want b to be everything but a. Just need to understand this piece {b=a;i++} .
Guess I better find a good book on awk and sed.
Last edited by Scott; 01-14-2010 at 06:53 PM..
Reason: When posting code, please use code tags
That's great and clearly cleaner than the round about way that I was trying to do it. I guess it's time to stop putting off learning awk and sed.
ls -r | awk -F. '{a=$1$2$3$4} b!=a {b=a;i++} i>2
I get that awk is splitting the name using the period with -F and grabbing the first 4 sections separated by a period ($1$2$3$4). Then you want b to be everything but a. Just need to understand this piece {b=a;i++} .
Guess I better find a good book on awk and sed.
I apologize for having edited my code after you saw it (at the time, i still had not seen this response).
The code extracts the current filename's build number into the variable a and compares it against the build number of the previous filename which is stored in b. If they are the same, nothing happens. If they are different, the current build number becomes the previous build number and i is incremented. i is the total number of build numbers encountered. when i exceeds 2, it starts printing the lines encountered (these are the directory names to be removed using xargs).
Hi,
I need the logic to utilize the command output to be feeded over to successive commands,
for example :
$ dtconf list-ls-data-sources -h hostname -P 636 -w ~/pwd.txt
DATA MASTER 1
DATA MASTER 2
DATA CONSUMER 1
DATA CONSUMER 1
DATA CONSUMER 1
Based on above output, i would like... (7 Replies)
test.txt is the dynamic file but some of combination are fix
like below are the lines
;wonder_off =
;wonder_off = disabled
wonder_off =
wonder_off = disabled
the test.txt can content them in any order
#cat test.xt
;wonder_off =
;wonder_off = disabled
wonder_off =
wonder_off =... (5 Replies)
I need to modify the find command below to exclude the output of the directory /usr/UDPM/PerfMgmt/shmlck
find / \( -fstype ctfs -o -fstype mntfs -o -fstype objfs -o -fstype proc -o ! local \) -prune -o -type f -perm -0002 -print 2>/dev/null
I have tried many iterations and placement of... (2 Replies)
Hi all,
I am trying to execute the following command:
find 'path' -ls -exec cksum {} \;
As you can see this simply finds files from a given path and runs cksum on them.
My problem is this, if i have a FIFO in a directory the find tries to execute cksum on it and gets stuck.
From the man page i... (9 Replies)
help pls...
i would like to change this
CURVE2 565489 789458 1258649
random data here...
CURVE2 565489 568795 6548921
random data here...
CURVE2 565489 123598 6446259
random data here...
CURVE2 565489 672956 2489657
into this
CURVE2 565489 586423 1258649
random data here...... (2 Replies)
How to I put my find command string into a script. It is currently to long to be entered manually at command line.
for FNAME in `find /unixsxxx/interface/x.x/xxxxxx -type f \( -name '*.KSH' -o -name '*.sh' -o -name '*.sql' -o -name '*.ksh' \) -exec grep -il xxx.xxx.xxx.xxx {} \;`; do C=`grep -c... (5 Replies)
Haven't worked in bash for ages. did a good bit of shell scripting in regular sh, but have forgotten most of it.
I have several thousand php files that now include the following line at the end of the file. There is no LF or CR/LF before it begins, it is just concatenated to the final line of... (3 Replies)
Perhaps the number one advanced find question is:
How to stop find from descending into subdirectories?
find command
Performing a non-recursive find in Unix
Use -prune with find command on AIX
Searching for files over 30 days old in current directory
disk space used for files with in a... (0 Replies)