找回密碼
 註冊
搜索
查看: 2233|回復: 0

[CentOS] shell指令碼遞迴遍歷目錄及子目錄的例子分享

[複製鏈接]
發表於 2020-1-9 15:16:18 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
vi scandir.sh

  1. #!/bin/sh

  2. function scandir() {
  3.     local cur_dir parent_dir workdir
  4.     workdir=$1
  5.     cd ${workdir}
  6.     if [ ${workdir} = "/" ]
  7.     then
  8.         cur_dir=""
  9.     else
  10.         cur_dir=$(pwd)
  11.     fi

  12.     for dirlist in $(ls -ll ${cur_dir})
  13.     do
  14.         if test -d ${dirlist};then
  15.             cd ${dirlist}
  16.             scandir ${cur_dir}/${dirlist}
  17.             cd ..
  18.         else
  19.             echo ${cur_dir}/${dirlist}
  20.         fi
  21.     done
  22. }

  23. if test -d $1
  24. then
  25.     scandir $1
  26. elif test -f $1
  27. then
  28.     echo "you input a file but not a directory,pls reinput and try again"
  29.     exit 1
  30. else
  31.     echo "the Directory isn’t exist which you input,pls input a new one!!"
  32.     exit 1
  33. fi

  34. scandir /home   //需要遞迴的目錄
複製代碼

chmod 711 scandir.sh
文章出處
https://codertw.com/%E5%89%8D%E7%AB%AF%E9%96%8B%E7%99%BC/393511/
 
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2025-6-17 07:08 , Processed in 0.021256 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回復 返回頂部 返回列表