把代码保存为scanfile.sh,用法是:./scanfile.sh /root

 
  
  1. #!/bin/sh 
  2.  
  3.  
  4. function scandir() { 
  5.  
  6.     local cur_dir parent_dir workdir 
  7.  
  8.     workdir=$1 
  9.  
  10.     cd ${workdir} 
  11.  
  12.     if [ ${workdir} = "/" ] 
  13.  
  14.     then 
  15.  
  16.         cur_dir="" 
  17.  
  18.     else 
  19.  
  20.         cur_dir=$(pwd) 
  21.  
  22.     fi 
  23.  
  24.  
  25.     for dirlist in $(ls ${cur_dir}) 
  26.  
  27.     do 
  28.  
  29.         if test -d ${dirlist};then 
  30.  
  31.             cd ${dirlist} 
  32.  
  33.             scandir ${cur_dir}/${dirlist} 
  34.  
  35.             cd .. 
  36.  
  37.         else 
  38.  
  39.             echo ${cur_dir}/${dirlist} 
  40.  
  41.         fi 
  42.  
  43.     done 
  44.  
  45.  
  46.  
  47. if test -d $1 
  48.  
  49. then 
  50.  
  51.     scandir $1 
  52.  
  53. elif test -f $1 
  54.  
  55. then 
  56.  
  57.     echo "you input a file but not a directory,pls reinput and try again" 
  58.  
  59.     exit 1 
  60.  
  61. else 
  62.  
  63.     echo "the Directory isn't exist which you input,pls input a new one!!" 
  64.  
  65.     exit 1 
  66.  
  67. fi