天行健,君子以自强不息;地势坤,君子以厚德载物;

CentOS 6 和CentOS 7 中grep命令的递归参数差异

习惯于用grep命令检索文件中是否包含某关键字,对某个目录中所有文件内关键字的检索,习惯于用“-r”参数操作,偶然发现这个参数在CentOS 6和CentOS 7下的意义已经不同了。

先看CentOS 7场景:

~ $  cat /etc/centos-release
CentOS Linux release 7.2.1511 (Core) 
~ $  ls -al /usr/local/nginx/conf/include/
total 24
drwxr-xr-x 2 root root 4096 Mar  6  2019 ./
drwxr-xr-x 6 root root 4096 Mar  6  2019 ../
-rwxr-xr-x 1 root root  672 Mar  6  2019 default_server.conf*
lrwxrwxrwx 1 root root   41 Dec 18  2017 j.conf -> /tmp/j.nginx*

~ $  grep -r -i -P '^([ \t]*)ssl_certificate' /usr/local/nginx/conf/include/
/usr/local/nginx/conf/include/default_server.conf:      ssl_certificate                 certs/default_server.crt;
/usr/local/nginx/conf/include/default_server.conf:      ssl_certificate_key             certs/default_server.key;

~ $  grep -r -i -P '^([ \t]*)ssl_certificate' /usr/local/nginx/conf/include/j.conf
        ssl_certificate                certs/quwenqing.com.crt;
        ssl_certificate_key            certs/quwenqing.com.key;

j.conf文件中明明有ssl_certificate关键字,对目录级检索,却没有找到。

再看一下CentOS 6场景:

~ $  cat /etc/centos-release 
CentOS release 6.8 (Final)
~ $  ls -al /usr/local/nginx/conf/include/
total 24
drwxr-xr-x 2 root root 4096 Mar  6  2019 ./
drwxr-xr-x 6 root root 4096 Mar  6  2019 ../
-rwxr-xr-x 1 root root  672 Mar  6  2019 default_server.conf*
lrwxrwxrwx 1 root root   41 Dec 18  2017 j.conf -> /tmp/j.nginx*

~ $  grep -r -i -P '^([ \t]*)ssl_certificate' /usr/local/nginx/conf/include/
/usr/local/nginx/conf/include/default_server.conf:      ssl_certificate                 certs/default_server.crt;
/usr/local/nginx/conf/include/default_server.conf:      ssl_certificate_key             certs/default_server.key;
/usr/local/nginx/conf/include/j.conf:     ssl_certificate                certs/quwenqing.com.crt;
/usr/local/nginx/conf/include/j.conf:     ssl_certificate_key            certs/quwenqing.com.key;

可以在目录级检索中正确查找到关键字。

仔细看上面场景,同样执行 grep -r -i -P '^([ \t]*)ssl_certificate' /usr/local/nginx/conf/include/ 这个命令CentOS 6中可以查到软链文件中的关键字,而CentOS 7中却没有查询到。

最好的解惑方式是看帮助手册,果然有收获。

## CentOS 6
~ $  man grep
       -R, -r, --recursive
              Read all files under each directory, recursively; this is equivalent to the -d recurse option.

## CentOS 7
~ $  man grep
       -r, --recursive
              Read  all  files  under each directory, recursively, following symbolic links only if they are on the command line.  This is equivalent to the -d recurse
              option.

       -R, --dereference-recursive
              Read all files under each directory, recursively.  Follow all symbolic links, unlike -r.

原来,在CentOS 7中对-r、-R参数功能做了拆分,适用于不同的应用场景。

点赞

发表评论