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)
- Create, delete, copy, move, and rename
touch
commands to create file syntax>$ touch file0 [file1 file2 ...]>$ touch file{0..9}.txt
touch
command 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.
mkdir
command to create a directory syntax
>$ mkdir [-p] dir1 [dir2 dir3 ...]>$ mkdir [-p] dir1{0..9}
mkdir
command 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}
rm
command to delete a file or directory syntax
>$ rm [-rf] dir1/file1 [dir2/file2 ...]
rm
command to delete a file or directory.
-r
parameter indicates that you want to delete a directory.
-f
The 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.
cp
Command copy directory or file syntax
>$ cp [-rfv] src dest
cp
command to copy the file or directory specified by the dest
parameter to the location specified by the parameter. src
-r
Parameters 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.
-f
parameter indicates forced replication.
-v
Parameters represent the process of printing replication.
mv
command to move or rename file and directory syntax
>$ mv [-fv] src dest
mv
The 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
.
-f
Parameter indicates force move or rename.
-v
Parameters 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
- File read/write
echo
Command output string syntax
echo [-n] "string"
Use echo
the command string
to print a string of parameters to the console.
-n
The 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
.
\n
This 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
.
cat
Command view file full content syntax
>$ cat [-n] file1 [file2 ...]
cat
The 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
).
-n
The parameter represents the printed line number.
Example
>$ cat file1.txtabcdef
If the file1.txt
file is empty, the cat
command has no output.
head
To view the beginning of a file syntax
>$ head [-n N] file
head
The command reads the file
data from line 1th of the file, reads a total of 10 rows, and prints to the console.
-n
Parameter can require head
the command to read N
the row data, which N
defaults to 10.
tail
To view the end of a file section syntax
>$ tail [-n N] [-f] file.log
tail
The 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.
-f
Parameters 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.
more
command 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.
more
The 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.
more
Commands are suitable for browsing long files or used in pure shell (non-simulated terminal) scenarios.
less
Command more flexible Page view of file syntax
>$ less file.txt
less
Commands are more
very similar to commands, and can be seen as an enhanced version of the more
command.
less
The 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