Hi,
I have a directory named (/output). this directory has files in the below format
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.
lkl
October 6, 2010, 3:30am
2
You should use command rm -R /output/*
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)
wempy
October 6, 2010, 4:41am
4
try
#!/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
kurumi
October 6, 2010, 5:24am
5
$ 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)} } '
The below code keep the latest .txt file only
ls -ct *.txt | awk '
BEGIN{ flag=1; }
{
if (flag){
# keep the latest file
flag=0;
} else {
# Remove the old files
print "rm "$1;
}
}
'| sh