Raspberry Pi_Eng_16.2.3 Creating Text File


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


16.2.3  Creating Text File

 

There are many ways to create a text file in Raspberry Pi. There is a method to create it from the Shell Terminal, and a method to process it from the Desktop window.

 

There are several ways to create simple text files from the Shell Terminal as shown below:

    How to use "touch" command

    How to use "echo" command

    How to use the text edit program

 

16.2.3.1      File Creation Using "touch" Command

Executing the "touch" command on a nonexistent file has the effect of creating a new file.

 

[Command Format]

touch    [OPTION]   <from-directory/file>

 

[Command Overview]

   This functions to change the modification time to the current time in the file information.

   User privilege          -- Normal user.

 

[Detail Description]

   Among the file information in the system, there are timestamps information about the file creation time and modification time. The "touch" command function to change the last modification time to the current time.

   If you run this command on a file that does not exist, it has the effect of creating a new file. The touch command creates a file on your system, but it does not include anything inside the file.

 

[Main Option]

--help

display this help and exit

 

[Used Example]

We will create a file here. The next screen shows the contents of the "testdata" directory before the operation.

 

pi@raspberrypi ~/testdata $ ls l

total 16

drwxr-xr-x 2 pi pi 4096 Apr 10 04:07 TestFolder01

drwxr-xr-x 2 pi pi 4096 Apr 10 03:58 TestFolder02

-rwxrwx--- 1 pi pi   18 Mar 24 02:10 user_guide01.txt

 

The following is that you create "testfile01" file with the "touch" command and inquire the contents of directory again. You can see that the "testfile01" file is newly created. You can see that you have run the "cat" command to query the contents of that file, but no data is displayed.

 

pi@raspberrypi ~/testdata $ touch testfile01

pi@raspberrypi ~/testdata $ ls l

total 12

-rw-r--r-- 1 pi pi    0 Apr 11 07:09 testfile01

drwxr-xr-x 3 pi pi 4096 Apr 11 02:17 TestFolder01

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

-rwxrwx--- 1 pi pi   18 Mar 24 02:10 user_guide01.txt

pi@raspberrypi ~/testdata $ cat testfile01

 

 


 

16.2.3.2    File Creation Using "echo" and ">" Command

 

The "echo" command is a command that displays a specific text message on the screen. And the ">" command is a command that sends the result of the previous command to another target without sending it to the screen. By combining these two commands, you can easily create a simple text file in which the text entered in the command becomes the contents of the file.

 

[Command Format]

echo  [OPTION]  "text string"  >  <"to-directory/file>

 

[Used Example]

We will create a file here. The next screen shows the contents of the testdata directory before the operation.

 

pi@raspberrypi ~/testdata $ ls l

total 12

-rw-r--r-- 1 pi pi    0 Apr 11 07:09 testfile01

drwxr-xr-x 3 pi pi 4096 Apr 11 02:17 TestFolder01

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

-rwxrwx--- 1 pi pi   18 Mar 24 02:10 user_guide01.txt

 

The following shows the screen of creating "testfile02" file with "echo" command and displaying contents of the directory again. You can see that "testfile02" file is newly created. You can see that the contents are displayed normally when you execute the "cat" command to see the contents of the file.

 

pi@raspberrypi ~/testdata $ echo "This is test file" > testfile02.tx

pi@raspberrypi ~/testdata $ ls l

total 16

-rw-r--r-- 1 pi pi    0 Apr 11 07:09 testfile01

-rw-r--r-- 1 pi pi   18 Apr 11 07:24 testfile02.txt

drwxr-xr-x 3 pi pi 4096 Apr 11 02:17 TestFolder01

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

-rwxrwx--- 1 pi pi   18 Mar 24 02:10 user_guide01.txt

pi@raspberrypi ~/testdata $ cat testfile02.txt

This is test file


 

16.2.3.3    File Creation Using Text Editor Program

 

In the Raspberry Pi system, several text editor programs are available. In this section, we will explain how to create a file using [Nano] text editor, which is a representative text editor program. A description of other programs is given in a separate chapter.

 

[Command Format]

nano   [OPTION]   <to-directory/file>

 

[Used Example]

The next screen shows the contents of the "testdata" directory before the operation.

 

pi@raspberrypi ~/testdata $ ls l

total 16

drwxr-xr-x 3 pi pi 4096 Apr 11 02:17 TestFolder01

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

-rwxrwx--- 1 pi pi   18 Mar 24 02:10 user_guide01.txt

 

First, name the file to be created as "testfile03.txt" and start the [Nano] program first.

 

pi@raspberrypi ~/testdata $ nano testfile03.tx

 

Then the editor screen appears as shown below. Enter the desired content in the blank space of the screen and save the contents using CTRL + O button.

 


 

Then, you will see the screen to check the file name again as shown below. Press [Enter] after confirming this.

 


 

 

Then, the contents are saved and the file is created. When finished, press CTRL + X to exit the program and return to the original screen.

 


 

If you inquire the contents of the directory again after the operation is completed, you can see that the "testfile03.txt" file is newly created. You can see that the contents are displayed normally by executing the "cat" command to inquire the file contents.

 

pi@raspberrypi ~/testdata $ ls l

total 16

-rw-r--r-- 1 pi pi   18 Apr 11 07:24 testfile03.txt

drwxr-xr-x 3 pi pi 4096 Apr 11 02:17 TestFolder01

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

-rwxrwx--- 1 pi pi   18 Mar 24 02:10 user_guide01.txt

pi@raspberrypi ~/testdata $ cat testfile03.txt

This is test file by nano program

 


 

16.2.3.4    Processing in Desktop Window

 

You can easily create an empty file by using the [File Manager] program in the Desktop window.

 

First, select the folder you want to create and keep a file. Then, you can do the same thing by selecting the menu ile à Create New àEmpty File or using the menu Create New à Empty File from the popup menu that appears when you click right mouse button on the right screen of the selected work folder.

 


Figure 16‑3 Create empty file in Desktop window

 

Then, a pop-up screen to specify a name for the new file appears. Enter the desired file name here and continue processing, the desired file is created.