Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:49:07+00:00 2026-06-18T03:49:07+00:00

I want to pass all the files as a single argument on Linux but

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:49:08+00:00Added an answer on June 18, 2026 at 3:49 am

    This is one way to do it

    pdftk $(ls | sort -n) cat output combinewd2.pdf
    

    or using backtick

    pdftk `ls | sort -n` cat output combinewd2.pdf
    

    For example, if the filenames are 100, 2, 9, 3.14, 10, 1 the command will be

    pdftk 1 2 3.14 9 10 100 cat output combinewd2.pdf
    

    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):

    #-- The following will handle special characters, and
    #   will sort filenames numerically
    #   e.g. filenames 100, 2, 9, 3.14, 10, 1 results in 
    #      ./1 ./2 ./3.14 ./9 ./10 ./100
    #
    find . -maxdepth 1 -type f -print0 |
      sort -k1.3n -z -t '\0' |
      xargs -0 sh -c 'pdftk "$@" cat output combinewd2.pdf' "$0"
    

    Alternatives to xargs (bash specific)

    xargs is an external command, in the previous example it invokes sh which in turn invokes pdftk.

    An alternative is to use the builtin mapfile if available, or use the positional parameters. The following examples use two functions, print0_files generates the NUL terminated filenames and create_pdf invokes pdftk:

    print0_files | create_pdf combinewd2.pdf
    

    The functions are defined as follows

    #-- Generate the NUL terminated filenames, numerically sorted
    print0_files() {
        find . -maxdepth 1 -type f -print0 |
            sort -k1.3n -z -t '\0'
    }
    
    #-- Read NUL terminated filenames using mapfile
    create_pdf() {
        mapfile -d ''
        pdftk "${MAPFILE[@]}" cat output "$1"
    }
    
    #-- Alternative using positional parameters
    create_pdf() {
        local -r pdf=$1
        set --
        while IFS= read -r -d '' f; do set -- "$@" "$f"; done
        pdftk "$@" cat output "$pdf"
    }
    

    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

    #-- The following will not sort numerically due to ./ prefix,
    #   e.g. filenames 100, 2, 9, 3.14, 10, 1 results in 
    #      ./1 ./10 ./100 ./2 ./3.14 ./9
    #
    find . -maxdepth 1 -type f -print0 |
      sort -zn |
      xargs -0 sh -c 'pdftk "$@" cat output combinewd2.pdf' "$0"
    

    It does not sort numerically due to each filename being prefixed by ./ by the find command. Some versions of the find command support -printf '%P\0' which would not include the ./ prefix. A simpler, portable fix is to add the -d, --dictionary-order option to the sort command so that it considers only blank spaces and alphanumeric characters in comparisons, but might still produce the wrong ordering

    #-- The following will not sort numerically due to decimals
    #   e.g. filenames 100, 2, 9, 3.14, 10, 1 results in 
    #      ./1 ./2 ./9 ./10 ./100 ./3.14
    #
    find . -maxdepth 1 -type f -print0 |
      sort -dzn |
      xargs -0 sh -c 'pdftk "$@" cat output combinewd2.pdf' "$0"
    

    If filenames contain decimals this could lead to incorrect numeric sorting. The sort command 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, fortunately sort -t '\0' specifies NUL as the field separator, and the find -print0 option indicates NUL is to be used as the delimiter between filenames, so sort -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'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a bash script that (recursively) processes all files of a
I want to do something like this: c:\data\> python myscript.py *.csv and pass all
I have a NameValueCollection with parameter. I want to pass all parameter in SqlCommand
I want to pass through configuration arguments to a class. These are all the
All i want is to pass a HTML (DOM object) from javascript to Actionscript.
I want to pass an ImageIcon instance by value as we know that by
I want to pass a string that contains few words to jQuery when click
I want to pass an uninitialized object pointer to some method. Within that method
I want to read files in an advanced mode. First: In this file, I
With This code I upload selected Files . That works without problem and at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.