List all old files except 15 latest files

 
Thread Tools Search this Thread
Operating Systems Linux Red Hat List all old files except 15 latest files
# 1  
Old 06-21-2011
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.

Someone please guide me on this since its fairly urgent.

I wish to use find by something like -
Code:
find . \! -newer $`\(ls -tp | head -11 \)` -exec rm {} \;

OR
Code:
find . -not -newer $(ls -tp | head -11) -print

Or anything that will make this condition work.

Thanks in advance,
RockF1Bull

Moderator's Comments:
Mod Comment Start using code tags, thanks.

Last edited by zaxxon; 06-21-2011 at 09:04 AM.. Reason: code tags
# 2  
Old 06-21-2011
One way:
Code:
ls -t | awk 'NR > 15'

# 3  
Old 06-21-2011
Thanks Franklin, that was simply awesome and worked straight away!!!

Though I am actually having a bit of a problem here -

While in a certain directory, when I do -
Code:
find . $(ls -tp | awk 'NR > 15') -exec rm {} \;

It throws me errors saying, -
Code:
find: filename1: No such file or directory
find: filename2: No such file or directory
find: filename3: No such file or directory
find: filename4: No such file or directory
find: filename5: No such file or directory

Please give me a clue if you know how to fix it.

I have tried following though it didn't work.
-
Code:
find . $'(ls -tp | awk 'NR > 15')' -exec rm {} \;

Cheers.

Moderator's Comments:
Mod Comment Use code tags - checkout (B) in https://www.unix.com/misc.php?do=cfrules, thanks.

Last edited by zaxxon; 06-21-2011 at 10:35 AM.. Reason: code tags
# 4  
Old 06-24-2011
Thanks guys, it indeed worked!!!

However, I was wondering if its possible for it to run like a loop in recursive folders.

Below is what I tried to do though it didn't work out and I got "syntax error" message.

Line -

find . $(ls -tp | awk 'NR > 15') -exec rm {} \;
OR
find . ! -newer $(ls -tp | awk 'NR > 15') -exec rm {} \;

If someone can shed more light on this, that will be really great.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with UNIX command to get the latest file from list of files with timestamp

Hi All, I have list of files like below with name abcxyz.timestamp. I need a unix command to pick the latest file from the list of below files. Here in this case the lates file is abcxyz.20190304103200. I have used this unix command "ls abcxyz*|tail -1" but i heard that it is not the appropriate... (2 Replies)
Discussion started by: rakeshp
2 Replies

2. Shell Programming and Scripting

Latest list of files of each type

find /tmp/testlog/kSR*"_"2018* -type f -printf '%T@ %p\n' | sort -n | tail -3 | cut -f2- -d" " /tmp/testlog/log/KSR04_2018-07-05.log /tmp/testlog/log/KSR04_2018-07-06.log /tmp/testlog/log/KSR01_2018-07-06.log But, I would see the following output(latest files for each KSR tuype) ... (3 Replies)
Discussion started by: jhonnyrip
3 Replies

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

4. Shell Programming and Scripting

I have this list of files . Now I will have to pick the latest file based on some condition

3679 Jul 21 23:59 belk_rpo_error_**po9324892**_07212014.log 0 Jul 22 23:59 belk_rpo_error_**po9324892**_07222014.log 3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log 22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log 0 Jul 20 05:50... (5 Replies)
Discussion started by: LoneRanger
5 Replies

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

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

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

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 (6 Replies)
Discussion started by: kamathg
6 Replies

10. Shell Programming and Scripting

ksh: How to get latest file from a list of files in a directory

Hi, I need to get the latest file from a list of files in a particular directory. Please could anyone help me out to get the file. Thank you, - Jay. (1 Reply)
Discussion started by: Jayathirtha
1 Replies
Login or Register to Ask a Question