[System] Summary of Linux commonly used commands (novice friendly)
Remarks
2020/9/22 Tuesday
I haven't written a blog for a long time. Today, on a whim, I decided to summarize the Linux commands that I have used for a long time.
One, directory commands
1.ls
ls
This is the most commonly used command in Linux to display the files at the current location. He will simply list the files in the current directory.
But sometimes we wantView file for more detailed information, This time we canAdd -l parameter to ls
ls -l
Some Linux versions will provide ll as an abbreviation of ls -l
ll
Sometimes we wantView hidden files of the system, We just needUse ls -aThat's it.
ls -a
1.cd
After viewing the directory with ls, we generally have to switch the directory location. At this time, we will use the cd command.The cd command is used to enter a folderFor the command, we only need to cd the name of the folder just listed by ls. We call this method a relative path.
cd [文件夹]
In Linux, use. To indicate the current location, and use .. to indicate the directory above the current location. and soUse cd when you want to return to the previous directory..
cd ..
In theory, we only need to use the cd command with the ls command to reach any folder in Linux, but in practice this may waste a long time, so Linux also provides another more direct way, the absolute path.
Before talking about absolute paths, we first need to briefly understand the basic structure of Linux directories.
Unlike Windows, Linux does not have disk directories such as c drive and d drive.All the files in Linux are under /, and / is called the root directory, The root directory generally has the following directories
bin | boot | dev | home | lib | where | etc | root | user | run |
---|
These folders have different functions, so I won't repeat them here. We can enter directly by adding a full path starting from the root directory through the cd command. Such as: LinuxEach user has a directory with the same name as the user under the home directory called the home directory, I use linux as the user name for example
cd /home/linux
You can enter the home directory.
Since most of our operations are performed in the home directory, we need to enter the home directory frequently. Linux also provides us with a more convenient way
cd ~
This command can directly enter the home directory.
2.pwd
Linux provides us with a command to view the current location
pwd
Second, the executable file
1. ./
When ourCurrent directoryWhen there is an executable file, we canRun it with ./ and file name (no spaces)
./[文件名]
2.file
Different from the obvious prompt ending with .exe as the suffix in Windows,Linux executable files generally have no suffix,Linux executable file is ELF, When we want to know the type of a file, we can useUse the file command to view the detailed information of the file
file [文件名]
Three, create and delete
1. Create a file
touch [文件名]
Add one with the touch commandFile name that does not existCan be in the current folderCreate an empty file
If you touch one in the current folderExisting fileWillUpdate the last modification date of this file。
2. Create a folder
mkdir [文件夹名]
mkdir can create a directory that does not exist, orTo create a series of directories continuously, you only need to add the -p parameter
mkdir -p [文件夹/文件夹/.../文件夹]
3. Delete
Delete command in Linux is a more dangerous command, because you can even delete the entire operating system, and this situation may really happen when you enter the wrong command. The delete operation can be performed with a simple rm.
rm [文件名]
The deleted one can also be an empty folder, if you wantTo delete a non-empty folder, you need to add the -r parameter
rm -r [文件夹]
However, in this way, the operating system generally asks whether to delete the files in the folder one by one. When there are many files in the folder, it becomes very troublesome, so we canAdd the -f parameter to force deletion without asking。
rm -rf [文件夹]
Be careful at this time, because if you accidentally type the order like this
rm -rf /文件夹/...
You may delete the root directory.
Four, view files
1.cat
When we have a file that we want to view, we generally use the cat command to view it
cat [文件名]
In addition to simple viewing, the cat command also provides some diversified options,
parameter | Explanation |
---|---|
-n | Number each row starting from 1 |
-s | Replace two or more consecutive blank lines with one line display |
2.tac
Linux also provides us with a very interesting command tac, the name of this command is the reverse of cat, and its role is to reverse the output and display of the file.
tac [文件名]
He also has a parameter,-b can be used to display a separator on each line。
3.more
4.less
Five, file permissions
In Linux, every file has strict permission requirements. When your user does not have the corresponding permissions, you will not be able to perform some operations. General files have to be modified, accessed, and executed. They are represented by w, r, and x respectively, and-represents no authority. Similarly, each directory also has three permissions. w represents the permission to use ls, r represents the permission to create and delete in the directory, and x represents the permission to use ls-l. When we want to modify the permissions of a file or directory, we need to use the chmod command.
chmod [用户][操作][权限] [文件名]
chmod has three parameters, you can fill in the following respectively
user | Description |
---|---|
u | File owner |
g | User group to which the owner of the file belongs |
O | All other users |
a | All users |
operating | Description |
---|---|
+ | Add to |
- | Remove |
= | Endow |
Authority | Description |
---|---|
r | read |
w | write |
x | carried out |
For example: want to add execute permission to a file for all users
chmod a+x [文件名]
For example: remove the modification permission for the user group where the file owner belongs
chmod g-w [文件名]
For example: directly grant the file owner read-write and execute permissions
chmod u=rwx [文件名]
In the chmod command, [user] can be left blank, and the system will default to the file owner u.
For example: adding execute permission for the file owner can be abbreviated
chmod +x [文件名]
Modifying permissions in this way is simple and intuitive, but there is a small problem. When we want to grant different permissions to different users at the same time, we need to repeat this command many times. Linux provides us with another way to modify permissions to solve this problem.
For the four permissions, we give them a value
Authority | value |
---|---|
r | 4 |
w | 2 |
x | 1 |
- | 0 |
It is not difficult to see that when we add any combination of these four permissions and the results are unique, we can get the following table
Authority | value |
---|---|
r w x | 7 |
r w - | 6 |
r - x | 5 |
r - - | 4 |
- w x | 3 |
- w - | 2 |
- - x | 1 |
- - - | 0 |
In this way, we can directly use a number to represent the status of the three permissions, and arrange the three numbers in the order of u, g, and o to grant permissions to different users with only one chmod.
For example: give all users all permissions
chmod 777 [文件名]
In addition, when we use the ls-l command, we will find that in addition to three sets of wrx permissions for each file, there is a letter or-before the permissions, which is the type identifier of the file
Identifier | Types of |
---|---|
- | Normal file |
d | table of Contents |
c | Character device file |
b | Block device file |
l | Symbolic link file |
Six, user group
在我们使用Windows的时候似乎从来没有关心过是否拥有一个文件或目录的权限,但是Linux不同,每一个用户都拥有不同的权限。因为Linux是一个可以实现多用户同时登陆的操作系统,多个用户可能会共享一些资源,但他们也需要独立的空间和权限,不同的用户不可以随意查看修改或执行其他用户的文件。