Need a command to delete all files apart from last file generated


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a command to delete all files apart from last file generated
# 1  
Old 10-06-2010
Power Need a command to delete all files apart from last file generated

Hi,

I have a directory named (/output). this directory has files in the below format
Code:
abc.* , 
xyz*, 
djj*, 
iwe*, 
weewe*, 
rier*, 
3948903ddfgf*

these files are generated at random. what i need to do is. delete all the files of all kinds but keep only the last generated file of eachtime.

the final result will have only 1 type of file for each of the files present in the folder. the file that will be present will be the latest file.
# 2  
Old 10-06-2010
You should use command rm -R /output/*
# 3  
Old 10-06-2010
lkl i dont want to remove all the files. i just want to keep the last generated file for each type of file.

the above command that you said will delete all the files in my directory.,

(All my files are getting generated so i am unable to properly use mtime or atime option with find)
# 4  
Old 10-06-2010
try
Code:
#!/bin/bash

cd /output

for f in abc. xyz djj iwe weewe rier 3948903ddfgf; do

# ensure that we pick up a non-blank string 
# (not likely to happen, but if it does we will end up doing rm *)
# this will NOT work if the filenames have newlines in them

  if [ "x$f" != "x" ]
  then
     last=`ls -tr ${f}* |tail -1`
     save="save_$last"
     mv $last $save
     rm ${f}*
     mv $save $last
  fi

done


Last edited by wempy; 10-06-2010 at 05:43 AM.. Reason: corrected script error finger twiddle
# 5  
Old 10-06-2010
Code:
$ ruby -e '["abc.","xyz","djj""].each{|c| Dir["#{c}*"].sort{|x,y| File.mtime(x)<=>File.mtime(y)}[0..-2].each{|f| File.unlink(f)} } '

# 6  
Old 10-07-2010
The below code keep the latest .txt file only
Code:
ls -ct *.txt | awk '
BEGIN{ flag=1; }
{
if (flag){
# keep the latest file
flag=0;
} else {
# Remove the old files
print "rm "$1;
}
}
'| sh

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to bypass rm command when there are no files to delete?

Hi, Below command works fine when we have other files apart from hello.txtls | ggrep -v hello* | xargs rm -rfBut, if there is only one file i.e hello.txt the rm command does not find anything to delete and the script hangs. While there could be trivial ways to check using if conditions if... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

Please - Looking for a online command to delete files

I have list of files like below, I want to delete files older than 2 days except S0000000.LOG, I have command find /export/home/X_GZPQJK/out/file* -mtime +1 -exec rm {} \; but it is deleting S0000000.LOG, Can you please help me how to modify command to delete except S0000000.LOG. $ ls -ltr... (2 Replies)
Discussion started by: prince1987
2 Replies

3. Shell Programming and Scripting

Command to delete a word in all files

can anyone tell me what is the commands to delete the below particular word in the all files located in one particular file path files/ll>grep "/ftp/" test.kell ftp -m uskmc -d /ftp/ -i filename.zip output should be : ftp -m uskmc -d -i filename.zip (4 Replies)
Discussion started by: ramkumar15
4 Replies

4. Shell Programming and Scripting

Command to delete half of files in directory.

Hello Friends, I have directory called /tmp. which stores the log files. Whenever it becomes full, i want to delete half of files from all log files. even after deleting the files, if space is more than 90% then it should delete rest of half files. While deleting files, older files... (7 Replies)
Discussion started by: Nakul_sh
7 Replies

5. Shell Programming and Scripting

Find command to delete the files

Hi All, I've created 2 files touch -t 201309101234 aa10 touch -t 201309111234 aa11 Exact 60 days before from today date is SEPT 12th . As per the following command as i gave +60 means the files which were created before sept12th should be deleted find /etc/logs/*aa* -type f -atime +60... (5 Replies)
Discussion started by: smile689
5 Replies

6. UNIX for Dummies Questions & Answers

Find command to delete old files

Hi, I want to delete all the log files that was created on year 2008. My command is not working. Any idea? find . -name '*.log' -mtime 1460 -exec ls -lt {} \; Thank you. (2 Replies)
Discussion started by: samnyc
2 Replies

7. Shell Programming and Scripting

Files generated by a particular user

Hi I have the following files generated by different users on a directory -rw-rw-r-- 1 NAME1 database03 809 Nov 17 10:41 PCAS_CARD_TRANS_OFF.1111171041.lg -rw-rw-r-- 1 richard ccsdba 10968411 Nov 17 10:43 load_123_RX0_0.1111171016.lg -rw-rw-r-- 1 DEV db03 10713 Nov 17... (5 Replies)
Discussion started by: bobby1015
5 Replies

8. Shell Programming and Scripting

Retrieve logs generated in last 10 mins from a log file using 'grep' command

HI All, I have a log file where the logs will be in the format as given below: 2011-05-25 02:32:51 INFO PROCESS STARTING 2011-05-25 02:32:52 INFO PROCESS STARTED . . . I want to retrieve only the logs which are less than 5 mins older than current time using grep... (3 Replies)
Discussion started by: rvhg16
3 Replies

9. Solaris

core files not getting generated

Hi, We have an application ASPA . The application related processes are running in /ASPA/bin directory . now whenever a process terminates abruptly , a core file should be generated (correct me if i am wrong) in the /ASPA/bin directory . But i am not able to see any such files . The... (4 Replies)
Discussion started by: asalman.qazi
4 Replies

10. UNIX for Dummies Questions & Answers

delete or disable the system generated account

I have this unix version 3.0 "UNIX_SV server 4.0 3.0 3425 Pentium II(TM)-ISA/PCI" can i delete or disable the system generated account as "daemon";"uucp";"sys";"adm";"listen";"bin" and if yes how can i do it? Regards (1 Reply)
Discussion started by: sak900354
1 Replies
Login or Register to Ask a Question