rm 删除
删除所有文件,以下保留(参考):
rm !(textfile.txt|backup.tar.gz|script.php|database.sql|info.txt)
cut 截取
显示第1到第5个字符,第10到第20个字符:
$ who | cut -c 1-5,10-20
egrep 正则匹配
显示一行最后几个字符:
$ echo '1234567890' | egrep -o '.{1,5}$'
watch 监控变化
可以动态监控变化:
watch -n1 -d 'kubectl top pod --sort-by=cpu -A'
watch -n1 -d 'date'
其中-d表示显示变化的差异内容,-n指观察间隔,后面跟数字,-n1表示1秒观察一次。
Mac没有该命令,可以通过brew install watch安装,参考osx watch。
df 硬盘空间
查看文件系统硬盘空间使用情况:
df -h
du 文件空间
查看文件空间使用情况:
du -sh ./*
top 进程信息
显示系统进程信息,与CPU和内存。
find 查找文件
查找文件:
$ find ./ -name "jdk*"
./jdk-8u131-linux-x64.tar.gz
./jdk1.8.0_131
查找文件,并作为后面命令的输入:
$ find . -name "*.zip" | grep jmeter| xargs unzip -l
readlink
读取链接指向的目标值:
$ readlink -f home
/System/Volumes/Data/home
id 用户id
查看用户ID,群组ID。
nc 检测端口
检测端口是否可提供服务。可指定一个或多个端口,还可以指定端口范围。
$ nc -zv 127.0.0.1 80-89
Connection to 127.0.0.1 port 80 [tcp/http] succeeded!
nc: connectx to 127.0.0.1 port 81 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 82 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 83 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 84 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 85 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 86 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 87 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 88 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 89 (tcp) failed: Connection refused
telnet 检测网络连通
检测某个服务器的某个端口:
$ telnet www.nnjskz.cn 443
$ telnet 127.0.0.1 443
diff 对比文件
对比文件。
diff file1 file2
diff --brief --recursive dir1/ dir2/
systemctl相关
# 重新加载所有service服务
systemctl daemon-reload
# 开机启动
systemctl enable xxxx
# 查看该service是否开机启用
systemctl is-enabled xxxx
# 查询开机启动项服务
systemctl list-unit-files
# 过滤启动项
systemctl list-unit-files | grep enable
# 过滤未启动项
systemctl list-unit-files | grep disabled
# 过滤某个服务
systemctl list-unit-files | grep xxxx
# 启动该服务
systemctl start xxxx
# 查看该服务状态
systemctl status xxxx
# 重启服务
systemctl stop xxxx
# 停止服务状态
systemctl stop xxxx
DNS相关
主要有:nslookup/dig命令
Server: 198.18.0.2
Address: 198.18.0.2#53
Non-authoritative answer:
Name: www.nnjskz.cn
Address: 198.18.0.17
jensen@JENSENdeMacBook-Pro ~ % dig www.nnjskz.cn
; <<>> DiG 9.10.6 <<>> www.nnjskz.cn
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17101
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.nnjskz.cn. IN A
;; ANSWER SECTION:
www.nnjskz.cn. 1 IN A 198.18.0.17
;; Query time: 0 msec
;; SERVER: 198.18.0.2#53(198.18.0.2)
;; WHEN: Fri Mar 21 10:09:40 CST 2025
;; MSG SIZE rcvd: 47
useradd 添加用户
可以使用以下命令添加用户:
useradd [OPTIONS] USERNAME
只有root用户或者有sudo权限的用户才可以执行。
添加用户的默认配置在文件/etc/default/useradd里,可通过以下命令查看:
$ useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
添加完如果需要设置密码:
$ passwd username
创建用户的Home目录(-m/--create-home):
$ useradd -m username
创建的目录是/home/username
指定用户目录:
如果不想使用默认在/home下创建目录,则可通过(-d/--home)指定:
$ useradd -m -d /opt/username username
指定用户ID(-u/--uid):
$ useradd -u 2000 username
指定群组(-g/--gid):
$ useradd -g users username
加到多个groups:
$ useradd -g users -G wheel,developers username
指定登陆shell(-s/--shell):
useradd -s /bin/bash username
指定失效日期(-e/--expiredate):
useradd -e 2021-10-10 username
可通过以下命令查看失效日期:
$ chage -l username
更多请参考:useradd
zip/unzip 解压
压缩:
$ zip my.zip file1 file2 file3
$zip -r my.zip folder1
添加文件到压缩文件:
$ zip -u my.zip file4
删除压缩文件里的文件:
$ zip -d my.zip file4
解压到特定目录:
$ unzip file.zip -d destination_folder
解压特定文件到特定目录:
$ unzip file.zip filename -d destination_folder
解压到标准输出:
$ unzip -p file.zip filename
不解压文件查看:
$ unzip -l my.zip
更多请参考:zip/unzip
xmllint读取xml文件
$ xmllint --xpath "//*[local-name()='project']/*[local-name()='modules']" pom.xml
<modules>
<module>springboot-common</module>
<module>spring-data-jpa-audit</module>
<module>spring-security-jwt</module>
<module>spring-security-jwt-webflux</module>
<module>spring-data-cassandra</module>
<module>springboot-influxdb</module>
<module>spring-data-jpa-db2</module>
<module>springboot-jms-solace</module>
<module>springboot-ssl</module>
<module>springboot-ssl-tomcat</module>
<module>springboot-ssl-jetty</module>
<module>spring-boot-native-graalvm</module>
<module>spring-boot-native-without-buildtools</module>
</modules>
$ xmllint --xpath "//*[local-name()='project']/*[local-name()='artifactId']/text()" pom.xml
spring-boot
$ xmllint --xpath "//*[local-name()='project']/*[local-name()='artifactId']" pom.xml
<artifactId>spring-boot</artifactId>
ssh
连接ssh:
$ ssh root@196.168.1.1
当我们通过上面命令连接服务器时,如果同一个地址或hostname,但指向不同的服务器,就会报错:
$ ssh root@45.77.169.198
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ED25519 key sent by the remote host is
SHA256:k03xy8MD20Peefq0tZ9YS5DkW/FdB180bvMji2/ZLm8.
Please contact your system administrator.
Add correct host key in /Users/larry/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/larry/.ssh/known_hosts:42
Host key for 45.77.169.198 has changed and you have requested strict checking.
Host key verification failed.
执行下面命令后,再连接就可以了:
$ ssh-keygen -R 45.77.169.198
# Host 45.77.169.198 found: line 40
# Host 45.77.169.198 found: line 41
# Host 45.77.169.198 found: line 42
/Users/larry/.ssh/known_hosts updated.
Original contents retained as /Users/larry/.ssh/known_hosts.old
json
在shell中格式化json的response:
$ curl http://localhost:8080/api/katharsis/students/1 | jq .
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 139 0 139 0 0 4076 0 --:--:-- --:--:-- --:--:-- 5560
{
"data": {
"id": "1",
"type": "students",
"attributes": {
"name": "Larry Deng"
},
"links": {
"self": "https://www.pkslow.com/api/katharsis/students/1"
}
}
}
$ curl http://localhost:8080/api/katharsis/students/1 | python3 -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 139 0 139 0 0 13516 0 --:--:-- --:--:-- --:--:-- 34750
{
"data": {
"id": "1",
"type": "students",
"attributes": {
"name": "Larry Deng"
},
"links": {
"self": "https://www.pkslow.com/api/katharsis/students/1"
}
}
}
vim编辑器
vim hello.txt