0

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

8
  • Post the desired output. Apr 15, 2020 at 16:01
  • 1
    Shouldn't FILE.20200120.002 be before FILE.20200220.001?
    – terdon
    Apr 15, 2020 at 16:13
  • Why is FILE.20200120.002 after FILE.20200220.001? Apr 15, 2020 at 16:13
  • 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
    Apr 15, 2020 at 16:25
  • @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
    Apr 15, 2020 at 16:26

1 Answer 1

1

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.

3
  • Is there any benefit in using printf instead of a simple ls -l here?
    – terdon
    Apr 15, 2020 at 16:19
  • @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
    Apr 15, 2020 at 16:21
  • 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
    Apr 15, 2020 at 16:22

You must log in to answer this question.

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