LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   removing last created files (https://www.linuxquestions.org/questions/linux-general-1/removing-last-created-files-366807/)

Prasun1 09-25-2005 04:03 AM

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

druuna 09-25-2005 04:11 AM

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.

Prasun1 09-25-2005 04:39 AM

Thanks a lot, it will be really helpful.

Regards
Prasun

addy86 09-25-2005 04:53 AM

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?

Dark_Helmet 09-25-2005 05:09 AM

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.

addy86 09-25-2005 05:31 AM

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 :rolleyes: 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...

Prasun1 09-25-2005 09:47 AM

Thanks to all of you ...it really proved to be of great help.

Regards
Prasun

pbhj 09-25-2005 07:31 PM

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


All times are GMT -5. The time now is 10:17 AM.