How can I pass all arguments with xargs in middle of command in linux

前端 未结 8 2045
一个人的身影
一个人的身影 2020-12-09 08:37

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 outpu         


        
相关标签:
8条回答
  • 2020-12-09 09:06

    The most intuitive way I found was to:

    • first construct the commands with -I{} and "echo",
    • then execute the commands with "bash" (as if you are executing a shell script)

    Here is an example to rename extensions from ".txt" to ".txt.json":

    find .|grep txt$|xargs -I{} echo "mv {} {}.json"|bash
    

    Slightly advanced example to rename .txt to .json (removing .txt extension)

    find $PWD|grep txt$|cut -d"." -f1|xargs -I{} echo "mv {}.txt {}.json"|bash
    

    I once had a requirement to append the string "End of File" to all files.

    find .|grep txt|xargs -I{} echo "echo End of File >> {}"|bash
    

    If you do it right, xargs is the king of all commands!

    0 讨论(0)
  • 2020-12-09 09:10

    Use -I option:

    echo prefix | xargs -I % echo % post
    

    Output:

    prefix post
    
    0 讨论(0)
提交回复
热议问题