Delete all files except last modified


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete all files except last modified
# 1  
Old 07-19-2010
Delete all files except last modified

Hello All,

I need to delete all the files with a particular naming format at a fixed path except the latest 1. Please help.

example:

At path /tmp, I have 3 files viz: log1, log2 and log3. file log2 was the one that was modified most recently. So I need to find and delete log1 and log3 only.

Please suggest the shell code for this.
# 2  
Old 07-19-2010
Code:
ls -lrt /tmp/*log* | tail -1 > out_file #this is ur latest file

for file in `ls /tmp/*`
do
if [[ file == `echo out_file`]]
then 
echo "$file is the latest modified file"
else
rm file 
#dont do test ur script with this script it will definately delete files. so put some other command and then test it###
fi
done

i just found a better way of what u intend to do....

Code:
touch -t `date +%Y%m%d%H%M%S` /tmp/$$
find /tmp/ -type f -newer /tmp/$$ -exec rm {} \;
rm /tmp/$$

try this also

Last edited by dazdseg; 07-19-2010 at 11:10 AM.. Reason: better way
# 3  
Old 07-19-2010
MySQL

thanks...

it worked after combining while loop and find -newer. Smilie

will try the second code you have given as well, it looks more robust!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy files from one drive to another, keeping most recently modified files

Hi all, I am a bit of a beginner with shell scripting.. What I want to do is merge two drives, for example moving all data from X to Y. If a file in X doesn't exist in Y, it will be moved there. If a file in X also exists in Y, the most recently modified file will be moved to (or kept) in... (5 Replies)
Discussion started by: apocolapse
5 Replies

2. Solaris

Which files to modified in Solaris 10?

Hi All, In Solaris 10, where can I change or modified parameter like /dev/ip ip_ignore_redirect 1? If I use ndd command to change it, it will only last until the next reboot but I need to set it permanently. For Solaris 9, I will refer to /etc/rc2.d/S69inet but this file is no longer there... (2 Replies)
Discussion started by: mailbox80
2 Replies

3. Shell Programming and Scripting

help: find and modified files script

hello all im a newbie in the linux world ..i have just started creating basic scripts in linux ..i am using rhel 5 ..the thing is i wanted to create a find script where i could find the last modified file and directory in the directory given as input by the user and storing the output in a file so... (6 Replies)
Discussion started by: tarunicon
6 Replies

4. Shell Programming and Scripting

find files modified more than a day

Hi All, I am using the below command to check the files modified within last 24hours find /home/karthik -mtime -1 -type f -exec ls -l {} \; What parameter do i need to add in the above command to check the files modified in last 2 or 3 days Kindly let me know if any other alternative... (2 Replies)
Discussion started by: karthikn7974
2 Replies

5. UNIX for Dummies Questions & Answers

Delete the last modified file

Hi All, I have the following script to delete the last modified file in a directory. #!/bin/ksh if file in $(ls -t /home/KOP/Purge | head -1) then rm -f $file fi But I keep getting the error 0653-901 Cannot get file status Any suggestions or comments as to where I'm getting... (1 Reply)
Discussion started by: kingofprussia
1 Replies

6. UNIX for Dummies Questions & Answers

how to retrieve original contents of a modified file (modified using vi)

Made changes to a file using vi editor and saved those changes now realised that the changes are not required How can I get the previous version of the file.i.e the one which was there on which I had made changes (3 Replies)
Discussion started by: novice100
3 Replies

7. UNIX for Dummies Questions & Answers

list last 10 days modified files

All, Is there is anyother single command that will handle ls -lrt | tail -10 Please let me know Thanks, Arun. (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

8. Shell Programming and Scripting

Finding out the last modified time for files

I need to find out the last modified time for the files which are older than 6 months. If I use ls -l, the files which are older than 6 months, I am just getting the day, month and year instead of exact time. I am using Korn shell, and SUN OS. Thanks in Advance, Kiran (3 Replies)
Discussion started by: kumariak
3 Replies

9. UNIX for Dummies Questions & Answers

Finding modified files

Last week I was using the command: ' find /directory -mtime -2 -print' and it showed all the files modified within that period. However, now it only displays the directories and not the files modified. The only thing that changed is that I was granted access to some files. Thanks (2 Replies)
Discussion started by: rhayabusa
2 Replies
Login or Register to Ask a Question