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

  • Home
  • SEARCH
  • 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 8673837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:33:37+00:00 2026-06-12T19:33:37+00:00

I am trying to write a program that will search through a main directories

  • 0

I am trying to write a program that will search through a main directories sub-directories and list the files that have the largest number at the end. ex: filename_100.

find . -name "*_*" | sort -n | tail
  • 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-06-12T19:33:39+00:00Added an answer on June 12, 2026 at 7:33 pm

    sort sorts starting from the beginning of the string, so you can’t use it without first splitting off the leading part of the filename. This loop will do that; it prints out the part of the filename after the _, followed by the full filename.

    for fn in `find . -name '*_*'`; do
        echo "${fn##*_} $fn"
    done
    

    Then you can pipe the output to sort and tail to get the largest number, and then to cut to pick out only the filename itself.

    for fn in `find . -name '*_*'`; do
        echo "${fn##*_} $fn"
    done | sort -n | tail -n 1 | cut -d' ' -f 2-
    

    Then you’ll need to extract the part of the filename before the underscore. This is probably best done by storing the result of the last part in a variable,

    largest_filename="$(for fn in `find . -name '*_*'`; do
        echo "${fn##*_} $fn"
    done | sort -n | tail -n 1 | cut -d' ' -f 2-)"
    

    after which you can use bash’s suffix removal to strip off the part after the underscore, and then list all files that share that prefix.

    largest_filename="$(for fn in `find . -name '*_*'`; do
        echo "${fn##*_} $fn"
    done | sort -n | tail -n 1 | cut -d' ' -f 2-)"
    ls ${largest_filename%_*}_*
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple program that will compare the files in separate
I am trying to write a program that will, when executed, will go through
I'm trying to write a program that will loop through cells of a specific
I'm trying to write a program that will delete a set of files/folders that
Im trying to write a program in C++ that will take 2 files and
I have to write a program that will go through a given folder and
I am trying to write a shell program that will search my current directory
I am trying to write a program that will prompt you to enter someone's
Hey there. I'm trying to write a small program that will read the four
I'm trying to write a procedure for a baseball program that will use multiple

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.