I want to pass all the files as a single argument on Linux but I am not able to do that.
This is working
ls | sort -n | xargs -i pdftk {} cat output combinewd2.pdf
This passes a single argument per command, but I want all in one command.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is one way to do it
or using backtick
For example, if the filenames are 100, 2, 9, 3.14, 10, 1 the command will be
To handle filenames with spaces or other special characters consider this fixed version of @joeytwiddle’s excellent answer (which does not sort numerically, see discussion below):
Alternatives to xargs (bash specific)
xargsis an external command, in the previous example it invokesshwhich in turn invokespdftk.An alternative is to use the builtin
mapfileif available, or use the positional parameters. The following examples use two functions, print0_files generates the NUL terminated filenames and create_pdf invokespdftk:The functions are defined as follows
Discussion
As pointed out in the comments the simple initial answer does not work with filenames containing spaces or other special characters.
The answer by @joeytwiddle does handle special characters, although it does not sort numerically
It does not sort numerically due to each filename being prefixed by
./by thefindcommand. Some versions of thefindcommand support-printf '%P\0'which would not include the./prefix. A simpler, portable fix is to add the-d, --dictionary-orderoption to thesortcommand so that it considers only blank spaces and alphanumeric characters in comparisons, but might still produce the wrong orderingIf filenames contain decimals this could lead to incorrect numeric sorting. The
sortcommand does allow an offset into a field when sorting,sort -k1.3n, one must be careful though in defining the field separator if filenames are to be as general as possible, fortunatelysort -t '\0'specifies NUL as the field separator, and thefind -print0option indicates NUL is to be used as the delimiter between filenames, sosort -z -t '\0'specifies NUL as both the record delimiter and field separator– each filename is then a single field record. Given that, we can then offset into the single field and skip the./prefix by specifying the 3rd character of the 1st field as the starting position for the numeric sort,sort -k1.3n -z -t '\0'.