|
|
|
Help writing a unix script
|
Original Message
|
Name: Vertabreaker
Date: May 30, 2006 at 05:50:19 Pacific
Subject: Help writing a unix scriptOS: AIXCPU/Ram: P4 2.6 / 1 GB ram |
Comment: I'm fairly new to writing shell scripts and I'm working on one now I need help with. I have a test directory that I'm working with so that I won't damage any important files. The directory has 10+ files with different time stamps on each file. I want to keep the most recent 3. The script I want, will check each files' timestamp to see which is the most recent, keep the most recent 3 files/builds and then delete the rest. Not sure if this is even possible but any help would be appreciated.
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Dlonra
Date: May 30, 2006 at 09:48:22 Pacific
|
Reply: (edit)very possible, start with man ls i am unsure of the AIX version of ls - there should be options regarding "time" next, man head OR man tail
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: nails
Date: May 30, 2006 at 12:27:02 Pacific
|
Reply: (edit)I don't know how portable ls's sort by timestamp option, -t, is, but this works on Solaris: #!/bin/ksh rm $(ls -t | tail +4) I don't mean to elaborate on the obvious, but I'd make sure the ls/tail works the way you want it to before using the rm command.
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
|
Reply: (edit)Ok, here's where I'm at now. ls -t|tail +5 does the trick as far as listing the files I need to remove. It has to be +5 to account for the actual executable file being run. So I'm setting up another test directory for a back up before I start with the actual removing.
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
|
Reply: (edit)lt -t|tail +5|xargs rm works like a charm! Fooled around with it this morning for a bit and made sure it worked under different circumstances and it works exactly the way I need it to work. Thanks again for the help. :)
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: nails
Date: May 31, 2006 at 08:19:56 Pacific
|
Reply: (edit)You're welcome, but why don't you put your script in a different directory? Sooner or later, you're script file will be eligible to be deleted. ls -t /my/testdir|tail ....
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
|
Reply: (edit)I actually have to rework it now since there are multiple directories that I need to run this script for. Basically, there's a directory labeled builds with about 10 directories in it as well. I'm now trying to have this script at the root level and be able to go through each of these 10 sub-directories and remove all files except the most recent 3.
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: nails
Date: May 31, 2006 at 11:29:39 Pacific
|
Reply: (edit)If you are trolling thru a directory tree why not use the find command: # untested find /my/testdir -type f -print|xargs ls -t|tail .... This should work unless you need to keep exactly 3 files in each directory.
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
|
Reply: (edit)In the big picture what I'm interested in doing is trolling through the directory tree and running a script from the root directory that will go through my "builds" directory which contains multiple directories. Each directory within the builds directory has multiple files eating up space. I'm trying to get the script to go through, and delete all files from the builds directory that are not the most recent 3. So, say there's 3 directories within the builds directory, DirA, DirB, and DirC. For example: DirA has 10 files dating anywhere from todays date back to a couple months ago. DirB has 3 files. DirC has 5 files from 5 months ago. I want this script to go through and do the following: A) If the directory has 3 files in it, then the script doesn't touch that directory, so DirB would remain the same. B) If there are more then 3 files, then the script should go through the files, determine which are the 3 most recent...then remove the other files. I can get it to search by directly specifying the directory, but for some reasons I can't get it to look in any of the directories when I'm in the root directory.
Report Offensive Follow Up For Removal
|
|
Response Number 10
|
Name: nails
Date: June 1, 2006 at 09:23:00 Pacific
|
Reply: (edit)There are many ways to do this, but I'd use the find command to search for the directories in your directory structure: #!/bin/ksh sdir=/home/nails/sct3/build find "$sdir" -type d -print|while read xdir do if [[ $xdir == $sdir ]] then # skip the build directory continue fi echo $xdir echo "changed to directory $xdir in build tree" # perform the rm here done # end script After changing to the directory, run your remove command string previously covered. Note that I'm using a full path to the directory to be searched. Using a relative path forces you to cd back to where you started from. Let me know if you have any further questions. Nails
Report Offensive Follow Up For Removal
|
|
Response Number 11
|
|
Reply: (edit)Nails, Good news and bad news lol. Good news is that the following works...somewhat. #!/bin/ksh sdir=/builds find "$sdir" -type d -print|while read xdir do if $xdir==$sdir;then continue fi echo $xdir echo "Changed to directory $xdir" ls -t $xdir|tail +4 ##havn't added |xargs rm yet## done Bad news is that I get this output: ./removeOld.sh[11]: /builds==/builds: not found /builds Changed to directory /builds ./removeOld.sh[11]: /builds/removeOld==/builds: not found /builds/removeOld Changed to directory /builds/removeOld testEar1.0.6.01.ear testEar1.0.5.01.ear testEar1.0.4.01.ear testEar1.0.3.01.ear ./removeOld.sh[11]: /builds/removeOBLess==/builds: not found /builds/removeOBLess Changed to directory /builds/removeOBLess ./removeOld.sh[11]: /builds/removeOld2==/builds: not found /builds/removeOld2 Changed to directory /builds/removeOld2 Now...the other good news is that it DOES work. I got the output I was expecting to see. The files : testEar1.0.6.01.ear testEar1.0.5.01.ear testEar1.0.4.01.ear testEar1.0.3.01.ear should be listed since they are going to be deleted and there should be no other files from the other directories since they contain 3 and no more. But I'm confused on why I'm getting an error from the conditional statement. Also, I took the "[[ ]]" out of the statement because I was receiving an error stating that it couldn't find a "then" even though it was present. Thanks in advance for any suggestions. Regards ~Verta
Report Offensive Follow Up For Removal
|
|
Response Number 12
|
Name: nails
Date: June 2, 2006 at 13:07:11 Pacific
|
Reply: (edit)I may have slightly mislead you. I meant to change to the directories. What happens if you do this: # untested #!/bin/ksh sdir=/builds find "$sdir" -type d -print|while read xdir do if $xdir==$sdir;then continue fi cd $xdir echo "Changed to directory $xdir" ls -t |tail +4 ##havn't added |xargs rm yet## done
Report Offensive Follow Up For Removal
|
|
Response Number 13
|
|
Reply: (edit)Nails, Currently this is my script: #!/bin/ksh sdir=/builds find "$sdir" -type d -print|while read xdir do echo "Changed to directory $xdir" cd $xdir ls -t $xdir|tail +4|xargs rm done Now, it does everything I need it to do. But the only catch is, is that if there's more then 3 directories in my "Builds" directory it tries to delete the oldest ones. Now, it's not really a problem/issue since I'm not issueing the rmdir command, but it's kind of an eyesore lol. So right now I'm trying to find a way to get it to only delete the files inside the subdirectories of the "Builds" directory and only the files.
Thanks again for your help. If you can come up with anything to fix this minor issue it'd be appreciated. Regards ~Verta
Report Offensive Follow Up For Removal
|
|
Response Number 14
|
|
Reply: (edit)Nails, Just an update. The following seems to work 100%. #!/bin/ksh sdir=/buildsTest find "$sdir" -type d -print|while read xdir do if [ $sdir = xdir ]; then echo "Nothing to remove" else cd $xdir echo "Changing to directory $xdir" ls -t $xdir|tail +4|xargs rm fi done Thanks for everything. Regards ~Verta
Report Offensive Follow Up For Removal
|
|
Response Number 15
|
Name: nails
Date: June 6, 2006 at 10:47:20 Pacific
|
Reply: (edit)You're welcome. Glad to help. Be advised that you may have trouble with the above script if any directory name contains a space. It'll help if you have double quotes around your variables such as: cd "$xdir"
Report Offensive Follow Up For Removal
|
|
Response Number 16
|
|
Reply: (edit)Thanks for that tip. I created a test folder with a space in the name on purpose and using "" proved to work. I actually ran into another problem. That was the script was comparing files and subdirectories alike(if there were subdirectories). So I modified it a little and this is the outcome which works 100% in all cases, regardless of spaces, directories within directories, or number of files in the directory. #!/bin/ksh sdir=/builds find "$sdir" -type d -print|while read xdir do if [ $sdir = $xdir ]; then continue else cd "$xdir" echo "Changed to directory $xdir" ls -lt|awk '$1!~/^drwx/{print $9}'|tail +5|xargs rm fi done Thanks again for the help, it's appreciated. Regards ~Verta
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|