每天2分钟学习unix/linux系统shell编程(六)find,netstat,ln,管道

内容提要,这一期将要学习的是find,通配符,netstat,ln,管道等仍然是一些常用命令,后续会介绍一下linux的基本环境变量和主要目录介绍,后面还要再简要学习下vi,在linux系统开做开发,不会vi也不太行。因为它太常用了。下面开始。

一、shell命令:find
命令:find
作用:查询文件命令,这个比较常用,常用于搜索文件名包含某关键字的文件
选项:用表格来展示

-name 后面跟上要匹配的名字或者通配符

-atime 后面跟上-n表示近n天访问过的文件

-type 按类型查找,后面跟上类型

c字符设备文件

b块设备文件

l连接文件

d目录

f一般文件

参数:要搜索的目录
使用示例:

root@debian:~/test# ls
a.sh  b.txt  c  d  d.log  e.abc  g.txt
root@debian:~/test# find . -name "*.txt"
./g.txt
./b.txt
root@debian:~/test# find . -type f
./d.log
./g.txt
./a.sh
./b.txt
./e.abc
root@debian:~/test# find . -type d
.
./d
./d/b
./d/b/c
./c
./c/b
./c/b/c
root@debian:~/test# find . -atime -7
.
./d.log
./g.txt
./d
./d/b
./d/b/c
./a.sh
./c
./c/b
./c/b/c
./b.txt
./e.abc
root@debian:~/test# 

二、shell中的通配符
通配符意思就是文件名匹配某一规则被选中,然后可以批做一些事情。

  • 可以代表任意多个任意字符

[] 可以通配一个字符,例如:

<1> cp file[135] ~ 这个意思复制file1,file3,file5到用户自己的目录(~)

<2>rm file_[a-z] 这个意思是删除file_a,file_b,file_c,,,,直接到file_z所有文件

? 匹配任意一个字符

{}直接举例:

<1>{c1,c2} 匹配c1或者c2

<2>{[0-9]abc,[efg]}匹配0abc,1abc,,,,直接9abc,或者单独匹配e,f,g的对象。

使用示例:

root@debian:~/test# ls ./*.txt
./b.txt  ./g.txt
root@debian:~/test# ls ./[bg].txt
./b.txt  ./g.txt
root@debian:~/test# ls ./?.txt
./b.txt  ./g.txt
root@debian:~/test# ls ./{b,g}.txt
./b.txt  ./g.txt
root@debian:~/test# touch file{1,3,5}
root@debian:~/test# ls
a.sh  b.txt  c  d  d.log  e.abc  file1  file3  file5  g.txt
root@debian:~/test# 

三、shell命令:netstat
命令:netstat
作用:查看系统网状态
选项:用表格来展示,只列出了较为常用的

-a 显示当前系统中所有的网络连接

-A 后面跟上网络类型t表示tcp,u表示udp,列出相应网络类型的连接

-n 直接使用ip地址,而不通过域名服务器

-p 显示正在使用Socket的程序pid和程序名称

-u 显示UDP传输协议的网络连接状况

-t 显示TCP传输协议的网络连接状况

参数:无
使用示例

root@debian:/home/test# netstat -naput
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:32947         0.0.0.0:*               LISTEN      501/containerd      
tcp        0      0 10.0.2.15:50208         60.191.80.15:80         TIME_WAIT   -                   
tcp6       0      0 :::5000                 :::*                    LISTEN      484/docker-registry 
udp        0      0 0.0.0.0:49831           0.0.0.0:*                           416/avahi-daemon: r 
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           416/avahi-daemon: r 
udp        0      0 10.0.2.15:68            10.0.2.2:67             ESTABLISHED 419/NetworkManager  
udp6       0      0 :::5353                 :::*                                416/avahi-daemon: r 
udp6       0      0 :::54515                :::*                                416/avahi-daemon: r 
root@debian:/home/test# netstat -naput|grep 80
tcp        0      0 10.0.2.15:50208         60.191.80.15:80         TIME_WAIT   -                   
root@debian:/home/test# netstat -naput|grep ESTABLISHED
udp        0      0 10.0.2.15:68            10.0.2.2:67             ESTABLISHED 419/NetworkManager  
root@debian:/home/test# 

注意:netstat配合|管道和grep命令可以搜索到自己想要的结果,较为常用。

四、shell命令:ln
命令:ln
作用:为文件建立连接
选项:-s表示软件,不加-s表示硬连接
参数:源文件 目标连接名
解释说明:linux的文件连接分为硬连接和软连接(符号)连接。

硬连接:通过文件的索引节点进行链接。一个文件的索引节点对应多个文件名,当文件名都删除时文件才会被删除。不太常用。

软连接:软链接文件本身仅仅存储指向源文件的位置信息。常用。

使用示例:

root@debian:~/test# ln -s g.txt g2.txt
root@debian:~/test# ls -l
总用量 16
-rwxr-xr-x 1 root root   67 11月 16 23:54 a.sh
-rw-r--r-- 1 root root    0 11月 14 23:37 b.txt
drwxr-xr-x 3 root root 4096 11月 15 23:20 c
drwxr-xr-x 3 root root 4096 11月 15 23:13 d
-rw-r--r-- 1 root root    0 11月 15 23:05 d.log
-rw-r--r-- 1 root root    0 11月 15 23:05 e.abc
-rw-r--r-- 1 root root    0 11月 17 22:21 file1
-rw-r--r-- 1 root root    0 11月 17 22:21 file3
-rw-r--r-- 1 root root    0 11月 17 22:21 file5
lrwxrwxrwx 1 root root    5 11月 17 22:41 g2.txt -> g.txt
-rw-r--r-- 1 root root 3888 11月 16 23:56 g.txt
root@debian:~/test# tail -n 2 g2.txt 
998
999
root@debian:~/test# tail -n 2 g.txt 
998
999
root@debian:~/test# rm g2.txt 
root@debian:~/test# ls
a.sh  b.txt  c  d  d.log  e.abc  file1  file3  file5  g.txt
root@debian:~/test# ln g.txt g2.txt
root@debian:~/test# ls
a.sh  b.txt  c  d  d.log  e.abc  file1  file3  file5  g2.txt  g.txt
root@debian:~/test# ls -l
总用量 20
-rwxr-xr-x 1 root root   67 11月 16 23:54 a.sh
-rw-r--r-- 1 root root    0 11月 14 23:37 b.txt
drwxr-xr-x 3 root root 4096 11月 15 23:20 c
drwxr-xr-x 3 root root 4096 11月 15 23:13 d
-rw-r--r-- 1 root root    0 11月 15 23:05 d.log
-rw-r--r-- 1 root root    0 11月 15 23:05 e.abc
-rw-r--r-- 1 root root    0 11月 17 22:21 file1
-rw-r--r-- 1 root root    0 11月 17 22:21 file3
-rw-r--r-- 1 root root    0 11月 17 22:21 file5
-rw-r--r-- 2 root root 3888 11月 16 23:56 g2.txt
-rw-r--r-- 2 root root 3888 11月 16 23:56 g.txt
root@debian:~/test# rm g2.txt 
root@debian:~/test# ls -l
总用量 16
-rwxr-xr-x 1 root root   67 11月 16 23:54 a.sh
-rw-r--r-- 1 root root    0 11月 14 23:37 b.txt
drwxr-xr-x 3 root root 4096 11月 15 23:20 c
drwxr-xr-x 3 root root 4096 11月 15 23:13 d
-rw-r--r-- 1 root root    0 11月 15 23:05 d.log
-rw-r--r-- 1 root root    0 11月 15 23:05 e.abc
-rw-r--r-- 1 root root    0 11月 17 22:21 file1
-rw-r--r-- 1 root root    0 11月 17 22:21 file3
-rw-r--r-- 1 root root    0 11月 17 22:21 file5
-rw-r--r-- 1 root root 3888 11月 16 23:56 g.txt
root@debian:~/test# 

五、shell管道
管道从字面意思解释就是通道,类似一个水管。就是把上一条件的输出做为下一个命令的输入。

使用示例:

root@debian:~/test# cat g.txt | grep 983
983
root@debian:~/test# 
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容