开发者

Searching for information in files in several directories

开发者 https://www.devze.com 2023-04-09 19:25 出处:网络
I need to check several files which are in different locations for a specific informat开发者_StackOverflowion.

I need to check several files which are in different locations for a specific informat开发者_StackOverflowion.

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/

/opt/log/

/var/status/


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

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


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.

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


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


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


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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消