search
HomeOperation and MaintenanceLinux Operation and MaintenanceOrganize a list of commonly used Linux commands (summary sharing)

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

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

# 会打开 sudoers 配置文件
sudo visudo

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

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

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

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

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

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] 属主名:属组名 文件名

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

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

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 文件或目录

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

chomd 750 index.html

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

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

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

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

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

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

chmod +x index.html

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

5. su 切换身份

# 切换为 git 用户
su git

6. whoami 显示用户名

# whoami 
root

7. pwd 显示当前目录

[git@iZ2ze www]$ pwd
/home/www

8. cd 切换工作目录

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

10. mkdir 创建目录

mkdir 创建目录:

mkdir new_folder

mkdir -p 递归创建目录:

mkdir -p one/two/three

11. touch 创建文件

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

touch new_file

12. echo 打印输出

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

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

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

echo "test content" > index.html

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

[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

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

查看文件内容:

cat ~/.ssh/id_rsa.pub

清空 index.html 内容:

cat /dev/null > index.html

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

cat index.html > second.html

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

cat index.html >> second.html

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

cat index.html second.html >> third.html

14. cp 复制文件或目录

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

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

15. mv 移动并重命名

文件改名:

mv index.html index2.html

隐藏文件:

# 文件名上加上 .
mv index.html .index.html

移动文件:

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

批量移动:

mv  /home/www/website/*  /home/www/static

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

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

17. vi/vim

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

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

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

vim index.html

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

i  切换到输入模式。

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

:  切换到底线命令模式。

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

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

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

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

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

  • w 保存文件

  • q 退出程序

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

18. ssh 远程连接工具

注意 ssh 监听是 22 端口。

其基本语法为:

ssh [OPTIONS] [-p PORT] [USER@]HOSTNAME [COMMAND]

监听端口示例:

ssh -p 300 git@8.8.8.8

打开调试模式:

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

相关推荐:《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
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

How to choose Hadoop version in DebianHow to choose Hadoop version in DebianApr 13, 2025 am 11:48 AM

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

TigerVNC share file method on DebianTigerVNC share file method on DebianApr 13, 2025 am 11:45 AM

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version