Linux 0 Basics Introductory Lesson IV

Source: Internet
Author: User

According to the contents of the live handout, start with lesson four. The contents of the first three lessons will be sorted out if there is energy in the back.

Basic operation of the file (top)
    1. Create, delete, copy, move, and rename touch commands to create file syntax
      >$ touch file0 [file1 file2 ...]>$ touch file{0..9}.txt
      touchcommand to create an empty file with the file name as an argument. You can create multiple files at the same time by passing multiple filenames.
      If you use file{0..9} as a parameter, 10 empty files will be created automatically, with the name file0.txt file1.txt 、...、 file9.txt .
      If the file passed to touch is already on disk, the file is not created, but the timestamp of the file is updated.


mkdircommand to create a directory syntax
>$ mkdir [-p] dir1 [dir2 dir3 ...]>$ mkdir [-p] dir1{0..9}

mkdircommand to create a directory with a directory name as an argument.
Pass multiple directory names to create multiple directories at the same time.
If you want to create subdirectories at the same time, you need to use -p parameters, otherwise you can only create empty directories.

Example
# 在当面目录下创建名为 dir1 的空目录。>$ mkdir dir1# 在当前目录的子目录 dir1 内创建子目录 dir2。>$ mkdir dir1/dir2# 错误:dir3 不存在,创建多级子目录应使用 -p 参数。>$ mkdir dir3/dir4# 同时创建 dir3 目录及其子目录 dir4。>$ mkdir -p dir3/dir4# 创建 3 个目录,名字分为为 dir5、dir6、dir7。>$ mkdir dir{1..3}


rmcommand to delete a file or directory syntax
>$ rm [-rf] dir1/file1 [dir2/file2 ...]

rmcommand to delete a file or directory.
-rparameter indicates that you want to delete a directory.
-fThe parameter indicates force delete, and silently deletes, does not ask the user, the deleted file does not exist and does not error.
Note: This command is more dangerous, the deleted files are almost unrecoverable and you need to use caution.


cpCommand copy directory or file syntax
>$ cp [-rfv] src dest

cpcommand to copy the file or directory specified by the dest parameter to the location specified by the parameter. src
-rParameters are used to recursively replicate files in the directory, and if this parameter is not used when replicating directories, only the directory itself is copied, and the files and subdirectories in the directory are not copied.
-fparameter indicates forced replication.
-vParameters represent the process of printing replication.


mvcommand to move or rename file and directory syntax
>$ mv [-fv] src dest

mvThe command has two functions, when src the file or dest directory specified by the parameter is the same directory as the path specified by the parameter, it is equivalent to renaming, otherwise src it is equivalent to moving to dest .
-fParameter indicates force move or rename.
-vParameters represent the process of printing movement.

Example
# 当前目录 dir 与文件 file1 在相同的目录中。>$ tree.├── dir└── file11 directory, 1 file# 将 file1 重命名为 file2。>$ mv file1 file2>$ tree.├── dir└── file21 directory, 1 file# 将 file2 移动到 dir 目录下。>$ mv file2 dir>$ tree.└── dir    └── file21 directory, 1 file
    1. File read/write
echoCommand output string syntax
echo [-n] "string"

Use echo the command string to print a string of parameters to the console.
-nThe parameter indicates that the line break is not output \n .

The echo string command actually writes a string of arguments to its standard output stream stdout , but because the stdout console is the default, the command appears echo to print a string to the console.

Shell redirection Operators >And >>

Through the shell's redirection operators > and the >> ability to write (redirect) the contents of the standard output stream () to the stdout console, it should be written (redirected) to the file.

Grammar
>$ echo "abc" > file1.txt>$ echo "def" >> file1.txt

>The redirect Operator echo writes the command to a file that prints a string to the standard output stream ( stdout ), where abc file1.txt file1.txt the contents are abc\n .
\nThis is Linux the line break of the system.
If file1.txt it does not exist, it is created automatically by the redirect operator.

>>The redirect operator echo appends the command to the standard output stream () to the end of the stdout def file, where file1.txt the contents of the file1.txt are abc\ndef\n .


catCommand view file full content syntax
>$ cat [-n] file1 [file2 ...]

catThe command reads fileN The contents of the file from beginning to the beginning and prints it to the console (actually printing to standard output stdout ).
-nThe parameter represents the printed line number.

Example
>$ cat file1.txtabcdef

If the file1.txt file is empty, the cat command has no output.

headTo view the beginning of a file syntax
>$ head [-n N] file

headThe command reads the file data from line 1th of the file, reads a total of 10 rows, and prints to the console.
-nParameter can require head the command to read N the row data, which N defaults to 10.


tailTo view the end of a file section syntax
>$ tail [-n N] [-f] file.log

tailThe command is the opposite of the head command, starting at the end of the file, N reading the data and printing it to the console, which N defaults to 10 rows, which can also be -n specified by parameters.
-fParameters are useful, and file.log the tail command can listen to this change and continuously print the latest content to the console if there is constant content writing in the file. This parameter is commonly used when viewing logs.


morecommand to view file syntax by page
>$ more file.txt

When the contents of the file.txt file to be viewed are particularly long, it cat is not convenient to use commands to view them, and the more commands just meet the requirements.
moreThe command reads file.txt The contents of the file from the beginning, and the data is displayed as one page full of the entire screen. When we press ENTER, the contents of the next line are scrolled, and when we press the space bar, we scroll to display the contents of the next page. The file.txt more command does not exit until the end of the file is reached.
moreCommands are suitable for browsing long files or used in pure shell (non-simulated terminal) scenarios.


lessCommand more flexible Page view of file syntax
>$ less file.txt

lessCommands are more very similar to commands, and can be seen as an enhanced version of the more command.
lessThe command not only supports more the same paging, and backward reading of the file as the command, 回车键 空格键 but also supports the use 光标控制键(上下箭头) , vim光标控制键(j、k) and the Emacs光标控制键(C-n、C-p) backward or forward reading of the file. Enables users to view files in a more flexible way.

Linux 0 Basics Introductory Lesson IV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.