Summary and explanation of common Linux commands (super detailed version)

Foreword:

Linux  is an open source Unix-like operating system based on the Linux kernel, which was first released by Linus Torvalds on September 17, 1991. Linux is usually packaged as a Linux distribution.

Linux was originally developed for personal computers based on the Intel x86 architecture, but has since been ported to more platforms than any other operating system. Due to the dominance of Linux-based Android on smartphones, Linux (including Android) has the largest install base of all general-purpose operating systems as of May 2022.

Linux can also run on embedded systems, which are devices where the operating system is often built into the firmware and is highly customized for the system. This includes routers, automation controls, smart home devices, video game consoles, and TVs (Samsung and LG smart TVs use Tizen and WebOS respectively)

Linux is one of the most prominent examples of free and open source software collaboration. Anyone can use, modify and distribute the source code commercially or non-commercially under the terms of their respective licenses, such as the GNU General Public License (GPL). For example, the Linux kernel is licensed under GPLv2, but there is a special exception for system calls, because without a system call exception, any program that calls the kernel would be considered a derivative program, and therefore the GPL must apply to that program.

Link: https://pan.baidu.com/s/19mS5N9XJ_AotF20kUwSA3w?pwd=p5kx 
Extraction code: p5kx 
Copy this content and open the Baidu Netdisk mobile app. The operation is more convenient.


1. Simple system commands

# 查看ip地址
	ip a
	ip addr
# ping网络(测试网络连通)
	ip 目标机器的ip
# 查看系统时间
	date
# 注销
	logout
# 关机
	shutdown now
# 重启
	reboot
# 清屏
	clear

2.Liunx file system

· core

1.Linux一切皆文件
2.只有一个顶级目录,不像windows分C盘、D盘、E盘

· Directory Structure

· File meaning

Linux meaning windows
/bin Where all basic commands available to users are stored Windows does not have a fixed command storage directory
/sbin Commands that require administrator privileges
/boot Files that need to be loaded and used when the linux system starts
/dev After the peripheral is connected to Linux, the location where the corresponding file is stored Similar to the U disk in Windows, the symbol file of the CD.
/etc Home directory, every time a new user is created in Linux, a folder will be automatically assigned to the user in home. Similar to "My Documents" in Windows, each user has his own directory.
/root The home directory of the root account, only used by the root account Similar to the "My Documents" of the Administrator account in Windows
/lib Linux commands and system startup require the use of some public dependencies, which are placed in lib, similar to the jdk jar that needs to be introduced to execute the code we develop.
/usr The default installation path for many system software Similar to the Program Files directory under the C drive in Windows.

3. File management naming

Note: Commands are case sensitive
# 1. 查看文件列表
	ls [-参数1参数2] [目标文件夹]
# 查看当前目录下的文件列表
	ls
# 查看指定目录下的文件
	ls /
# 查看详细信息,元数据信息(用户、组、大小、创建时间、权限信息、文件类型)
	ls -l
# 查看隐藏文件
	ls -a 
# 参数并用
	ls -la

# 2. 切换目录
	cd 目标文件夹
# 绝对路径切换
	cd 绝对路径
# 相对路径切换
	cd 相对路径
# 例子:切换到/etc/sysconfig/networks-scripts 目录下

# 3. 查看当前命令所在的目录
[root@centos7 network-scripts]# pwd
/etc/sysconfig/network-scripts
# 特殊目录符号
	~ 当前用户的home目录
	. 当前目录
	.. 上一级目录

# 4. 新建文件夹及文件
# 在当前位置新建文件夹
	mkdir 文件夹名
# 在指定目录位置,创建文件夹,并创建父文件夹
	mkdir -p /a/b/文件夹名
# 在当前目录下新建文件
	touch 文件名

# 5. 删除文件
# 删除文件
	rm 文件
# 删除文件夹
	rm -r 文件夹
# 强制删除不询问
	rm -rf 文件

# 6. 拷贝文件
# 拷贝文件
	cp 原文件  新文件
# 拷贝文件夹
	cp -r 源文件夹 新文件夹

# 7. 移动文件或修改文件名
# 移动源文件到目标文件夹中
	mv 文件  文件夹
# 修改文件A的名字为文件B
	mv 文件A 文件B

# 8. 获取文件的md5指纹(数字签名)
md5sum 文件名
# 简介
1. 数字签名,又称数字指纹
2. 可以验证文件是否被修改
3. 一个文件通过计算得到的一串字符串,文件内容的唯一标记(文件内容不变,指纹不会变)

4. Text content viewing command

# cat命令

# 查看文件中的全部信息(适合查看小文档)
	cat 文件名

# less命令

# 以分页的方式浏览文件信息(适合查看大文档),进入浏览模式
	less 文件名
# 浏览模式快捷键
	↑ #上一行
    ↓ #下一行
    G #最后一页
    g #第一页
    空格 #下一页
    /关键词 #搜索关键词
# 退出浏览模式,回到Linux命令行模式
    q #退出

# tail 命令

# 实时滚动显示文件的最后10行信息(默认10行)
tail -f 文件名
# 显示文件的最后20行信息
tail -n 20 文件名
tail -n -20 文件名
# 显示文件信息从第20行至文件末尾
tail -n +20 文件名

5.File search

5-1. File name search

# 语法
	find 搜索路径 -name "文件名关键词"
# 例子
	find / -name "passwd"
	find / -name "ifcfg-*"

5-2. Search file content

# 语法
	grep -参数 要查找的目录范围
	# 参数
	-n 显示查找结果所在行号
	-R 递归查找目录下的所有文件
# 例子
	grep aries /etc
	grep aries /etc/passwd

6.File link

6-1.Linux file management

· Model diagram

· illustrate

# 文件名
	该文件的名字
# inode
	该文件的元数据
# datablock
	该文件真正保存的数据
Note:
1. The metadata of the file is saved in the inode.
2. The ls command views the metadata information of Linux
. 3. The real data of the file is in the data block.

6-2.Hard link

· picture

· Order

ln 源文件 硬链接文件

6-3.Soft link

· picture

· Order

ln -s 目标文件或文件夹 软连接名字

7. System management

# 静态查看系统进程
	ps -aux

# 实时查看系统进程
	top
	# 快捷键
		↑ 下翻
		↓ 上翻
		q 退出

# 关闭进程
	kill 进程id 
# 强制关闭进程(谨慎使用)
	kill -9 进程id

8.Output

# 覆盖输出
# 将命令1的执行结果,输出到后面的文件中。
`覆盖写入`
	命令1 > 文件
# 例子
	date > date.log
# 追加输出
# 将命令1的执行结果,输出到后面的文件中。
`追加写入`
	命令1 >> 文件
# 例子
	date >> date.log

9. Pipeline

# 管道
# 语法,将命令1的输出结果,作为命令2的输入
命令1 | 命令2

# 例子
查找aries用户:cat /etc/passwd | grep -n “baizhi”
查找aries组:cat /etc/group | grep -n “baizhi”
查找sshd进程:ps -aux | grep sshd

10.File editing

Configure MobaXterm’s default text editor
to avoid the trouble of vim commands

system Authority

user group

· picture

· Related commands

1. 创建组
  `groupadd 组名`
2. 删除组
  `groupdel 组名`
3. 查找系统中的组
  `cat /etc/group | grep -n “组名”`
  说明:系统每个组信息都会被存放在/etc/group的文件中

user

· Related commands

1. 创建用户
  `useradd -g 组名 用户名`
2. 设置密码
  `passwd 用户名`
3. 查找系统账户
  说明:系统每个用户信息保存在`/etc/passwd`文件中
4. 切换用户
  `su 用户名`
5. 删除用户
  `userdel -r 用户名`

Permissions

· question

In the future, the Linux system may be used by programmers, users, database administrators, project managers and other roles and levels of people. There may even be unidentified people connecting to Linux. Poor control over permissions can easily lead to system crashes, data loss and other problems. .

· Permission meaning

· ACL access control list

· Order

# 查看权限
ls -la 文件
ll 文件

# 设置文件所有者

语法:chown [-R] user名:group名 文件名
参数:-R 如果是文件夹,需要使用这个参数,可以将文件夹及其内部所有文件的所有者和组全部修改
注意:命令权限需要root
## 修改文件所有者
	chown 用户名 文件名
## 修改文件所属组
	chown :组名 文件名
## 修改文件所有者和所属组
	chown 用户名:组名 文件名
## 修改文件夹的所有者和所属组
	chown [-R] 用户名:组名 文件夹

# 权限设置1

语法:chmod u±rwx,g±rwx,o±rwx 文件名
运算符:
	- 删除权限
	+ 添加权限
	= 赋值权限
## 给文件的所有者添加执行权限
chmod u+x 文件名
## 给文件的其他人删除所有权限
chmod o-rwx 文件名
## 给文件的所属组设置读写权限
chmod g=wx 文件名

# 权限设置2

# 文件的每个归属方的权限的值使用rwx之和计算出来的。
# 语法
	`chmod [-R] nnn 文件` 
	-R 递归设置文件夹内所有文件
# 设置文件的权限为(所有者可读可写可执行,所属组可读可写,其他人可读)
	chmod 764 文件名

System software management

Compression and decompression

rpm software

Introduction: Similar to .exe program in windows
1. 安装rpm软件
  语法:`rpm -ivh xxx.rpm`
2. 查看系统中是否已安装的过该rpm软件
  语法:`rpm -qa 软件名`
3. 卸载rpm软件
  语法:`rpm -e 软件名`
4. 例子:安装tree工具
  作用:查看某个目录下的文件信息
  # 以树状结构查看2层文件信息
  tree -L 2 要查看的路径

in

Yum is implemented based on rpm. In addition to rpm's functions of installing software and uninstalling software, yum also automatically finds and downloads software, automatically handles dependencies between software, and downloads and installs dependent packages.
## 列出所有可以安装的软件包
	yum list
## 安装软件
	yum install -y 软件名
## 卸载软件
	yum remove 软件名
## 查找软件包
	yum search all 软件名

Linux services

# 例如:sshd network firewalld 等

# 服务器管理命令
	systemctl status 服务名
# 启动服务
	systemctl start 服务名
# 重启服务
	systemctl restart 服务名
# 停止服务
	systemctl stop 服务名
# 禁止服务随linux启动。
	systemctl disable 服务名
# 设置服务随linux启动。
	systemctl enable 服务名

ip settings

Service name: network
[root@centos7 dirnew]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
----------------网卡对应的文件内容---------------------
    TYPE="Ethernet"
    PROXY_METHOD="none"
    BROWSER_ONLY="no"
    BOOTPROTO="none"
    DEFROUTE="yes"
    IPV4_FAILURE_FATAL="no"
    IPV6INIT="yes"
    IPV6_AUTOCONF="yes"
    IPV6_DEFROUTE="yes"
    IPV6_FAILURE_FATAL="no"
    IPV6_ADDR_GEN_MODE="stable-privacy"
    NAME="ens33"
    UUID="0bd5d8a5-fe1b-42de-82bd-bfa7d2984b95"
    DEVICE="ens33"
    ONBOOT="yes"
    IPADDR="192.168.199.8" # 修改这里的ip地址即可
    PREFIX="24"
    GATEWAY="192.168.199.2"
    DNS1="192.168.199.2"
    DNS2="8.8.8.8"
    IPV6_PRIVACY="no"
[root@centos7 dirnew]# systemctl restart network #重启网卡服务

firewall

Service name: firewalld
# 开启防火墙
systemctl start 服务名
# 关闭防火墙(服务器重启后还会自动开启防火墙)
systemctl stop firewalld
# 禁止防火墙开机启动
systemctl disable firewalld

CPU name

# 查看主机名
hostname
# 设置主机名
hostnamectl set-hostname 主机名

IP mapping

· DNS

· Local hosts editing

[root@centos7 ~]# vim /etc/hosts
--------------下面是文件------------------
	192.168.199.8 centos7

Summary of commonly used codes

Login and logout

1)	sudo useradd lilei  //添加用户 (不能被立即使用,需设置密码 sudo passwd lilei)  
2)	sudo adduser lilei  //添加用户
3)	login  //登录或切换用户
4)	logout //注销用户(命令行)  exit(shell-退出控制台)
5)	shutdown -h 10  //10分钟后自动关机	shutdown -c  //取消
6)	halt(root用户)  //关闭所有进程后自动关机
7)	poweroff //同上
8)	shutdown -r 10 //十分钟后自动重启
9)	init 6  //重启 (0-停机,1-单用户,2-多用户,3-完全多用户,4-图形化,5-安全模式,6-重启)
10)	reboot  //重启

Directories and files

1)	pwd   //显示当前工作目录
2)	mkdir mydir  //创建工作目录
3)	cd mydir  //更改工作目录
4)	rmdir mydir //删除工作目录
5)	touch myfile  //创建文件
6)	mv myfile mydir  //移动目录或文件
7)	cp myfile myfir  //复制目录或文件
8)	rm -rf mydir  //删除目录或文件
9)	ls -l myfile  //查看文件最后被编辑时间
10)	ls -lu myfile //查看文件最后被访问时间
11)	touch -at 01011212 myfile  //修改文件最后被访问时间
12)	ls //列出所有文件和目录
13)	ls -a //查看所有文件
14)	ls -i //显示文件索引节点号
15)	ls -l //详细显示
16)	ls -m //以逗号分隔
17)	sudo apt-get install tree 
18)	tree -l//以树状图列出目录内容
19)	tree -a //所有
20)	tree -i //不以阶梯状
21)	tree -s  //列出文件或目录大小
22)	tree -t  //按更改时间
23)	file -b myfile  //显示目录或文件的详细信息
24)	stat myfile  //同上

File content display

1)	cat > myfile  //创建文件并编辑内容(ctrl+D结束编辑)
2)	cat -n myfile  //查看文件
3)	chmod [u/g/o/a][+/-/=][r/w/x] myfile  //更改文件权限
u-user,g-group,o-others,a-all  .   +-添加,--删除,=-重置   .
r-read读(4),w-write写(2),x-execute执行(1)
4)	more myfile  //分页往后显示文件(Space空格)
5)	less myfile  //分页自由显示文件(Page Down / Page Up)
6)	head (-10) myfile  //指定显示文件前若干行(默认前10)
7)	tail (-10) myfile  //指定显示文件后若干行(默认后10)

File content processing

1)	sort myfile  //对文件内容进行排序
2)	sort -r myfile  //逆序
3)	uniq myfile  //检查文件中的重复内容
4)	grep (-c)‘a’ myfile  //在文件中查找指定内容 (显示行号)
5)	diff myfile01 myfile02  //对不同文件进行比较
6)	diff3 myfile01 myfile02 myfile03  //三个文件
7)	sdiff myfile01 myfile02  //合并
8)	cmp myfile01 myfile02  //通过字节对不同文件进行比较
9)	comm myfile01 myfile02  //对有序文件进行比较
10)	cut -b(-c)(-d) 2(3) myfile  //对文件内容进行剪切
11)	paste myfile02 myfile01 //对文件内容进行粘贴 02-)01
12)	wc (-参数) myfile  //对文件内容进行统计 (c-字符数,w-单词数,l-行数)

compression

1)	zip myfile.zip myfile  //压缩
2)	zip -d myfile.zip myfile  //添加
3)	zip -m myfile.zip myfile  //删除
4)	unzip -o myfile.zip  //解压(覆盖)
5)	unzip -n myfile.zip  //解压(不覆盖)
6)	zipinfo myfile.zip  //列出压缩文件信息

Get help

1)	man ls  //获取帮助
2)	man -k ls  //不清楚完整名字
3)	whatis ls  //获取帮助
4)	help cd  / cd –help  //获取帮助 -d(简短描述) -s(用法简介)
5)	info who  //获取帮助

Other commands

1)	clear  //清楚屏幕信息
2)	echo xx  //显示文本  x=0  echo $x . echo -e \$x . echo $(pwd)
3)	date  //显示日期和时间(+%y 年  +%m 月  +%d日)
4)	cal  //显示当前日期  cal -y
5)	ps  //查看当前进程  -A(所有)  U  lilei (用户lilei)
6)	kill -9 2315  //终止某一进程  
7)	ps -ef | grep Jincheng
8)	pkill Jincheng
9)	killall Jincheng
10)	last  //显示最近登录系统的用户信息-6列
11)	history (10) //显示历史指令-默认1000行
12)	sudo adduser lilei sudo  //给普通用户赋予root权限
13)	sudo usermod -G sudo lilei  //同上
14)	alias l=’ls’  //定义命令别名
15)	unalias l  //删除别名
16)	alias  //列出别名

SSH login, password-free login

# 远程登录linux
ssh 远程linux的ip或者映射域名

# 简介
ssh登录远程linux,免去输入密码的麻烦

# 生成公钥和私钥
[root@centos7 ~]# ssh-keygen
------------执行结果-----------
    [root@centos7 ~]# tree .ssh
    .ssh
    ├── id_rsa # 私钥
    ├── id_rsa.pub # 公钥
    └── known_hosts
# 发送公钥
	`保存公钥的文件为/root/.ssh/authorized_keys`
[root@centos7 .ssh]# cat id_rsa.pub > authorized_keys
# 发送公钥
[root@centos7 .ssh]# ssh-copy-id 目标主机的ip

Guess you like

Origin blog.csdn.net/qqerrr/article/details/135324032