我正在Linux上编写bash脚本,需要遍历给定目录中的所有子目录名。如何遍历这些目录(并跳过常规文件)?

例如: 指定目录为/tmp/ 目录包括:/tmp/A、/tmp/B、/tmp/C

我想找回A B C。


当前回答

处理包含空格的目录

灵感来自Sorpigal

while IFS= read -d $'\0' -r file ; do 
    echo $file; ls $file ; 
done < <(find /path/to/dir/ -mindepth 1 -maxdepth 1 -type d -print0)

原帖(不适用空格)

灵感来自Boldewyn:使用find命令的循环示例。

for D in $(find /path/to/dir/ -mindepth 1 -maxdepth 1 -type d) ; do
    echo $D ;
done

其他回答

到目前为止,所有答案都使用find,这里有一个只有外壳的答案。在您的情况下不需要外部工具:

for dir in /tmp/*/     # list directories in the form "/tmp/dirname/"
do
    dir=${dir%*/}      # remove the trailing "/"
    echo "${dir##*/}"    # print everything after the final "/"
done
find . -mindepth 1 -maxdepth 1 -type d -printf "%P\n"

你可以循环遍历所有目录,包括隐藏目录(以点开始):

for file in */ .*/ ; do echo "$file is a directory"; done

注意:只有当文件夹中至少存在一个隐藏目录时,使用列表*/ .*/才能在ZSH中工作。在bash中也会显示出来。和. .


bash包含隐藏目录的另一种可能性是使用:

shopt -s dotglob;
for file in */ ; do echo "$file is a directory"; done

如果你想排除符号链接:

for file in */ ; do 
  if [[ -d "$file" && ! -L "$file" ]]; then
    echo "$file is a directory"; 
  fi; 
done

要在每个解决方案中只输出后面的目录名(A,B,C作为质疑),在循环中使用这个:

file="${file%/}"     # strip trailing slash
file="${file##*/}"   # strip path and leading slash
echo "$file is the directoryname without slashes"

示例(这也适用于包含空格的目录):

mkdir /tmp/A /tmp/B /tmp/C "/tmp/ dir with spaces"
for file in /tmp/*/ ; do file="${file%/}"; echo "${file##*/}"; done

处理包含空格的目录

灵感来自Sorpigal

while IFS= read -d $'\0' -r file ; do 
    echo $file; ls $file ; 
done < <(find /path/to/dir/ -mindepth 1 -maxdepth 1 -type d -print0)

原帖(不适用空格)

灵感来自Boldewyn:使用find命令的循环示例。

for D in $(find /path/to/dir/ -mindepth 1 -maxdepth 1 -type d) ; do
    echo $D ;
done
cd /tmp
find . -maxdepth 1 -mindepth 1 -type d -printf '%f\n'

简单解释一下:

find finds files (quite obviously) . is the current directory, which after the cd is /tmp (IMHO this is more flexible than having /tmp directly in the find command. You have only one place, the cd, to change, if you want more actions to take place in this folder) -maxdepth 1 and -mindepth 1 make sure that find only looks in the current directory and doesn't include . itself in the result -type d looks only for directories -printf '%f\n prints only the found folder's name (plus a newline) for each hit.

就是这样!