Find the answer to your Linux question:
Results 1 to 4 of 4
Hi guys, Sorry if this is a bit of a novice question but I need file names in the form obj10**.fits to list before obj9**.fits, where ** are just numbers. ...
  1. #1
    Just Joined!
    Join Date
    Jan 2012
    Posts
    1

    Getting thousands to list before hundreds

    Hi guys,

    Sorry if this is a bit of a novice question but I need file names in the form obj10**.fits to list before obj9**.fits, where ** are just numbers.

    At the moment, obj1000 lists before obj999 due to the 1. Is there any quick way to get the files with thousands to list after files with hundreds?

    I thought about adding a zero to the front of each hundred, ie to get obj0999, but I dont know a quick way I could do that to work on hundreds of files.

    Thanks for any help!

  2. #2
    Linux User sgosnell's Avatar
    Join Date
    Oct 2010
    Location
    Baja Oklahoma
    Posts
    358
    Adding leading zeros is the quickest way I know of. Batch (or bulk) renaming of files isn't that hard, and Google should help with that. Lots of how-tos show up in a Google search. You'll need a script to test for the first number, and add a zero if it's not a 1, nothing if it is. That's assuming the numbers are all above 100. If they start from 1, it gets more complicated, but still possible.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    It is as sgosnell said - you need to add leading zeros to the number since file names are sorted alphabetically, and naturally '9' is after '1'...
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    This may not always work, but will handle simple cases like this:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate ls sort by "version".
    
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C ls
    
    # Create data files.
    touch data999 data1000
    
    pl " Results, default order:"
    ls -lgG data*
    
    pl " Results, sort by \"version\":"
    ls -v -lgG data*
    
    exit 0
    producing:
    Code:
    $ ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.32-71.29.1.el6.i686, i686
    Distribution        : CentOS Linux release 6.0 (Final)
    GNU bash 4.1.2
    ls (GNU coreutils) 8.4
    
    -----
     Results, default order:
    -rw-r--r--. 1 0 Jan  7 05:38 data1000
    -rw-r--r--. 1 0 Jan  7 05:38 data999
    
    -----
     Results, sort by "version":
    -rw-r--r--. 1 0 Jan  7 05:38 data999
    -rw-r--r--. 1 0 Jan  7 05:38 data1000
    See info ls for details ... cheers, drl

    (Recently posted on LQ, I seem to recall.)

    ( edit 1: post results as run on CentOS rather than Debian )
    Last edited by drl; 01-07-2012 at 10:43 AM.
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...