Take the tour ×  I have a directory where our deployments go. A
deployment (which is itself a directory) is named in the format:
e.g. trader-gui_20091102  There are multiple applications
deployed to this same parent directory, so the contents of the
parent directory might look something like this:   I want to
write a bash script to clean out all deployments except for the
most current of each application. (The most current denoted by
the date in the name of the deployment). So running the bash
script on the above parent directory would leave:   Any help
would be appreciated.  share|improve this question add comment
3 Answers  up vote 1 down vote accepted  A quick one-liner:  ls
| sed 's/_[0-9]\{8\}$//' | uniq | while read name; do     rm
$(ls -r ${name}* | tail -n +2)  List the files, chop off an
underscore followed by eight digits, only keep unique names. For
each name, remove everything but the most recent.     • the most
recent will be last when sorted alphabetically. If that's not
the case, add a sort