Home Operation and Maintenance Linux Operation and Maintenance Organize a list of commonly used Linux commands (summary sharing)

Organize a list of commonly used Linux commands (summary sharing)

Jan 05, 2022 pm 05:53 PM
linux

This article brings you a basic and sufficient Linux command, which will cover various commands used in the series of blog building articles to facilitate query and learning. I hope it will be helpful to everyone.

Organize a list of commonly used Linux commands (summary sharing)

File Owner (Owner)

When a user is created, Linux will create a The home directory, the path is /home/, we can use cd ~ to quickly enter the home directory. If you want to put a private file, you can put it in your home directory and set it so that only you can view it.

Group

Each user has a user group, which is convenient for assigning permissions to a group of people when multiple people operate. When a user is created, a user group with the same name is automatically created.

If a user belongs to multiple groups at the same time, the user needs to switch between user groups to have the permissions of other user groups.

Others

Users who are neither the file owner nor a member of the group to which the file belongs are Others.

Super User (Root)

Root user is a special type of user who can access all files.

1. adduser to add user and passwd to change password

# 添加一个名为 git 的用户
adduser git
# 设置 git 用户的密码
passed git
Copy after login

But because the created user has low permissions, sometimes we need to To elevate privileges, we can do this:

# 会打开 sudoers 配置文件
sudo visudo
Copy after login

Note that you are also editing the sudoers configuration file. Using this command will be safer than using sudo vim /etc/ sudoers. In addition to verifying the syntax, it will also Lock files during multi-user editing.

After opening the sudoers configuration file, we add this line of configuration:

# Allow git to run any commands anywhere
git ALL=(ALL:ALL) ALL
Copy after login

Briefly explain this sentence git ALL=(ALL:ALL) ALL:

git represents the rules Applied user name

  • The first ALL indicates that the rule applies to all hosts

  • The second ALL indicates that the rule applies to all users

  • The third ALL indicates that the rule applies to all groups

  • The fourth ALL indicates that the rule applies to all commands

After we save and exit, the git user will obtain root permissions.

2. ls lists files and directories

ls lists files and directories

[root@iZ2ze learn-typescript.git]# ls
branches  config  description  HEAD  hooks  index  info  objects  refs
Copy after login

ls -la by- a displays all files and directories (including hidden) and -l displays a detailed list:

[root@iZ2ze learn-typescript.git]# ls -la
总用量 20
drwxrwxr-x  7 git git  132 12月 15 12:33 .
drwx------  3 git git  127 12月 15 14:51 ..
drwxrwxr-x  2 git git    6 12月 15 12:21 branches
-rw-rw-r--  1 git git   66 12月 15 12:21 config
-rw-rw-r--  1 git git   73 12月 15 12:21 description
-rw-rw-r--  1 git git   23 12月 15 12:21 HEAD
drwxrwxr-x  2 git git 4096 12月 15 13:10 hooks
-rw-rw-r--  1 git git  217 12月 15 12:33 index
drwxrwxr-x  2 git git   21 12月 15 12:21 info
drwxrwxr-x 10 git git   90 12月 15 12:33 objects
drwxrwxr-x  4 git git   31 12月 15 12:21 refs
Copy after login

Each line has 7 columns. We use branches as an example to explain the meaning of each column:

Organize a list of commonly used Linux commands (summary sharing)

Focus on the content of column 1, taking drwxrwxr-x as an example. There are 10 digits in total. The first digit represents the file type, where - represents an ordinary file and d represents a directory file.

The 2nd to 4th digits represent owner permissions, where r represents read permissions, w represents write permissions, x represents executable permissions, - represents no permissions, and the 2nd to 5th digits are rwx, which represents all It can be read, written and executed.

The 5th to 7th digits represent group user permissions, here they are also rwx.

The 8th to 10th bits indicate other user permissions. Here is r-x, which indicates readable and executable permissions but no write permissions.

One more thing to add here:

The default permissions for a root user to create a folder are rwxr-xr-x:

[root@iZ2ze www]# mkdir test
[root@iZ2ze www]# ls -l
drwxr-xr-x  2 root root  6 12月 17 23:53 test
Copy after login

and the default permissions for a file to be created are rw- r--r--, note that creating a file will remove the x permission by default:

[root@iZ2ze www]# touch index.html
[root@iZ2ze www]# ls -l
-rw-r--r--  1 root root  0 12月 17 23:54 index.html
Copy after login

This is why we sometimes need to add execution permissions after creating the file.

3. chown changes the file owner, and you can also change the file group at the same time

chown (change owner) Syntax:

# -R:递归更改文件属组
chown [–R] 属主名 文件名
chown [-R] 属主名:属组名 文件名
Copy after login

Change the owner of index.html to git:

[root@iZ2ze www]# chown git index.html
[root@iZ2ze www]# ls -
-rw-r--r-- 1 git  root  0 12月 17 23:54 index.html
Copy after login

Change the owner and group of index.html to git:

[root@iZ2ze www]# chown git:git index.html
[root@iZ2ze www]# ls -l
-rw-r--r-- 1 git  git   0 12月 17 23:54 index.html
Copy after login

4. chmod changes file permissions

In addition to using r w x, permissions can also be expressed as numbers. The corresponding relationship between arrays and letters is:

  • r:4

  • w:2

  • x:1

All such correspondences are mainly for the convenience of derivation. For example, if we want a file to be readable and writable, then we can easily set the permission to 6 (4 2). Similarly, if we know that a permission is 3, we can also deduce The exit permission is writable and executable, because only 2 1 can be equal to 3.

Let’s take a look at the specific syntax of chmod (change mode):

# -R:递归更改文件属组
chmod [-R] xyz 文件或目录
Copy after login

xyz represents the permissions of Owner, Group, and Others respectively. If we set the permissions of a file like this:

chomd 750 index.html
Copy after login

We can know that the Owner's permission is 7, which means it can read, write and execute, the Group's permission is 5, which means it can read and execute, and the Others' permission is 0, which means it cannot read, write and execute. The corresponding letters are: rwxr-x---.

In addition to this numerical method, there is also a way to change permissions using symbolic types:

在这种方式里,我们将三种身份 Owner、Group、Others,分别简写为 u(User)、g、o,用 a 表示所有身份,再使用 + - = 表示加入、去除、设定一个权限,r w x 则继续表示读,写,执行权限,举个例子:

chomd u+x,g-x,o-x index.html
Copy after login

意思就是 Owner 加上执行权限,Group 和 Others 去除执行权限。

当然我们也可以直接设定权限

chmod u=rwx,g=rx,o=r index.html
Copy after login

此时文件的权限就相当于 -rwxr-xr--。

此外,我们还可以省略不写 ugoa 这类身份内容,直接写:

chmod +x index.html
Copy after login

此时相当于使用了 a,会给所有身份添加执行权限。

5. su 切换身份

# 切换为 git 用户
su git
Copy after login

6. whoami 显示用户名

# whoami 
root
Copy after login

7. pwd 显示当前目录

[git@iZ2ze www]$ pwd
/home/www
Copy after login

8. cd 切换工作目录

# 进入 /home/www/
cd /home/www
# 进入自己的主目录
cd ~
# 进入当前目录的上上两层 :
cd ../..
Copy after login

10. mkdir 创建目录

mkdir 创建目录:

mkdir new_folder
Copy after login

mkdir -p 递归创建目录:

mkdir -p one/two/three
Copy after login

11. touch 创建文件

用于修改文件或者目录的时间属性,当文件不存在,系统会创建空白文件

touch new_file
Copy after login

12. echo 打印输出

echo 是 Shell 命令,用于打印输出:

# 显示转义字符
echo "\"test content\""
Copy after login

创建或覆盖文件内容为 "test content":

echo "test content" > index.html
Copy after login

如果是想追加内容,就用 >> :

[root@iZ2ze www]# echo "test content" > index.html
[root@iZ2ze www]# cat index.html
test content
[root@iZ2ze www]# echo "test content" >> index.html
[root@iZ2ze www]# cat index.html
test content
test content
Copy after login

13. cat 连接文件并打印输出

查看文件内容:

cat ~/.ssh/id_rsa.pub
Copy after login

清空 index.html 内容:

cat /dev/null > index.html
Copy after login

把 index.html 的内容写入 second.html:

cat index.html > second.html
Copy after login

把 index.html 的内容追加写入 second.html:

cat index.html >> second.html
Copy after login

把 index.html 和 second.html 追加写入 third.html:

cat index.html second.html >> third.html
Copy after login

14. cp 复制文件或目录

将目录 website/ 下的所有文件复制到新目录 static 下:

# -r:若给出的源文件是一个目录文件,此时将复制该目录下所有的子目录和文件。
cp –r website/ static
Copy after login

15. mv 移动并重命名

文件改名:

mv index.html index2.html
Copy after login

隐藏文件:

# 文件名上加上 .
mv index.html .index.html
Copy after login

移动文件:

# 仅仅移动
mv  /home/www/index.html   /home/static/
# 移动又重命名
mv /home/www/index.html   /home/static/index2.html
Copy after login

批量移动:

mv  /home/www/website/*  /home/www/static
Copy after login

16. rm 删除一个文件或者目录

# 系统会询问
rm file
# -f 表示直接删除
# -r 表示目录下的所有文件删除
# 删除当前目录下的所有文件及目录
rm -r  * 
# 跑路
rm -rf /*
Copy after login

17. vi/vim

Linux 内建 vi 文书编辑器,Vim 是从 vi 发展出来的一个文本编辑器。

基本上 vi/vim 共分为三种模式,分别是命令模式(Command mode),输入模式(Insert mode)和底线命令模式(Last line mode)。我们边操作边介绍这三种模式:

我们执行 vim index.html,如果没有该文件,则会创建文件:

vim index.html
Copy after login

此时是命令模式,在命令模式下,输入的任何字符都会被视为命令,接下来几个常用的命令:

i 切换到输入模式。

x 删除当前光标所在处的字符。

: 切换到底线命令模式。

我们按下 i,便会进入输入模式

输入模式下,左下角有 -- INSERT -- 标志:

此时我们可以进行各种输入,当输入完毕后,按下 ESC 回到命令模式

此时左下角的 INSERT已经消失不见了,如果我们要保存退出,我们先输入 : ,进入底线命令模式

在底线命令模式中,常见的命令有

  • w 保存文件

  • q 退出程序

我们输入 wq,表示保存并退出,此时我们就会发现并创建了一个 HTML 文件。

18. ssh 远程连接工具

注意 ssh 监听是 22 端口。

其基本语法为:

ssh [OPTIONS] [-p PORT] [USER@]HOSTNAME [COMMAND]
Copy after login

监听端口示例:

ssh -p 300 git@8.8.8.8
Copy after login

打开调试模式:

# -v 冗详模式,打印关于运行情况的调试信息
ssh -v git@8.8.8.8
Copy after login

相关推荐:《Linux视频教程

The above is the detailed content of Organize a list of commonly used Linux commands (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

What to do if the docker image fails What to do if the docker image fails Apr 15, 2025 am 11:21 AM

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

See all articles