share
Unix & LinuxCreate file list in ascending order based on filename
[0] [1] RLe
[2020-04-15 15:59:49]
[ ls sort ]
[ https://unix.stackexchange.com/questions/580256/create-file-list-in-ascending-order-based-on-filename ]

I need to create a file list which display the file name in ascending order. My file name has file pattern: FILE.YYYYMMDD.XXX

For example:

$ ls -ltr
-rwxr-xr-x. 1 honle 1036 Apr 14  2020 FILE.20200102.001
-rwxr-xr-x. 1 honle 426832 Apr 14  2020 FILE.20200102.002
-rwxr-xr-x. 1 honle 426832 Apr 14  2020 FILE.20200102.003
-rwxr-xr-x. 1 honle 426832 Apr 10  2020 FILE.20200110.001
-rw-r--r--. 1 honle 426832 Apr 14 22:50 FILE.20200220.001
-rwxr-xr-x. 1 honle 1036 Apr 15 16:30 FILE.20200110.001

Noted for input why FILE.20200120.002 before FILE.20200220.001. FILE.20200120.002 was processed and coming to current dir before FILE.20200220.001. That is why i need to sort it based on the YYYYMMDD.XXX filename pattern.

Desired output:

FILE.20200102.001
FILE.20200102.002
FILE.20200102.003
FILE.20200110.001
FILE.20200120.002
FILE.20200220.001

The time file was created is not in order so I can't use ls -ltr. Any idea how I can sort them? I am using Linux.

Thanks

Post the desired output. - Arkadiusz Drabczyk
(1) Shouldn't FILE.20200120.002 be before FILE.20200220.001? - terdon
Why is FILE.20200120.002 after FILE.20200220.001? - Arkadiusz Drabczyk
Hi, the file FILE.20200120.002 was done process and sent to dest dir before FILE.20200120.001. That was why it was before FILE.20200220.001 when i do ls - RLe
@RLe Only if you used ls -t to ask ls to sort by "last modified" timestamp. It is unclear what order you want the files in. Sorted on filename, or sorted by modification time. - Kusalananda
@Kusalananda sort by filename pattern time - RLe
@RLe In that case, your expected output is wrong. Also, what is .HH. that you have mentioned a couple of times but that doesn't seem to be part of the actual filenames? - Kusalananda
Hi I edit the input - RLe
[+1] [2020-04-15 16:14:35] Kusalananda

If the issue is to get the filenames in a single column rather than in the multi-column format that ls produces, then you may use the -1 ("minus one") option to ls:

ls -1

The filenames would be sorted by name.


printf '%s\n' FILE.*

Assuming that the pattern FILE.* matches all relevant filenames, and nothing else, the above command would also print the filenames in lexicographical order, each on a line by itself, which seems to be what you want.


Is there any benefit in using printf instead of a simple ls -l here? - terdon
@terdon ls -1, that would give a single column output. After re-reading, I'm a bit confused as to what the issue is. Possibly they are just over-specifying options... -t would sort on mtime, but just leave that option off then... - Kusalananda
Yes, I think the OP most likely isn't aware of -1, but that's why I'm waiting to see if they answer my comment about the ordering. Assuming the desired output is wrong, they just need ls -1, methinks. - terdon
1