---
title: "linux  shell 递归目录、文件夹 "
author: "gaozhonghui"
date: "2012-05-03T11:29:08Z"
tags: []
categories: ["编程语言"]
slug: "linux-shell"
draft: false
lastmod: "2012-05-03T11:29:08Z"
id: 1508446
url: "https://toptree.iteye.com/blog/1508446"
---

```python

#!/bin/sh
#########################################
#desc:递归
#########################################
today=`date  +%Y%m%d`

isDir()
{
  local dirName=$1
  if [ ! -d $dirname ]; then
    return 1
  else
    return 0
  fi
}

recursionDir()
{

    local dir=$1
    if  isDir "${dir}"
        then :
        else
            echo "error,please pass a dirctory";
            exit 1
    fi

   echo "working in ${dir}"

    local filelist=`ls -tr "${dir}"`

    for filename in $filelist
    do
        local fullpath="${dir}"/"${filename}";
        if isDir "${fullpath}";then
                recursionDir "${fullpath}"
        else
                echo "file ${fullpath}" >> /home/Gzh/logs/$today.log
        fi
    done
}

recursionDir "$1"

```