I would like to delete a certain number of the largest files in a directory.
I can get the filenames of the first ten with:
ls -S | head
(or a richer albeit more complex to parse ls -lS | head
)
How would I pass those to rm ? via xargs ?
So would ls -S | head | xargs rm
work (even with filenames with spaces etc?)
Is there a better / safer way ?
Sorry but I could not test this myself in fear of goofing (the directory has stuff I really don't want to zap).
I use macOS.
echo
. E.G.ls -S | head | xargs echo rm
– phemmer Oct 23 '16 at 18:22foo bar
, which he wants to delete, the command you suggest will not alert him to the fact that his command will delete his smaller filesfoo
andbar
(which he wants to retain). – Scott Oct 23 '16 at 19:18E.G.
). As for your objection, you can easily overcome this withxargs -n 1
. – phemmer Oct 23 '16 at 20:38