If your file names don't have any newlines in them, you might be able to do it with just telling the shell to split the output of ls
only on newlines, not any whitespace as the default. The splitting is controlled by the IFS
variable, any characters contained in IFS
are used as delimiters.
IFS=$'\n' # set it to just a newline
convert $(ls -vd ./*.png) output.pdf
This may still have problems if the file names are funny enough, or if ls
mangles them for display. When printing to a terminal, ls
usually lists the files in multiple columns. But when the output doesn't go to a terminal (the shell reads it, here), it acts as if -1
was given.
To work with the idea you started with, you could use eval
.
eval: eval [arg ...]
Combine ARGs into a single string, use the result as input to the shell, and execute the resulting commands.
But the problem with eval
is that anything and everything in the command line gets parsed again, and even stuff that's usually safe, isn't. Think of a file called $(touch HELLO)
and what happens if a name like that is dropped on the command line.
Also, if you go this way, you may want to use --quoting-style=shell
instead of -Q
as it may match the shell's processing of the special characters more closely. (Both may be specific to GNU ls, but I think -v
is, too.)
ls
... – jasonwryan Sep 22 '16 at 22:14find . -maxdepth 1 -name '*.png' -print0 | sort -zV | xargs -0
will give the desired ordering? – steeldriver Sep 22 '16 at 22:23find . -maxdepth 1 -name '*png' -print0 | sort -zV | xargs -0 -I files -t convert files out.pdf
but that calls convert once for each filename. – user313032 Sep 22 '16 at 22:53eval convert $(ls -Qv *png) out.pdf
. I know that I could also rename the files, but the format with spaces and parentheses is how they are exported by an ipad app, and I just want to transform them into something useful with minimal effort. So ilkkachu if you post your answer again, or whoever deleted it undeletes it, I will accept it. – user313032 Sep 22 '16 at 23:09-I
. BSD find has-J
which is similar, but doesn't have that limitation. – ilkkachu Sep 22 '16 at 23:09eval
can work here, but it's... a bit like pointing around with a gun. I deleted that answer to work on a less dangerous solution. – ilkkachu Sep 22 '16 at 23:12ls
sorting. Unless you have directories involved, but yourpng
extension seems to indicate that you don't. I question your conclusions and request that you show us the "wrong order" you are getting. – Wildcard Sep 23 '16 at 00:37-v
option ofls
; silly me. There are other ways to get the files sorted the way you want, though. I would not useeval
for this use case no matter what. – Wildcard Sep 23 '16 at 00:42