Article Directory
-
- pwd displays the absolute path of the current working path
- ls lists the contents of a directory
- cd switch directory
- mkdir creates a new directory
- delete an empty directory
- touch to create a new file
- cp copies files or directories
- rm removes a file or directory
- mv moves files and directories, renames files
- cat to view the contents of the file and create a new file
- View the content of more files in split screen
- less split screen display file content
- echo output content
- head Display the header information of the file
- tail output file tail content
- > output redirection and >> append
- ln soft link
Written in the front: The article explains the commands related to Linux file operations
Shell can be regarded as a command interpreter, which provides us with an interactive text control interface. We can enter commands through the console, which will be interpreted by sell and finally handed over to the kernel for execution. Similar to python's interactive window, that is, the output is the output, and each step of the command will be executed immediately and the result will be returned.
pwd displays the absolute path of the current working path
Full name: print working directory
Print the absolute path of the working directory
Usage:pwd
Example: To
expand knowledge:
Absolute path: relative to the path of the root node, which is the only definite path that really exists on the computer
Relative path: relative to the path of the working path, only relative to the file location of a certain directory, the reference directory is different results also different
ls lists the contents of a directory
Full name: list List directory contents
Usage: ls [option] [directory file]
options | effect |
---|---|
-a | show all files |
-l | Show more detailed file information ls -l Short formll |
There are some other options,
Example:
options can be used at the same time to
view the structure under the specified directory
Expand knowledge:
the .
folder starting with is the hidden folder
..
's upper level directory (cd command will be used)
.
the current directory
cd switch directory
Full name: Change directory Switch working path
Usage:cd [参数]
Order | effect |
---|---|
cd absolute path/relative path | Switch the path according to the given path |
cd .. |
Return to the previous directory of the current directory |
cd - |
Return to the previous directory |
cd |
Return to the user's home directory |
cd . |
Current directory |
Example:
mkdir creates a new directory
Full name: make directory Create a new directory
Usage: mkdir [option] The name of the directory to be created
options | effect |
---|---|
-p | Create multi-level directories |
Example:
create a directory, create a multi-level directory
delete an empty directory
Full name: remove directory to delete an empty directory
Usage: rmdir the directory to be deleted
options | effect |
---|---|
-p | Delete multi-level directories |
Example:
Delete the fish1 and fish2 folders created above. Note that this command can only delete empty directories. If the directory is not empty, an error will occur.
Multiple directories can be deleted at the same time, and the directories are separated by spaces.
# 同时删除dir1 dir2
rmdir dir1 dir2
The deletion of multi-level directories can be realized through -p
parameters. When deleting a multi-level directory, it can only be deleted when the directory is empty
. Example: To delete aa/bb/cc, first determine whether the cc directory is empty, delete it if it is empty, and then determine whether the bb directory is empty Delete, if it is not empty, exit delete, and then cycle the above operations in turn
touch to create a new file
Full name: touch
Usage: touch The name of the file to be created
Example: create a file in the current directory Create aa.txt aa.txt
file
in the root folder
Expand knowledge:
use vim, vi, you can also create a file,
many commands can be combined >
to >>
create a file and write content to the file
cp copies files or directories
Full name: copy copy file
usage: cp [option] source dest copy source to dest
options | effect |
---|---|
-r | copy entire folder recursively |
source
Source file/directory
dest
Target file/directory
Example:
copy aa.txt to the aa directory
When the last specified file is not a directory but a file, you will be asked to choose whether to overwrite: y
overwrite n
or not overwrite, cancel copy
, copy the entire directory folder to another directory
Expand knowledge:
\cp forces overwriting without prompting
rm removes a file or directory
Full name: remove deletes a directory or file
Usage: rm [option] the directory/file to be deleted
options | effect |
---|---|
-r | Recursively delete all contents of a directory |
-f | Force delete without prompting |
-in | Display the detailed execution process of the command |
Example:
Delete an ordinary file
Delete all contents in the directory (the directory will also be deleted)
To expand knowledge:
to delete all files in a directory, you can use
/* * to represent all content
mv moves files and directories, renames files
Full name: move Move files or files with the same name
Usage:
mv 原文件名 新文件名
Renamemv 原文件地址 要移动到的地址
move folder
Example:
Rename aa.txt to bb.txt and
move the bb.txt file to the root directory. After the move, there will be no local files.
Move and rename
cat to view the contents of the file and create a new file
Full name: concatenate View the contents of the file, starting from the first line
Usage: cat [option] the file to view
options | effect |
---|---|
-n | Display line numbers for all lines, including empty lines |
-b | Display line numbers, excluding blank lines |
-s | When a blank line with more than two consecutive lines is encountered, it is replaced with a blank line of one line. |
Example:
View file content
Create a file and write data
Append content to the end of the file
Expand knowledge:
cat can view files, create files, clear files, and append file content
cat is usually used to view files with small content and not much content
View the content of more files in split screen
Full name: more A text filter based on the vi editor, which displays the contents of the file page by page in a full-screen manner
Usage: more The file to be viewed
operate | effect |
---|---|
space | page down |
Enter | next row |
b | page up |
q | quit more |
= | Output the line number of the bottom line on the screen |
:f | output filename and current line number |
Ctrl +F | scroll down one screen |
Ctrl +B | scroll up one screen |
Example:
less split screen display file content
Full name: less is very similar to more, and its function is more powerful than more. When less loads files, it does not load them all at once, but loads them as needed.
Usage: less To view files
Example:
operate | effect |
---|---|
space | page down |
Enter | next row |
b | page up |
q | quit more |
G | jump to end |
g | jump to beginning |
/keyword | Find keywords n down, N up |
= | Output the line number of the bottom line on the screen |
:f | output filename and current line number |
Ctrl +F | scroll down one screen |
Ctrl +B | scroll up one screen |
Expand knowledge:
For large files, using less is more efficient than using more.
echo output content
Full name: echo output content to the console
Usage: echo [option] [output content]
options | effect |
---|---|
-and | support for escape characters |
Example:
View system variables
Output system PATH
Write environment variables to a file
Expand knowledge:
With cooperation >
>>
, the output content can be written into the file, and the file can be created if the file does not exist
head Display the header information of the file
Full name: head is used to display the content at the beginning of the file, and the first 10 lines are displayed by default
Usage: head [option] file displays the first few lines of the file
options | effect |
---|---|
-n number | Specify the number of lines to display the header of the file |
Example:
display the first 10 lines and the first 3 lines of the file
tail output file tail content
Full name: tail Display the content at the end of the file, the last 10 lines at the end of the default file
Usage: tail [option] file
options | effect |
---|---|
-n number | Specifies the number of lines to display at the end of the file |
-f | Track all updates to this document in real time |
Example:
Output the last 10 lines and the last 4 lines of the file
Expand knowledge:
tail -f 文件
It is suitable for monitoring log files, it will not exit after execution, and it will always monitor changes in content
> output redirection and >> append
Need to be used in conjunction with other commands
operate | effect |
---|---|
ls -l > file | Write the output content to the specified file, create the file if the file does not exist, overwrite the file if the file exists |
ls -al >> file | Append the output to the end of the file |
cat file1 > file2 | Overwrite the contents of file 1 to file 2 |
echo content >> file | Append the output to the specified file |
… | … |
Anything that can be output to the console can be used >
>>
to write the content into
the file. There are many corresponding operations
ln soft link
ln: link
soft links are also called symbolic links, similar to shortcuts in windows
Basic syntax
ln -s 原文件或目录 软链接名
Create a soft link to the original file
delete soft link
rm -rf 软链接名
Do not use rm -rf 软链接名/
this means that the content of the original file is deleted
Example
Create a soft link
The instructions on Linux file operations are here. The article only explains some of the most common commands and options during use, which is enough for daily operations. If there are mistakes in the article, you are welcome to point them out