Computing.Net > Forums > Unix > Help writing a unix script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help writing a unix script

Reply to Message Icon

Name: Vertabreaker
Date: May 30, 2006 at 05:50:19 Pacific
OS: AIX
CPU/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.



Sponsored Link
Ads by Google

Response Number 1
Name: Dlonra
Date: May 30, 2006 at 09:48:22 Pacific
Reply:

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



0

Response Number 2
Name: nails
Date: May 30, 2006 at 12:27:02 Pacific
Reply:

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.


0

Response Number 3
Name: Vertabreaker
Date: May 31, 2006 at 04:27:42 Pacific
Reply:

Thank you for the quick responses. I will try out both suggestions and post back my finds.


0

Response Number 4
Name: Vertabreaker
Date: May 31, 2006 at 04:49:03 Pacific
Reply:

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.


0

Response Number 5
Name: Vertabreaker
Date: May 31, 2006 at 05:42:00 Pacific
Reply:

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. :)


0

Related Posts

See More



Response Number 6
Name: nails
Date: May 31, 2006 at 08:19:56 Pacific
Reply:

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 ....


0

Response Number 7
Name: Vertabreaker
Date: May 31, 2006 at 09:36:18 Pacific
Reply:

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.


0

Response Number 8
Name: nails
Date: May 31, 2006 at 11:29:39 Pacific
Reply:

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.



0

Response Number 9
Name: Vertabreaker
Date: June 1, 2006 at 06:37:15 Pacific
Reply:

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.


0

Response Number 10
Name: nails
Date: June 1, 2006 at 09:23:00 Pacific
Reply:

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


0

Response Number 11
Name: Vertabreaker
Date: June 2, 2006 at 06:43:10 Pacific
Reply:

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


0

Response Number 12
Name: nails
Date: June 2, 2006 at 13:07:11 Pacific
Reply:

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



0

Response Number 13
Name: Vertabreaker
Date: June 5, 2006 at 05:06:41 Pacific
Reply:

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


0

Response Number 14
Name: Vertabreaker
Date: June 6, 2006 at 06:15:52 Pacific
Reply:

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


0

Response Number 15
Name: nails
Date: June 6, 2006 at 10:47:20 Pacific
Reply:

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"



0

Response Number 16
Name: Vertabreaker
Date: June 7, 2006 at 06:31:14 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Help writing a unix script

Writing a Unix script to ftp files www.computing.net/answers/unix/writing-a-unix-script-to-ftp-files/3934.html

help redoing unix script student db www.computing.net/answers/unix/help-redoing-unix-script-student-db/5151.html

Need help with a unix script www.computing.net/answers/unix/need-help-with-a-unix-script/4288.html