Mastering the Power of xargs Command for your work

Introduction

xargs Command powerful tool that can revolutionize the way you handle various tasks on your system. Whether it’s processing files, executing commands in parallel, or manipulating data streams, xargs is a versatile Swiss Army knife for any Linux enthusiast.

What does the xargs command in Linux?

xargs is a great command that reads streams of data from standard input, then generates and executes command lines.

In this blog, we will explore the ins and outs of the xargs command, its practical applications, and how it can make your life as a Linux user much easier.

Syntax

xargs [options] [command]

Here are some common options used with the xargs command:

  • -n: Specifies the maximum number of items to be passed as arguments to the command.
  • -I: Allows you to specify a placeholder (usually {}) for the argument, which is replaced by each item from the input.
  • -t: Prints the command being executed before running it.
  • -p: Asks for confirmation before executing each command.
  • -r: Prevents the command from running if there is no input.
  • -a: Specifies the input file from which xargs should read the items instead of STDIN.
  • -P: Sets the maximum number of parallel processes to run at once.

xargs command Tips and Tricks

How to create multiple files with xargs command in Linux.

echo devopsrolesfile1 devopsrolesfile2 devopsrolesfile3 | xargs touch

The output terminal is below

xargs command

Creates a file with blanks in its name

echo 'my new file devopsroles' | xargs -d '\n' touch

The output terminal is below

xargs command

Changes permissions on all PNG files within the /home/vagrant directory

find /home/vagrant -name "*.png" -type f | xargs chmod 640

To view the command that is run by xargs, add the -t option:

echo devopsrolesfile1 devopsrolesfile2 devopsrolesfile3 | xargs -t touch

We use find to locate files that haven’t been updated in more than four weeks and xargs to remove them.

find . -mtime +29 | xargs rm

finding and removing empty files

find . -size 0 | xargs rm
# or
find . -size 0 | xargs -I{} rm -v {}

How to count the characters in each file.

ls -Srp | grep -v '/$' | xargs -I X wc -c X

The output terminal as below

xargs command

Check the most recent four logins for each currently logged-in user.

who | awk '{print $1}' | xargs -I x last -4 x

The output terminal is below

xargs command

Conclusion

You have to use xargs command for your work daily. The xargs command is an indispensable tool that empowers Linux users to streamline their tasks and increase productivity.

The xargs command is a versatile tool that can greatly enhance your command-line productivity. Whether you’re processing files, running commands in parallel, or performing batch operations, xargs can simplify and automate many tasks. However, it’s important to use it with care, especially when dealing with commands that modify or delete files.

Its ability to handle large sets of data, parallelize operations, and simplify complex tasks makes it a valuable asset in any Linux user’s toolkit. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.