# List numerically in ascending order

**URL:** <https://community.unix.com/t/list-numerically-in-ascending-order/377074>\
**Category:** Shell Programming and Scripting\
**Created:** [January 21, 2020, 12:15pm UTC](https://community.unix.com/t/list-numerically-in-ascending-order/377074 "2020-01-21T12:15:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![baris35](https://community.unix.com/letter_avatar/baris35/32/6_449c533f586939b6e5f246632a46421f.png) [@baris35](https://community.unix.com/u/baris35)\
**Post date:** [January 21, 2020, 12:15pm UTC](https://community.unix.com/t/list-numerically-in-ascending-order/377074/1 "2020-01-21T12:15:19Z")

</div>

Hello,  
I am running ubuntu 16.04 and trying to list all files inside a directory, I need to sort them in ascending order. While surfing on the site, I found an old thread but somehow it did not work.  
[Link](https://www.unix.com/linux/122581-using-sort-command-get-numeric-ascending-order.html)

Ascending order with `sort -nk2 myfile.txt` command gives below output:

```plaintext
file media_w1956306127_b2500000_0.ts
file media_w1956306127_b2500000_100.ts
file media_w1956306127_b2500000_101.ts
file media_w1956306127_b2500000_102.ts
..
..
file media_w1956306127_b2500000_10.ts
file media_w1956306127_b2500000_110.ts
file media_w1956306127_b2500000_111.ts
..
..
file media_w1956306127_b2500000_699.ts

```

Other method is a bit longer to do that. ( `Rename` prefix media\_w1956306127\_b2500000\_ in each `filename` , then, print the file names with `for` loop, variable from 0 to 699)

Could you please let me know how I may sort it as expected? Maybe `sort` command takes all `strings` as alphabet so it does not take into account the value of the `string` so that listing in `ascending` order does not give what I expected to get.

Thank you  
Boris

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [January 21, 2020, 12:26pm UTC](https://community.unix.com/t/list-numerically-in-ascending-order/377074/2 "2020-01-21T12:26:02Z")

</div>

Hi, try

```nohighlight
sort -t_ -k1,3 -k4n

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [January 21, 2020, 12:27pm UTC](https://community.unix.com/t/list-numerically-in-ascending-order/377074/3 "2020-01-21T12:27:40Z")

</div>

Not sure I correctly understand your request. Are we talking of sorting a file, or a directory listing? Did you check for the links at the bottom of this page? Given "media\_w1956306127\_b2500000" is and stays constant in your data, how far would

```nohighlight
sort -t"_" -nk4 file
..
..
..
..
file media_w1956306127_b2500000_0.ts
file media_w1956306127_b2500000_10.ts
file media_w1956306127_b2500000_100.ts
file media_w1956306127_b2500000_101.ts
file media_w1956306127_b2500000_102.ts
file media_w1956306127_b2500000_110.ts
file media_w1956306127_b2500000_111.ts
file media_w1956306127_b2500000_699.ts

```

get you?

---

<div class="post-metadata">

**Author:** ![baris35](https://community.unix.com/letter_avatar/baris35/32/6_449c533f586939b6e5f246632a46421f.png) [@baris35](https://community.unix.com/u/baris35)\
**Post date:** [January 21, 2020, 12:40pm UTC](https://community.unix.com/t/list-numerically-in-ascending-order/377074/4 "2020-01-21T12:40:20Z")

</div>

Thank you Dear Scrutinizer and Dear Rudic,  
Your commands work good. Thank you again.  
The text file includes all files inside the directory.  
I do not understand why we need to specify underscore as limiter in column2. Normally each line is space seperated.  
Thank you so much for your return.

Kind regards  
Boris

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [January 21, 2020, 12:51pm UTC](https://community.unix.com/t/list-numerically-in-ascending-order/377074/5 "2020-01-21T12:51:57Z")

</div>

> [@baris35](#):
>
> ... why we need to specify underscore as limiter in column2 ...

That how we "isolate" the relevant numerical (!) `sort` field from the rest of fields that are considered strings. We're lucky here in that `sort` seems to disregard the `.ts` extension when extracting / calculating the numerical field.

---

<div class="post-metadata">

**Author:** ![nezabudka](https://community.unix.com/letter_avatar/nezabudka/32/6_449c533f586939b6e5f246632a46421f.png) [@nezabudka](https://community.unix.com/u/nezabudka)\
**Post date:** [January 21, 2020, 1:49pm UTC](https://community.unix.com/t/list-numerically-in-ascending-order/377074/6 "2020-01-21T13:49:17Z")

</div>

Hi  
You can use the -V option

```nohighlight
sort -V

```

or even sort right away in a file

```nohighlight
vim -nes +'%!sort -V' +wq file

```

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [July 25, 2020, 5:49pm UTC](https://community.unix.com/t/list-numerically-in-ascending-order/377074/7 "2020-07-25T17:49:54Z")

</div>

Automatically bumped on July 25, 2020, 5:49pm UTC



---

<div class="post-metadata">

**Author:** ![Mannu25251](https://community.unix.com/user_avatar/community.unix.com/mannu25251/32/14185_2.png) [@Mannu25251](https://community.unix.com/u/Mannu25251)\
**Post date:** [July 25, 2020, 6:26pm UTC](https://community.unix.com/t/list-numerically-in-ascending-order/377074/8 "2020-07-25T18:26:08Z")

</div>

Another option would be at the time of listing

> ls -1v \*media\*

```plaintext
mannu25251@debian8.9:~/dir$ ls -1v *media\*
    media_w1956306127_b2500000_0.ts
    media_w1956306127_b2500000_10.ts
    media_w1956306127_b2500000_100.ts
    media_w1956306127_b2500000_101.ts
    media_w1956306127_b2500000_102.ts
    media_w1956306127_b2500000_109.ts
    media_w1956306127_b2500000_110.ts
    media_w1956306127_b2500000_111.ts
    media_w1956306127_b2500000_699.t
    media_w1956306127_b2500000_699.ts
    media_w1956306127_b2500000_1112.ts

```
