LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-25-2005, 04:03 AM   #1
Prasun1
Member
 
Registered: Jun 2005
Posts: 41

Rep: Reputation: 15
removing last created files


Hi,

I have been trying to figure this out. Suppose I have just created 10 files.Then when I do a ls -lrt I get the files in the order with the lastly created file name printed at the last. I understand till this. Suppose now I want to remove the lastly created n files. Is there a way to do this.


Please help me with the command or the programming ( shell scripting) to do this.

Also it will be very nice if you can provide me with some cool links regarding unix. I mean sites that can help me in studying and developing concepts regarding the operating system.


Thanks and Regards
Prasun
 
Old 09-25-2005, 04:11 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

To answer the first question:

tail and head will help you:

ls -tr | tail -4 Will give you the last 4 files. Head does the same, but for the first files.

man tail (head) for more details.

Hope this helps.
 
Old 09-25-2005, 04:39 AM   #3
Prasun1
Member
 
Registered: Jun 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks a lot, it will be really helpful.

Regards
Prasun
 
Old 09-25-2005, 04:53 AM   #4
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Important: don't forget -1 (that's a one, not an ell) as option to ls,
otherwise you might end up with several files on a line (which are all deleted).

ls -rt1

If your files contain whitespaces, also include -Q (not sure if that helps).

Edit: No, it doesn't seem to help. Does anyone have a solution?

Last edited by addy86; 09-25-2005 at 04:54 AM.
 
Old 09-25-2005, 05:09 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
It's a rather lengthy command, but find can do it:
Code:
find . -maxdepth 1 -type f -newer $( ls -tr | tail -n 5 | head -n 1 ) -exec rm {} \;
That will delete the four most recently modified files. For the original problem of the last 10 files:
Code:
find . -maxdepth 1 -type f -newer $( ls -tr | tail -n 11 | head -n 1 ) -exec rm {} \;
This obviously won't work if there are only 10 files in the directory. In that case though, it makes more sense to use "rm *" because the last 10 modified files are the only files present.

Standard disclaimer: Never, never, never execute any command responsible for moving or deleting files without verifying the command selects the files as you expect. Rip off the "-exec rm {} \;" portion to test until satisfied.
 
Old 09-25-2005, 05:31 AM   #6
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
I just found a solution myself:
ls -Q1rt | tail -4 | xargs rm


By the way: I just deleted 3 GiB of videos because I forgot to cd into the test directory I created to find the solution What do we learn from this? Exactly: never ever use rm without checking that you're in the right directory and entered the correct file names (especially when using wildcards or tab-completion followed by a fast ENTER ).
The same can happen with mv:
mv file1 file2 some_dir
Imagine you forget to type some_dir...
 
Old 09-25-2005, 09:47 AM   #7
Prasun1
Member
 
Registered: Jun 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks to all of you ...it really proved to be of great help.

Regards
Prasun
 
Old 09-25-2005, 07:31 PM   #8
pbhj
Member
 
Registered: Dec 2002
Location: UK
Distribution: Slackware 12; Ubuntu 7.10
Posts: 358

Rep: Reputation: 32
alias?

Perhaps you shoudl alias rm to "rm -i"? Then call rm with full path when you really mean it!?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing files wihtout removing containing Direcotry caps_phisto Linux - General 2 10-07-2004 08:16 AM
Date interrogation for files created chunky Linux - General 2 08-03-2004 03:31 PM
If I delete files are new ones created?? BajaNick Linux - Software 1 07-06-2004 11:07 PM
Permission for files created in NT for Linux use strik3r Linux - General 1 02-25-2004 04:05 AM
how are /dev files created? jkobrien Linux - Hardware 3 10-17-2003 06:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration