Page 1 of 1

[SOLVED] Delete 10 oldest files

PostPosted: Apr 22nd, '13, 22:51
by Per
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.

Re: Delete 10 oldest files

PostPosted: Apr 23rd, '13, 09:22
by Mayavimmer
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.

Re: Delete 10 oldest files

PostPosted: Apr 23rd, '13, 20:51
by Per
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])

Re: [SOLVED] Delete 10 oldest files

PostPosted: Apr 23rd, '13, 22:46
by doktor5000
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.