Linux开发

4-16 Linux中的管道符

2021-12-19  本文已影响0人  捌千里路雲和月

1、管道符用 | (一竖)表示。

2、管道符的作用:

3、实操练习:

[root@localhost ~]# ll /etc/ | more
total 1076
-rw-r--r--.  1 root root     16 Mar  5  2021 adjtime
-rw-r--r--.  1 root root   1529 Apr  1  2020 aliases
-rw-r--r--.  1 root root  12288 Mar 22  2021 aliases.db
drwxr-xr-x.  2 root root    236 Mar 22  2021 alternatives
-rw-------.  1 root root    541 Aug  9  2019 anacrontab
-rw-r--r--.  1 root root     55 Aug  8  2019 asound.conf
drwxr-x---.  3 root root     43 Mar 22  2021 audisp
drwxr-x---.  3 root root     83 Mar 22  2021 audit
drwxr-xr-x.  2 root root     46 Apr  2  2021 bash_completion.d
-rw-r--r--.  1 root root   2853 Apr  1  2020 bashrc
drwxr-xr-x.  2 root root      6 Feb  3  2021 binfmt.d
-rw-r--r--.  1 root root     37 Nov 23  2020 centos-release
-rw-r--r--.  1 root root     51 Nov 23  2020 centos-release-upstream
drwxr-xr-x.  2 root root      6 Oct 13  2020 chkconfig.d
drwxr-xr-x.  2 root root     21 Mar 22  2021 cron.d
drwxr-xr-x.  2 root root     42 Mar 22  2021 cron.daily
-rw-------.  1 root root      0 Aug  9  2019 cron.deny
drwxr-xr-x.  2 root root     22 Mar 22  2021 cron.hourly
drwxr-xr-x.  2 root root      6 Jun 10  2014 cron.monthly
--More--      ## <---- more 分页显示

②、利用管道符配合 grep,列出指定关键字的目录和文件。

[root@localhost ~]# ll /etc/ | grep cron
-rw-------.  1 root root    541 Aug  9  2019 anacrontab
drwxr-xr-x.  2 root root     21 Mar 22  2021 cron.d
drwxr-xr-x.  2 root root     42 Mar 22  2021 cron.daily
-rw-------.  1 root root      0 Aug  9  2019 cron.deny
drwxr-xr-x.  2 root root     22 Mar 22  2021 cron.hourly
drwxr-xr-x.  2 root root      6 Jun 10  2014 cron.monthly
-rw-r--r--.  1 root root    457 Oct 19 15:10 crontab
drwxr-xr-x.  2 root root      6 Jun 10  2014 cron.weekly
[root@localhost ~]# 

[root@localhost ~]# 
[root@localhost ~]# ll /etc/ | grep cron | grep '^d'
drwxr-xr-x.  2 root root     21 Mar 22  2021 cron.d
drwxr-xr-x.  2 root root     42 Mar 22  2021 cron.daily
drwxr-xr-x.  2 root root     22 Mar 22  2021 cron.hourly
drwxr-xr-x.  2 root root      6 Jun 10  2014 cron.monthly
drwxr-xr-x.  2 root root      6 Jun 10  2014 cron.weekly
[root@localhost ~]# 

[root@localhost ~]# ll /etc/ | grep cron | grep '^-'
-rw-------.  1 root root    541 Aug  9  2019 anacrontab
-rw-------.  1 root root      0 Aug  9  2019 cron.deny
-rw-r--r--.  1 root root    457 Oct 19 15:10 crontab
[root@localhost ~]# 

## 方法 2:目录取反,因为除了目录剩下就是文件。取反用 grep -v
[root@localhost ~]# ll /etc/ | grep cron | grep -v '^d'
-rw-------.  1 root root    541 Aug  9  2019 anacrontab
-rw-------.  1 root root      0 Aug  9  2019 cron.deny
-rw-r--r--.  1 root root    457 Oct 19 15:10 crontab
[root@localhost ~]# 

③、管道符还经常配合 ps aux 查看进程使用。

[root@localhost ~]# 
[root@localhost ~]# ps aux | grep cron
root        594  0.0  0.0 126384  1664 ?        Ss   09:05   0:00 /usr/sbin/crond -n
root       1836  0.0  0.0 112812   972 pts/0    R+   16:23   0:00 grep --color=auto cro
[root@localhost ~]# 

## cron 进程输出重定向到 cron.txt 文件
[root@localhost ~]# ps aux | grep cron > cron.txt
[root@localhost ~]# 
[root@localhost ~]# cat cron.txt 
root        594  0.0  0.0 126384  1664 ?        Ss   09:05   0:00 /usr/sbin/crond -n
root       1838  0.0  0.0 112812   972 pts/0    R+   16:26   0:00 grep --color=auto cron
[root@localhost ~]# 

上一篇 下一篇

猜你喜欢

热点阅读