Need to delete the latest two files..Help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to delete the latest two files..Help needed
# 1  
Old 01-13-2009
Bug 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 0 Jan 13 11:36 ali
-rw-r--r-- 1 oracle dba 0 Jan 13 11:36 bala
-rw-r--r-- 1 oracle dba 0 Jan 13 11:36 dinesh
-rw-r--r-- 1 oracle dba 0 Jan 13 11:36 bikash

So I want a script which will delete all the files except the latest 2 files...
Can anyone please help me out///I am new to scripting..
# 2  
Old 01-13-2009
Remove echo if working as expected...
Code:
ls -t | tail -n +3 | xargs echo rm

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 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. Shell Programming and Scripting

Needed shell script to get the latest file based on date

hi i need the shell script to get the file with latest date. for example in my input folder i have files like file_20130212.txt file_20130211.txt now my output folder should have the file with latest date i.e..file_20120212.txt i want to get the latest file .. i.e is should take... (6 Replies)
Discussion started by: hemanthsaikumar
6 Replies

4. Shell Programming and Scripting

Help needed with delete script.

I have searched lots of forums and couldn't find an answer to this question. We have a synology diskstation that runs linux and we want to use it as a temporary share disk for our employees. Here lies the problem since every script that I have found looks at the date from when it was edited, but... (3 Replies)
Discussion started by: Pettry
3 Replies

5. Shell Programming and Scripting

Delete all except latest datetime entery folder

Hello Expert, I have a strange requirement, I have folder where weekly backup are taken... week1_bkup_02102011 week2_bkup_09102011 week3_bkup_16102011 . . everyweek a backup is created I want to delete all folder except latest one for example in above a week4_bkup_23102011 is added... (3 Replies)
Discussion started by: aks_1902
3 Replies

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

7. Shell Programming and Scripting

Delete duplicate data and pertain the latest month data.

Hi I have a file with following records It contains three months of data, some data is duplicated,i need to access the latest data from the duplicate ones. for e.g; i have foll data "200","0","","11722","-63","","","","11722","JUL","09" "200","0","","11722","-63","","","","11722","JUL","09"... (10 Replies)
Discussion started by: vee_789
10 Replies

8. UNIX for Dummies Questions & Answers

Cron to delete email help needed

I would like to know if I can place a cron job (and what it might be of course) to delete all the mail in an inbox? Here are the servers specs: Operating systemLinuxService StatusClick to ViewKernel version2.6.28.9Machine Typei686Apache version2.2.11 (Unix)PERL version5.8.8Path to... (2 Replies)
Discussion started by: markmatu
2 Replies

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

10. 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
Login or Register to Ask a Question