[SOLVED] Delete 10 oldest files

This forum is dedicated to basic help and support :

Ask here your questions about basic installation and usage of Mageia. For example you may post here all your questions about getting Mageia isos and installing it, configuring your printer, using your word processor etc.

Try to ask your questions in the right sub-forum with as much details as you can gather. the more precise the question will be, the more likely you are to get a useful answer

[SOLVED] Delete 10 oldest files

Postby Per » Apr 22nd, '13, 22:51

As the heading says, I want to delete the 10 oldest files (according to access time) in a directory. I've tried the following:
Code: Select all
for a in `ls -u|tail`;do rm "$a";done

but that doesn't work if there is spaces in the filenames.
Last edited by Per on Apr 23rd, '13, 20:52, edited 1 time in total.
Per
 
Posts: 24
Joined: Jul 30th, '11, 17:58

Re: Delete 10 oldest files

Postby Mayavimmer » Apr 23rd, '13, 09:22

Code: Select all
ls -u |tail |while read a; do rm -f "$a"; done

Of course, be safe when removing stuff.
Your version does not work because the backticks phrase is inline replaced by the output of its commands, coalescing all newlines and other whitespace to single spaces.
Mayavimmer
 
Posts: 27
Joined: Nov 30th, '12, 10:17

Re: Delete 10 oldest files

Postby Per » Apr 23rd, '13, 20:51

Mayavimmer wrote:
Code: Select all
ls -u |tail |while read a; do rm -f "$a"; done

Of course, be safe when removing stuff.
Your version does not work because the backticks phrase is inline replaced by the output of its commands, coalescing all newlines and other whitespace to single spaces.

Thanks. The only thing I wanted more was to get prompted for each deletion. I googled a bit more and found that zsh is your friend:
Code: Select all
rm *(Oa[1,10])
Per
 
Posts: 24
Joined: Jul 30th, '11, 17:58

Re: [SOLVED] Delete 10 oldest files

Postby doktor5000 » Apr 23rd, '13, 22:46

Simply don't use -f, or explicitly use rm -i, which is the default - that way it's interactively, and asks you for every file.
Cauldron is not for the faint of heart!
Caution: Hot, bubbling magic inside. May explode or cook your kittens!
----
Disclaimer: Beware of allergic reactions in answer to unconstructive complaint-type posts
User avatar
doktor5000
 
Posts: 18065
Joined: Jun 4th, '11, 10:10
Location: Leipzig, Germany


Return to Basic support

Who is online

Users browsing this forum: No registered users and 1 guest