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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to delete the older files other than the recently added 5 files
# 1  
Old 02-22-2012
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..??
# 2  
Old 02-22-2012
What have you tried?

Take a look at:
Code:
ls -lt

https://www.unix.com/unix-dummies-que...ate-order.html
You will also want to search through the forums.

The above will show you how to see your files in date order. What do you think would be the next step?
# 3  
Old 02-22-2012
ya.. it will list the files in date order.. but i hav to delete the files other than recently added 5 files.. (There should be only 5 files which are been recently added.. it should delete all the other files..)
# 4  
Old 02-22-2012
Hello dude just try the below code
Code:
ls -lt | awk 'NR == 1 {next} {print $9}' | sed -ne '6,$p' | xargs rm -rf



Moderator's Comments:
Mod Comment Please use next time code tags for your code and data
# 5  
Old 02-22-2012
You can save one pipe by replacing:
Code:
ls -lt | awk 'NR == 1 {next} {print $9}'

by
Code:
 ls -t -c1

... Unless you are trying to learn awk of course...Smilie
# 6  
Old 02-22-2012
Hi,
Try this,
Code:
ls -lt | awk '{if(NR > 6){print "rm -f",$9;}}' | sh

ls -lt | awk '{if(NR > 6){print $9;}}' | xargs rm -f

Cheers,
Ranga:-)
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 copy the two most recently added files to another directory - HP-UX?

Hello, I am attempting to find and copy the two most recently added files to a specific directory, that fit a specific format. I was able to find the command to list the two most recently added files in directory: ls -1t | head -n 2 The command lists the two files names in a vertical list,... (11 Replies)
Discussion started by: mattkoz
11 Replies

2. UNIX for Dummies Questions & Answers

How to delete all the files older than a date?

Hi, I need a command for deleting all the compress files *.Z that are older than the current date - 5 days. Basically I have a directory where daily I meet some back up files and I want to remove automatically the ones 5 days (or more) older than the current date. How can I write a 'rm' command... (1 Reply)
Discussion started by: Francy
1 Replies

3. UNIX for Dummies Questions & Answers

How to archive old files from the recently added 10 files?

Hi there, I am very new to unix and having trouble with a fairly simple statement: cd /user ls -t -c1 | sed -ne '11,$p' | mv xargs archive/ What I want the code to do is sort all files in a directory by timestamp, select all of the files after the 10th file, and then move those files... (3 Replies)
Discussion started by: DSIReady
3 Replies

4. Shell Programming and Scripting

To delete files older than 24 hrs

I have to retain only 1 day files in my system an I have to delete all the other files which are older than 24 hrs. Please let me know the option I have to give in the find -mtime command. (3 Replies)
Discussion started by: rajesh8s
3 Replies

5. Shell Programming and Scripting

Delete files older than today

is it -mtime +1 as i need all files older than today to be deleted (6 Replies)
Discussion started by: dinjo_jo
6 Replies

6. Solaris

Delete files older than 30 days

Hi all, I want to delete log files with extension .log which are older than 30 days. How to delete those files? Operating system -- Sun solaris 10 Your input is highly appreciated. Thanks in advance. Regards, Williams (2 Replies)
Discussion started by: William1482
2 Replies

7. UNIX for Dummies Questions & Answers

Delete files older than 30 days

This is driving me crazy. How can I delete files in a specifc directory that are over 30 days old? Thanks in advance. (3 Replies)
Discussion started by: tlphillips
3 Replies

8. Shell Programming and Scripting

how to delete the files which are the 30 min older...?

Hi all, i have a simple question that i want to find out the 30 minutes older files and delete those files from the particular location(Folder) Generally for this purpose used to retreive the files with "atime" command For example: find and delete the 2 days older log files use this below... (2 Replies)
Discussion started by: psiva_arul
2 Replies

9. UNIX for Dummies Questions & Answers

How can I delete files older than 7 days?

I will like to write a script that delete all files that are older than 7 days in a directory and it's subdirectories. Can any one help me out witht the magic command or script? Thanks in advance, Odogboly98:confused: (3 Replies)
Discussion started by: odogbolu98
3 Replies

10. UNIX for Dummies Questions & Answers

delete files older than 7 days

can anyone tell me how I would write a script in ksh on AIX that will delete files in a directory older than 7 days? (1 Reply)
Discussion started by: lesstjm
1 Replies
Login or Register to Ask a Question