TShopping

 找回密碼
 註冊
搜索
查看: 1590|回復: 0
打印 上一主題 下一主題

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

[複製鏈接]
跳轉到指定樓層
1#
發表於 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, 2024-5-13 15:49 , Processed in 0.187663 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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