Linux常用命令(合集)

rm 删除

删除所有文件,以下保留(参考):

代码语言:javascript
复制
rm !(textfile.txt|backup.tar.gz|script.php|database.sql|info.txt)

cut 截取

显示第1到第5个字符,第10到第20个字符:

代码语言:javascript
复制
$ who | cut -c 1-5,10-20

egrep 正则匹配

显示一行最后几个字符:

代码语言:javascript
复制
$ echo '1234567890' | egrep -o '.{1,5}$'

watch 监控变化

可以动态监控变化:

代码语言:javascript
复制
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 硬盘空间

查看文件系统硬盘空间使用情况:

代码语言:javascript
复制
df -h

du 文件空间

查看文件空间使用情况:

代码语言:javascript
复制
du -sh ./*

top 进程信息

显示系统进程信息,与CPU和内存。

find 查找文件

查找文件:

代码语言:javascript
复制
$ find ./ -name "jdk*"
./jdk-8u131-linux-x64.tar.gz
./jdk1.8.0_131

查找文件,并作为后面命令的输入:

代码语言:javascript
复制
$ find . -name "*.zip" | grep jmeter| xargs unzip -l

readlink

读取链接指向的目标值:

代码语言:javascript
复制
$ readlink -f home
/System/Volumes/Data/home

id 用户id

查看用户ID,群组ID。

nc 检测端口

检测端口是否可提供服务。可指定一个或多个端口,还可以指定端口范围。

代码语言:javascript
复制
$ 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

参考:check remote port in liux

telnet 检测网络连通

检测某个服务器的某个端口:

代码语言:javascript
复制
$ telnet www.pkslow.com 443
$ telnet 127.0.0.1 443

diff 对比文件

对比文件。

代码语言:javascript
复制
diff file1 file2
diff --brief --recursive dir1/ dir2/

DNS相关

主要有:nslookup/dig命令

代码语言:javascript
复制
$ cat /etc/resolv.conf 
# To view the DNS configuration used by this system, use:
#   scutil --dns
#
# SEE ALSO
#   dns-sd(1), scutil(8)
#
# This file is automatically generated.
#
search DHCP HOST
nameserver 192.168.3.1
nameserver 192.168.1.1

$ nslookup www.pkslow.com
Server: 192.168.3.1
Address: 192.168.3.1#53

Non-authoritative answer:
Name: www.pkslow.com
Address: 8.129.104.229

$ dig www.pkslow.com

; <<>> DiG 9.10.6 <<>> www.pkslow.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6227
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 18

;; QUESTION SECTION:
;www.pkslow.com. IN A

;; ANSWER SECTION:
www.pkslow.com. 283 IN A 8.129.104.229

;; AUTHORITY SECTION:
pkslow.com. 172483 IN NS dns20.hichina.com.
pkslow.com. 172483 IN NS dns19.hichina.com.

;; ADDITIONAL SECTION:
dns19.hichina.com. 172303 IN A 140.205.81.17
dns19.hichina.com. 172303 IN A 140.205.81.27
dns19.hichina.com. 172303 IN A 106.11.141.117
dns19.hichina.com. 172303 IN A 106.11.141.127
dns19.hichina.com. 172303 IN A 106.11.211.57
dns19.hichina.com. 172303 IN A 106.11.211.67
dns19.hichina.com. 172303 IN A 140.205.41.17
dns19.hichina.com. 172303 IN A 140.205.41.27
dns20.hichina.com. 3490 IN A 140.205.41.28
dns20.hichina.com. 3490 IN A 140.205.81.18
dns20.hichina.com. 3490 IN A 140.205.81.28
dns20.hichina.com. 3490 IN A 106.11.141.118
dns20.hichina.com. 3490 IN A 106.11.141.128
dns20.hichina.com. 3490 IN A 106.11.211.58
dns20.hichina.com. 3490 IN A 106.11.211.68
dns20.hichina.com. 3490 IN A 140.205.41.18
dns19.hichina.com. 3081 IN AAAA 2400:3200:2000:38::1
dns20.hichina.com. 172303 IN AAAA 2400:3200:2000:39::1

;; Query time: 9 msec
;; SERVER: 192.168.3.1#53(192.168.3.1)
;; WHEN: Wed May 05 09:49:22 CST 2021
;; MSG SIZE rcvd: 408

useradd 添加用户

可以使用以下命令添加用户:

代码语言:javascript
复制
useradd [OPTIONS] USERNAME

只有root用户或者有sudo权限的用户才可以执行。

添加用户的默认配置在文件/etc/default/useradd里,可通过以下命令查看:

代码语言:javascript
复制
$ useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

添加完如果需要设置密码:

代码语言:javascript
复制
 passwd username</code></pre></div></div><p>创建用户的Home目录(-m/--create-home):</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> useradd -m username

创建的目录是/home/username

指定用户目录:

如果不想使用默认在/home下创建目录,则可通过(-d/--home)指定:

代码语言:javascript
复制
 useradd -m -d /opt/username username</code></pre></div></div><p>指定用户ID(-u/--uid):</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> useradd -u 2000 username

指定群组(-g/--gid):

代码语言:javascript
复制
 useradd -g users username</code></pre></div></div><p>加到多个groups:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> useradd -g users -G wheel,developers username

指定登陆shell(-s/--shell):

代码语言:javascript
复制
useradd -s /bin/bash username

指定失效日期(-e/--expiredate):

代码语言:javascript
复制
useradd -e 2021-10-10 username

可通过以下命令查看失效日期:

代码语言:javascript
复制
 chage -l username</code></pre></div></div><p>更多请参考:useradd</p><h2 id="eolbh" name="zip/unzip-%E8%A7%A3%E5%8E%8B"><strong>zip/unzip 解压</strong></h2><p>压缩:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> zip my.zip file1 file2 file3
zip -r my.zip folder1</code></pre></div></div><p>添加文件到压缩文件:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> zip -u my.zip file4

删除压缩文件里的文件:

代码语言:javascript
复制
 zip -d my.zip file4</code></pre></div></div><p>解压到特定目录:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> unzip file.zip -d destination_folder

解压特定文件到特定目录:

代码语言:javascript
复制
 unzip file.zip filename -d destination_folder</code></pre></div></div><p>解压到标准输出:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> unzip -p file.zip filename

不解压文件查看:

代码语言:javascript
复制
 unzip -l my.zip</code></pre></div></div><p>更多请参考:zip/unzip</p><h2 id="3rclo" name="xmllint%E8%AF%BB%E5%8F%96xml%E6%96%87%E4%BB%B6"><strong>xmllint读取xml文件</strong></h2><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> 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:

代码语言:javascript
复制
 ssh root@196.168.1.1</code></pre></div></div><p>当我们通过上面命令连接服务器时,如果同一个地址或hostname,但指向不同的服务器,就会报错:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> 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.

执行下面命令后,再连接就可以了:

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
$ 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编辑器

代码语言:javascript
复制
vim hello.txt