如何递归列出子目录?

如何递归列出子目录?

显而易见的

ls -dR

不起作用。

我目前正在使用

find /path/ -type d -ls

但输出不是我需要的(子文件夹的简单列表)

有出路吗?

答案1

假设您只想要每个目录的名称:

find /path/ -type d -print

答案2

我过去一直在寻找同样的东西并发现了这个:

树.sh

#!/bin/sh
#######################################################
#  UNIX TREE                                                            
#  Version: 2.3                                       
#  File: ~/apps/tree/tree.sh                          
#                                                     
#  Displays Structure of Directory Hierarchy          
#  -------------------------------------------------  
#  This tiny script uses "ls", "grep", and "sed"      
#  in a single command to show the nesting of         
#  sub-directories.  The setup command for PATH       
#  works with the Bash shell (the Mac OS X default).  
#                                                     
#  Setup:                                             
#     $ cd ~/apps/tree                                
#     $ chmod u+x tree.sh                             
#     $ ln -s ~/apps/tree/tree.sh ~/bin/tree          
#     $ echo "PATH=~/bin:\${PATH}" >> ~/.profile      
#                                                     
#  Usage:                                             
#     $ tree [directory]                              
#                                                     
#  Examples:                                          
#     $ tree                                          
#     $ tree /etc/opt                                 
#     $ tree ..                                       
#                                                     
#  Public Domain Software -- Free to Use as You Like  
#  http://www.centerkey.com/tree  -  By Dem Pilafian  
#######################################################

echo
if [ "$1" != "" ]  #if parameter exists, use as base folder
   then cd "$1"
   fi
pwd
ls -R | grep ":$" |   \
   sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
# 1st sed: remove colons
# 2nd sed: replace higher level folder names with dashes
# 3rd sed: indent graph three spaces
# 4th sed: replace first dash with a vertical bar
if [ `ls -F -1 | grep "/" | wc -l` = 0 ]   # check if no folders
   then echo "   -> no sub-directories"
   fi
echo
exit

我想要一个也列出文件的东西,我了解了 sed 并写了这个:

完整树

#!/bin/sh
#############################################
# Script that displays a recursive formatted folder and file listing
# @author Corbin
# @site iamcorbin.net
#Folder Seperator
BREAK='-------------------------------------------------------------------------------------'

#Optional: if a folder is passed as an argument, run fulltree on that folder rather than the current folder
if [ "$1" != "" ]
   then cd "$1"
   fi
pwd

## Recursive Directory Listing with files
 # 1- preserve directories from being removed in 2 & 3
 # 2- strip first 4 columns
 # 3- strip size and date
 # 4- prepend '  -- ' on each line
 # 5- remove '  -- ' from directories
 # 6- remove extra lines
 # 7- Insert a line break after directories
 # 8- Put a | at the beginning of all lines
 # 9- Indent and process 1st level sub dirs
 #10- Indent and process 2nd level sub dirs
ls -Rhl | sed \
    -e 's/^\.\//x x x x 00:00 |-/' \
    -e 's/^\([^\ ]*.\)\{4\}//' \
    -e 's/.*[0-9]\{2\}:[0-9]\{2\}//' \
    -e 's/^/  -- /' \
    -e 's/\ \ --\ \ |-//'  \
    -e '/--\ $/ d' \
    -e '/^[^ ]/ i\'$BREAK \
    -e 's/^/| /' \
| sed -e '/[^/]*\//,/'$BREAK'/ s/^|/\t&/' -e '/^\t/,/'$BREAK'/ s/'$BREAK'/\t&/' -e 's/[^/]*\//\t\| /' \
| sed -e '/[^/]*\//,/'$BREAK'/ s/^\t|/\t&/' -e '/^\t\t/,/'$BREAK'/  s/'$BREAK'/\t&/' -e 's/[^/]*\//\t\t\| /' \
| sed -e '/[^/]*\//,/'$BREAK'/ s/^\t\t/\t&/' -e 's/[^/]*\//\t\t\t\| /'
echo $BREAK

答案3

你可以得到“tree”包,在 ArchLinux 和 Ubuntu 上它都被称为“tree”

因此,如果您位于 ~/ 中,您可以tree -d获取 ~/ 中所有内容的完整目录列表(以树结构形式)

答案4

zsh全局限定符:

print -rl /path/**/*(D/)

排除隐藏目录:

print -rl /path/**/*(/)

相关内容