Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 677341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:01:44+00:00 2026-05-14T01:01:44+00:00

I have a directory where our deployments go. A deployment (which is itself a

  • 0

I have a directory where our deployments go. A deployment (which is itself a directory) is named in the format:

<application-name>_<date>

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:

trader-gui_20091106
trader-gui_20091102
trader-gui_20091010
simulator_20091106
simulator_20091102
simulator_20090910
simulator_20090820

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:

trader-gui_20091106
simulator_20091106

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:01:44+00:00Added an answer on May 14, 2026 at 1:01 am

    A quick one-liner:

    ls | sed 's/_[0-9]\{8\}$//' | uniq |
    while read name; do
        rm $(ls -r ${name}* | tail -n +2)
    done
    

    List the files, chop off an underscore followed by eight digits, only keep unique names. For each name, remove everything but the most recent.

    Assumptions:

    • the most recent will be last when sorted alphabetically. If that’s not the case, add a sort that does what you want in the pipeline before tail -n +2
    • no other files in this directory. If there are, limit the output of the ls, or pipe it through a grep to select only what you want.
    • no weird characters in the filenames. If there are… instead of directly using the output of the inner ls pipeline, you’d probably want to pipe it into another while loop so you can quote the individual lines, or else capture it in an array so you can use the quoted expansion.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In our web application we have a directory which resides outside of the project.
Within our Active Directory domain, we have a MS SQL 2005 server, and a
In my current project we have directory with all of our third party (jQuery,
I have a directory with PDF files that I need to create an index
I have a directory structure like the following; script.php inc/include1.php inc/include2.php objects/object1.php objects/object2.php soap/soap.php
I have a directory with several subdirectories with files. How can I copy all
I have a directory of bitmaps that are all of the same dimension. I
I have a directory full of files and I need to pull the headers
I have a directory of files that I'd like to append file extension to
I have a directory with 500,000 files in it. I would like to access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.