Linux command learning record notes

Linux Getting Started Commands

1. Create a directory
mkdir myqxin   	// 创建一个名为myqxin的目录
2. Create a blank file
touch myqxin.txt	// 创建一个名为myqxin的txt文件
3. Path selection
cd /home/myqxin/	// 指定进入/home/myqxin目录下
cd ..	// 返回上一级目录
cd -	// 返回上一次进入的目录位置
cd ~	// 返回家目录
4. Delete a file
rm myqxin.txt	// 删除名为myqxin的txt文件
5. Delete a directory
rmdir myqxin	// 删除名为myqxin的目录(目录下没有文件)
rm -d myqxin	// 删除名为myqxin的目录(目录下没有文件)
rm -rf myqxin   // 删除名为myqxin的目录包含目录下的所有文件
6. Copy directories and files
# -r 参数是复制目录,复制文件不需要加-r
cp -r myqxin zfwy	// 复制myqxin目录为zfwy
cp myqxin.txt zfwy.txt	// 复制myqxin.txt文件为zfwy.txt
7. Move or rename directories and files
mv myqxin test		// 移动myqxin目录到test目录,将当前目录移动到当前目录,就相当于重新命令
mv myqxin.txt zfwy.txt		// 移动myqxin.txt文件到zfwy.txt文件,将当前目录文件移动到当前目录文件,就相当于重新命令
8. View the entire contents of the file
cat /home/myqxin.txt	// 查看/home目录下的myqxin.txt文件内容
cat -n /home/myqxin.txt		// 查看/home目录下的myqxin.txt文件内容,并标注了行号
9. View the partial content of the file
head /home/myqxin.txt		// 查看/home目录下的myqxin.txt文件,head默认展示前十行内容
head -n 22	/home/myqxin.txt		// 查看/home目录下的myqxin.txt文件前22行内容,-n 用来指定显示多少行内容
tail /home/myqxin.txt		// 查看/home目录下的myqxin.txt文件,head默认展示后十行内容
tail -n 22	/home/myqxin.txt		// 查看/home目录下的myqxin.txt文件后22行内容,-n 用来指定显示多少行内容
tail -f /home/myqxin.txt	// 查看/home目录下的myqxin.txt文件后10行内容,-f 参数会实时更新,动态查看
10. Use vi to edit file content
vi myqxin.txt	// 打开myqxin.txt文件,按a或者i进入编辑状态
# 退出编辑状态,按Esc,然后输入
:q(退出不保存本次编辑内容)
:wq(退出并保存本次编辑内容)
:q!(强制退出不保存本次编辑内容)
:wq!(强制保存并退出本次编辑内容)
11. Find files
# find 搜索范围 选项 目标文件
find /etc/ -name myqxin.txt		// 查找/etc/目录下的名为myqxin.txt文件
# find 目录 -user 指定用户名
find /etc -user root
# find 目录 -size 文件大小
find /etc -size 1M
# -name、-user、-size组合使用
find /etc/ -name myqxin.txt -size 1M	// 查找/etc/目录下的名为myqxin.txt文件并且文件大小为1M
12. Create a shortcut
# ln -s 源文件或目录 软连接名
ln -s /usr /home/myqxin/myusr	// 在/home/myqxin/目录下创建一个软连接myusr连接到/usr目录
# 创建软连接,如果源文件或目录被删掉,快捷方式无法使用
-s 创建软连接必须加上(目录必须加,如果是文件不加-s参数就相当于复制并不能称为软连接)
13. Compress a file
# gzip只能将文件压缩成*.gz格式
gzip myqxin.txt	// 将myqxin.txt文件压缩成myqxin.txt.gz压缩文件

tar -zcvf myqxin.tar myqxin.txt		// 将myqxin.txt文件压缩成myqxin.tar压缩文件
tar -zcvf myqxin.tar.gz myqxin.txt		// 将myqxin.txt文件压缩成myqxin.tar.gz压缩文件
14. Unzip a file
gunzip myqxin.txt.gz	// 将myqxin.txt.gz文件解压成myqxin.txt文件

tar -zxvf myqx.tar		// 将myqxin.tar文件进行解压
tar -zxvf myqxin.tar.gz		// 将myqxin.tar.gz文件进行解压
15. Tips to improve efficiency
# table键:补全命令和补全文件的功能
table
# history:查看已经执行过的命令
history
# 上下键:返回上一次距离最近命令(通常是先进行了上键,才能配合下键使用)
↑ ↓

Linux advanced commands

1. Add new users
# useradd 用户名
# sudo 临时获取管理员权限,如果是root用户来创建用户就不需要加这个
sudo useradd myqxin		// 新增一个名为myqxin的用户,并且会在/home/目录下生成一个跟用户名相同的目录(默认是在/home/下生成)
sudo useradd myqxin -d /home/zfwy/myqxin		// 新增一个名为myqxin的用户,并且会在/home/zfwy/目录下生成一个跟用户名相同的目录
2. Set a password for the user
# sudo 临时获取管理员权限,如果是root用户来设置用户密码就不需要加这个
sudo passwd myqxin		// 为myqxin用户设置密码(如果passwd后面不指定用户,默认会为当前登录用户设置密码)

The system will then prompt you to set a password and then confirm the password. After success, it will prompt:

passwd: all authentication tokens updated successfully.
3. Switch users
su - myqxin		// 切换到myqxin用户(如果提示输入密码,输入即可)
# 查看当前登录的用户
whoami
# 退出当前用户
exit
4. Delete user
# -r 表示在删除用户的同时删除用户的家目录
sudo userdel -r myqxin		// 删除用户名为myqxin并且删除它的目录
sudo userdel myqxin		// 删除用户名为myqxin,不删除它的目录

Basic user information: stored in the /etc/passwd file
User password information: stored in the /etc/shadow file
Basic user group information: stored in the /etc/group file
User group information: stored in /etc/gshadow In the file,
the home directory is located by default at /home/username

5. Display user UID and GID
# id 不指定用户名,会显示当前登录用户的信息
id root		// 查看root用户的UID和GID
#能看到uid(用户ID)、gid(初始组ID), groups是用户所在组,这里既可以看到初始组,如果有附加组,则也能看到附加组
uid=0(root) gid=0(root) groups=0(root)
6. Add new user group
sudo groupadd myqxin	// 新增用户组myqxin
# 查看用户组信息
cat /etc/group
7. Delete user group
sudo groupdel myqxin	// 删除用户组myqxin
8. Add or remove users from user groups
# -a 将用户加入到用户组中
sudo gpasswd -a myqxin zfwy		// 将用户myqxin加入到zfwy用户组
# -d 将用户从用户组中移出
sudo gpasswd -d myqxin zfwy		// 将用户myqxin从zfwy用户组移出
9. Basic introduction to file permissions
# 查看文件权限
ls -l
drwxr-xr-x 2 csdn csdn 4096 83  2021 Code

Insert image description here
For file types and permissions, except for the first parameter marked as file type, there are three parameters in a group (excluding the number of links and subsequent content). For example: rwx is a group, rx
is a group, and rx is A group

Insert image description here

10. Modify file permissions
# chomd 修改权限,加三个参数,每个参数最高为7,最低为1
chmod 777 myqxin	// 将myqxin目录设置为最高级别权限(拥有者,所属用户组,其他用户都是顶级)
# 读取权限用数字4表示,写权限用数字2表示,执行权限用数字1表示
r=4 w=2 x=1
# 给读写执行权限
rwx=4+2+1=7
# 给读写权限
rw=4+2=6
# 给读执行权限
rx=4+1=5
# 给写执行权限
wx=2+1=3


#思路:drwxr-xr-x 2 csdn csdn 4096 8月   3  2021 Code
#1.所有者权限不变。数值相加为7
#2.组权限不变。数值相加为5
#3.其它所有者权限要变更,数值相加为5
#4,前面说过文件类型和权限那一块,三个参数为一组,每一组相加最高为7,所有说我们最高的权限为777  rwxrwxrwx
结果:755

Demonstration effect: This only changes the owner's permissions, and the user group and other user permissions will not be modified.

Insert image description here

11. Modify file owner
# chown 用户名 目录或文件名
sudo chown myqxin /home/myqxin.txt		// 修改myqxin.txt文件所有者为myqxin
sudo chown -R myqxin /home/myqxin		// 修改myqxin目录及该目录下所有文件的所有者为myqxin(仅修改目录不需要加-R)
12. Modify the user group to which the file belongs
# chgrp 用户名 目录或文件名
sudo chgrp myqxin /home/myqxin.txt		// 修改myqxin.txt文件所属用户组为myqxin
sudo chgrp -R myqxin /home/myqxin		// 修改myqxin目录及该目录下所有文件的所属用户组为myqxin(仅修改目录不需要加-R)
13. View all processes in the system
# aux参数的含义  
# a 显示现行终端机下的所有程序,包括其他用户的程序
# u 以用户为主的格式来显示程序状况
# x 显示所有程序,不以终端机来区分
ps aux
USER   PID %CPU %MEM  VSZ   RSS   TTY   STAT START  TIME COMMAND
csdn    1  0.0  0.1  40352  5040 pts/0  Ss+  22:09  0:00 /bin/zsh
logo meaning
USER Which user created this process?
PID Process ID
%CPU The percentage of CPU resources occupied by this process, the higher the percentage occupied. The process consumes more resources
-%MEM- The percentage of physical memory occupied by this process. The higher the percentage occupied. The process consumes more resources
VSZ The size of the virtual memory occupied by this process, in KB
RSS The actual physical memory size occupied by the process, in KB
TTY In which terminal the process is running. Among them, tty1~tty7 represent the local console terminal (you can switch to different terminals through the alt+F1 ~ F7 shortcut keys). tty1 tty6 is a local character interface terminal, and tty7 is a graphical terminal. pts/0 255 represents a virtual terminal, usually a remote connection terminal. The first remote connection occupies pts/0, the second remote connection occupies pts/1, and they increase in order.
STAT process status
START The start time of the process
TIME This process occupies CPU computing time, please note that it is not system time.
COMMAND The command name that spawned this process
14. Real-time monitoring of program operation
# 默认每隔3秒刷新一次
top

# ? 或 h:显示交互模式的帮助
# P:按照 CPU 的使用率排序,默认就是此选项
# M:按照内存的使用率排序
# N:按照 PID 排序
# T:按照 CPU 的累积运算时间排序,也就是按照 TIME+ 项排序
# k:按照 PID 给予某个进程一个信号。一般用于中止某个进程,信号 9 是强制中止的信号
# r:按照 PID 给某个进程重设优先级(Nice)值
# q:退出 top 命令

Insert image description here

The red box part: displays the resource usage status of the entire system. We use this output to judge the resource usage status of the server. The
yellow box part: displays the information of the processes in the system.

Insert image description here

The first line is task queue information:

content illustrate
16:51:31 System current time
up 9 min System running time, this machine has been running for 9 minutes
0 users There are currently 0 users logged in
load average:0.08,0.04,0.00 The average load of the system in the previous 1 minute, 5 minutes, and 15 minutes

The second line of process information:

content illustrate
Tasks: 4 total Total number of processes in the system
1 running Number of running processes
3 sleeping Number of sleeping processes
0 stopped Number of processes being stopped
0 zombie Number of zombie processes. If it is not 0, you need to manually check the zombie process

The third line of CPU information:

content illustrate
%Cpu(s): 0.5 us CPU percentage occupied by user mode
0.5 and CPU percentage occupied by system mode
It is 0.0 The percentage of CPU occupied by user processes that have changed priorities
99.0 id CPU percentage occupied by idle CPU
0.0 of Percentage of CPU occupied by processes waiting for input/output
0.0 hi CPU percentage occupied by hard interrupt request service
0.0 and CPU percentage occupied by softirq request service
0.0 st st (steal time) means virtual time percentage

第四行为物理内存信息:

内容 说明
Mem : 4301840 total 物理内存的总量,单位为KB
4054420 free 已经使用的物理内存数量
119156 user 空闲的物理内存数量。我们使用的是虚拟机,总共分配了628MB内存,所以只有119MB的空闲内存
128264 buff/cache 作为缓冲的内存数量

第五行为交互分区(swap)信息:

内容 说明
Swap: 0 total 交换分区(虚拟内存)的总大小
0 free 已经使用的交换分区的大小
0 user 空闲交换分区的大小
4115616 avail Mem 作为缓存的交换分区的大小

第六行为系统进程资源具体使用情况(默认按照CPU的占用量排序):

内容 说明
PID 进程ID,我们杀掉某个进程就可以使用kill -9 PID
USER 进程所有者的用户名
PR 优先级
NI nice值(负值表示高优先级,正值表示低优先级)
VIRT 进程使用的虚拟内存总量(KB)
RES 进程使用的未被换出的物理内存大小(KB)
SHR 共享内存大小(KB)
S 进程状态
%CPU 进程上次更新到现在的CPU时间占用百分比
%MEM 物理内存占比
TIME+ 进程使用的CPU时间总计(单位1/100秒)
COMMAND 命令名
15,定时任务
# 1,编辑 crontab 文件
sudo crontab -e
# 2,写入执行命令并保存
*/1 * * * * /bin/echo "myqxin" >> /home/myqxin/test.txt	// 向/home/myqxin/目录下的test.txt文件每隔一分钟写入myqxin内容
# 3,使用tail命令验证
tail -f /home/myqxin/test.txt

查看任务列表

crontab -l

查看任务状态

crontab status

Linux实用命令

1,grep命令
  • 使用grep来查找文本内容:
# 查找文件myqxin.txt中含有"hello"字符串的行
grep hell myqxin.txt
# 查找文件myqxin.txt中没有"hello"字符串的行
grep -v hello myqxin.txt
# 查找文件myqxin.txt中含有“hello”字符串的行以及它前面的3行
grep -v hello -B 3 myqxin.txt
# 查找文件myqxin.txt中含有“hello”字符串的行以及它后面的3行
grep -v hello -A 3 myqxin.txt
# 查找文件myqxin.txt中含有“hello”字符串的行以及它前后的3行
grep -v hello -C 3 myqxin.txt
2,awk命令
  • 使用awk来过滤文本:

awk ‘BEGIN{ commands } pattern{ commands } END { commands }’ file’

BEGIN{ commands } 指定最开始执行的脚本
pattern{ commands } 对文件的每一行遍历,判断是否满足pattern的模式,如果满足则执行脚本
END{ commands } 指定最后执行的脚本

# 打印“开始”,打印每行,打印“结束”
awk 'BEGIN{ print "开始" }{ print } END { print "结束" }' myqxin.txt
# 打印每行的行号
awk '{print NR}' myqxin.txt
# 打印每行的文本
awk '{print $0}' myqxin.txt
# 打印每行的第一列(默认用空格分隔)
awk '{print $1}' myqxin.txt
# 打印每行的最后一列(默认用空格分隔)
awk '{print $NF}' myqxin.txt
# 打印每行的倒数第二列(默认用空格分隔)
awk '{print $(NF-1)}' myqxin.txt
# 打印每行,并为每行带上行号
awk '{print NR":",$0}' myqxin.txt
3,sed命令
  • 使用sed命令来替换或删除文本行
# 查找并替换每行第一个“hello”为“你好”
sed 's/hello/你好/' myqxin.txt 
# 查找并替换每行中所有的“hello”为“你好”
sed 's/hello/你好/g' myqxin.txt
# 删除含有“hello”的行
sed '/hello/d' myqxin.txt
# 删除第二行
sed '2d' myqxin.txt
# 删除第三行到第五行
sed '3,5d' myqxin.txt
4,losf命令
  • The losf command can be used to view "processes with open files", "files opened by processes," "TCP or UDP ports opened by processes", "processes occupying a certain TCP or UDP port", etc. The most commonly used ones are as follows:

-a: List the processes that have open files
-c <process name>: List the files opened by the specified process
-d <file number>: List the processes occupying the file number
-p <process number>: List the specified File opened by process number
-i<condition>: List processes that meet the conditions (protocol, port, @IP)

# 查看那个进程占用了8888端口
lsof -i:8888
# 列出进程10532所打开的文件
lsof -p 10532
# 监听tcp链接进程信息
lsof -i tcp
# 查看占用了tcp端口9999的进程
lsof -i tcp:9999
5. df and du commands
  • Use df and du together to check the space usage

The df command is used to check the space usage of the disk. Select -h to indicate K, M, G as the unit to improve the readability of the information. The
du command is used to check the space usage of the directory. By default, all subdirectories will be counted. The option -h means to use K, M, G as the unit to improve the readability of the information, and the option -s means to display only statistical summary information.

# 查看系统磁盘的空间占用情况
df -h
# 查看指定目录系统磁盘的空间占用情况
df -h /home/
# 查看指定目录的空间占用情况
du /home/ -h
# 查看指定目录的空间占用情况,只显示汇总信息
du /home/ -sh
# 查询指定目录下的直接子目录空间占用情况,只显示汇总信息
du /var/*/ -sh
6. tcpdump command
7. netstat command
8. cURL and wget commands
9. Pipeline

Common LInux commands

1. Check the directory that takes up the most memory.
du -h -x --max-depth=1
2. Query the specified port process
netstat -lnp|grep 80

Guess you like

Origin blog.csdn.net/qq_45752401/article/details/125637731
Recommended