当前位置: > shell编程 >

在指定目录查找指定后缀的文件的shell脚本

时间:2014-09-15 02:40来源:linux.it.net.cn 作者:it

在指定目录查找指定后缀的文件的shell脚本。

复制代码代码如下:

#!bin/sh  
# 在指定位置查找指定后缀的文件,包括子目录  
# 用法:  
# findf $1 $2  
# 第一个参数为后缀  
# 查找指定后缀的文件并打印出来  
# link:www.it.net.cn
# date:2013/2/26
  
f()  
{  
  list=`find $2|grep "/.$1/>"`  
  for i in $list  
    do  
    echo $i  
  done  
}  
  
# 打印用法  
print()  
{  
 echo "用法:"  
 echo "$1 /$1 /$2"  
 echo "第一个参数为指定的后缀名,如'h'"  
 echo "第二个参数为指定的目录,如果省略此参数则默认为当前目录"  
 exit -1  
}  
  
# 在当前目录查找  
f1()  
{  
  f "$1" "*"  
}  
  
# 在指定的目录查找  
f2()  
{  
  cd $2  
  f "$1" "*"  
}  
  
if [ "$#" -lt "1" ]  
then  
   echo "给定的参数太少,最少需要一个参数."  
   print "$0"  
fi  
  
if [ "$#" -gt "2" ]  
then  
  echo "给定的参数太多,最多需要二个参数."  
  print "$0"  
fi  
  
if [ "$#" -eq "1" ]  
then  
  f1 $1  
  exit 0  
fi  
  
if [ "$#" -eq 2 ]  
then  
  f2 $1 $2  
  exit 0  
fi 

 

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容