[LINUX] If you don't know it, it's dangerous. Carefully explain how to use the xargs command

Basic

Click here for the video (click the image to go to youtube) [How to use the less command. When it's just a cat [linux command]] watch? v = BnwD2rX9kv8)

xargs command

You can read a list from standard input or a file and pass it as an argument to the command you want to execute! By the way, the reading is generally "X-Agus". Since it is an abbreviation for arguments, it is taken from arguments (probably)

option

appendix

sample sample command

Try to actually type the command

Prepare two files.

[bitnami@ /tmp/com_xargs]$ ls
aaa.txt  bbb.txt

The contents are like this

[bitnami@ /tmp/com_xargs]$ cat aaa.txt bbb.txt
asdf
qwer
34

qweriu
qweuf
werpoi

For example, try typing a command like this.

[bitnami@ /tmp/com_xargs]$ ls | xargs wc
 4  3 14 aaa.txt
 3  3 20 bbb.txt
 7  6 34 total

This is equivalent to typing the wc command on aaa.txt and bbb.txt. That is, "you can pass the result of ls to the wc command"

Let's check what kind of command is being executed with the -p option.

[bitnami@ /tmp/com_xargs]$ ls | xargs -p wc
wc aaa.txt bbb.txt ?...

As you can see, it says "wc aaa.txt bbb.txt? ...". You can see that you can pass the result of ls as an argument of the wc command. If you enter y here and press enter, it will be executed. (The -p option is convenient for debugging, so I definitely want to remember it)

By the way, if you are depressed without using xargs, this will happen.

[bitnami@ /tmp/com_xargs]$ ls | wc
      2       2      16

What this means is that you can think of it as passing the following result directly to the wc command (that is, it is easy to think of it as a file written as aaa.txt and bbb.txt).

(By the way, the meaning of the wc command is the number of lines, the number of words, the number of bytes from the left)

[bitnami@ /tmp/com_xargs]$ ls | cat
aaa.txt
bbb.txt

Recommended Posts

If you don't know it, it's dangerous. Carefully explain how to use the xargs command
How to find if you don't know the Java installation directory
If you don't know how to draw the graph you want with matplotlib, it is convenient to look at the gallery.
Don't you know it? pip command
[Linux] How to use the echo command
How to use the Linux grep command
[Python] What is pip? Explain the command list and how to use it with actual examples
[Django] Carefully explain the escape route if you really want to use the table of another application
Linux user addition, how to use the useradd command
How to use the grep command and frequent samples
(Remember quickly) How to use the LINUX command line
How to use the generator
I didn't know how to use the [python] for statement
If you want your colleagues to use the same language
How to use the decorator
Don't write Python if you want to speed it up with Python
Indispensable if you use Python! How to use Numpy to speed up operations!
If you don't know how many arguments you will have 2 ** kwargs Variadic arguments
What to do if you can't use the trash in Lubuntu 18.04.
How to use the optparse module
How to use MBDyn (command setting)
How to use the ConfigParser module
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
If you want to count words in Python, it's convenient to use Counter.
How to use the Spark ML pipeline
[python] How to use __command__, function explanation
How to calculate Use% of df command
How to use CUT command (with sample)
Don't use rm command to delete files
How to use the IPython debugger (ipdb)
3 best ways to use the less command
file name. You can have a half-width space in pathlib, but you should escape the half-width space when you hit it in the subprocess. If you use check_output, you don't need to escape.
Linux command that can be used from today if you know it (Basic)
How to use the C library in Python
How to use MkDocs for the first time
How to use the graph drawing library Bokeh
When you don't know the SCP when you port forward
How to use the Google Cloud Translation API
How to use the NHK program guide API
[Algorithm x Python] How to use the list
How to install Cascade detector and how to use it
You should know if you use Python! 10 useful libraries
I tried to scrape YouTube, but I can use the API, so don't do it.
How to use the grep command to recursively search directories and files to a specified depth
How to use any or all to check if it is in a dictionary (Hash)
How to install and use pyenv, what to do if you can't switch python versions
What to do if you don't want to use Japanese column names when using ortoolpy.logistics_network