且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

shell脚本来遍历目录

更新时间:2023-12-04 20:19:04

我的猜测是,你会在两个层次深,但只回来了一个级别。尝试 LS后加入 CD ../.. ,或使用 PUSHD POPD 来代替。

My guess is that you're going in two levels deep, but only coming back up one level. Try adding a cd ../.. after the ls, or use pushd and popd instead.

例如:

for dir in $dirlist
do
  pushd $dir
  echo $dir
  ls
  popd
done

由于@shellter指出,如果这些目录中有空格,那么这样的事情可能会更好地工作:

As @shellter points out, if those directories have spaces, then something like this might work better:

find $1 -mindepth 1 -maxdepth 1 -type d | while read -r dir
do
  pushd "$dir"  # note the quotes, which encapsulate whitespace
  echo $dir
  ls
  popd
done