0%

我们编程吧 集锦

BOOK

目前共分享了** 43 ** 个技巧,详细参考: BOOK 集锦

C

目前共分享了** 126 ** 个技巧,详细参考: C 集锦

CPP

目前共分享了** 16 ** 个技巧,详细参考: CPP 集锦

GIT

目前共分享了** 113 ** 个技巧,详细参考: GIT 集锦

LINUX

目前共分享了** 457 ** 个技巧,详细参考: LINUX 集锦

MAC

目前共分享了** 6 ** 个技巧,详细参考: MAC 集锦

PGPLOT

目前共分享了** 86 ** 个技巧,详细参考: PGPLOT 集锦

PYTHON

目前共分享了** 112 ** 个技巧,详细参考: PYTHON 集锦

MATPLOTLIB

目前共分享了** 11 ** 个技巧,详细参考: MATPLOTLIB 集锦

QT

目前共分享了** 34 ** 个技巧,详细参考: QT 集锦

VIM

目前共分享了** 50 ** 个技巧,详细参考: VIM 集锦

后记

目前共分享了** 1445 个技巧,平均每天 0.23 **个。持续更新中,欢迎来访。

Summary Plot

SMILI 安装说明

安装依赖

1
2
3
$ sudo apt install x11*
$ sudo apt install libxt-dev
$ sudo apt install libopenblas-dev

安装 OpenBLAS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Clone the current repository
git clone https://github.com/xianyi/OpenBLAS

# Compile and install
# macOS MacPorts users may not use USE_OPENMP=1 option, and need to omit it.
cd OpenBLAS
make USE_OPENMP=1 CC=gcc FC=gfortran
make PREFIX="~/local" install

# 确认是否安装正确
# 是否有输出
$ pkg-config --debug openblas

# 如果没有,增加pkg的path
export PKG_CONFIG_PATH=~/local/lib/pkgconfig:$PKG_CONFIG_PATH

安装FFTW3

1
2
3
4
5
$ apt install fftw*

# 确认是否安装正确
# 是否有输出
$ pkg-config --debug fftw3

安装 FINUFFT

1
2
3
4
# 下载源码
$ cd ~/local
$ git clone https://github.com/flatironinstitute/finufft
$ cd finufft

准备make.inc文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Compilers
CXX=g++
CC=gcc
FC=gfortran

# (compile flags for use with GCC are as in linux makefile)
CFLAGS +=

# If you're getting warning messages of the form:
# ld: warning: object file (lib-static/libfinufft.a(finufft1d.o)) was built for
# newer OSX version (10.13) than being linked (10.9)
# Then you can uncomment the following two lines with the older version number
# (in this example -mmacosx-version-min=10.9)
#
#CFLAGS += "-mmacosx-version-min=<OLDER OSX VERSION NUMBER>"

# if you are macOS homebrew users, uncomment this.
# (assuming that /usr/local is your homebrew's PREFIX)
#CFLAGS += -I src -I/usr/local/include
#LIBS += -L/usr/local/lib

# if you are macOS MacPorts users, uncomment this.
# (assuming that /opt/local is your MacPorts' PREFIX)
#CFLAGS += -I src -I/opt/local/include
#LIBS += -L/opt/local/lib

# Your FFTW3's installation PREFIX
CFLAGS += -I$HOME/local/include
LIBS += -L$HOME/local/lib

# You can keep them
FFLAGS = $(CFLAGS)
CXXFLAGS = $(CFLAGS) -DNEED_EXTERN_C

# OpenMP with GCC on OSX needs following...
OMPFLAGS = -fopenmp
OMPLIBS = -lgomp
# since fftw3_omp doesn't work in OSX, you need to uncomment this
#FFTWOMPSUFFIX=threads

开始编译库

1
$ make lib

新建finufft.pc文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
# This is an example pkg-config file. Here is an brief instruction.
# (1) Please change finufftdir depending on your install directory.
# (2) please change its filename to finufft.sample.pc and
# copy to a directory specified in $PKG_CONFIG_PATH
finufftdir=$(HOME)/local/finufft
libdir=${finufftdir}/lib-static
includedir=${finufftdir}/include

Name: FINUFFT
Description: Flatiron Institute Nonuniform Fast Fourier Transform libraries
Version: github
Libs: -L${libdir} -lfinufft
Cflags: -I${includedir}

确认是否安装完毕

1
2
3
4
5
6
# 确认是否安装正确
# 是否有输出
$ pkg-config --debug finufft

# 如果没有,增加pkg的path
export PKG_CONFIG_PATH=~/local/finufft/:$PKG_CONFIG_PATH

安装SMILI

1
2
3
4
5
6
# Clone the repository
$ git clone https://github.com/astrosmili/smili
$ cd smili
# 通过conda创建虚拟环境,不然编译会有问题
$ ./configure
$ make install

linux中最常用的网络命令

仅个人想法,会持续不间断更新和改进。

虚虚假假,真真实实,如何快速的去伪存真,抽丝剥茧。

需要在开放的网络世界掌握一些最基础的命令,让自己知己知彼。


查看网络信息的原初 ifconfig

Linux ifconfig命令用于显示或设置网络设备,在调试或调优的时间经常使用。

官方定义为:

ifconfig - configure a network interface

对于这个命令,一般只要掌握如何查看,如何设置IP地址基本就可以了,对于网络钻的比较深的,还需要更多一些参数。

使用方法为:

1
2
3
4
5
# 显示
$ ifconfig [-v] [-a] [-s] [interface]

# 设置
$ ifconfig [-v] interface [aftype] options | address ...

一些参数的含义为:

  • -a :显示所有网卡的状态,即使是down的状态
  • -s:显示一个短列表
  • interface mtu N 设置最大传输单元【需要管理员权限】
  • netmask addr:设置掩码地址【需要管理员权限】
  • interface up 激活网卡【需要管理员权限】
  • interface down 关闭网卡【需要管理员权限】
  • interface hw ether xx.xx.xx.xx.xx.xx 设置MAC地址【需要管理员权限】

默认无参数使用

如果不指定任何参数,直接显示当前活动的接口,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.123 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 xxxx::xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x20<link>
inet6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x0<global>
ether xx:xx:xx:xx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 5634431 bytes 4994127142 (4.6 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 858051 bytes 109858013 (104.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device memory 0xc7320000-c733ffff

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.6.123 netmask 255.255.255.0 broadcast 192.168.6.255
inet6 xxxx::xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x20<link>
ether xx:xx:xx:xx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 1547215 bytes 92862867 (88.5 MiB)
RX errors 0 dropped 6 overruns 0 frame 0
TX packets 3230 bytes 922051 (900.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 219608 bytes 105943591 (101.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 219608 bytes 105943591 (101.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

其中一般希望看到的信息包括:

  • inet:为IP地址
  • ether:为MAC地址
  • MTU:最大传输单元

不加任何参数只会显示已经配置并且活跃的网卡信息,如果使用ifconfig -a就可以显示全部的网卡状态了,即使有些网卡是down的状态。

亦或者指定一个interface,比如上面的eth1,则只输出这个网卡的信息,如下:

1
2
3
4
5
6
7
8
9
10
$ ifconfig eth1

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.6.123 netmask 255.255.255.0 broadcast 192.168.6.255
inet6 xxxx::xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x20<link>
ether xx:xx:xx:xx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 1547215 bytes 92862867 (88.5 MiB)
RX errors 0 dropped 6 overruns 0 frame 0
TX packets 3230 bytes 922051 (900.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

-s显示短列表

如果只想看到MTU以及数据包的状态,可以用该参数,如下:

1
2
3
4
5
$ ifconfig -s
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 5665450 0 0 0 867639 0 0 0 BMRU
eth1 1500 3489187217 0 101054 0 501260400 0 0 0 BMU
lo 65536 219708 0 0 0 219708 0 0 0 LRU

输出信息主要包含了MTU值,发送及接收的数据情况。

配置IP地址

如下对eth0网卡配置IP地址、掩码以及广播地址,当然可以分布操作

1
2
3
4
5
6
7
8
# 给eth0配置IP地址
$ ifconfig eth0 192.168.1.123

# 给eth0配置IP地址和子网掩码
$ ifconfig eth0 192.168.1.123 netmask 255.255.255.0

# 给eth0配置IP地址、子网掩码还有广播地址
$ ifconfig eth0 192.168.1.123 netmask 255.255.255.0 broadcast 192.168.1.255

修改MTU

在某些情况下可能需要修改MTU值,比如增到到MTU为9000,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ ifconfig eth1
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.6.123 netmask 255.255.255.0 broadcast 192.168.6.255
inet6 xxxx::xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x20<link>
ether xx:xx:xx:xx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 1547215 bytes 92862867 (88.5 MiB)
RX errors 0 dropped 6 overruns 0 frame 0
TX packets 3230 bytes 922051 (900.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# 修改MTU
$ ifconfig eth1 MTU 9000

$ ifconfig eth1
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 9000
inet 192.168.6.123 netmask 255.255.255.0 broadcast 192.168.6.255
inet6 xxxx::xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x20<link>
ether xx:xx:xx:xx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 1547215 bytes 92862867 (88.5 MiB)
RX errors 0 dropped 6 overruns 0 frame 0
TX packets 3230 bytes 922051 (900.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

已经看到输出的信息已经把mtu更新为了9000.

这个值对网络传输影响很大。

启动关闭网卡

启动关闭主要的应用场景为重新设置了IP地址,或者暂时对某个网卡进行操作。

1
2
3
4
5
# 关闭eth0
$ ifconfig eth0 down

# 启动eth0
$ ifconfig eth0 up

不过需要注意的是

  1. 很多的设置操作都需要管理员权限;
  2. 很多操作在重启后设置都会还原,如果需要永久设置,需要更改network的一些配置文件;
  3. 这个程序基本被淘汰了,已经不在更新,所有的操作或者用法均可以通过ip来搞定。等明天~。

网络中不中,先看ping行不行

linux系统里面如果想判断网络的好坏,脑海中蹦出的第一个命令就是ping了。

官方定义为:

ping - send ICMP ECHO_REQUEST to network hosts

ping命令基本是最常用的网络命令,它可以用来测试与目标主机的连通性。

ping使用ICMP传输协议,通过发送ICMP ECHO_REQUEST数据包到网络主机,并显示返回的相应情况,根据这些信息就可以判断目标主机是否可以访问,在发送的过程中还会有一个时间戳用来计算网络的状态。

不过有些服务器为了防止通过ping探测到,可能会在防火墙或者内核参数中禁止ping命令,这样的话,可能虽然目标主机可以访问,但是无法ping通,所以并不能说ping不通的网络就是不能访问的。

需要注意linux下的ping和windows下的ping稍有区别,linux下ping不会自动终止,需要按ctrl+c终止或者用参数-c指定要求完成的回应次数。

语法

ping的使用说实话挺复杂,挺多的,不过常用的这篇短文基本就够了。

详细如下:

1
2
3
4
5
# ALL
$ ping [-aAbBdDfhLnOqrRUvV46] [-c count] [-F flowlabel] [-i interval] [-I interface] [-l preload] [-m mark] [-M pmtudisc_option] [-N node‐info_option] [-w deadline] [-W timeout] [-p pattern] [-Q tos] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp option] [hop ...] destination

# 较常用的选项如下:
$ ping [-c count] [-i interval] destination

参数说明

  • -c <完成次数> 设置完成要求回应的次数。

  • -i interval 指定收发信息的间隔时间。

不加任何参数

如果不加任何参数,查看是否ping

1
2
3
4
5
6
7
8
9
10
11
12
$ ping www.baidu.com   
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27: icmp_seq=1 ttl=52 time=6.06 ms
64 bytes from 115.239.210.27: icmp_seq=2 ttl=52 time=5.56 ms
64 bytes from 115.239.210.27: icmp_seq=3 ttl=52 time=5.67 ms
64 bytes from 115.239.210.27: icmp_seq=4 ttl=52 time=5.82 ms
64 bytes from 115.239.210.27: icmp_seq=5 ttl=52 time=5.70 ms
64 bytes from 115.239.210.27: icmp_seq=6 ttl=52 time=5.79 ms
^C # 此处输入了Ctrl+C强制退出
--- 192.168.1.123 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.152/0.159/0.172/0.017 ms

可以看到可以pingwww.baidu.com,时延还算比较OK,几个毫秒量级。

这里看一下几个字段的含义,其中:

56(84) bytes of data:表示默认的数据包长度为56字节;

time=5.56ms:表示响应的时间,值越小,证明连接越快;

TTL=52:TTL是Time To Live的缩写,表示DNS记录在DNS服务器上存在的时间,是IP协议包的一个值,告诉路由器啥时候抛弃这个数据包,(大体上可以通过这个值来判断目标类型的操作系统。)

发送指定数目

可以通过 参数-c 来发送指定数目的包后停止

1
2
3
4
5
6
7
8
9
10
11
$ ping www.baidu.com -c 5
PING www.a.shifen.com (115.239.211.112) 56(84) bytes of data.
64 bytes from 115.239.211.112: icmp_seq=1 ttl=52 time=6.03 ms
64 bytes from 115.239.211.112: icmp_seq=2 ttl=52 time=5.96 ms
64 bytes from 115.239.211.112: icmp_seq=3 ttl=52 time=5.79 ms
64 bytes from 115.239.211.112: icmp_seq=4 ttl=52 time=5.79 ms
64 bytes from 115.239.211.112: icmp_seq=5 ttl=52 time=6.21 ms

--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 5.791/5.958/6.215/0.186 ms

此时将在发送5次数据包以后自动停止,在Linux里面,如果不加这个参数,是会一直发送运行的。

设定发送时间间隔

可以通过 参数 -i N指定每个N秒发送一次信息,如下将每隔3秒发送一次ping信息。

1
2
3
4
5
6
7
8
9
10
11
12
$ ping www.baidu.com -i 3
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=55 time=28.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=6 ttl=55 time=28.6 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 15041ms
rtt min/avg/max/mdev = 28.650/28.670/28.697/0.139 ms

如上,每隔3秒会发送一次,对于需要持续检测或者记录的可以考虑适当加大这个时间间隔。

注意,只有管理员可以设置小于0.2秒的时间间隔。所以这个数值可以是浮点数~

组合使用

上面的几个例子是可以配合使用的,比如

1
2
3
4
5
6
7
8
9
10
11
$ ping www.baidu.com -c 4 -i 5 
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=55 time=29.4 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=55 time=29.3 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=55 time=29.4 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=4 ttl=55 time=29.4 ms

--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 20045ms
rtt min/avg/max/mdev = 29.396/29.428/29.461/0.110 ms

这个例子为:每个5秒查询一次,一共查询4次,然后退出。

Linux ip命令

Linux ip 命令与 ifconfig 命令类似,但比 ifconfig 命令更加强大,主要用于显示或设置网络设备。

已经在Linux 2.2 加入到了内核。所以ip是加强版的网络配置工具,用来替代ifconfig并强化其他功能。

官方定义为:

ip - show / manipulate routing, devices, policy routing and tunnels

对于这个命令,命令集是相当的多。先说一些基础的,其他就要自己摸索了。

使用方法为:

1
2
3
4
5
6
7
8
9
$ ip [ OPTIONS ] OBJECT { COMMAND | help }

$ ip [ -force ] -batch filename

# OBJECT的取值
# OBJECT := { link | address | addrlabel | route | rule | neigh | ntable | tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm | netns | l2tp | tcp_metrics | token | macsec }

# OPTIONS的取值
# OPTIONS := { -V[ersion] | -h[uman-readable] | -s[tatistics] | -d[etails] | -r[esolve] | -iec | -f[amily] { inet | inet6 | ipx | dnet | link } | -4 | -6 | -I | -D | -B | -0 | -l[oops] { maximum-addr-flush-attempts } | -o[neline] | -rc[vbuf] [size] | -t[imestamp] | -ts[hort] | -n[etns] name | -a[ll] | -c[olor] }

COMMAND的值主要取决于OBJECT,可能有所不同,一般可以使用adddeleteshow(或者list),均可以输入help来进行查询。

OBJECT中常用的为:

  • link 网络设备
  • address 设备上的协议地址
  • -s, -stats, -statistics 统计化输出

显示网络设备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 显示网络设备
$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

# 显示IP等更多信息
$ ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
inet 192.168.1.123/24 brd 192.168.254.255 scope global noprefixroute eno1
valid_lft forever preferred_lft forever
inet6 xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/64 scope global noprefixroute
valid_lft forever preferred_lft forever
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc mq state DOWN group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

命令中的show为默认,也可以直接使用ip link或者ip address,结果一致。

设置IP地址

可以通过ip addr add/del xxx.xxx.xxx.xxx dev interface 来设置或者删除IP地址。

如下设置or删除eth0的IP地址。

1
2
3
4
5
# 设置IP地址
$ ip addr add 192.168.0.1/24 dev eth0

# 删除IP地址
$ ip addr del 192.168.0.1/24 dev eth0

启动关闭网卡

与ifconfig类似,也使用up与down来进行启动和关闭,具体如下:

1
2
3
4
5
# 开启网卡
$ ip link set eth0 up

# 关闭网卡
$ ip link set eth0 down

统计方便阅读

选项-s可以统计一些信息方便我们阅读,如下看看网络的情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ ip -s link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
871883256468 251700492 0 0 0 0
TX: bytes packets errors dropped carrier collsns
871883256468 251700492 0 0 0 0
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
64930085920632 50955323447 0 613156 0 472190933
TX: bytes packets errors dropped carrier collsns
17534345850354 17448077191 0 0 0 0
3: eno2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 9000 qdisc mq state DOWN mode DEFAULT group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0

可以看到对输出进行了一些格式化,看起来更直观。

探索网络连接的netstat

在Linux系统中,网络是至关重要的部分,而netstat命令是管理和监视网络连接的强大工具之一。

它提供了关于网络接口和路由表的详细信息,有助于了解网络连接状态、统计信息以及网络协议的使用情况。

也更方便我们对网络的管理、故障排除以及安全监控等等。

基本概述

netstat命令比较简单,通过简单的参数组合,可以获得各种网络相关的信息。

以下是一些常用的参数及其功能:

  • -a:显示所有连接和监听端口。
  • -t:仅显示TCP连接。
  • -u:仅显示UDP连接。
  • -n:以数字形式显示地址和端口号。
  • -p:显示进程标识符和程序名称。
  • -r:显示路由表。
  • -s:显示统计信息。

非交互的下载工具 wget

Linux系统中的wget是一个下载文件📀的命令行工具,特别普遍 。

对于Linux用户是必不可少的工具,对于经常要下载一些软件或从远程服务器恢复备份到本地服务器,这个命令尤为重要。

wget支持很多协议,比如HTTPHTTPSFTP协议,还可以使用HTTP代理。

wget的有诸多特点,比如

  • 自动下载 wget支持自动下载,即wget可以在用户退出系统的之后在后台执行。这意味着你可以登录系统,启动一个wget下载任务,然后退出系统,wget将在后台执行直到任务完成,这是个牛气冲天的功能。
  • 完全重建 wget 可以跟踪HTML页面上的链接依次下载来创建远程服务器的本地版本,完全重建原始站点的目录结构。这又常被称作”递归下载”。在递归下载的时候,wget 遵循Robot Exclusion标准(/robots.txt). wget可以在下载的同时,将链接转换成指向本地文件,以方便离线浏览。
  • 高稳定 wget 非常稳定,它在带宽很窄的情况下和不稳定网络中有很强的适应性.如果是由于网络的原因下载失败,wget会不断地尝试,直到整个文件下载完毕。如果是服务器打断下载过程,它会再次联到服务器上从停止的地方继续下载。这对从那些限定了链接时间的服务器上下载大文件非常有用。

命令格式

1
$ wget [参数] [URL地址]

用于从网络上下载资源,没有指定目录,下载资源会默认为当前目录。wget虽然功能强大,但是使用起来还是比较简单:

使用范例

wget的命令参数很多,不过常用的为下面几个,详细的可以看进阶。

使用wget下载单个文件

比如,我们下载个Ubuntu的最新版本,试下效果如何

1
$ wget http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso

在下载的过程中会显示进度条,包含(下载完成百分比,已经下载的字节,当前下载速度,剩余下载时间)。

使用wget -O下载并以不同的文件名保存

这个对于动态链接的下载比较有用,特别是有些文件的名字实在是太……………….长了

1
$ wget -O wordpress.zip http://www.ubuntu.com/download.aspx?id=1234

使用wget -c断点续传

1
2
3
4
5
$ wget -c http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso

# or

$ wget --continue http://releases.ubuntu.com/16.04/ubuntu-16.04-desktop-amd64.iso

使用wget -c重新启动下载中断的文件,对于我们下载大文件时突然由于网络等原因中断非常有帮助,我们可以继续接着下载而不是重新下载一个文件。需要继续中断的下载时可以使用-c参数。

使用wget -o把下载信息存入日志文件

1
$ wget -o download.log URL

不希望下载信息直接显示在终端而是在一个日志文件,可以使用,特别注意需要与-O来区分开~


  • groups - show group memberships
  • finger - shows information about users
  • last - displays most recent user logins

linux中最常用的用户信息命令

仅个人想法,会持续不间断更新和改进。

用户信息,重中之重。

虽然21世纪最重要的是人才。

但对于我们个人而言,用户信息也是极其重要并特别需要留意的。


还有谁 last

Linux last 命令用于显示用户最近的登录信息。

官方定义为:

last, lastb - show listing of last logged in users

通过读取/var/log/wtmp文件来获取这些信息。

语法

1
$ last [-R] [-num] [ -n num ] [-adFiowx] [ -f file ] [ -t YYYYMMDDHHMMSS] [name...]  [tty...]

参数

  • -R 省略 hostname 的栏位

  • -n 展示前 num 个

  • username 展示 username 的登入讯息

  • tty 限制登入讯息包含终端机代号

一般使用方法

1
2
3
4
5
6
7
8
9
10
11
12
$ last
username2 pts/17 192.168.100.123 Wed Mar 23 22:14 still logged in
username3 pts/20 localhost:11.0 Wed Mar 23 14:26 - 15:48 (01:21)
username4 pts/23 localhost:11.0 Wed Mar 23 14:26 - 15:48 (01:21)
username4 pts/4 192.168.100.125 Thu Jun 10 18:37 - 22:57 (04:20)
username5 pts/4 192.168.100.125 Thu Jun 10 18:21 - 18:21 (00:00)
username6 pts/9 192.168.100.126 Thu Jun 10 18:11 - 18:20 (00:09)
username7 pts/15 192.168.100.122 Thu Jun 10 18:04 - 23:44 (1+05:40)
username8 pts/14 192.168.100.121 Thu Jun 10 17:59 - 07:50 (13:50)
username9 pts/9 192.168.100.126 Thu Jun 10 17:59 - 18:03 (00:04)

wtmp begins Thu Jun 10 17:33:14 2013

查看最近登陆的三个用户

1
2
3
4
5
6
7
$ last -3

username2 pts/17 192.168.100.123 Wed Mar 23 22:14 still logged in
username3 pts/20 localhost:11.0 Wed Mar 23 14:26 - 15:48 (01:21)
username4 pts/23 localhost:11.0 Wed Mar 23 14:26 - 15:48 (01:21)

wtmp begins Thu Jun 10 17:33:14 2013

省略hostname

1
2
3
4
5
6
$ last -3 -R
username2 pts/17 Wed Mar 23 22:14 still logged in
username3 pts/20 Wed Mar 23 14:26 - 15:48 (01:21)
username4 pts/23 Wed Mar 23 14:26 - 15:48 (01:21)

wtmp begins Thu Jun 10 17:33:14 2013

显示最后一列显示主机IP地址

1
2
3
4
5
6
7
8
$ last -n 5 -a -i
username3 pts/17 Wed Mar 23 22:14 still logged in 192.168.100.123
username5 pts/20 Wed Mar 23 14:26 - 15:48 (01:21) 0.0.0.0
username6 pts/23 Wed Mar 23 14:26 - 15:48 (01:21) 0.0.0.0
username7 pts/19 Wed Mar 23 13:46 - 15:48 (02:01) 192.168.100.123
username8 pts/17 Wed Mar 23 13:18 - 15:47 (02:29) 192.168.100.123

wtmp begins Thu Jun 10 17:33:14 2013

我是谁 whoami

我知道你是谁,但我不知道我是谁,此时whoami可以帮助你,哈哈。

whoami将打印当前用户的名字。与id -un类似。

官方定义为:

whoami - print effective userid

用法为:

1
$ whoami [option] ..

这命令,基本没有参数。

我暂时。。也没有想到为什么会有这个命令。

唯一的可能使你找管理员来配置个啥,然后他需要知道你是谁,不,我是谁。

我看了一下源码,果然简洁:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>

#include "system.h"
#include "die.h"
#include "error.h"
#include "long-options.h"
#include "quote.h"

/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "whoami"

#define AUTHORS proper_name ("Richard Mlynarik")

void
usage (int status)
{
if (status != EXIT_SUCCESS)
emit_try_help ();
else
{
printf (_("Usage: %s [OPTION]...\n"), program_name);
fputs (_("\
Print the user name associated with the current effective user ID.\n\
Same as id -un.\n\
\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
emit_ancillary_info (PROGRAM_NAME);
}
exit (status);
}

int
main (int argc, char **argv)
{
struct passwd *pw;
uid_t uid;
uid_t NO_UID = -1;

initialize_main (&argc, &argv);
set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);

atexit (close_stdout);

parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
Version, true, usage, AUTHORS,
(char const *) NULL);

if (optind != argc)
{
error (0, 0, _("extra operand %s"), quote (argv[optind]));
usage (EXIT_FAILURE);
}

errno = 0;
uid = geteuid ();
pw = (uid == NO_UID && errno ? NULL : getpwuid (uid));
if (!pw)
die (EXIT_FAILURE, errno, _("cannot find name for user ID %lu"),
(unsigned long int) uid);
puts (pw->pw_name);
return EXIT_SUCCESS;
}

其中使用的即为uid = geteuid ();

谁?who

.. note::
物是人非事事休,欲语泪先流。
李清照《武陵春·春晚》

知道了我是谁,接下来就要知道谁是谁了。

who将显示谁在登录,显示的内容可能包括用户名、终端登录口,登录的时间等等信息。

官方定义为:

who - show who is logged on

用法为:

1
$ who [OPTION]... [ FILE | ARG1 ARG2 ]

常用的参数为:

  • -q , --count:只显示登入系统的帐号名称和总人数;
  • -s:此参数将忽略不予处理,仅负责解决who指令其他版本的兼容性问题;
  • -a, --all:效果为加上 -b -d --login -p -r -t -T -u
  • -b, --boot:上一次系统的重启时间
  • -d, --dead:打印dead进程
  • -H, --heading:打印每一列的表头
  • -q, --count:所有登录的用户名以及用户登录的数量
  • -s, --short:打印USER/LINE/WHEN(默认为这个参数)

默认使用

显示当前登录系统的用户

1
2
3
$ who       
user pts/0 2012-03-02 10:12
user2 pts/1 2012-03-10 09:12

系统的运行时间

这个信息显示系统自上一次重启后的运行时间。

1
2
$ who -b
system boot 2012-02-16 14:05

显示表头信息

使用-H或者--heading可以看到表头信息

1
2
3
4
$ who -H
USER LINE WHEN
user pts/0 2012-03-02 10:12
user2 pts/1 2012-03-10 09:12

显示登录的人员及总数

1
2
3
$ who -q
user1 user1 user2 user2 user3 user4
# users=6

什么?谁?w (who & what)

w可以认为是加强版的who,果然越简洁越强大,就比如lessmore是功能更多的。

w不仅可以显示谁在登录,还可以打印他们在做什么。w显示的信息如下:

  • 登录的用户;
  • 运行的程序;
  • 第一行显示的信息:当前时间、系统运行的时间、多少用户登录、系统的负载(分贝为1,5,15分钟)

官方定义为:

w - Show who is logged on and what they are doing.

用法为:

1
$ w [options] user [...]

常用的两个选项为:

  • -h  不显示各栏位的标题信息列。

  • -s  简洁格式列表,不显示用户登入时间,JCPU或者PCPU的时间

默认的显示

显示当前用户的登录信息及执行的命令

1
2
3
4
5
6
7
8
9
$ w
16:29:03 up 26 days, 2:49, 6 users, load average: 1.00, 0.97, 0.96
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user pts/4 :1 07Sep21 20days 9:59 1:53m bash
user pts/0 :2 08Sep21 6days 0.70s 1:53m zsh
user pts/1 :3 08Sep21 20days 1:13m 1:53m bash
user :0 :0 15Sep21 6days 27days 21.36s zsh
user pts/2 :0 15Sep21 14days 0.25s 0.25s zsh
user pts/3 :3 16Sep21 24:45m 0.22s 0.22s bash

不显示标题行

1
2
3
4
5
6
7
8
9
$ w -h
16:29:16 up 26 days, 2:49, 6 users, load average: 1.20, 0.67, 0.76
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user pts/4 :1 07Sep21 20days 9:59 1:53m bash
user pts/0 :2 08Sep21 6days 0.70s 1:53m zsh
user pts/1 :3 08Sep21 20days 1:13m 1:53m bash
user :0 :0 15Sep21 6days 27days 21.36s zsh
user pts/2 :0 15Sep21 14days 0.25s 0.25s zsh
user pts/3 :3 16Sep21 24:45m 0.22s 0.22s bash

简洁模式显示

1
2
3
4
5
6
7
8
9
$ w -s
16:29:26 up 26 days, 2:49, 6 users, load average: 1.50, 0.67, 0.36
USER TTY FROM IDLE WHAT
user pts/4 :1 20days bash
user pts/0 :2 6days zsh
user pts/1 :3 20days bash
user :0 :0 6days zsh
user pts/2 :0 14days zsh
user pts/3 :3 24:45m bash

不要告诉别人的passwd

passwd用于创建或者更新用户密码,是管理员必备的命令之一。

这个命令最终的实现是通过调用Linux-PAM 和Libuser API来实现的。

官方的定义为:

passwd - update user’s authentication tokens

使用的方法为:

1
$ passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S] [--stdin] [username]

其中很常用的options为:

  • -S, --status:显示密码的状态信息
  • -d, --delete:删除用户密码,此时该用户将处于无密码状态

不太常用的options为:

  • --stdin:可以通过标准输入,亦可以为一个pipe
  • -l, --lock:锁定账号,不过也不是完全锁定,因为用户可以通过ssh key来继续访问
  • -u, --unlock:与上面的-l选项相反,属于解锁用户
  • -w, --warning DAYS:口令到期前通知用户,具备password lifetime的才支持

修改或更新密码

这个是最常用的用法,用于设置或者修改更新用户密码

1
2
3
4
5
$ sudo passwd user  		#设置用户user的密码
Enter new UNIX password: #输入新密码,输入的密码不显示
Retype new UNIX password: #再次输入确认密码
passwd: password updated successfully
# 此时设置成功

删除用户密码

1
2
$ sudo passwd -d user 
passwd: password expiry information changed.

此时用户处于无密码的状态,很类似最近说的,没有密码就是最安全的密码。

查看密码的状态

1
2
3
$ sudo passwd -S user
[sudo] password for oper:
user PS 2013-02-11 0 99999 7 -1 (Password set, SHA512 crypt.)

说到密码,有两个比较重要的原则

  1. 保护好你的密码,不写下来而是记在脑海里,定时修改;
  2. 选择一个很难猜的密码,而不是最容易被攻破的top密码;

Linux中最常用的搜索命令

仅个人想法,会持续不间断更新和改进。

在Linux的庞大世界中,搜索犹如明灯,可以拨开云雾见青天,照亮我们前行大道路。

无论你是在浩瀚的代码库中搜索一个特定的函数,还是在庞大的文件系统中寻找一个文件,搜索命令着实是不可或缺尤为重要的工具。

而其中最绕不开的当属以下几个。


一切皆可查的 find

find命令用来在指定目录下查找文件,功能相当之强大。

官方定义为:

find - search for files in a directory hierarchy

Linux的哲学是一切皆文件,那么find的使命就是一切皆可查

语法

使用语法为:

1
$ find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

比较常用的几个参数为:

  • -exec <执行指令>:假设find指令的回传值为True,就执行该指令;
  • -size <文件大小>:查找符合指定的文件大小的文件;
  • -mtime <24小时>:查找在指定时间曾被更改过的文件或目录,单位以24小时计算;
  • -name<范本样式>:指定字符串作为寻找文件或目录的范本样式;
  • -type <文件类型>:只寻找符合指定的文件类型的文件;

无参数

如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件,并且将查找到的子目录和文件全部进行显示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ls -l
total 310M
-rw-rw-r-- 1 user user 10M Mar 21 20:01 a
drwxrwxr-x 2 user user 22 Mar 21 20:01 aa
-rw-rw-r-- 1 user user 100M Mar 21 20:01 b
-rw-rw-r-- 1 user user 200M Mar 21 20:01 c


$ find
.
./a
./b
./c
./test

查找小于,等于和大于100MB的文件

通过-size大小来查找文件

1
2
3
4
5
6
7
8
9
10
$ find . -size -100M
.
./a
./aa
$ find . -size 100M
./b
$ find . -size +100M
./c
./aa/d

查找多长时间修改过

可以通过参数-mtime来查找文件的修改时间,比如如下可以查找当前目录下最近60天没有被修改的文件。

1
2
3
4
$ find . -mtime +60

# 最近2天以内未修改
$ find . –mtime -2

稍微复杂但是很有用的命令

我经常把 find 命令和他的选项 exec一起使用,比如我想查找一个目录中的所有文件并将其更改其权限。可以通过以下简单命令完成:

1
$ find /path/ -type f -exec chmod 644 {} \;

这个命令会递归搜索指定目录内/path/下的所有文件,并对找到的文件执行 chmod 命令。

精准快速定位的locate

.. note::

众里寻他千百度,蓦然回首,那人却在灯火阑珊处
-李煜

Linux locate命令用于查找符合条件的文档、程序、目录等等。这个命令会在数据库中查找符合条件的各种信息。

一般情况我们只需要输入 locate name 即可查找。

官方定义为:

locate - list files in databases that match a pattern

使用方法为:

1
2
3
4
$ locate  [-d  path  |  --database=path]  [-e  | -E | --[non-]existing] [-i | --ignore-case] [-0 | --null] [-c |
--count] [-w | --wholename] [-b | --basename] [-l N | --limit=N] [-S | --statistics] [-r | --regex ] [--regex‐
type R] [--max-database-age D] [-P | -H | --nofollow] [-L | --follow] [--version] [-A | --all] [-p | --print]
[--help] pattern...

看着很复杂,不过常用的参数倒是不多,基本为:

  • -n : 至多显示 n个输出。
  • -i, --ignore-case : 忽略大小写

默认无参数

默认情况下,locate直接跟上需要查找的信息就可以了,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ locate set_vis.cpp
/home/user/mycode/src/set_vis.cpp

# 以查找apropos为例
$ locate apropos
/usr/bin/apropos
/usr/local/difmap/help/apropos.hlp
/usr/share/emacs/24.3/lisp/apropos.elc
/usr/share/man/de/man1/apropos.1.gz
/usr/share/man/es/man1/apropos.1.gz
/usr/share/man/fr/man1/apropos.1.gz
/usr/share/man/id/man1/apropos.1.gz
/usr/share/man/it/man1/apropos.1.gz
/usr/share/man/ja/man1/apropos.1.gz
/usr/share/man/man1/apropos.1.gz
/usr/share/man/nl/man1/apropos.1.gz
/usr/share/man/pl/man1/apropos.1.gz
/usr/share/man/ru/man1/apropos.1.gz

太多需要简单化

如果输出的信息很多,仅仅希望看到前面的几个,使用-n参数既可

1
2
3
4
5
# 仅仅查看前的3个
$ locate -n 3 apropos
/usr/bin/apropos
/usr/local/difmap/help/apropos.hlp
/usr/share/emacs/24.3/lisp/apropos.elc

不区分大小写

部分情况下,可能有大小写混淆的情况,此时使用-i参数既可

1
2
3
$ $ locate -i set_vis.cpp
/home/user/mycode/src/set_vis.cpp
/home/user/mycode_CPP/src/set_VIS.cpp

说明

不过刚按照的系统,这个命令并不一定有输出,主要是因为locatefind 不同, find 直接在硬盘找,而locate 只在数据库中查找。

这个数据库在CentOS系统默认的为 /var/lib/mlocate/mlocate.db 中,所以 locate 的查找会比较快,但并一定是实时的,而是以数据库的更新为准。

可以通过下面的命令手工升级数据库 ,命令为:

1
$ updatedb

然后就可以使用了。

文件内容搜索利器 - grep

Linux grep 命令用于查找文件里符合条件的字符串。

官方定义为:

grep, egrep, fgrep - print lines matching a pattern

grep支持正则表达式,是一个强大的文本搜索工具。

语法

语法也挺复杂,因为功能确实很强大。

1
2
3
$ grep [OPTION...] PATTERNS [FILE...]
$ grep [OPTION...] -e PATTERNS ... [FILE...] # 使用egrep
$ grep [OPTION...] -f PATTERN_FILE ... [FILE...] # 使用fgrep

常用的参数为:

  • -r 或 –recursive : 此参数的效果和指定”-d recurse”参数相同
  • -v 或 –invert-match : 显示不包含匹配文本的所有行
  • -i 或 –ignore-case : 忽略字符大小写的差别
  • -n 或 –line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。

假定有如下3个文件,1个文件夹,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a
This is a
Hello a

b
this is b
Hello b
c
This is c
Hello c

d/d
This is d
Hello d

默认无参数

在当前目录搜索包含is字符串,可以看到**a/b/c**三个文件均有输出,而d因为是目录,暂时无输出。

1
2
3
4
5
$ grep is *
a:This is a
b:this is b
c:This is c
grep: d: Is a directory

增加文件夹

与其他命令类似,增加-r参数,递归搜索

1
2
3
4
5
$ grep -r is *
a:This is a
b:this is b
c:This is c
d/d:This is d

反向查找

在某些情况下,或许正想找到不包含某些字符串的内容,如下:

1
2
3
4
5
$ grep -rv is *
a:Hello a
b:Hello b
c:Hello c
d/d:Hello d

此时可以看到,不包含is的内容显示了出来。

不区分大小写

而某些情况下,或许我们希望找到不区分大小写的内容,比如对于This/this而言:

1
2
3
4
5
6
7
8
9
10
$ grep -r This *
a:This is a
c:This is c
d/d:This is d

$ grep -ri This *
a:This is a
b:this is b
c:This is c
d/d:This is d

可以看到此时有可能笔误,或者其他原因的b文件已经被找到了。

显示行数,精准定位

如果文件内容比较多,此时显示内容在哪一行,是很重要的,加上-n参数既可解决。

1
2
3
4
$ grep -rn This *
a:1:This is a
c:1:This is c
d/d:1:This is d

Linux中最常用的目录导航命令

仅个人想法,会持续不间断更新和改进。

在Linux世界中,为了更加高效地浏览和定位文件,需要一些常用的目录导航命令。


探索未知世界的cd

.. note::
月桥花院,琐窗朱户,只有春知处。
宋 辛弃疾《青玉案·元夕》

cd 命令应该是除了 ls 命令外用的最多的命令了。除非你大门不出二门不迈,做个大家闺秀。

cd 命令的含义为

cd - change directory

可以让我们访问不同的文件夹。

最简单的用法为:

1
$ cd /the/path/you/want/to/go/

接下来说一些技巧,让效率加倍。

让cd命令对参数大小写不敏感

如果你需要同时显示大写和小写的目录名(即便是你给的参数只是小写的),执行下面的bind命令,此时就可以避免Linuxlinux的尴尬。

1
$ bind "set completion-ignore-case on"

进入刚才的目录

想要进入刚才进入的地方(目测没有很多人再用,但是真的很好用)运行:

1
$ cd

快速返回家目录

需要快速地回到你的家目录,输入cd即可,这里其实不用一级一级的进入

1
$ cd

进入某用户的家目录

这个需要你有root权限

1
cd ~username

进入username的家目录。

这些是一些比较基础和入门的,还有一些高级一点的,这些技巧可能用的比较少,不过也是很有帮助的。

结合CDPATH的妙用

变量CDPATH定义了目录的搜索路径:

1
$ export CDPATH=/the/path/you/add/:/another/path/

现在,不用输入cd /the/path/you/add/hello/ 这样长了,我可以直接输入下面的命令进入 /the/path/you/add/hello/

1
$ cd html

!$

这个命令目测,用的人不多,其实比较有用,且有效。

1
$ cd !$

表明的意思是将上一个命令的参数作为cd的参数来使用。

用shopt –s cdspell自动纠正cd命令的目录名输入错误

使用shopt -s cdspell可以自动修正cd时拼写错误的目录名。

如果你在输入时经常犯些错误,这个命令是很有用的。详见以下示例:

1
2
3
4
5
6
7
8
9
10
11
# cd /etc/mall

-bash: cd: /etc/mall: No such file or directory

# shopt -s cdspell

# cd /etc/mall

# pwd

/etc/mail

注: 当我错误的把mail敲成了mall,用这个命令mall就自动被换成了mail

最常用的且没有之一的 ls

.. note::
寻寻觅觅,冷冷清清,凄凄惨惨戚戚。
宋 李清照《声声慢·寻寻觅觅》

如果linux命令来个排名,ls命令应该是最常用的命令,除非你像黄蓉的母亲,有过目不忘的本领,惹得黄药师抱憾终身。

ls命令是list的缩写,通过ls命令,我们可以查看目录的内容,确定各种重要文件和目录的属性。

命令格式

1
ls [参数] [路径]

不加任何参数

如果不加任何参数,默认列出当前目录的内容。

1
2
3
4
5
6
$ ls /etc/sysconfig/network-scripts
ifcfg-em1
ifcfg-em2
ifcfg-em3
ifcfg-em4
....

使用-l显示更多细节

-l 就是使用long listing format长格式,来显示更多的内容信息。

1
2
3
4
5
6
7
$ ls -l /etc/sysconfig/network-scripts
total 264
-rw-r--r--. 1 root root 341 Nov 30 10:56 ifcfg-em1
-rw-r--r--. 1 root root 294 May 13 2016 ifcfg-em2
-rw-r--r--. 1 root root 272 May 10 2016 ifcfg-em3
-rw-r--r--. 1 root root 272 May 10 2016 ifcfg-em4
......

使用-t按照时间排序

如果希望看到最近创建的文件,就需要用到-t参数了。

1
2
3
4
5
6
7
$ ls -lt /etc/sysconfig/network-scripts/
total 264
-rw-r--r--. 1 root root 341 Nov 30 10:56 ifcfg-em1
-rw-r--r--. 1 root root 294 May 13 2016 ifcfg-em2
-rw-r--r--. 1 root root 272 May 10 2016 ifcfg-em4
-rw-r--r--. 1 root root 272 May 10 2016 ifcfg-em3
...

使用-r按照时间逆序

如果希望删除很早以前的文件,看到最早创建的文件,就需要用到-r参数了。

1
2
3
4
5
6
7
$ ls -ltr /etc/sysconfig/network-scripts/
total 264
...
-rw-r--r--. 1 root root 272 May 10 2016 ifcfg-em3
-rw-r--r--. 1 root root 272 May 10 2016 ifcfg-em4
-rw-r--r--. 1 root root 294 May 13 2016 ifcfg-em2
-rw-r--r--. 1 root root 341 Nov 30 10:56 ifcfg-em1

使用-S根据文件大小排序

1
2
3
4
5
6
7
$ ls -lS /etc/sysconfig/network-scripts/
total 264
...
-rw-r--r--. 1 root root 341 Nov 30 10:56 ifcfg-em1
-rw-r--r--. 1 root root 294 May 13 2016 ifcfg-em2
-rw-r--r--. 1 root root 272 May 10 2016 ifcfg-em3
-rw-r--r--. 1 root root 272 May 10 2016 ifcfg-em4

查看目录命令pwd

pwd命令的作用是查看当前目录,没有参数,输入后回车即可显示当前绝对路径。

官方定义为:

pwd - print name of current/working directory

所以pwdPrint Working Directory第一个字的缩写。

唯二需要了解的参数如下:

  • -L, --logical:打印逻辑路径,与pwd一致
  • -P, --physical:打印物理路径,这里可以从超级链接直达原处

实例展示

此时比如我们进入一个目录,然后在打印出来,如下:

1
2
3
4
$ cd /etc/sysconfig/network-scripts/

$ pwd
/etc/sysconfig/network-scripts

可以看到pwd将输出完全路径

逻辑与物理路径

比如如下:

1
2
3
4
5
6
7
$ pwd
/opt/test

$ ls -l
总用量 1
lrwxrwxrwx 1 root root 14 Jan 15 2012 dir -> source/dir
drwxrwxrwx 1 root root 14 Jan 15 2012 source

可以看到此时的路径在/opt/test/里面有两个目录sourcedir,其中dir链接到source里面的dir。

接下来对比一下-L和-P的区别。

1
2
3
4
5
6
7
8
9
10
$ cd dir

$ pwd
/opt/test/dir

$ pwd -L
/opt/test/dir

$ pwd -P
/opt/test/source/dir

从上面的输出可以发现,-P参数会显示文件最原始的路径;而-L则是逻辑上的路径。

Linux tree命令

Linux tree命令以树状图列出目录的内容。

执行tree指令,它会列出指定目录下的所有文件,包括子目录里的文件。

官方定义为:

tree - list contents of directories in a tree-like format.

使用方法为:

1
2
3
$ tree  [-acdfghilnpqrstuvxACDFQNSUX] [-L level [-R]] [-H baseHREF] [-T title] [-o filename] [--nolinks] [-P pattern] [-I pat‐
tern] [--inodes] [--device] [--noreport] [--dirsfirst] [--version] [--help] [--filelimit #] [--si] [--prune] [--du]
[--timefmt format] [--matchdirs] [--fromfile] [--] [directory ...]

参数比较多,也比较复杂。其中常用的选项为:

  • -d 显示目录名称而非内容。
  • -D 列出文件或目录的更改时间。

实例 默认显示

默认显示当前目录的信息,比如tree和tree .的含义一样。命令有如下输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ tree   
.
├── a
├── aa
│ ├── aab
│ ├── aac
│ ├── aad
│ └── aae
├── b
├── bb
│ └── bbb
├── c
├── d
├── e
└── f

2 directories, 11 files

只显示目录

1
2
3
4
$ tree -d         
.
├── aa
└── bb

显示具体的修改时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ tree -D
.
├── [Apr 7 22:34] a
├── [Apr 7 22:37] aa
│ ├── [Apr 7 22:35] aab
│ ├── [Apr 7 22:35] aac
│ ├── [Apr 7 22:35] aad
│ └── [Apr 7 22:35] aae
├── [Apr 7 22:34] b
├── [Apr 7 22:39] bb
│ └── [Apr 7 22:39] bbb
├── [Apr 7 22:34] c
├── [Apr 7 22:34] d
├── [Apr 7 22:33] e
└── [Apr 7 22:33] f

2 directories, 11 files

默认情况下tree可能没有安装,可以通过apt/yum install tree来安装。


Linux中最常用的文件操作命令

仅个人想法,会持续不间断更新和改进。

在Linux的庞大世界中,搜索犹如明灯,可以拨开云雾见青天,照亮我们前行大道路。

无论你是在浩瀚的代码库中搜索一个特定的函数,还是在庞大的文件系统中寻找一个文件,搜索命令着实是不可或缺尤为重要的工具。

而其中最绕不开的当属以下几个。


一切皆可查的 find

find命令用来在指定目录下查找文件,功能相当之强大。

官方定义为:

find - search for files in a directory hierarchy

Linux的哲学是一切皆文件,那么find的使命就是一切皆可查

语法

使用语法为:

1
$ find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

比较常用的几个参数为:

  • -exec <执行指令>:假设find指令的回传值为True,就执行该指令;
  • -size <文件大小>:查找符合指定的文件大小的文件;
  • -mtime <24小时>:查找在指定时间曾被更改过的文件或目录,单位以24小时计算;
    -name<范本样式>:指定字符串作为寻找文件或目录的范本样式;
  • -type <文件类型>:只寻找符合指定的文件类型的文件;

无参数

如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件,并且将查找到的子目录和文件全部进行显示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ls -l
total 310M
-rw-rw-r-- 1 user user 10M Mar 21 20:01 a
drwxrwxr-x 2 user user 22 Mar 21 20:01 aa
-rw-rw-r-- 1 user user 100M Mar 21 20:01 b
-rw-rw-r-- 1 user user 200M Mar 21 20:01 c


$ find
.
./a
./b
./c
./test

查找小于,等于和大于100MB的文件

通过-size大小来查找文件

1
2
3
4
5
6
7
8
9
10
$ find . -size -100M
.
./a
./aa
$ find . -size 100M
./b
$ find . -size +100M
./c
./aa/d

查找多长时间修改过

可以通过参数-mtime来查找文件的修改时间,比如如下可以查找当前目录下最近60天没有被修改的文件。

1
2
3
4
$ find . -mtime +60

# 最近2天以内未修改
$ find . –mtime -2

稍微复杂但是很有用的命令

我经常把 find 命令和他的选项 exec一起使用,比如我想查找一个目录中的所有文件并将其更改其权限。可以通过以下简单命令完成:

1
$ find /path/ -type f -exec chmod 644 {} \;

这个命令会递归搜索指定目录内/path/下的所有文件,并对找到的文件执行 chmod 命令。

精准快速定位的locate

.. note::

众里寻他千百度,蓦然回首,那人却在灯火阑珊处
-李煜

Linux locate命令用于查找符合条件的文档、程序、目录等等。这个命令会在数据库中查找符合条件的各种信息。

一般情况我们只需要输入 locate name 即可查找。

官方定义为:

locate - list files in databases that match a pattern

使用方法为:

1
2
3
4
$ locate  [-d  path  |  --database=path]  [-e  | -E | --[non-]existing] [-i | --ignore-case] [-0 | --null] [-c |
--count] [-w | --wholename] [-b | --basename] [-l N | --limit=N] [-S | --statistics] [-r | --regex ] [--regex‐
type R] [--max-database-age D] [-P | -H | --nofollow] [-L | --follow] [--version] [-A | --all] [-p | --print]
[--help] pattern...

看着很复杂,不过常用的参数倒是不多,基本为:

  • -n : 至多显示 n个输出。
  • -i, --ignore-case : 忽略大小写

默认无参数

默认情况下,locate直接跟上需要查找的信息就可以了,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ locate set_vis.cpp
/home/user/mycode/src/set_vis.cpp

# 以查找apropos为例
$ locate apropos
/usr/bin/apropos
/usr/local/difmap/help/apropos.hlp
/usr/share/emacs/24.3/lisp/apropos.elc
/usr/share/man/de/man1/apropos.1.gz
/usr/share/man/es/man1/apropos.1.gz
/usr/share/man/fr/man1/apropos.1.gz
/usr/share/man/id/man1/apropos.1.gz
/usr/share/man/it/man1/apropos.1.gz
/usr/share/man/ja/man1/apropos.1.gz
/usr/share/man/man1/apropos.1.gz
/usr/share/man/nl/man1/apropos.1.gz
/usr/share/man/pl/man1/apropos.1.gz
/usr/share/man/ru/man1/apropos.1.gz

太多需要简单化

如果输出的信息很多,仅仅希望看到前面的几个,使用-n参数既可

1
2
3
4
5
# 仅仅查看前的3个
$ locate -n 3 apropos
/usr/bin/apropos
/usr/local/difmap/help/apropos.hlp
/usr/share/emacs/24.3/lisp/apropos.elc

不区分大小写

部分情况下,可能有大小写混淆的情况,此时使用-i参数既可

1
2
3
$ $ locate -i set_vis.cpp
/home/user/mycode/src/set_vis.cpp
/home/user/mycode_CPP/src/set_VIS.cpp

说明

不过刚按照的系统,这个命令并不一定有输出,主要是因为locatefind 不同, find 直接在硬盘找,而locate 只在数据库中查找。

这个数据库在CentOS系统默认的为 /var/lib/mlocate/mlocate.db 中,所以 locate 的查找会比较快,但并一定是实时的,而是以数据库的更新为准。

可以通过下面的命令手工升级数据库 ,命令为:

1
$ updatedb

然后就可以使用了。

文件内容搜索利器 - grep

Linux grep 命令用于查找文件里符合条件的字符串。

官方定义为:

grep, egrep, fgrep - print lines matching a pattern

grep支持正则表达式,是一个强大的文本搜索工具。

语法

语法也挺复杂,因为功能确实很强大。

1
2
3
$ grep [OPTION...] PATTERNS [FILE...]
$ grep [OPTION...] -e PATTERNS ... [FILE...] # 使用egrep
$ grep [OPTION...] -f PATTERN_FILE ... [FILE...] # 使用fgrep

常用的参数为:

  • -r 或 –recursive : 此参数的效果和指定”-d recurse”参数相同
  • -v 或 –invert-match : 显示不包含匹配文本的所有行
  • -i 或 –ignore-case : 忽略字符大小写的差别
  • -n 或 –line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。

假定有如下3个文件,1个文件夹,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a
This is a
Hello a

b
this is b
Hello b
c
This is c
Hello c

d/d
This is d
Hello d

默认无参数

在当前目录搜索包含is字符串,可以看到**a/b/c**三个文件均有输出,而d因为是目录,暂时无输出。

1
2
3
4
5
$ grep is *
a:This is a
b:this is b
c:This is c
grep: d: Is a directory

增加文件夹

与其他命令类似,增加-r参数,递归搜索

1
2
3
4
5
$ grep -r is *
a:This is a
b:this is b
c:This is c
d/d:This is d

反向查找

在某些情况下,或许正想找到不包含某些字符串的内容,如下:

1
2
3
4
5
$ grep -rv is *
a:Hello a
b:Hello b
c:Hello c
d/d:Hello d

此时可以看到,不包含is的内容显示了出来。

不区分大小写

而某些情况下,或许我们希望找到不区分大小写的内容,比如对于This/this而言:

1
2
3
4
5
6
7
8
9
10
$ grep -r This *
a:This is a
c:This is c
d/d:This is d

$ grep -ri This *
a:This is a
b:this is b
c:This is c
d/d:This is d

可以看到此时有可能笔误,或者其他原因的b文件已经被找到了。

显示行数,精准定位

如果文件内容比较多,此时显示内容在哪一行,是很重要的,加上-n参数既可解决。

1
2
3
4
$ grep -rn This *
a:1:This is a
c:1:This is c
d/d:1:This is d

linux中最常用的帮助命令

仅个人想法,会持续不间断更新和改进。

Linux有好几个关于帮助的命令,可以让我们在不上网的情况下获取一些丰硕的信息。

唯一的要求就是英语好一些,唯二的情况就是不确定的情况下在此确认后再执行。


此man非man的意思

首先,这man是什么意思?

最开始很多人认为是不知道这个什么意思,找man呀。

其实man是manual的缩写,也就是手册的意思。

man命令提供了系统命令的详细帮助信息。

Linux提供了丰富的帮助手册,当你需要查看某个命令的参数时不必到处上网查找,只要man一下即可。这个也是每个程序员必备的功能,在没有网络的情况下,man能解决很多问题和疑惑。

看一下官方定义:

Man - format and display the on-line manual pages

man 的格式

如果要读懂并使用man,首先需要了解man命令输出的格式,下面的几个是比较常用且需要注意的:

同时也可以使用man man 查看man的使用方法。

章节 含义
NAME 命令名称及功能简要说明
SYNOPSIS 用法说明,包括可用的选项
DESCRIPTION 命令功能的详细说明,可能包括每一个选项的意义
OPTIONS 每一选项的意义
EXAMPLES 一些使用示例

man的操作

比如输入man ls 后,跳出下面的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
LS(1)                                               User Commands                                              LS(1)

NAME
ls - list directory contents

SYNOPSIS
ls [OPTION]... [FILE]...

DESCRIPTION
List information about the FILEs (the current directory by default). Sort entries alphabetically if none of
-cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.

-a, --all
do not ignore entries starting with .

-A, --almost-all
do not list implied . and ..

--author
with -l, print the author of each file

-b, --escape
print C-style escapes for nongraphic characters

--block-size=SIZE
scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576
bytes; see SIZE format below

-B, --ignore-backups
Manual page ls(1) line 1 (press h for help or q to quit)

此时可以通过空格键或者回车键来向后翻屏或者翻页,可以使用b或者k向前查看。

 查看关键词时可以使用:

/关键词 向后查找 n:下一个

?关键词 向前查找 N:前一个

可以通过q来退出。

ls后面还有一个(1),详细的解释可以参考《Linux 安装 man 帮助程序》

类似于whatis命令

man有个参数为-f,就是whatis的功能,比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ man -f ls cd file cat more less
ls (1) - list directory contents
ls (1p) - list directory contents
cd (1) - bash built-in commands, see bash(1)
cd (1p) - change the working directory
cd (n) - Change working directory
file (1) - determine file type
file (1p) - determine file type
file (n) - Manipulate file names and attributes
cat (1) - concatenate files and print on the standard output
cat (1p) - concatenate and print files
more (1) - file perusal filter for crt viewing
more (1p) - display files on a page-by-page basis
less (1) - opposite of more
less (3pm) - perl pragma to request less of something

与whatis命令完全一致

类似于apropos命令

man有个参数为-k,就是apropos的功能,比如:

1
2
3
4
5
6
7
8
9
10
11
$ man -k  who
at.allow (5) - determine who can submit jobs via at or batch
at.deny (5) - determine who can submit jobs via at or batch
btrfs-filesystem (8) - command group of btrfs that usually work on the whole filesystem
docker-trust-signer (1) - Manage entities who can sign Docker images
ipsec_newhostkey (8) - generate a new raw RSA authentication key for a host
ipsec_showhostkey (8) - show host's authentication key
w (1) - Show who is logged on and what they are doing.
who (1) - show who is logged on
who (1p) - display who is on the system
whoami (1) - print effective userid

与apropos命令完全一致

使用man的小技巧

如果遇到一个不熟悉或者完全不知道的命令,此时可以通过下面的3个步骤来了解:

  1. 首先用man -k command 查询所有类似帮助文件信息,或许有可能就能找到你需要的信息;
  2. 然后man -f command 查询以command开始的相关帮助信息列表
  3. man N command 通过直接定位N获得详细帮助信息

你是干什么的 whatis

其实整个命令已出现,你的脑海里面应该浮现的是:

What is your name?

如题所述,这个命令用于查询一个命令到底执行了什么功能,并将查询的结果输出出来,相当于man的一个选项-f

whatis的官方定义为:

whatis - display manual page descriptions

仅仅提供一个比较简单的命令描述.

使用方法也比较简单,如下:

1
$ whatis [options] name

其中的name可以是Linux命令、系统调用、库函数、系统等等内容

以前面的命令为例,执行如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ whatis ls cd file cat more less
ls (1) - list directory contents
ls (1p) - list directory contents
cd (1) - bash built-in commands, see bash(1)
cd (1p) - change the working directory
cd (n) - Change working directory
file (1) - determine file type
file (1p) - determine file type
file (n) - Manipulate file names and attributes
cat (1) - concatenate files and print on the standard output
cat (1p) - concatenate and print files
more (1) - file perusal filter for crt viewing
more (1p) - display files on a page-by-page basis
less (1) - opposite of more
less (3pm) - perl pragma to request less of something

可以看到whatis是支持同时查询多个命令的

拓展

whatis可以通过-w-r以及-C等选项来设定通配符、正则表达式以及配置文件等等,不过最简单的还是简单查看一个命令的简单描述,其他的可以交给man来处理。

指定目录的定位 whereis

Linux whereis 命令用于定位查找一个命令的二进制、源文件或帮助文件。

不过这些文件一般是位于特定目录的。

其他的程序定位可以考虑使用locate命令。

官方的定义为:

whereis - locate the binary, source, and manual page files for a command

使用语法

使用语法如下:

1
$ whereis [options] [-BMS directory... -f] name...

其他的选项可以为:

  • -b : 查找二进制文件

  • -m:查找手册

  • -s:查找源文件

  • -B <directory>  在设置的目录下查找二进制文件。

  • -M <directory>  在设置的目录下查找说明文件。

  • -S <directory>  在设置的目录下查找原始代码文件。

实例

比如查找bash的位置,输入如下命令:

1
2
$ whereis bash
bash: /usr/bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

可以看到,以上的输出信息从左至右分别为程序名、bash路径、bash的man帮助手册路径。

单独查找文件

可以通过不同的参数来查找不同的文件,如下:

1
2
3
4
5
6
7
8
9
10
11
# 查找二进制文件
$ whereis -b bash
bash: /usr/bin/bash /etc/bash.bashrc

# 查找帮助文件
$ whereis -m bash
bash: /usr/share/man/man1/bash.1.gz

# 查找源文件
$ whereis -s bash
bash:

刚刚好合适的 apropos 命令

apropos的中文含义就是恰好的、合适的,奈何这个单词或者命令确实不好记,当然是可以扩充词汇量的。

什么时候会用到这个命令呢,先看看这个命令的定义。

apropos 命令的官方定义为:

search the manual page names and descriptions

意思很明显,如果我不记得命令或者不知道该用什么命令的时候,可以通过关键词来索引查找这些命令,比如我们想用linux绘制图像,但是不知道什么命令,测试可以使用:

1
2
3
4
5
6
$ apropos plot

bno_plot (1) – generate interactive 3D plot of IO blocks and sizes
gnuplot (1) – an interactive plotting program
pbmtoplot (1) – convert a PBM image into a Unix 'plot' file

或许每个人的输出不同,这个主要取决于安装的软件包和索引的数据库。以上。

再来一个实例,这个应该大部分的都类似:

1
2
3
4
5
6
7
8
9
10
11
12
$ apropos who

at.allow (5) - determine who can submit jobs via at or batch
at.deny (5) - determine who can submit jobs via at or batch
btrfs-filesystem (8) - command group of btrfs that usually work on the whole filesystem
docker-trust-signer (1) - Manage entities who can sign Docker images
ipsec_newhostkey (8) - generate a new raw RSA authentication key for a host
ipsec_showhostkey (8) - show host's authentication key
w (1) - Show who is logged on and what they are doing.
who (1) - show who is logged on
who (1p) - display who is on the system
whoami (1) - print effective userid

这个命令平时用的不多,跟whatis类似,因为这些功能都被加到了包罗万象的man命令。


Linux最有趣的几个命令

仅个人想法,会持续不间断更新和改进。

这次介绍一些好玩有趣的命令,不过说实话,实用性倒不是很大,纯粹好玩,给单调的命令行界面增加了一些童趣和欢乐。


命运的小火车sl

发现这个命令是在输入ls命令的时候,一不小心敲反了,直接命运的小火车开始跑了起来。

sl 是一个 “Steam Locomotive” 的缩写,它会显示一个老式的蒸汽火车在终端中穿梭。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ sl


( ) (@@) ( ) (@) () @@ O @ O @ O
@)




______ ___________
\__I_I_____===__|_________|
______/ | | =|___ ___| _________________
| | | ||_| |_|| _| \_____A
__--------------------| [___] | =| |
__|_____/[][]~\_______| | -| |
-I_____I [][] [] D |=======|____|________________________|_
=====O=====O\ ____Y___________|__|__________________________|_
|| || |_____/~\___/ |_D__D__D_| |_D__D__D_|
/ \__/ \__/ \_/ \_/ \_/ \_/ \_/


点亮终端的艺术之光figlet

在Linux的世界中,figlet是一个神奇的命令,可以将文字艺术化.

特别适合作为标题、口号或者问候语,还有很多软件程序的招呼语,也可以用这个来实现。

比如:

1
2
3
4
5
6
7
$ figlet HELLO          
_ _ _____ _ _ ___
| | | | ____| | | | / _ \
| |_| | _| | | | | | | | |
| _ | |___| |___| |__| |_| |
|_| |_|_____|_____|_____\___/

活灵活现的终端:发掘cowsay

cowsay顾名思义就是一头牛为你加持,一款让命令行界面生动有趣的工具。

cowsay 可说话、可思考,与fortune加持更可以变为一头睿智的牛。

该命令接受一个文本字符串,并输出一个牛说话的图形。

下面是一头牛在说它喜欢 Linux:

1
2
3
4
5
6
7
8
9
$  cowsay I love linux.
_______________
< I love linux. >
---------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

探索Linux世界的智慧——fortunes命令

在Linux中,有一条神奇的命令连接着智慧与幽默,那就是fortunes命令。

看似普通的指令,背后却藏匿着千言万语,无论是名人箴言还是妙趣横生的笑话,在这里都能找到它们的踪迹。

另外最重要的,还可以根据自己的需求进行增删,目前就用基于唐诗宋词的库。

fortune并非只是简单的一句话,而是承载着古今中外智慧的涌泉。

每次执行,它都会带来截然不同的感受。或许是一位哲人的深刻格言,或是一句调皮的笑话,或者唐诗,或者宋词,与先贤对话,岂不快哉。

最简单的方法就是把这个命令,加到.bashrc文件中,每次启动,总会有触动。

1
2
3
4
5
6
7
8
9
10
$ fortune 
何以称英雄?识以领其先

    - 清·袁枚
$ fortune
一件作品的固有力量从来不会被长期地埋没或禁锢。一件艺术品可能被时间遗忘,可能遭到查禁,可能被埋进棺材,但威力强大的东西总要战胜没有过大前途的东西。

    - 茨威格
$ fortune
Yow! We're going to a new disco!

缤纷绚烂的终端体验:探索lolcat

lolcat是一款让终端从黑白灰变得缤纷多彩的神奇工具。

通过将文字渲染成彩虹般的颜色,让你的终端充满欢乐与活力。

可以把这个命令替换掉cat,这样你的内容都灵动了起来

比如lolcat /etc/resolv.conf会生成如下信息:

进入数字的魔幻世界:探秘cmatrix

来到黑客帝国的命令cmatrix,这不仅仅是一串字符在终端中的运动,它仿佛是数字的舞蹈,变幻着无穷的图案。从绿色的数字雨到模拟Matrix电影的效果,cmatrix让我们进入了数字的奇妙世界。

显示系统风貌的screenfetch命令

screenfetch命令的神奇之处在于其简单而又直观的功能,该命令能够快速地收集系统信息并以一种富有个性的方式展示出来。

从使用的发行版到内核版本,再到处理器和内存,一目了然地展现系统的全貌。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ screenfetch
./+o+- oper@localhost
yyyyy- -yyyyyy+ OS: Ubuntu
://+//////-yyyyyyo Kernel: aarch64 Linux 6.4.16-linuxkit
.++ .:/++++++/-.+sss/` Uptime: 33m
.:++o: /++++++++/:--:/- Packages: 134
o:+o+:++.`..```.-/oo+++++/ Shell: bash 5.1.16
.:+o:+o/. `+sssoo+/ Disk: 32G / 59G (57%)
.++/+:+oo+o:` /sssooo. CPU: 12x Apple
/+++//+:`oo+o /::--:. RAM: 877MiB / 7844MiB
\+/+o+++`o++o ++////.
.++.o+++oo+:` /dddhhh.
.+.o+oo:. `oddhhhh+
\+.++o+o``-````.:ohdhhhhh+
`:o+++ `ohhhhhhhhyo++os:
.o:`.syhhhhhhh/.oo++o`
/osyyyyyyo++ooo+++/
````` +oo+++o\:
`oo++.