List files sorted numerically

  • I have a bunch of files from log1 to log164.

    I'm trying to LIST the directory (sorted) in a UNIX terminal but the sort functions are only providing the format like this:

    home:logs Home$ ls -1 | sort
    log1.gz
    log10.gz
    log100.gz
    log101.gz
    log102.gz
    log103.gz
    log104.gz
    log105.gz
    log106.gz
    ...etc
    

    What I want is

    home:logs Home$ ls -1 | sort
    log1.gz
    log2.gz
    log3.gz
    log4.gz
    log5.gz
    log6.gz
    log7.gz
    ...{more here}
    log99.gz
    log100.gz
    log101.gz
    log102.gz
    ...etc
    

    Any suggestions in what I could use to do this?

    This is absolutely a programming question, and does not deserve to be migrated just because the answer involves the lowly shell programming language!!!

    If you know in advance that they're named `log1.gz` through `log164.gz`, then what do you even need to `ls -1` them for?

    @ruakh ls -1 displays the results in one column rather then across

    @Rabiani: I know what `ls -1` does: it lists filenames. Since you already knew the filenames, I didn't understand what you needed it for. But since you've accepted Kevin's answer, I now know: you *didn't* need it. Which makes more sense. :-)

  • Kevin

    Kevin Correct answer

    9 years ago

    bash's braces, {}, will enumerate them in order:

    for file in log{1..164}.gz; do
        process "$file"
    done
    

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM