grep如何--include匹配此文件(当我不想要它时)

时间:2022-01-12 06:13:48

Ok I'm not sure I understand why this grep line is matching files that are not included in the --include option. I've tried escaping a number of ways however can't seem to get it to do what I want.

好吧我不确定我理解为什么这个grep行匹配--include选项中没有包含的文件。我试过逃避了许多方法,但似乎无法让它做我想要的。

Dir listing

ryan@ubuntu:~/dir$ ls -lR
.:
total 20
-rw-rw-r-- 1 ryan ryan   19 Sep 23 00:40 bar
-rw-rw-r-- 1 ryan ryan   19 Sep 23 06:18 bar.shit
drwxrwxr-x 2 ryan ryan 4096 Sep 23 04:51 boo
-rw-rw-r-- 1 ryan ryan    4 Sep 23 05:25 foo.txt
-rwxrwxr-x 1 ryan ryan 1597 Sep 23 06:18 rfind
lrwxrwxrwx 1 ryan ryan   13 Sep 23 00:30 ruby -> /usr/bin/ruby

./boo:
total 4
-rw-rw-r-- 1 ryan ryan  0 Sep 23 04:51 bar.foo.txt
-rw-rw-r-- 1 ryan ryan 86 Sep 23 00:44 foo
-rw-rw-r-- 1 ryan ryan  0 Sep 23 04:51 foo.txt

Running the following grep line match file 'bar' when I'm expecting it to only match the file extensions I've picked via --include

运行以下grep行匹配文件'bar'时,我希望它只匹配我通过的文件扩展名--include

ryan@ubuntu:~/ass2$ grep --include=*.{rb,erb,js,css,html,yml,txt} -inHR foo ./*
./bar:1:inside here is foo
./bar.shit:1:inside here is foo

I'm either not escaping something right or missing how the file with no extension is becoming valid.

我要么没有正确地逃避某些事情,要么错过了没有扩展名的文件如何变得有效。

Thanks

2 个解决方案

#1


1  

This happens because of the glob ./* in the end of the command, which causes all files in the current directory to be included, without considering the --include directive. If you change your command to

这是因为命令末尾的glob ./*导致包含当前目录中的所有文件,而不考虑--include指令。如果您将命令更改为

grep --include=*.{rb,erb,js,css,html,yml,txt} -inHR foo .
#                                                       ^- Instead of ./*

then you should get the output you expect.

那么你应该得到你期望的输出。

#2


0  

It expands itself with glob expansion with the shell since it's in the open. The proper way to do that is:

由于它处于开放状态,因此它通过外壳扩展与外壳进行扩展。正确的方法是:

grep --include='*.rb' --include='*.erb' --include='*.js' --include='*.css' --include='*.html' --include='*.yml' --include='*.txt' -inHR foo ./*

#1


1  

This happens because of the glob ./* in the end of the command, which causes all files in the current directory to be included, without considering the --include directive. If you change your command to

这是因为命令末尾的glob ./*导致包含当前目录中的所有文件,而不考虑--include指令。如果您将命令更改为

grep --include=*.{rb,erb,js,css,html,yml,txt} -inHR foo .
#                                                       ^- Instead of ./*

then you should get the output you expect.

那么你应该得到你期望的输出。

#2


0  

It expands itself with glob expansion with the shell since it's in the open. The proper way to do that is:

由于它处于开放状态,因此它通过外壳扩展与外壳进行扩展。正确的方法是:

grep --include='*.rb' --include='*.erb' --include='*.js' --include='*.css' --include='*.html' --include='*.yml' --include='*.txt' -inHR foo ./*