I want to delete all but the 4 newest directories in my parent directory. How would you do this in Bash?
ls -atrd */ | head --lines=-4 | xargs rm -rf
Edit: added 'a' argument to ls
Please clarify if you mean “delete all directories but the four newst ones” or “delete everything (files and directories) except for the four newest directories”.
Please also note that creation times are not known for directories. One can only tell when a directory was last modified, that is, had files added, removed or renamed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With