【shell脚本】递归查找某种类型的文件

写了一个查找某种类型的文件的脚本,代码如下:(代码实现是查找后缀名为.c的文件)

#!/bin/bash
# get the number of functions in RTS code

# $1是运行脚本时,输入的第一个参数,这里指的是使用者希望搜索的目录
# 下面的代码是对目录进行判断,如果为空则使用脚本所在的目录;否则,搜索用户输入的目录
if [[ -z "$1" ]] || [[ ! -d "$1" ]]; then echo "The directory is empty or not exist!" echo "It will use the current directory." nowdir=$(pwd) else nowdir=$(cd $1; pwd) fi echo "$nowdir"
# 递归函数的实现 function SearchCfile() { cfilelist=$(ls | grep '\.c$')  # 获取后缀为.c的文件列表 for cfilename in $cfilelist do echo "$cfilename" done
  # 遍历当前目录,当判断其为目录时,则进入该目录递归调用该函数; dirlist
=$(ls) for dirname in $dirlist       do if [ -d "$dirname" ]; then   cd $dirname SearchCfile cd .. fi done }
# 调用上述递归调用函数 SearchCfile

参考文献:

[1].http://blog.csdn.net/bluecy/article/details/6729465

[2].鸟哥的Linux私房菜-基础篇关于shell脚本的介绍,http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts.php

原文地址:https://www.cnblogs.com/cnpirate/p/2687519.html