当前位置: 技术问答>linux和unix
文件查找问题
来源: 互联网 发布时间:2015-05-10
本文导语: 现在我想找一个文件,这个文件里面包含字符串“slewing",但是这个文件所在的目录很深,我也不知道这个目录,只知道根目录是/usr2,请问如何在终端(命令行下)查找? | #!/bin/bash ##############...
现在我想找一个文件,这个文件里面包含字符串“slewing",但是这个文件所在的目录很深,我也不知道这个目录,只知道根目录是/usr2,请问如何在终端(命令行下)查找?
|
#!/bin/bash
##########################################################
# Usage: search #
# Disigned by mengge #
##########################################################
#给定的参数个数不等于2就返回
if [ $# -ne 2 ]; then
/bin/echo "Usage: $0 "
exit 1;
fi
#自定义一个函数,如果当前的文件是一个目录的话就直接进到这个目录搜索
isdir(){
cd $1
for f in `/bin/ls `
do
if [ -f $f ]; then
[ `/bin/grep $KeyWord $f |/usr/bin/wc -C ` -gt 0 ] && /bin/echo `/bin/pwd `"/"$f
fi
if [ -d $f ]; then
isdir $f
fi
done
cd ..
}
#将第一、第二个参数分别赋给变量PATH、KeyWord
PATH=$1
KeyWord=$2
if [ -f $1 ]; then
#如果第一个参数是文件的话直接就在该文件中搜索字符串
[ `/bin/grep $KeyWord $1 |/usr/bin/wc -C ` -gt 0 ] && /bin/echo `/bin/pwd `"/"$f
exit 0;
elif [ -d $1 ]; then
isdir $1
exit 0;
else
/bin/echo "Please check:"
/bin/echo " you input is not a file or a directory..."
exit 1;
fi
#The End.
##########################################################
# Usage: search #
# Disigned by mengge #
##########################################################
#给定的参数个数不等于2就返回
if [ $# -ne 2 ]; then
/bin/echo "Usage: $0 "
exit 1;
fi
#自定义一个函数,如果当前的文件是一个目录的话就直接进到这个目录搜索
isdir(){
cd $1
for f in `/bin/ls `
do
if [ -f $f ]; then
[ `/bin/grep $KeyWord $f |/usr/bin/wc -C ` -gt 0 ] && /bin/echo `/bin/pwd `"/"$f
fi
if [ -d $f ]; then
isdir $f
fi
done
cd ..
}
#将第一、第二个参数分别赋给变量PATH、KeyWord
PATH=$1
KeyWord=$2
if [ -f $1 ]; then
#如果第一个参数是文件的话直接就在该文件中搜索字符串
[ `/bin/grep $KeyWord $1 |/usr/bin/wc -C ` -gt 0 ] && /bin/echo `/bin/pwd `"/"$f
exit 0;
elif [ -d $1 ]; then
isdir $1
exit 0;
else
/bin/echo "Please check:"
/bin/echo " you input is not a file or a directory..."
exit 1;
fi
#The End.
|
grep -n -ri "slewing" /usr2
显示哪个文件的第几行
其他参数你man grep吧
显示哪个文件的第几行
其他参数你man grep吧
|
Linux中文件查找技术大全
http://chinaunix.net/jh/4/16140.html
http://chinaunix.net/jh/4/16140.html
|
谁说没有,你用的什么系统?
|
1. 将上面文本内容存为文件myfind
2. 修改文件myfind属性为可执行:chmod +x myfind
3. 执行 ./myfind /usr2 slewing
2. 修改文件myfind属性为可执行:chmod +x myfind
3. 执行 ./myfind /usr2 slewing
|
linux系统中,grep应该有-r选项,
实在没有,可以结合find命令,不过那样也需要有脚本,还是比较麻烦。
实在没有,可以结合find命令,不过那样也需要有脚本,还是比较麻烦。
|
如果你只想知道哪些文件里面有你想查找的字符串,可以这样
find /usr2 -exec grep -ls {} ;
find /usr2 -exec grep -ls {} ;
|
错了,
find /usr2 -exec grep -ls slewing {} ;
find /usr2 -exec grep -ls slewing {} ;
|
你的意思是文件中的内容包含slewing?
如果是文件名的话直接用whereis命令即可
如果是文件名的话直接用whereis命令即可
|
这是我在freebsd下试验的,linux没用过,大概可以吧,你可以试试,不行的话man 一下grep,估计也只是格式不太一样,参数应该一致的
|
不应该吧,那个参数我可是看linux的命令大全查的
|
所有的UNIX-based操作系统里面的grep都应该是含有-r参数的。
|
曾经写过这样的一个脚本文件,我找找看吧!
|
偶喜欢用locate