Unix: Searching for a word in files situated at different paths

My requirenment is to Search the logs and find some specific entry. Log files are situated at two different paths. And I want to search in both.

Usually I login to server, go to the path and execute grep -i word *filename*

I want to prepare a script which will accept input from users for word and filename and search for it.


您可以根据需要搜索尽可能多的路径:

grep -i "word" *filename* /some/other/path/*filename*

像这样的东西应该有所帮助

#!/bin/sh
#Purpose: To find a given text in a file
if [ -z "$3" ]
then
  echo Usage: $0 DirectoryToSearchIn testToSearch filePattern  
  exit 1 
fi

find $1  -name '$3' | while read f
do   
  cat "$f" | grep "$3" && echo "[in $f]"
done

With find and grep you can do it over multiple directories (without knowning the directory name before)

find /path/to/files -type f -name '*filename*' -exec grep -i word /dev/null {} ;

This example finds files that contain filename (but could be anything) . I also added a sample path but you can use . in order to start the search in the current directories. The -type f returns files (not directories).

OR you can use recursive grep, as pointed out here.

grep -rnw 'directory' -e "*filename*"
链接地址: http://www.djcxy.com/p/13680.html

上一篇: 删除一个符号链接到一个目录

下一篇: Unix:在位于不同路径的文件中搜索单词