The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing the values to the secondary script when it invoked by primary script venu_eie UNIX for Advanced & Expert Users 2 07-03-2008 08:10 AM
Passing the values to the secondary script when it invoked by primary script venu_eie Shell Programming and Scripting 1 07-03-2008 07:16 AM
create a shell script that calls another script and and an awk script magikminox Shell Programming and Scripting 0 06-26-2008 03:50 AM
help me in sending parameters from sqlplus script to unix shell script Hara Shell Programming and Scripting 2 01-29-2008 03:31 PM
Shell Script: want to insert values in database when update script runs ring Shell Programming and Scripting 1 10-25-2007 04:06 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-22-2008
sharp859 sharp859 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 3
Exclamation regarding a script

Hello any body,

I am looking for a help in writing a script to delete a directory as below scenario.

I have a directory called builds in that many directories are there named say 12343,45686,76543(number named )and other name like changelist,maven,logs ,name.txt,and other .sh files etc. I am looking a script which should keep all directories and 4 latest numbered directories.i.e I need to delete numbered directories and keeping latest 4 directories at any time.I have written a script which keeps 4 latest directories or files at any point of time.please look my script. I should modify such a way tht it should not deleted any files or directories other than numbered directory and at the same time I need to have 4 latest numbered directories (it may be 1 months old or what ever) oldest from fifth directory should be removed.

#!/bin/bash




# Scans a directory and deletes all and retains user specfied directory

#Usage: remov.sh /path/ dir number of files to keep





if [ $# -ne 2 ]; then

echo 1>&2 "Usage: $0 dir_path number of files to keep=4 "

exit 1

fi





cd $1

files_in_dir=`ls | wc -l`

files_to_delete=`expr $files_in_dir - $2`

if [ $files_to_delete -gt 0 ]; then

ls -t | tail -n $files_to_delete | xargs rm -rf

#ls -t will list files based on date and tail -n tails the list



#xargs will break the list of arguments into sublists small enough to be acceptable

if [ $? -ne 0 ]; then

echo “An error ocurred deleting the files”

exit 1

else

echo “$files_to_delete file deleted.”

fi

else

echo “Nothing to delete!”

fi
  #2 (permalink)  
Old 12-22-2008
Padow Padow is offline
Registered User
  
 

Join Date: Dec 2008
Location: Maryland
Posts: 178
This will list the files/directories with numerals in the name, except the 4 oldest.

ls -ldt *[0-9]* | tail +5 | awk '{ print $9 }'
  #3 (permalink)  
Old 12-22-2008
sharp859 sharp859 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 3
regarding a script

it is working fine for me thanks a lot Padow,as i used n option I can keep as many directory as I want cheers

Thanks
  #4 (permalink)  
Old 01-02-2009
sharp859 sharp859 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 3
Can any body give me a script which should keep n(user defind) latest numbered directory (alpha numeric like 12345,3456)at any point of time,my script works for all files,need to list only numbered files?

Thanks,
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0