DEV Community

mhsohag11
mhsohag11

Posted on

Answer: Delete all files except the newest 3 in bash script

This will list all files except the newest three:

ls -t | tail -n +4

This will delete those files:

ls -t | tail -n +4 | xargs rm --

This will also list dotfiles:

ls -At | tail -n +4

and delete with dotfiles:

ls -At | tail -n

Top comments (0)