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.2 Redirection Command – Change Input and Output Device
In Linux, when you run a particular command, it sends its execution results to a predefined output device. However, if necessary, the execution result can be sent to an output device other than the predefined output device.
It is "<" and ">" (redirection) that is used in this case. If you use this command, it sends the execution result to the output device specified after this command. "redirect" means to change the default input device and the default output device where are set as the keyboard and monitor to another device.
Note that there are several commands as follows for user to redirect input and output with:
■ < and << -- Sends the processing result of the right to the left.
■ > and >> -- Sends the processing result of the left to the right.
■ < and > -- Overwrite existing file if one exists
■ << and >> -- Append existing file if one exists
[Command Format]
send-command > redirect-output-device |
[Command Overview]
■ This sends the processing result of the previous command to the specified output device.
■ User privilege -- Normal user.
[Detail Description]
■ "/dev/null" redirect-output device
If you specify an output device as "/dev/null", it will send to null, which will not send any output. This device is not used well under normal circumstances.
[Used Example]
Before you begin, check the contents of the "testdata" directory. It comes out as follows.
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 |
In this example we will use the "echo" command to explain the ">" (switching output) case. The "echo" command displays the specified string on the standard output device screen. It will be executed as below.
pi@raspberrypi ~/testdata $ echo "This is rsapbian guide" |
This is rsapbian guide |
This time, we will send the same result to "pi_user_guide.txt" file.
pi@raspberrypi ~/testdata $ echo "This is rsapbian guide" > pi_user_guide.txt |
|
When you execute the above command, nothing appears on the screen unlike the previous processing. If you check the contents of the directory, you can see that "pi_user_guide.txt" is created. That is, the result is not sent to the screen, but to the file using the ">" (redirection) command.
If you check the contents of the file using the "cat" command, you can see that the data entered by the first "echo" command is stored in the file.
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 -rw-r--r-- 1 pi pi 40 Apr 24 02:50 pi_user_guide.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 |
pi@raspberrypi ~/testdata $ cat pi_user_guide.txt |
This is rsapbian guide for the beginner |
This time we will combine together the two files into one using the ">" (redirect) command. In the "testdata" directory, there are the "customer_list.txt file" and the "customer_product.txt" file, and each contents are as follows.
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 |
pi@raspberrypi ~/testdata $ cat customer_list.txt |
Microsoft IBM 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 |
Let's combine their contents to the new file with the "cat" command and the ">" (redirect) command. Execute the following command. There is also no message on the screen.
pi@raspberrypi ~/testdata $ cat customer_list.txt customer_product.txt > customer_full.txt |
|
Now, when you check the contents of the directory, you will see that "customer_full.txt" is created. In other words, the output from the "cat" command is sent to a file without output to the screen using the ">" (redirection) command.
pi@raspberrypi ~/testdata $ ls -l |
-rw-r--r-- 1 pi pi 185 Apr 24 03:04 customer_full.txt -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 |
If you check the contents of the file using the "cat" command, you can see that the contents of the two files are merged together.
pi@raspberrypi ~/testdata $ cat customer_full.txt |
Microsoft IBM LG Samsung Sony Hewlett-Packard Microsoft PC Google Search IBM Super Computer Facebook SNS LG Electrics Samsung Mobile Sony Movie Hewlett-Packard Printer |
Now let's output the result to the null device. The null device is "/dev/null". Let's check the contents of the same processing using the "cat" command. The result of the "cat" command is not displayed at all on the screen. You can also see that no file is created in the directory.
pi@raspberrypi ~/testdata $ cat customer_full.txt > /dev/null |
|