• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Friday, May 9, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

12 Practical Examples of Linux Xargs Command for Beginners

How VPS by How VPS
November 2, 2018
in Linux
0
0
SHARES
13
VIEWS
Share on FacebookShare on Twitter

Xargs is a great command that reads streams of data from standard input, then generates and executes command lines; meaning it can take output of a command and passes it as argument of another command. If no command is specified, xargs executes echo by default. You many also instruct it to read data from a file instead of stdin.

There are several ways in which xargs is useful in daily usage of the command line. In this article, we will explain 12 practical Linux xargs command examples for beginners.

1. The first example shows how to find out all the .png images and archive them using the tar utility as follows.

Here, the action command -print0 enables printing of the full file path on the standard output, followed by a null character and -0 xargs flag effectively deals with space in filenames.

$ find Pictures/tecmint/ -name "*.png" -type f -print0 | xargs -0 tar -cvzf images.tar.gz
12 Practical Examples of Linux Xargs Command for Beginners

Find Images and Archive Using Tar

2. You can also convert muti-line output from ls command into single line using xargs as follows.

$ ls -1 Pictures/tecmint/
$ ls -1 Pictures/tecmint/ | xargs
12 Practical Examples of Linux Xargs Command for Beginners

List Files in Single Line

3. To generate a compact list of all Linux user accounts on the system, use the following command.

$ cut -d: -f1 < /etc/passwd | sort | xargs
12 Practical Examples of Linux Xargs Command for Beginners

Find List of Linux Users

4. Assuming you have a list of files, and you wish to know the number of lines/words/characters in each file in the list, you can use ls command and xargs for this purpose as follows.

$ ls *upload* | xargs wc
12 Practical Examples of Linux Xargs Command for Beginners

Count Number of Lines, Words and Characters in Files

5. Xarags also allows you to find and recursively remove a directory, for example the following command will recursively remove DomTerm in the directory Downloads.

$ find Downloads -name "DomTerm" -type d -print0 | xargs -0 /bin/rm -v -rf "{}"
12 Practical Examples of Linux Xargs Command for Beginners

Find and Recursively Delete Directory

6. Similarly to the previous command, you can also finds all files named net_stats in the current directory and delete them.

$ find . -name "net_stats" -type f -print0 | xargs -0 /bin/rm -v -rf "{}"

7. Next, use xargs to copy a file to multiple directories at once; in this example we are trying to copy the file.

$ echo ./Templates/ ./Documents/ | xargs -n 1 cp -v ./Downloads/SIC_Template.xlsx 
12 Practical Examples of Linux Xargs Command for Beginners

Copy File to Multiple Directories

8. You can also use the find command, xargs and rename commands together to to rename all files or subdirectories in a particular directory to lowercase as follows.

$ find Documnets -depth | xargs -n 1 rename -v 's/(.*)//([^//]*)/$1///L$2/' {} /;

9. Here is another useful usage example for xargs, it shows how to delete all files within a directory except one or few files with a given extension.

$ find . -type f -not -name '*gz' -print0 | xargs -0 -I {} rm -v {}

10. As mentioned earlier, you can instruct xargs to read items from a file instead of standard input using the -a flag as shown.

$ xargs -a rss_links.txt

11. You can enable verbosity using the -t flag, which tells xargs to print the command line on the standard error output before executing it.

$ find Downloads -name "DomTerm" -type d -print0 | xargs -0 -t /bin/rm -rf "{}"

12. By default, xargs terminates/delimits items using blank spaces, you can use the -d flag to set the delimiter which may be a single character, a C-style character escape such as /n, or an octal or hexadecimal escape code.

In addition, you can also prompt the user about whether to run each command line and read a line from the terminal, using the -p flag as shown (simply type y for yes or n for no).

$ echo ./Templates/ ./Documents/ | xargs -p -n 1 cp -v ./Downloads/SIC_Template.xlsx 
12 Practical Examples of Linux Xargs Command for Beginners

Prompt User Before Running Command

For more information, read the xargs man page.

$ man xargs 

That’s it for now! Xargs is a powerful utility for building a command line; it can help you pass output of one command as an argument of another command for processing. In this article, we’ve explained 12 practical xargs command examples for beginners. Share you thoughts or questions with us via the feedback form below.

Source: tecmint.com

Tags: Linux Commandslinux guidelinux vps setup guide
Previous Post

10 ‘free’ Commands to Check Memory Usage in Linux

Next Post

10 SCP Commands to Transfer Files/Folders in Linux

Next Post

10 SCP Commands to Transfer Files/Folders in Linux

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet