在多个目录中搜索文件中的信息。

时间:2021-12-12 10:00:52

I need to check several files which are in different locations for a specific information.

我需要检查位于不同位置的几个文件以获得特定的信息。

So, how to make a script which checks for the argument word through several directories?

那么,如何创建一个脚本,通过几个目录检查参数字?

The directories are in different locations. For ex.

目录在不同的位置。前女友。

/home/check1/

/home/check1/

/opt/log/

/ opt /日志/

/var/status/

/var/status/

5 个解决方案

#1


1  

You could also do (next to ´find´) do a

你也可以做(´旁边找到´)做一个

for DIR in /home/check1 /opt/log /var/status ; do 
    grep -R searchword $DIR; 
    done

#2


1  

At the very simplest, it boils down to

最简单的说,就是

find . -name '*.c' | xargs grep word

to find a given word in all the .c files in the current directory and below.

在当前目录和下面的所有.c文件中查找给定的单词。

grep -R may also work for you, but it can be a problem if you don't want to search all files.

grep -R可能也适用于您,但是如果您不想搜索所有的文件,那么它可能是一个问题。

#3


1  

Use the grep -R (recursive) option and give grep multiple directory arguments.

使用grep -R(递归)选项并为grep提供多个目录参数。

#4


0  

Try find http://content.hccfl.edu/pollock/Unix/FindCmd.htm using your searchwords and the directories.

尝试使用搜索词和目录查找http://content.hccfl.edu/pollock/Unix/FindCmd.htm。

#5


0  

The man page of grep should explain what you need. Anyway, if you need to search recursively you can use:

grep的man页面应该解释您需要什么。无论如何,如果你需要递归搜索,你可以使用:

grep -R --include=PATTERN "string_to_search" $directory

You can also use:

您还可以使用:

--exclude=PATTERN to skip some file
--exclude-dir=PATTERN to skip some directories

The other option is use find to get the files and pipe it to grep to search the strings.

另一种选择是使用find获取文件并将其传输到grep中以搜索字符串。

#1


1  

You could also do (next to ´find´) do a

你也可以做(´旁边找到´)做一个

for DIR in /home/check1 /opt/log /var/status ; do 
    grep -R searchword $DIR; 
    done

#2


1  

At the very simplest, it boils down to

最简单的说,就是

find . -name '*.c' | xargs grep word

to find a given word in all the .c files in the current directory and below.

在当前目录和下面的所有.c文件中查找给定的单词。

grep -R may also work for you, but it can be a problem if you don't want to search all files.

grep -R可能也适用于您,但是如果您不想搜索所有的文件,那么它可能是一个问题。

#3


1  

Use the grep -R (recursive) option and give grep multiple directory arguments.

使用grep -R(递归)选项并为grep提供多个目录参数。

#4


0  

Try find http://content.hccfl.edu/pollock/Unix/FindCmd.htm using your searchwords and the directories.

尝试使用搜索词和目录查找http://content.hccfl.edu/pollock/Unix/FindCmd.htm。

#5


0  

The man page of grep should explain what you need. Anyway, if you need to search recursively you can use:

grep的man页面应该解释您需要什么。无论如何,如果你需要递归搜索,你可以使用:

grep -R --include=PATTERN "string_to_search" $directory

You can also use:

您还可以使用:

--exclude=PATTERN to skip some file
--exclude-dir=PATTERN to skip some directories

The other option is use find to get the files and pipe it to grep to search the strings.

另一种选择是使用find获取文件并将其传输到grep中以搜索字符串。