LarryDpk
发布于 2021-05-03 / 4923 阅读
0

Linux常用命令整理

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

参考:check remote port in liux

telnet 检测网络连通

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

$ telnet www.pkslow.com 443
$ telnet 127.0.0.1 443

diff 对比文件

对比文件。

diff file1 file2

diff --brief --recursive dir1/ dir2/

DNS相关

主要有:nslookup/dig命令

$ 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 添加用户

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

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"
        }
    }
}