Retain 3 latest files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retain 3 latest files
# 1  
Old 08-24-2007
Retain 3 latest files

Guys,

Please can you tell me how to retain 3 latest files in a directory and get rid of the rest ?

Thanks very much

Regards,
Ganesh
# 2  
Old 08-24-2007
What do you mean for latest? The three more recent files (excluding directories)? If so, try this:

Code:
rm `ls -rtp | grep -v "/$" | tail -3`

# 3  
Old 08-24-2007
ls -ltr | tail -3
# 4  
Old 08-24-2007
Thanks very much for your help.. yes, i meant 3 recent files.
# 5  
Old 08-24-2007
Sorry my question was :

Delete any files in the directiry except latest 3. Your script will delete latest 3 files.

Thanks
# 6  
Old 08-24-2007
Code:
rm `ls -tp | grep -v "/$" | awk '{ if (NR > 3) print; }'`

# 7  
Old 08-24-2007
You are star ! Cheers mate.. Works a treat !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. OS X (Apple)

Batch file to move video files and retain sub-directories

I have just purchased my first ever Apple computer - and am therefore new to UNIX also. I would like to create a simple "batch file" (apologies if this is the wrong terminology) to do the following: When I plug my camera into the MAC it automatically downloads photos and videos into a new... (1 Reply)
Discussion started by: mm0mss
1 Replies

4. 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

5. Shell Programming and Scripting

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. (3 Replies)
Discussion started by: spkandy
3 Replies

6. Shell Programming and Scripting

move files and retain subdir structure

hi: I have some files like this folder1/recording1.mp3 folder1/docs/budget.doc folder2/others/misc.mp3 folder3/others/notes.doc all this folders and files are under the mp3 folder. I would like to move just the mp3s to another folder but retain the subdir structure i have. So if... (4 Replies)
Discussion started by: jason7
4 Replies

7. UNIX for Dummies Questions & Answers

To list only the very latest files

Hi, There are huge no of files in the directory. If i say ls -ltr it is taking too much time. i want to see only the files for Feb,2009. Please help. Thanks (3 Replies)
Discussion started by: venkatesht
3 Replies

8. 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

9. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies

10. Shell Programming and Scripting

retain create/mod date and time of files

Hi, I have a requirement by which I need to take a snapshot of a certain directory of a certain types of files into a target directory within the same server (HP-UX 9000). The problem is that the files created in the target directory has the date and time of when the files were copied over. ... (5 Replies)
Discussion started by: jerardfjay
5 Replies
Login or Register to Ask a Question