Score:2

Sort files according to the name containing numeric values

ma flag

I have files in my system like this -

-rw-r--r-- 1 abc     abcadm  14852497 Jul 23 01:11 ABCD_72_3_20210722_163502.csv
-rw-r--r-- 1 abc     abcadm  14853145 Jul 23 01:11 ABCD_72_1_20210722_163502.csv
-rw-r--r-- 1 abc     abcadm  14839699 Jul 23 01:11 ABCD_72_2_20210722_163502.csv
-rw-r--r-- 1 abc     abcadm  14842673 Jul 23 01:11 ABCD_72_5_20210722_163502.csv
-rw-r--r-- 1 abc     abcadm  14843811 Jul 23 01:11 ABCD_72_4_20210722_163502.csv

I want to sort those files to print only file name and with sorted manner like below:

ABCD_72_1_20210722_163502.csv
ABCD_72_2_20210722_163502.csv
ABCD_72_3_20210722_163502.csv
ABCD_72_4_20210722_163502.csv
ABCD_72_5_20210722_163502.csv

I am using the below to command to sort it in ascending order and to print just name but the list is not getting sorted.

ls -l | ABCD_72_[0-9]*_20210722_163502* | awk '{print $9}' | sort

Please suggest me where I am wrong or any alternative of this?

Score:2
cn flag

First, don't parse the output of ls. We can modify the answer to a different question a bit to make it non-recursive, and then find ./ -maxdepth 1 -printf "%f\n", and you want it sorted, so pipe it into sort. The final command is

$ find ./  -maxdepth 1 -printf "%f\n" | sort

As pointed out by @Rinzwind, if you want to numerically sort (so 2 could come before 12), add --numeric-sort to the end of the command making it.

find ./  -maxdepth 1 -printf "%f\n" | sort --numeric-sort
bac0n avatar
cn flag
This breaks on *newline*, could do `-printf %f\\0 | sort -zn | xargs -0 -n 1`
cocomac avatar
cn flag
@bac0n Then either edit my answer, or post your own. I didn’t consider newlines, because why would someone have a beeline in a file name?!?
Score:0
hr flag

If you switch to zsh, you can use its numeric glob qualifier:

   n      sets the NUMERIC_GLOB_SORT option for the current pattern

(see Glob Qualifiers under the FILENAME GENERATION section of man zshexpn).

Ex.

 % print -rC1 ABCD_*.csv(n)
ABCD_72_1_20210722_163502.csv
ABCD_72_2_20210722_163502.csv
ABCD_72_3_20210722_163502.csv
ABCD_72_4_20210722_163502.csv
ABCD_72_5_20210722_163502.csv

or (in reverse numeric order)

 % print -rC1 ABCD_*.csv(nOn)
ABCD_72_5_20210722_163502.csv
ABCD_72_4_20210722_163502.csv
ABCD_72_3_20210722_163502.csv
ABCD_72_2_20210722_163502.csv
ABCD_72_1_20210722_163502.csv
bac0n avatar
cn flag
Actually really fast.
Score:0
cn flag

Have you considered simply

ls -v -1

This can also be employed naturally with a file specification such as:

ls -v -1 ABCD*

or

ls -v -1 A*.csv
Score:-1
cn flag

Modifying your own command this should do the job:

ls -l | awk '{print $NF}'  | sort -nk3
cocomac avatar
cn flag
This fails to handle directories that have spaces. E.g., if I create a directory called `test dir` (with `mkdir test\ dir`), and run your command, it will print `test`, but not `test dir`. Example directory line that fails `drwxr-xr-x 2 ubuntu ubuntu 4096 Oct 7 09:53 'test dir'`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.