1

I have files like :

4-some file.mp4
1-another file.mp4
3-one more file.mp4
2-got another file.mp4

and so on.

Using the command line to play the files in vlc using vlc * plays them in unsorted order.

So I tried to play the files using vlc "vlc < <(ls * | sort -V)" does not work.

Trying to change the timestamp of the files using for i in "$(ls [!R]* | sort -V)"; do touch "$i";sleep 1; done is not working because "$(ls [!R]* | sort -V)" represents the complete list of files as one argument but I cannot remove the double quotes since files have spaces in their names.

4
  • is that the actual name of the file 4 - some file.mp4 Like with space in it?
    – 123
    Commented Jul 4, 2016 at 8:56
  • yes the actual names of the files is like 235-Learning Objectives.mp4 , 232-Exercise 24.mp4 i have named them like this
    – munish
    Commented Jul 4, 2016 at 8:58
  • The number of digits before the - differs?
    – phk
    Commented Jul 4, 2016 at 9:03
  • yes it goes from 1 to 420
    – munish
    Commented Jul 4, 2016 at 9:10

2 Answers 2

6

This should work

find . -name "*mp4" -print0 | sort -Vz | xargs -0 vlc
5
  • Why not -n for sort? This should also fix the problems with a differing amounts of digits.
    – phk
    Commented Jul 4, 2016 at 9:08
  • @phk Try it with n.
    – 123
    Commented Jul 4, 2016 at 9:09
  • @123 It works for me.
    – phk
    Commented Jul 4, 2016 at 9:16
  • 4
    @phk no, the -n won't work. The -V seems to be doing something clever. To use -n, you would need to define the field to sort on. For example: find . -name '*mp4' -printf '%f\0' | sort -t'-' -k 1.1n,1.3 -z. If it works for you, try it again with a file name like 100-foo.mp4. Or even 11-foo.
    – terdon
    Commented Jul 4, 2016 at 9:18
  • 1
    I think the path name (./) in the beginning if find output is what confuses sort -n. It seems to work if you tell it to skip the first letters: find . -name "*.mp4" -print0 | sort -z -k 1.3n | xargs -0 echo. Also plain simple ls -U | sort -n works.
    – ilkkachu
    Commented Jul 4, 2016 at 11:27
1

GNU ls also has -v natural sort of (version) numbers within text, like sort -V.

The spaces in the filename can be dealt with by removing the space from $IFS, (assuming you don't have newlines in file names):

$ IFS=$'\n' 
$ for x in $(ls -v) ; do echo "> $x" ; done
> 1-aa aa
> 2-cc cc
> 10-bb bb
> 21-dd dd
$ vlc $(ls -v)

(If you do that interactively, IFS will remain modified afterwards.)

For more information about IFS, see David Wheeler's writings about special characters in file names

You must log in to answer this question.

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