Raspberry Pi_Eng_10.8.1 “xargs” Command


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


10.8   Commands to Adjust Input and Output

 

10.8.1  "xargs" Command

 

This command allows you to use the output of the previously executed command as an argument input to the next command.

 

[Command Format]

xargs  [option]   exec-command

 

[Command Overview]

   This makes the output of the executed command to be used as an argument to the next command.

   User privilege          -- Normal user.

 

[Detail Description]

   If there are no other commands before the xargs command, read the data from the standard input, and pass it as an argument to the run command.

   If you use it normally, use "pipe" command (|) to get the result of the previous executed command and pass it to the next command. Let's illustrate the example below.

ex) find /tmp -name core -type f print0   |   xargs rm -f

The "find" command is executed earlier, followed by the "rm" command. This command uses the "xargs" command to pass the results from the previous "find" command to the "rm" command. The overall effect is to find and delete all files with "core" in the filename in the "/tmp" directory.

   "exec-command" is executed for each data passed through "xargs". Thus, if the number of arguments is more than one, then the "exec-command" is also executed multiple times.

 

[Main Option]

--arg-file=file    -a file

Read items from file instead of standard input. If you use this option, stdin remains unchanged when commands are run. Otherwise, stdin is redirected from /dev/null.

 

[Used Example]

First, there is a "customer_list.txt" file and a "customer_product.txt" file in the "testdata" directory of the "pi" account. You can check these file as shonw below.

 

pi@raspberrypi ~/testdata $ ls -l

-rw-r--r-- 1 pi pi   62 Apr 23 13:06 customer_list.txt

-rw-r--r-- 1 pi pi  123 Apr 23 13:00 customer_product.txt

-rw-rw---- 2 pi pi   80 Apr 12 15:12 DebianManual.txt

drwxr-xr-x 4 pi pi 4096 Apr 22 09:52 TestFolder01

drwxr-xr-x 2 pi pi 4096 Apr 10 13:32 TestFolder02

-rw-r--r-- 1 pi pi  656 Apr 22 09:30 test_for_editor.txt

-rw-rw---- 1 pi pi  113 Apr 23 08:22 user_guide01.txt

 

Checking the content by the "cat" command is as follows.

 

pi@raspberrypi ~/testdata $ cat customer_list.txt

Microsoft

Google

IBM

Facebook

LG

Samsung

Sony

Hewlett-Packard

pi@raspberrypi ~/testdata $ cat customer_product.txt

Microsoft PC

Google  Search

IBM     Super Computer

Facebook SNS

LG      Electrics

Samsung Mobile

Sony    Movie

Hewlett-Packard Printer

 

First, let's look at the example that passes the data input from the standard input as the argument of the next command through the "xargs" command. If you execute the command as shown below, you can input through standard input.

 

pi@raspberrypi ~/testdata $  xargs cat

 

When you execute the above command, the following screen will be displayed, but the cursor will be on the next line without displaying anything on the screen. This means that you need to enter the required value for standard input.

 


 

Then, enter the name of the "customer_list.txt" file you want to view and press [Enter] key. Next, press CTRL + D to indicate that the input is complete, and the EOF (End Of File) is processed for standard input. Then, the input operation is completed, and the next operation is processed.

The value entered in the standard input is then treated as input to the "xargs" command, which is passed as the argument to the next command "cat", and the "cat" command is executed with that argument. Then the contents of the specified file will appear on the screen.

When all the processing is completed, the following operation result will be displayed.

 

pi@raspberrypi ~/testdata $  xargs cat

customer_list.txt

Microsoft

Google

IBM

Facebook

LG

Samsung

Sony

Hewlett-Packard

 

This time, we don't directly specify the file name of the "cat" command, but specify it by passing the file name of the "echo" command to the "cat" command through the "xargs" command. This gets the same results as if you had specified the file name directly in the "cat" command. This means that the "xargs" command takes the output of the "echo" command, "customer_list.txt'", and passes it as an argument to the "cat" command.

pi@raspberrypi ~/testdata $ echo 'customer_list.txt' | xargs cat

Microsoft

Google

IBM

Facebook

LG

Samsung

Sony

Hewlett-Packard

 

In this case, we create "customer_file.txt" as below and enter the above two file names to save it and check the contents.

pi@raspberrypi ~/testdata $ cat customer_file.txt

customer_list.txt

customer_product.txt

 

Then, run the "xargs" command as follows. You can see that the contents of the "customer_list.txt" file and the "customer_product.txt" file are displayed at once. This means that the "cat" command has been repeatedly executed for each line in the "customer_file".

pi@raspberrypi ~/testdata $ xargs -a customer_file.txt cat

Microsoft

Google

IBM

Facebook

LG

Samsung

Sony

Hewlett-Packard

Microsoft PC

Google  Search

IBM     Super Computer

Facebook SNS

LG      Electrics

Samsung Mobile

Sony    Movie

Hewlett-Packard Printer