[Solved] How to remove multiple files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] How to remove multiple files?
# 1  
Old 03-11-2014
[Solved] How to remove multiple files?

Hi Gurus,

I have below files in one directory. the file name has date and time portion which is exactly the file be created. I need keep only lasted created file which is abc_20140101_1550 and remove rest of the file.

Code:
abc_20140101_1300
abc_20140101_1200
abc_20140101_1400
abc_20140101_1500
abc_20140101_1530
abc_20140101_1550

I have tried below code, it works, but looks completed, anybody has some simple way.
Code:
#!/bin/ksh
find ./ -name "abc*" |sed 's/\.\///g'> filelist
numfiles=`cat filelist |wc -l`
echo $numfiles
let nfile=$numfiles-1
echo $nfile
tail filelist -$nfile > tailfile
for loop to delete file

Thanks in advance

Last edited by ken6503; 03-11-2014 at 11:48 AM..
# 2  
Old 03-11-2014
Quote:
Originally Posted by ken6503
I need keep only lasted created file which is abc_20140101_1550 and remove rest of the file.
You can simply use "ls -rt" to get your files sorted by date/time (instead of name) and then remove the last line (the latest file) and delete the others. Use the following blueprint:

Code:
ls -rt <optional file mask> |\
sed '$d'|\
while read FILE ; do
     rm -f "$FILE"
done

I hole this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
# 3  
Old 03-11-2014
Quote:
Originally Posted by bakunin
You can simply use "ls -rt" to get your files sorted by date/time (instead of name) and then remove the last line (the latest file) and delete the others. Use the following blueprint:

Code:
ls -rt <optional file mask> |\
sed '$d'|\
while read FILE ; do
     rm -f "$FILE"
done

I hole this helps.

bakunin
I think this code should work, I was unable to find a way to remove last line as you mentioned.

I will try and let you know.

thanks

---------- Post updated at 12:00 PM ---------- Previous update was at 10:59 AM ----------

Quote:
Originally Posted by bakunin
You can simply use "ls -rt" to get your files sorted by date/time (instead of name) and then remove the last line (the latest file) and delete the others. Use the following blueprint:

Code:
ls -rt <optional file mask> |\
sed '$d'|\
while read FILE ; do
     rm -f "$FILE"
done

I hole this helps.

bakunin
this code works
This User Gave Thanks to ken6503 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] List Files With Multiple Name Patterns

Hi, We have created a script that would accept the an indicator as a parameter and archive files present in a directory. The indicator would drive what the name pattern of the files to be archived should be. If the indicator is 1, then the pattern to look out for is FACT*. If the indicator is... (2 Replies)
Discussion started by: jerome_rajan
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Search, Extract and Rename Multiple Files

Hi, I want perl script for the below requirement. We have lot of files like below name in the directory 750464921-RE-file2.csv 750452173-RE-file1.csv 750385426-RE-file3.csv 750373470-RE-file4.csv And also we have another file as "Group.csv" in the same directory as per the below format... (9 Replies)
Discussion started by: armsaran
9 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Grep multiple files and display first match

I have a need to grep a large number of files, but only display the first result from each file. I have tried to use grep, but am not limited to it. I can use perl and awk as well. Please help! (9 Replies)
Discussion started by: dbiggied
9 Replies

4. Shell Programming and Scripting

[Solved] Multiple input files and output files

Hi, I have many test*.ft1 files to which I want to read as input for a script called pipe2txt.tcl and print the output in each separate file. For example, pipe2txt.tcl < test001.ft1 > test001.txt How can I read many files in this maner? thank you very much, Best, Pahuja (5 Replies)
Discussion started by: Pahuja
5 Replies

5. UNIX for Dummies Questions & Answers

[SOLVED] Rename multiple files

Hi. I have a large number of files with names like: t_ 0.20000E-02.dat There is actually a space after the underscore. These files are numbered numerically, i.e. t_ 0.20000E-02.dat, t_ 0.21000E-02.dat, t_ 0.22000E-02.dat and so on. What I would like to do is rename such that the file with... (8 Replies)
Discussion started by: lost.identity
8 Replies

6. Shell Programming and Scripting

[SOLVED] moving multiple files? mv

HI I have a list of files that are incorrectely names and I need to move them to new name .. I tried few things that has not worked so far can you help ? I need to rename all thes eifle ( tere are over 100 ) xldn0357bap.orig.new xldn0389bap.orig.new xldn0439bap.orig.new... (12 Replies)
Discussion started by: mnassiri
12 Replies

7. UNIX for Dummies Questions & Answers

[Solved] remove all files of 2010

Hi, how to use "rm" to delete only files from year 2010 ? Thank you. (2 Replies)
Discussion started by: big123456
2 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Renaming Multiple Files in Unix

I have mulitiple files in a unix directory e.g. FILE10001.txt FILE10002.txt I want the above two files to get renamed as by a single command. I tried with mv but it does not work FILE20001.txt FILE20002.txt Paresh (6 Replies)
Discussion started by: Pash
6 Replies

9. Shell Programming and Scripting

[Solved] Mathematical operation in multiple files

Hi experts, I need to do a mathematical calculation between each data in 3 different files. Output is using formula (A11+B11)/(1+C11). INPUT : File A.txt A11 A12 A21 A22 File B.txt B11 B12 B21 B22 File C.txt C11 C12 C21 C22 OUTPUT: (A11+B11)/(1+C11) (A12+B12)/(1+C12)... (3 Replies)
Discussion started by: guns
3 Replies

10. Shell Programming and Scripting

[SOLVED] Handling multiple files using awk

Hi, I am trying to process 2 files simultaneously using awk satisfying following condition, Both files contain 3 columns. It should take entry from column 1 from first file, look for that entry in file 2 and if found, add column 2 and column 3 from both files and output to third file. For e.g.... (4 Replies)
Discussion started by: muazfarooqaslam
4 Replies
Login or Register to Ask a Question