xargs command for your work

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 tutorial, How to use xargs command for your work daily.

xargs command

How to create multiple file with xargs command

echo devopsrolesfile1 devopsrolesfile2 devopsrolesfile3 | xargs touch

The output terminal as below

Creates a file with blanks in its name

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

The output terminal as below

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

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 as below

Conclusion

You have to use xargs command for your work daily. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. 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 *

Comment moderation is enabled. Your comment may take some time to appear.

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