0

I'm having issues with programs reading files in the wrong order, and also outputting in an undesirable order with utilities such as ls.

I've tried some of the LC_COLLATE options, but none of them fit the preference I seek, so I figured there must be something more I can do.

Examples of commands producing wrong order:

user@host: /home/user/Video $ mpv *.mkv

There are 150 files in /home/user/Video, and here are the first 12 a command from a program like mpv *.mkv would read:

TVSeriesName - 01.mkv
TVSeriesName - 02.mkv
TVSeriesName - 03.mkv
TVSeriesName - 04.mkv
TVSeriesName - 05.mkv
TVSeriesName - 06.mkv
TVSeriesName - 07.mkv
TVSeriesName - 08.mkv
TVSeriesName - 09.mkv
TVSeriesName - 10.mkv
TVSeriesName - 100.mkv
TVSeriesName - 101.mkv

It's reading 100 before it reads 11, even with leading zero.

Ideally, it should read them in the correct 1-150 order even without a leading zero.

What I want is to have a universal order in which files are read and sorted that resembles the default of the ranger file manager.

Example:

.1-hiddendir/
.2-hiddendir/
.a-hiddendir/
.b-hiddendir/
.C-hiddendir/
.d-hiddendir/
.E-hiddendir/
1-dir/
2-dir/
A-dir/
b-dir/
c-dir/
D-dir/
.1-dotfile
.2-dotfile
.a-dotfile
.b-dotfile
.C-dotfile
1-file
2-file
a-file
B-file
c-file

OS: Arch Linux, FS: ext4

What can I do with environment settings, or anything, in a Linux distribution to achieve this?

  • 2
    Surely you want two leading zeroes? – Michael Homer Mar 27 '16 at 2:05
  • you need to rename your files so that they ALL have the SAME number of digits in the numeric portion of the filename, left-padded with zeroes. e.g. if you have more than 99 files, you'll need three digits: 001, 011, 100, etc. if more than 999, then 4 digits: 0001, 0011, 0100, etc. – cas Mar 27 '16 at 2:10
  • 1
    I don't want two leading zeroes. I'm looking for consistency, and I'm asking this question because I believe there must be some way for the shell or filesystem to determine order other than by having matching number of digits for all files in a directory. – nixon Mar 27 '16 at 2:22
  • I don't see why it seemed surprising that one didn't work, is all. I don't think the locale can have that sort of collation behaviour, but I would be interested to find out. – Michael Homer Mar 27 '16 at 3:00
  • 2
    "The order in which files are read by programs" is the order you told them to, not anything to do with the program reading them. I suspect you'd have to write your own locale to get general sorting to behave that way, and I'm not sure it's even possible to do it then. A more focused question on that topic might be more likely to be seen by someone who knows about doing that. – Michael Homer Mar 27 '16 at 6:22
1
find . -maxdepth 1 -type f  -name '*.mkv' -print0 | sort -Vz | xargs -0r mpv

This uses find to output a NUL-separated of list all filenames matching '*.mkv' in the current directory, then GNU sort (with -z or --zero-terminated for NUL-separated input and -V or --version-sort to sort the filenames), and finally, xargs -0r mpv to run mpv with all the filenames in sorted order as the arguments.

This will work with any filename, even those with spaces, line-feeds, or shell meta-characters in the name.

AFAIK, only GNU sort and FreeBSD's sort currently support the -z or --zero-terminated option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.