在Bash中仅列出使用ls的目录?

在Bash中仅列出使用ls的目录?,bash,directory,ls,Bash,Directory,Ls,此命令列出当前路径中的目录:ls-d*/ 模式*/的具体功能是什么 我们如何在上面的命令(例如,ls-d/home/alice/Documents)中给出仅列出该路径中的目录的绝对路径?*/是一种匹配当前目录中所有子目录的模式(*将匹配所有文件和子目录;/将其限制为目录)。类似地,要列出/home/alice/Documents下的所有子目录,请使用ls-d/home/alice/Documents/*/四种方法完成此操作,每种方法的输出格式不同 1.使用echo 示例:echo*/,echo*

此命令列出当前路径中的目录:
ls-d*/

模式
*/
的具体功能是什么


我们如何在上面的命令(例如,
ls-d/home/alice/Documents
)中给出仅列出该路径中的目录的绝对路径?

*/
是一种匹配当前目录中所有子目录的模式(
*
将匹配所有文件和子目录;
/
将其限制为目录)。类似地,要列出/home/alice/Documents下的所有子目录,请使用
ls-d/home/alice/Documents/*/

四种方法完成此操作,每种方法的输出格式不同 1.使用
echo
示例:
echo*/
echo*/*/

以下是我得到的:

cs/ draft/ files/ hacks/ masters/ static/  
cs/code/ files/images/ static/images/ static/stylesheets/  
cs/     files/      masters/  
draft/  hacks/      static/  
2.仅使用
ls
示例:
ls-d*/

下面就是我得到的:

cs/ draft/ files/ hacks/ masters/ static/  
cs/code/ files/images/ static/images/ static/stylesheets/  
cs/     files/      masters/  
draft/  hacks/      static/  
或作为列表(包含详细信息):
ls-dl*/

3.使用
ls
grep
示例:
ls-l | grep“^d”
以下是我得到的:

drwxr-xr-x  24 h  staff     816 Jun  8 10:55 cs  
drwxr-xr-x   6 h  staff     204 Jun  8 10:55 draft  
drwxr-xr-x   9 h  staff     306 Jun  8 10:55 files  
drwxr-xr-x   2 h  staff      68 Jun  9 13:19 hacks  
drwxr-xr-x   6 h  staff     204 Jun  8 10:55 masters  
drwxr-xr-x   4 h  staff     136 Jun  8 10:55 static  
cs  
draft  
files  
hacks  
masters  
static
4.Bash脚本(不建议用于包含空格的文件名) 示例:
表示$中的i(ls-d*/);do echo${i%%/};done

以下是我得到的:

drwxr-xr-x  24 h  staff     816 Jun  8 10:55 cs  
drwxr-xr-x   6 h  staff     204 Jun  8 10:55 draft  
drwxr-xr-x   9 h  staff     306 Jun  8 10:55 files  
drwxr-xr-x   2 h  staff      68 Jun  9 13:19 hacks  
drwxr-xr-x   6 h  staff     204 Jun  8 10:55 masters  
drwxr-xr-x   4 h  staff     136 Jun  8 10:55 static  
cs  
draft  
files  
hacks  
masters  
static
如果希望以“/”作为结束字符,命令将是:
fori in$(ls-d*/);do echo${i};done

cs/  
draft/  
files/  
hacks/  
masters/  
static/
我使用:


这将创建一个没有尾部斜杠的单列-在脚本中很有用。

我使用以下方法部分解决了它:

cd "/path/to/pricipal/folder"

for i in $(ls -d .*/); do sudo ln -s "$PWD"/${i%%/} /home/inukaze/${i%%/}; done

ln:«/home/inukaze/»:无法覆盖目录
项次:«/home/inukaze/./…»:无法覆盖目录
项次:根据«/home/inukaze/.config»:符号链接级别过多
ln:根据«/home/inukaze/.disruptive»:太多的符号链接级别
ln:根据«/home/inukaze/innovations»:太多的符号链接级别
项次:根据«/home/inukaze/sarl»:符号链接级别过多
ln:根据«/home/inukaze/.e_old»:太多的符号链接级别
ln:根据«/home/inukaze/.gnome2_private»:太多的符号链接级别
ln:根据«/home/inukaze/.gvfs»:太多的符号链接级别
ln:根据«/home/inukaze/.kde»:符号链接级别过多
ln:根据«/home/inukaze/.local»:太多的符号链接级别
项次:根据«/home/inukaze/.xvideoservicestepher»:太多符号链接级别

好的,这对我来说,主要部分:)

一行代码只列出“here”中的目录

与文件计数

for i in `ls -d */`; do g=`find ./$i -type f -print| wc -l`; echo "Directory $i contains $g files."; done
对于没有子文件夹的所有文件夹:

find/home/alice/Documents-最大深度1-类型d
对于包含子文件夹的所有文件夹:

find/home/alice/Documents-type d

显示不带
/
的文件夹列表:

ls -d */|sed 's|[/]||g'
$ set -- */; printf "%s\n" "${@%/}"        ### Correct with spaces and newlines.

再加上使其完整循环,检索每个文件夹的路径,使用Albert的答案以及Gordans的组合。这应该非常有用

for i in $(ls -d /pathto/parent/folder/*/); do echo ${i%%/}; done
输出:

/pathto/parent/folder/childfolder1/
/pathto/parent/folder/childfolder2/
/pathto/parent/folder/childfolder3/
/pathto/parent/folder/childfolder4/
/pathto/parent/folder/childfolder5/
/pathto/parent/folder/childfolder6/
/pathto/parent/folder/childfolder7/
/pathto/parent/folder/childfolder8/

这是我正在使用的


ls-d1/Directory/Path/*

*/
是与当前目录中的目录相匹配的文件名匹配模式

要仅列出目录,我喜欢此功能:

# Long list only directories
llod () {
  ls -l --color=always "$@" | grep --color=never '^d'
}
将其放入.bashrc文件中

用法示例:

llod       # Long listing of all directories in current directory
llod -tr   # Same but in chronological order oldest first
llod -d a* # Limit to directories beginning with letter 'a'
llod -d .* # Limit to hidden directories
注意:如果使用
-i
选项,它将断开。这里有一个解决方案:

# Long list only directories
llod () {
  ls -l --color=always "$@" | egrep --color=never '^d|^[[:digit:]]+ d'
}
这个命令在这里也非常有用。默认情况下,它将以完整的深度显示所有文件和目录,一些ASCII字符显示目录树

$ tree
.
├── config.dat
├── data
│   ├── data1.bin
│   ├── data2.inf
│   └── sql
|   │   └── data3.sql
├── images
│   ├── background.jpg
│   ├── icon.gif
│   └── logo.jpg
├── program.exe
└── readme.txt
但是,如果我们只想获得目录,而不需要ASCII树,并使用当前目录的完整路径,则可以执行以下操作:

$tree-dfi
.
/数据
/data/sql
/图像
理由如下:

-d仅列出目录。
-f打印每个文件的完整路径前缀。
-我使树不打印缩进行,这在与-f选项结合使用时很有用。
如果需要绝对路径,可以从指定当前目录的完整路径开始:

$tree-dfi“$(pwd)”
/home/alice/Documents
/主页/alice/文档/数据
/home/alice/Documents/data/sql
/主页/爱丽丝/文档/图像
要限制子目录的数量,可以使用
-L level
设置子目录的最大级别,例如:

$tree-dfi-L 1“$(pwd)”
/home/alice/Documents
/主页/alice/文档/数据
/主页/爱丽丝/文档/图像
更多参数可通过。

四个(更多)可靠选项查看。 未加引号的星号
*
将被shell解释为模式(glob)。shell将在路径名扩展中使用它。然后,它将生成与模式匹配的文件名列表

一个简单的星号将匹配PWD(当前工作目录)中的所有文件名。更复杂的模式如
*/
将匹配以
/
结尾的所有文件名。因此,所有目录。这就是为什么命令:

1.-回声。 将(通过shell)扩展到PWD中的
echo
所有目录


要测试这一点:创建一个名为test dir的目录(
mkdir
),并将
cd
放入其中:

mkdir test-dir; cd test-dir
创建一些目录:

mkdir {cs,files,masters,draft,static}   # Safe directories.
mkdir {*,-,--,-v\ var,-h,-n,dir\ with\ spaces}  # Some a bit less secure.
touch -- 'file with spaces' '-a' '-l' 'filename'    # And some files:
ls -l | grep '^d'

即使使用奇数命名文件,
echo./*/
命令仍将保持可靠:

./--/ ./-/ ./*/ ./cs/ ./dir with spaces/ ./draft/ ./files/ ./-h/
./masters/ ./-n/ ./static/ ./-v var/
但是文件名中的空格使读取有点混乱


如果使用
ls
而不是
echo
。shell仍然在扩展文件名列表。shell是获取PWD中目录列表的原因。
ls
-d
选项使其列出当前目录条目,而不是每个目录的内容(默认情况下显示)

但是,该命令(有些)不太可靠。上面列出的奇数命名文件将失败。它将被几个名字扼杀。你需要一个一个地抹掉直到你消失
$ for i in $(ls -d */); do echo ${i%%/}; done
$ listdirs(){ set -- */; printf "%s\n" "${@%/}"; }
$ listdirs
--
-
*
cs
dir with spaces
draft
files
-h
masters
-n
static
-v var
ls -l | grep "^d" | awk -F" " '{print $9}'
ls -Al | grep "^d" | awk -F" " '{print $9}'
find -maxdepth 1 -type d | awk -F"./" '{print $2}'
alias lsd='ls -ld */'
[prompt]$ ls -d */
app//  cgi-bin//  lib//        pub//
\ls -d */
ls -l | grep '^d'
ls -1p | grep '/$'
ls -1p | grep '/$' | sed 's/\/$//'
ls -1p | grep -P '(?=/$)'
for i in $(ls); do test -d $i && echo $i ; done
ls | perl -nle 'print if -d;'
ls -ltr | grep drw
ls -l | grep ^d
ls -l | grep -v ^d 
ls -ld */
ls -1d */
ls -1d */ | cut -c 1- | rev | cut -c 2- | rev | sort
tree -d | grep -E '^[├|└]' | cut -d ' ' -f2
tree -d | grep -E '^[├|└]' | awk '{print $2}'
ls -l | grep "^d" | awk '{print $9}'