I have a folder named baypass which contains 2181 files, looks like this (just a few files as an example):


I want to have all of them sorted, so I used

ls -v -1

which did the sorting job for me, but I want to have these sorted files saved in a directory. So when I type "ls -v -1" files become sorted but if I type pwd or ls or any other command they will be unsorted again. So how can I save the sorted files? I want to have them sorted and saved to be able to merge them. The order of files are very important.

  • it sounds like you would be helped by renaming the files such that "anacovis2_210" became "anacovis2_0210", at which point it would sort "naturally" with ls. Are you open to that idea?
    – Jeff Schaller
    Sep 11, 2017 at 18:49

3 Answers 3


This is the command you need finally if your goal is merging these files in numerical order. Use shell brace expansion.

cat anacovis2_{1..2181}_summary_betai_reg.out_new >> one_file
  • 1
    Yes, thank you Afshin. I got my desired output.
    – Anna1364
    Sep 11, 2017 at 18:53

In zsh, you'd do:


(n) being a glob qualifier that causes the glob to be sorted numerically (can also be toggled globally for all globs with set -o numericglobsort).

And then:

do-something-with $files

In bash, ksh93 or mksh (or zsh) you can store the files as listed by that GNU ls in an array with:

eval "list=($(ls -v --quoting-style=shell-always))"
do-something-with "${files[@]}"

You can use an alias:

alias ls='ls -1v'
  • Thanks @abitmol, well yes I get the them sorted and saved but when I run cat command "cat *>covariable1" it merges files based on unsorted order.
    – Anna1364
    Sep 11, 2017 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .