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 8681547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:32:23+00:00 2026-06-12T21:32:23+00:00

I’m looking to make a bash alias that will change the results of ls.

  • 0

I’m looking to make a bash alias that will change the results of ls. I am constantly dealing with large sequences of files, that do not follow the same naming conventions. The only common thing about them is that the number is 4 padded (sorry not really sure of correct way to say that) and immediately precedes the extension.

eg – filename_v028_0392.bgeo, test_x34.prerun.0012.simdata, filename_v001_0233.exr

I would like for the sequences to be listed each as 1 element, so that

filename_v003_0001.geo
filename_v003_0002.geo
filename_v003_0003.geo
filename_v003_0004.geo
filename_v003_0005.geo
filename_v003_0006.geo
filename_v003_0007.geo
filename_v003_0032.geo
filename_v003_0033.geo
filename_v003_0034.geo
filename_v003_0035.geo
filename_v003_0036.geo
testxxtest.0057.exr
testxxtest.0058.exr
testxxtest.0059.exr
testxxtest.0060.exr
testxxtest.0061.exr
testxxtest.0062.exr
testxxtest.0063.exr

would be displayed as somethign along the lines of

[seq]filename_v003_####.geo (1-7)
[seq]filename_v003_####.geo (32-36)
[seq]testxxtest.####.exr (57-63)

while still listing non sequences unaltered.

I’m really not sure where to start approaching this. I know a decent amount of python, but not sure if that would really be the best way to go about it. Any help would be greatly appreciated!

Thanks

  • 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-12T21:32:24+00:00Added an answer on June 12, 2026 at 9:32 pm

    I got a python 2.7 script that solves your problem by solving the more general problem of collapsing several lines changing only by a sequence number

    import re
    
    def do_compress(old_ints, ints):
        """
        whether the ints of the current entry is the continuation of the previous
        entry
        returns a list of the indexes to compress, or [] or False when the current
        line is not part of an indexed sequence
        """
        return len(old_ints) == len(ints) and \
            [i for o, n, i in zip(old_ints, ints, xrange(len(ints))) if n - o == 1]
    
    def basic_format(file_start, file_stop):
        return "[seq]{} .. {}".format(file_start, file_stop)
    
    
    def compress(files, do_compress=do_compress, seq_format=basic_format):
        p = None
        old_ints = ()
        old_indexes = ()
    
        seq_and_files_list = [] 
            # list of file names or dictionaries that represent sequences:
            #   {start, stop, start_f, stop_f}
    
        for f in files:
            ints = ()
            indexes = ()
    
            m = p is not None and p.match(f) # False, None, or a valid match
            if m:
                ints = [int(x) for x in m.groups()]
                indexes = do_compress(old_ints, ints)
    
            # state variations
            if not indexes: # end of sequence or no current sequence
                p = re.compile( \
                    '(\d+)'.join(re.escape(x) for x in re.split('\d+',f)) + '$')
                m = p.match(f)
                old_ints = [int(x) for x in m.groups()]
                old_indexes = ()
                seq_and_files_list.append(f)
    
            elif indexes == old_indexes: # the sequence continues
                seq_and_files_list[-1]['stop'] = old_ints = ints
                seq_and_files_list[-1]['stop_f'] = f
                old_indexes = indexes
    
            elif old_indexes == (): # sequence started on previous filename
                start_f = seq_and_files_list.pop()
                s = {'start': old_ints, 'stop': ints, \
                    'start_f': start_f, 'stop_f': f}
                seq_and_files_list.append(s)
    
                old_ints = ints
                old_indexes = indexes
    
            else: # end of sequence, but still matches previous pattern
                old_ints = ints
                old_indexes = ()
                seq_and_files_list.append(f)
    
        return [ isinstance(f, dict) and seq_format(f['start_f'], f['stop_f']) or f 
            for f in seq_and_files_list ]
    
    
    if __name__ == "__main__":
        import sys
        if len(sys.argv) == 1:
            import os
            lst = sorted(os.listdir('.'))
        elif sys.argv[1] in ("-h", "--help"):
            print """USAGE: {} [FILE ...]
    compress the listing of the current directory, or the content of the files by
    collapsing identical lines, except for a sequence number
    """
            sys.exit(0)
        else:
            import string
            lst = [string.rstrip(l, '\r\n') for f in sys.argv[1:] for l in open(f)])
        for x in compress(lst):
            print x
    

    That is, on your data:

    bernard $ ./ls_sequence_compression.py given_data
    [seq]filename_v003_0001.geo .. filename_v003_0007.geo
    [seq]filename_v003_0032.geo .. filename_v003_0036.geo
    [seq]testxxtest.0057.exr .. testxxtest.0063.exr
    

    It bases itself on the differences between the integers present in two consecutive lines that match on the non-digit text. This allows to deal with non-uniform input, on changes of the field used as basis for the sequence…

    Here is an example of input:

    01 - test8.txt
    01 - test9.txt
    01 - test10.txt
    02 - test11.txt
    02 - test12.txt
    03 - test13.txt
    04 - test13.txt
    05 - test13.txt
    06
    07
    08
    09
    10
    

    which gives:

    [seq]01 - test8.txt .. 01 - test10.txt
    [seq]02 - test11.txt .. 02 - test12.txt
    [seq]03 - test13.txt .. 05 - test13.txt
    [seq]06 .. 10
    

    Any comment is welcome!

    Hah… I nearby forgot: without arguments, this script outputs the collapsed contents of the current directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.