Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»xargs Command in Linux with Useful Examples

    xargs Command in Linux with Useful Examples

    By RahulApril 2, 20133 Mins ReadUpdated:February 11, 2021

    xargs is a Linux/Unix powerful command to build and execute command lines from standard input. It takes output of a command and pass it as argument of another command. xargs takes standard input, delimited by blanks or newlines, and executes the command one or more times with any arguments followed by items. Blank lines on the standard input are ignored.

    Advertisement

    Syntax

    xargs [OPTION]... COMMAND [INITIAL-ARGS]...
    

    Here the COMMAND is executed with arguments INITIAL-ARGS and more arguments read from the input.

    xargs Command Options

    The xargs commands provides a limited number of command line arguments but enough to utilize its features.

    • -0, --null – The items are separated by a null, not whitespace
    • -a, --arg-file=FILE – Read arguments from FILE, not the standard input.
    • -d, --delimiter=CHARACTER – The items in input stream are separated by CHARACTER, not whitespace
    • -E END – Set a string as login EOF. If the string found, the rest will be ignored.
    • -L, --max-lines=MAX-LINES – Specify maximum number of lines (non-blank) to take as input at command line
    • -n, --max-args=MAX-ARGS – Specify max arguments per command line
    • -P, --max-procs=MAX-PROCS – Specify maximum processes to run at a time
    • -p, --interactive – Run processes interactive with prompt before running commands
    • -r, --no-run-if-empty – Do not run command if input arguments are empty.
    • -t, --verbose – Print all commands on screen executing them

    xargs Command Examples

    Example 1. Copy large number of files to another folder.

    Some times we required to copy a long list of files, In that case cp command failed with error “Argument list too long”. We can use xargs to do that task.

    find /backup/ -type f | xargs -n1 -i cp {} /var/www/backup/ 
    
    Example 2: Delete multiple files from a folder.

    Some times we requird to delete a large number of files from a folder. Below example will delete all .log files from /var/log directory.

    find /var/www/tmp/ -type f | xargs rm -f 
    

    Above command will failed to remove files with spaces in named. To handle spaces in xargs command try below command.

    find /var/www/tmp/ -type f -print0 | xargs -0 rm -f 
    
    Example 3: Count number of lines in multiple files.

    Below example will count number of lines for each .txt file in /opt directory and its subdirectory

    find /opt -name "*.txt" | xargs wc -l 
    

    To handle files having spaces in there name, use following command.

    find /opt/ -name "*.log" -print0 | xargs -0 wc -l 
    
    Example 4: Make a backup of all configuration files.

    If you want to make a backup of all configuration files ( extension .conf ) in your system, use below command.

    find / -name "*.conf" |  xargs tar czf  config.tar.gz 
    
    ls -l config.tar.gz 
    
    -rw-r--r--. 1 root root 193310 Apr  1 13:26 config.tar.gz
    
    Example 5. Use custome delimeter with xargs.

    We can have also use custom delemeter with xargs command, By default it uses space and new line as delimeter. Use -d parameter to define delimeter.

    echo "1,2,3,4,5" | xargs -d, echo 
    

    Output:

    1 2 3 4 5
    
    Example 6: Show output in sepreate line with xargs.

    In example 5 ouput is showing in single line, We can also specify to show each output in seprate line.

    echo "1,2,3,4,5" | xargs -d, -L 1 echo 
    

    Output:

    1
    2
    3
    4
    5
    
    Example 7: Handling blank space in filenames or path.

    To handle spaces in names use -print0 with find command and -0 with xargs command as parameter.

    find /tmp -print0 | xargs -0 -L 1 echo 
    

    command linux xargs
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Sed Command to Delete Lines in a File

    Sed Command to Delete Lines in a File

    Sort Command in Linux with Practical Examples

    15 Practical Examples of Sed Command

    Sed Command in Linux with 15 Practical Examples

    View 1 Comment

    1 Comment

    1. krish on May 16, 2017 6:11 am

      plz explain the xargs options like -l, -n and what they r doing ? i didn’t understand example 3 . plz elaborate that also

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Error: EACCES: permission denied, scandir (Resolved)
    • How To Install Python 3.11 on Ubuntu 22.04 / 20.04
    • How to Install Python 3.11 on Amazon Linux 2
    • An Introduction to the “./configure” Command: Compiling Source Code in Linux
    • How to Install PHP 8.x on Pop!_OS
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.