Use a for loop:

for d in $(find /path/to/dir -maxdepth 1 -type d)
do
  #Do something, the directory is accessible with $d:
  echo $d
done >output_file
It searches only the subdirectories of the directory /path/to/dir. Note that the simple example above will fail if the directory names contain whitespace or special characters. A safer approach is:

find /tmp -maxdepth 1 -type d -print0 |
  while IFS= read -rd '' dir; do echo "$dir"; done
Or in plain bash:

for d in /path/to/dir/*; do
  if [ -d "$d" ]; then
    echo "$d"
  fi
done
(note that contrary to find that one also considers symlinks to directories and excludes hidden ones)

 

 

参考:

https://unix.stackexchange.com/questions/187167/traverse-all-subdirectories-in-and-do-something-in-unix-shell-script

相关文章:

  • 2021-08-16
  • 2021-10-21
  • 2022-01-22
  • 2021-08-26
  • 2021-08-19
  • 2021-10-17
  • 2021-05-25
  • 2021-10-13
猜你喜欢
  • 2022-12-23
  • 2021-06-02
  • 2021-08-10
  • 2022-12-23
  • 2022-01-09
  • 2021-12-03
  • 2021-10-31
相关资源
相似解决方案