Linux系统编程 10 根据内容检索 grep

学习笔记
根据内容检索
find的是以文件检索

grep -r 'copy' ./
-r是递归,也会到子目录去找

8.2 ps命令
监控后台进程的工作情况
-A 显示所有程序
-e -A相同
-u 查看用户所有者及一些详细信息
-x 显示没有控制终端的进程

$ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  1.2  0.0  34048  3332 ?        Ss   23:02   0:05 /sbin/init


和grep结合

$ps aux|grep 'message'
message+    867  0.1  0.0  40616  2508 ?        Ss   23:02   0:00 dbus-daemon --system --fork
ubuntu     2672  0.0  0.0 330896  2960 ?        Ssl  23:03   0:00 /usr/lib/x86_64-linux-gnu/indicator-messages/indicator-messages-service
ubuntu     3240  0.0  0.0  11756   924 pts/6    S+   23:10   0:00 grep --color=auto message  


ubuntu     3240  0.0  0.0  11756   924 pts/6    S+   23:10   0:00 grep --color=auto message  
这个是grep搜索命令自己。
所以如果搜索命令只有一条,
那么可以认为没有这条记录。


$ps aux|grep 'message'中
ps aux执行的结果是一个结果集。
结果集通过管道的东西| 交给grep

find ./ -type f | xargs ls -l 
$find ./ -type f -exec ls -l {} \;
-rwxrwxrwx 1 root root 32151 Nov 29 00:53 ./dir1/stdio.h
-rwxrwxrwx 1 root root 154 Nov 29 00:38 ./dir1/file1
-rwxrwxrwx 2 1001 root 13 Nov 30 12:32 ./file1
-rwxrwxrwx 2 1001 root 13 Nov 30 12:32 ./file1.h
$find ./ -maxdepth 1 -type f -exec ls -l {} \;
-rwxrwxrwx 2 1001 root 13 Nov 30 12:32 ./file1
-rwxrwxrwx 2 1001 root 13 Nov 30 12:32 ./file1.h
-rw-rw-r-- 1 ubuntu ubuntu 83 Nov 29 22:04 ./file2
$find ./ -type f | xargs ls -l 
-rwxrwxrwx 1 root   root     154 Nov 29 00:38 ./dir1/file1
-rwxrwxrwx 1 root   root   32151 Nov 29 00:53 ./dir1/stdio.h
-rwxrwxrwx 2   1001 root      13 Nov 30 12:32 ./file1
-rwxrwxrwx 2   1001 root      13 Nov 30 12:32 ./file1.h
-rw-rw-r-- 1 ubuntu ubuntu    83 Nov 29 22:04 ./file2


touch abc\ d  文件名中有空格
这个时候,可以发现xargs的小缺陷

$find ./ -type f | xargs ls -l 
$find ./ -type f -exec ls -l {} \;
-rw-rw-r-- 1 ubuntu ubuntu 0 Nov 30 23:25 ./abc de
-rwxrwxrwx 1 root root 32151 Nov 29 00:53 ./dir1/stdio.h
-rwxrwxrwx 1 root root 154 Nov 29 00:38 ./dir1/file1
-rwxrwxrwx 2 1001 root 13 Nov 30 12:32 ./file1
-rwxrwxrwx 2 1001 root 13 Nov 30 12:32 ./file1.h
-rw-rw-r-- 1 ubuntu ubuntu 83 Nov 29 22:04 ./file2
$find ./ -type f| xargs ls -l 
ls: cannot access ./abc: No such file or directory
ls: cannot access de: No such file or directory
-rwxrwxrwx 1 root   root     154 Nov 29 00:38 ./dir1/file1
-rwxrwxrwx 1 root   root   32151 Nov 29 00:53 ./dir1/stdio.h
-rwxrwxrwx 2   1001 root      13 Nov 30 12:32 ./file1
-rwxrwxrwx 2   1001 root      13 Nov 30 12:32 ./file1.h
-rw-rw-r-- 1 ubuntu ubuntu    83 Nov 29 22:04 ./file2

错误的原因是:abc de文件中的空格,被xargs误拆分
解决误拆分的方法:不让他用空格作为拆分的依据

xargs 默认是以空白字符 (空格, TAB, 换行符) 来分割记录的, 
因此文件名 abc de 被解释成了两个记录 file 和de, 不幸的是 rm 找不到这两个文件.
为了解决此类问题, 聪明的人想出了一个办法, 
让 find 在打印出一个文件名之后接着输出一个 NULL 字符('\0') 而不是换行符,
然后再告诉 xargs 也用 NULL 字符来作为记录的分隔符. 
这就是 find 的 -print0 和xargs 的 -0 的来历吧.

 $find ./ -type f -maxdepth 1 | xargs -print0 ls -l
$ls
abc de  dir1  file1.h  file2
$find ./ -type f -maxdepth 1 | xargs -print0 ls -l
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

ls -l ?...y
ls -l ?...total 12
-rw-rw-r-- 1 ubuntu ubuntu    0 Nov 30 23:53 abc de
drwxrwxrwx 2 root   root   4096 Nov 30 23:40 dir1
-rwxrwxrwx 1   1001 root     13 Nov 30 12:32 file1.h
-rw-rw-r-- 1 ubuntu ubuntu   83 Nov 29 22:04 file2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值