Delete all Files except the Latest Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete all Files except the Latest Files
# 1  
Old 04-03-2009
Bug Delete all Files except the Latest Files

Hi,
In my scenario, i just want to delete all the files except the latest one.
How can I do?
Please reply me.
# 2  
Old 04-03-2009
Several possibilities.

'find -mtime' or 'find -ctime' will find old files - but it only has ~ one day resolution.
See man find.

Or cou could do a 'ls -lrt' which will list files sorted by date in reverse (-r) order. Combine it with head, tail, awk, xargs, rm to get your desired result.

If it's about log files you could also have a look at the logrotate package, which gives you lots of possibilities of rotating, deleting, compressing files in an automated fashion.

Hope this helps,
n.
# 3  
Old 04-03-2009
you can use -amin instead of atime or mtime ( its not supported in some OS).

or find -newer

see the man page of find.
# 4  
Old 04-04-2009
Quote:
In my scenario, i just want to delete all the files except the latest one.
Try:

ls -1t |awk 'NR>=2 { print $ 0; }' | xargs rm
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory RSK_123_20141113_031500.txt RSK_123_20141113_081500.txt RSK_126_20141113_041500.txt RSK_126_20141113_081800.txt RSK_128_20141113_091600.txt Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its... (7 Replies)
Discussion started by: kridhick
7 Replies

2. Shell Programming and Scripting

Need help with finding the latest files

Hi, Actually i got a client requirment and i need experts help here. we have 30 parent directories and in that we have so many subdirectories and files. i want to find only latest timestamp files with out touching subdirectories and need to redirect the latest files into some other... (3 Replies)
Discussion started by: lkeswar
3 Replies

3. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

4. Shell Programming and Scripting

how to delete the older files other than the recently added 5 files

Number of files will get created in a folder automatically daily.. so i hav to delete the older files other than the recently added 5 files.. Could u help me through this..?? (5 Replies)
Discussion started by: shaal89
5 Replies

5. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

6. Red Hat

List all old files except 15 latest files

Guys, Someone please guide me to tell me how do I print and later remove all old files in a folder recursively but keep only latest 15 modified files. When I do - ls -tp | head -15 I get the list of last 15 modified files whereas I need the list of all OTHER files except these files. ... (3 Replies)
Discussion started by: rockf1bull
3 Replies

7. UNIX for Advanced & Expert Users

Getting Latest files

Hai I wolud like to know how to get the latest files. ex: file_ssss_00 file_ssss_01 i need to get file_ssss_01 files only. (in Unix script) Please give some idea ... (2 Replies)
Discussion started by: raju4u
2 Replies

8. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

9. Shell Programming and Scripting

Need to delete the latest two files..Help needed

Suppose I have a directory called jeet and inside that directory so many files will be there.... Example: /abc/xyz/jeet $ ls -ltr total 0 -rw-r--r-- 1 oracle dba 0 Jan 13 11:36 naresh -rw-r--r-- 1 oracle dba 0 Jan 13 11:36 sreeni -rw-r--r-- 1 oracle dba ... (1 Reply)
Discussion started by: satyajit007
1 Replies

10. UNIX for Dummies Questions & Answers

when I try to run rm on multiple files I have problem to delete files with space

Hello when I try to run rm on multiple files I have problem to delete files with space. I have this command : find . -name "*.cmd" | xargs \rm -f it doing the work fine but when it comes across files with spaces like : "my foo file.cmd" it refuse to delete it why? (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question