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
shell script to delete directories... Stephan Shell Programming and Scripting 5 05-16-2008 09:11 AM
How to delete files in UNIX using shell script theguy16 Shell Programming and Scripting 7 04-09-2008 05:40 AM
script to delete files vats Shell Programming and Scripting 12 09-01-2007 06:27 AM
Need Help: Delete a file by Shell Script r3edi Shell Programming and Scripting 5 07-11-2005 08:13 AM
how to delete empty files in a shell script rpnuge UNIX for Advanced & Expert Users 1 07-11-2002 05:56 PM

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 07-07-2006
krishnarao krishnarao is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 4
Exclamation shell script for delete old files

i want to delete all the files in my directory except the latest one. i need to do this from shell script.
say i have
a.txt - latest file
b.txt,
c.txt..
it should delete all the files except a.txt?
  #2 (permalink)  
Old 07-07-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798

Code:
file=$(ls -tr | tail -1)
find . ! -newer "$file" -maxdepth 1 -exec rm {} \;

  #3 (permalink)  
Old 01-13-2009
satyajit007 satyajit007 is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 18
It's not working sir..Any other option...
  #4 (permalink)  
Old 01-13-2009
angheloko's Avatar
angheloko angheloko is offline
Registered User
  
 

Join Date: Jul 2008
Location: Philippines
Posts: 125
modifying vino's script a little


Code:
x=`ls -lt | grep ^- | head -1 | awk '{print $NF}'`
find . ! -name . -prune -type f ! -name "$x" -exec rm {} \;

Of course, be sure to backup first before testing
  #5 (permalink)  
Old 01-13-2009
orailker orailker is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 3
Hi satyajit007,
Put the following 2 lines in your script file and make your script file executable with "chmod +x script_file" and then run your script file.

new_file=`ls -ltr|grep -v drw|tail -1`
find . -type f ! -newer $new_file -maxdepth 1 -exec rm {} \;
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 04:12 PM.


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