Command Line Tutorial 03: New Files and Folders

许仙人
·
·
IPFS
·
Move your hands and add things to the computer.

In the last issue we learned three commands:

  • ls to view files and folders in the current directory
  • cd to switch between different file directories
  • pwd shows the path of the current folder

Since then, I have the ability to roam in the computer's folder jungle.

Well, go and go, look and see. Get your hands dirty now and learn how to add things to your computer.

mkdir command: create a new folder

mkdir means make directory, which means to create a new folder (directory).

Enter the mkdir 文件夹名称in Terminal to create a new subfolder under the current folder.

Simple, right? It feels faster than creating a new folder with the right mouse button.

If you want to build the folder somewhere else, just do this: mkdir ~/xxx/xxx/xxx/文件夹名称. ~ refers to the root directory, and those xxx are the path to your target folder.

touch command: create a new file

The touch command works the same way as mkdir . It's just that the former is a new file, and the latter is a new folder.

Please note that the touch command can only create new plain text files , such as txt and md files, but other types of files cannot be created.

For example, touch ~/desktop/note.md create a new markdown document named note on the desktop ( ~ is the root directory, and desktop is a subfolder of the root directory).

By the way, when creating a new file, don't forget to add the suffix of the file . If the suffix is omitted, the computer will not take care of the command.

cat command: view file contents

In Terminal, we can view the contents of the file without opening the file.

I quietly posted a line of poetry in the newly created file note.md, so let's take a look now.

Enter the command cat ~/desktop/note.md (if you are in the desktop file directory, just enter cat note.md ).

Then you will see:

 colarxu@MacBook-Air ~ % cat ~/desktop/note.md

O friends, I am mad with love, and no one sees.    

See? You don't need to open the file, you can see the poem in Terminal.

echo command: add content to the file

Since you can view the file content in Terminal, can you write something in it?

Of course you can, just use the echo command.

For example, if I want to add a paragraph of text to the note.md document, just type echo "This is not true.” >> ~/desktop/note.md .

 colarxu@MacBook-Air ~ % echo "This is not true." >> ~/desktop/note.md 
colarxu@MacBook-Air ~ % cat ~/desktop/note.md 

O friends, I am mad with love, and no one sees. This is not true.

Write the content to be added in the double quotation marks “” after echo , followed by >> , and then enter the path of the target file, and the sentence will be written in.

Note that if you use > instead of >> in the middle of the command, the new content you add will overwrite the original content .

In addition to the echo command, you can also use the cat command to add content to a file.

for example:

  • cat note.md > poem.md "overwrites" the content of the poem with the content of the note.
  • cat note.md >> poem.md adds the content of the note to the poem.

Did you notice? The echo command is to add a piece of text to a file, and the cat command is to add the content of one file to another file. If the added object file does not exist, a new one will be created automatically.

summary

Well, learning this much at one time is enough. Review as usual.

In this issue, we learned four sets of commands:

  • mkdir 文件夹名称is inside the current folder, and a new subfolder is created.
  • touch 文件名称.文件类型(如txt、md) is inside the current folder, and a new file is created.
  • cat 文件名称.文件类型displays the contents of the file.
  • echo “想增加的内容” >> 文件名称.文件类型Add content to the file.

In the next installment, we'll learn how to delete, move, and copy files. see you again~

CC BY-NC-ND 2.0

Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!