此命令列出当前路径中的目录:ls -d */
模式*/
究竟做了什么?
我们如何在上述命令中给出绝对路径(例如ls -d /home/alice/Documents
)以仅列出该路径中的目录?
答案 0 :(得分:875)
*/
是匹配当前目录中所有子目录的模式(*
将匹配所有文件和子目录; /
将其限制为目录)。同样,要列出/ home / alice / Documents下的所有子目录,请使用ls -d /home/alice/Documents/*/
答案 1 :(得分:435)
echo
示例:echo */
,echo */*/
这是我得到的:
cs/ draft/ files/ hacks/ masters/ static/
cs/code/ files/images/ static/images/ static/stylesheets/
ls
示例:ls -d */
这正是我得到的:
cs/ files/ masters/
draft/ hacks/ static/
或列表(详细信息):ls -dl */
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
示例:for i in $(ls -d */); do echo ${i%%/}; done
这是我得到的:
cs
draft
files
hacks
masters
static
如果您希望将'/'作为结束字符,则命令将为:for i in $(ls -d */); do echo ${i}; done
cs/
draft/
files/
hacks/
masters/
static/
答案 2 :(得分:85)
我用:
ls -d */ | cut -f1 -d'/'
这会创建一个没有尾部斜杠的列 - 在脚本中很有用。
我的两分钱。
答案 3 :(得分:50)
对于所有没有子文件夹的文件夹:
find /home/alice/Documents -maxdepth 1 -type d
对于所有带子文件夹的文件夹:
find /home/alice/Documents -type d
答案 4 :(得分:33)
未加引号的星号*
将被shell解释为模式(glob)。
shell将在路径名扩展中使用它。
它将然后生成一个与模式匹配的文件名列表。
一个简单的星号将匹配PWD(当前工作目录)中的所有文件名。
更复杂的模式*/
将匹配所有文件名在/
结束。
因此,所有目录。这就是命令:
echo */
echo ./*/ ### avoid misinterpreting filenames like "-e dir"
将(通过shell)扩展到echo
PWD中的所有目录。
要测试这个:创建一个名为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:
即使有奇怪的命名文件,命令echo ./*/
仍然可靠:
./--/ ./-/ ./*/ ./cs/ ./dir with spaces/ ./draft/ ./files/ ./-h/
./masters/ ./-n/ ./static/ ./-v var/
但文件名中的空格使阅读有点令人困惑。
如果我们使用echo
代替ls
,那么shell仍然是扩展文件名列表的内容。 shell是获取PWD中目录列表的原因。 -d
的{{1}}选项使其列出当前目录条目而不是每个目录的内容(默认情况下显示)。
ls
然而,这个命令(有些)不太可靠。它将失败,上面列出了奇怪的命名文件。它会扼杀几个名字。你需要逐个擦除,直到找到有问题的那些。
GNU ls -d */
将接受“选项结束”(ls
)键。
--
要列出其自己行中的每个目录(在一列中,类似于ls -1),请使用:
ls -d ./*/ ### more reliable BSD ls
ls -d -- */ ### more reliable GNU ls
而且,更好的是,我们可以删除尾随的$ printf "%s\n" */ ### Correct even with "-", spaces or newlines.
:
/
这样的尝试:
$ set -- */; printf "%s\n" "${@%/}" ### Correct with spaces and newlines.
将失败:
$ for i in $(ls -d */); do echo ${i%%/}; done
)。ls -d */
。IFS
)。最后,使用函数内的参数列表不会影响当前运行的shell的参数列表。简单地:
IFS
提供此列表:
$ listdirs(){ set -- */; printf "%s\n" "${@%/}"; }
$ listdirs
对于多种类型的奇怪文件名,此选项是安全的。
答案 5 :(得分:17)
tree
命令在这里也很有用。默认情况下,它会将所有文件和目录显示为完整的深度,其中一些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
./data/sql
./images
论点是:
-d List directories only.
-f Prints the full path prefix for each file.
-i Makes tree not print the indentation lines, useful when used in conjunction with the -f option.
如果您想要绝对路径,可以先指定当前目录的完整路径:
$ tree -dfi "$(pwd)"
/home/alice/Documents
/home/alice/Documents/data
/home/alice/Documents/data/sql
/home/alice/Documents/images
要限制子目录的数量,可以使用-L level
设置子目录的最大级别,例如:
$ tree -dfi -L 1 "$(pwd)"
/home/alice/Documents
/home/alice/Documents/data
/home/alice/Documents/images
使用man tree
可以看到更多参数答案 6 :(得分:12)
如果您想知道为什么'ls -d * /'的输出会为您提供两个尾随斜杠,例如:
[prompt]$ ls -d */
app// cgi-bin// lib// pub//
这可能是因为你的shell或会话配置文件的某个地方将ls命令别名为包含-F标志的ls版本。该标志在每个输出名称(不是普通文件)上附加一个字符,表示它的类型。因此,一个斜杠是匹配模式'* /',另一个斜杠是附加的类型指示符。
要摆脱这个问题,你当然可以为ls定义一个不同的别名。但是,要暂时不调用别名,可以在命令前加上反斜杠:
\ ls -d * /
答案 7 :(得分:9)
我只是将它添加到我的.bashrc
文件中(如果你只需要/想要一个会话,你也可以在命令行输入它)
alias lsd='ls -ld */'
然后lsd将产生所需的结果。
答案 8 :(得分:8)
如果不需要列出隐藏目录,我提供:
ls -l | grep "^d" | awk -F" " '{print $9}'
如果需要列出隐藏目录,请使用:
ls -Al | grep "^d" | awk -F" " '{print $9}'
OR
find -maxdepth 1 -type d | awk -F"./" '{print $2}'
答案 9 :(得分:6)
仅列出目录:
ls -l | grep ^d
仅列出个文件:
ls -l | grep -v ^d
或者您也可以这样做: ls -ld * /
答案 10 :(得分:6)
显示没有/
ls -d */|sed 's|[/]||g'
答案 11 :(得分:6)
ls
解决方案,包括目录的符号链接此处的许多答案实际上并未使用ls
(或仅在ls -d
的简单意义上使用它,而使用通配符进行实际的子目录匹配。真正的ls
解决方案很有用,因为它允许使用ls
选项来排序顺序等。
已经给出了一个使用ls
的解决方案,但它与其他解决方案有所不同,因为将符号链接排除在目录之外:
ls -l | grep '^d'
(可能通过sed
或awk
来隔离文件名)
在(可能更常见的)应该包含目录符号链接的情况下,我们可以使用-p
的{{1}}选项,这使得它为目录名称附加一个斜杠字符(包括符号链接)的):
ls
或者,摆脱尾随斜杠:
ls -1p | grep '/$'
我们可以根据需要向ls -1p | grep '/$' | sed 's/\/$//'
添加选项(如果使用了长列表,则不再需要ls
。)
注意:如果我们想要尾随斜杠,但不希望它们由-1
突出显示,我们可以通过制作突出显示来删除突出显示该行的实际匹配部分为空:
grep
答案 12 :(得分:5)
以下是我正在使用的内容
ls -d1 /Directory/Path/*;
答案 13 :(得分:4)
测试该项目是否为test -d
的目录:
for i in $(ls); do test -d $i && echo $i ; done
答案 14 :(得分:2)
*/
是一个匹配模式,匹配当前目录中的目录。
仅列出目录,我喜欢这个功能:
# 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'
}
答案 15 :(得分:2)
当前目录的简单列表,它是:
ls -1d ./*/
您要对其进行分类和清洁吗?
ls -1d ./*/ | cut -c 3- | rev | cut -c 2- | rev | sort
记住:大写字符在排序中的行为不同
答案 16 :(得分:2)
使用Perl:
ls | perl -nle 'print if -d;'
答案 17 :(得分:2)
单行列表仅列出来自"此处"。
有文件计数。
for i in `ls -d */`; do g=`find ./$i -type f -print| wc -l`; echo "Directory $i contains $g files."; done
答案 18 :(得分:2)
仅供参考,如果您想要以多行打印所有文件,可以执行ls -1
,它将在单独的行中打印每个文件。
文件1
文件2
file3的
答案 19 :(得分:1)
我部分解决了:
cd "/path/to/pricipal/folder"
for i in $(ls -d .*/); do sudo ln -s "$PWD"/${i%%/} /home/inukaze/${i%%/}; done
ln: «/home/inukaze/./.»: can't overwrite a directory
ln: «/home/inukaze/../..»: can't overwrite a directory
ln: accesing to «/home/inukaze/.config»: too much symbolics links levels
ln: accesing to «/home/inukaze/.disruptive»: too much symbolics links levels
ln: accesing to «/home/inukaze/innovations»: too much symbolics links levels
ln: accesing to «/home/inukaze/sarl»: too much symbolics links levels
ln: accesing to «/home/inukaze/.e_old»: too much symbolics links levels
ln: accesing to «/home/inukaze/.gnome2_private»: too much symbolics links levels
ln: accesing to «/home/inukaze/.gvfs»: too much symbolics links levels
ln: accesing to «/home/inukaze/.kde»: too much symbolics links levels
ln: accesing to «/home/inukaze/.local»: too much symbolics links levels
ln: accesing to «/home/inukaze/.xVideoServiceThief»: too much symbolics links levels
嗯,这减少了我,市长部分:)
答案 20 :(得分:1)
尝试这个。这将适用于所有发行版 ls -ltr | grep drw
答案 21 :(得分:1)
添加以使其成为一个完整的圆圈,检索每个文件夹的路径,使用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/
答案 22 :(得分:1)
有史以来最短的命令(实际上是一个hack),仅列出目录。输入感兴趣目录的绝对路径或相对路径,然后按Enter键进入感兴趣目录。现在,键入cd
,然后按 tab键。现在仅显示目录。
user@ubuntu:~/Desktop$cd <tab key>
上面的命令现在应该只显示桌面中的文件夹。 确实最短。
答案 23 :(得分:1)
要回答最初的问题,*/
与ls
本身无关;它是由Shell / Bash通过称为
这就是echo */
和ls -d */
输出相同元素的原因。 (-d
标志使ls
输出目录名而不是目录内容。)
答案 24 :(得分:1)
这是一个使用树的变体,它仅在单独的行上输出目录名称,是的,但是很丑,但是,它可以工作。
tree -d | grep -E '^[├|└]' | cut -d ' ' -f2
或使用awk
tree -d | grep -E '^[├|└]' | awk '{print $2}'
这可能更好,它将保留目录名后的/
。
ls -l | grep "^d" | awk '{print $9}'
答案 25 :(得分:0)
如果您的文件夹名称中有空格,则 $9 打印将无法使用,请尝试以下命令
ls -l yourfolder/alldata/ | grep '^d' | awk '{print $9" " $10}'
输出
ls -l yourfolder/alldata/ | grep '^d' | awk '{print $9" " $10}'
testing_Data
Folder 1
答案 26 :(得分:0)
这里是我只列出目录名称的方法:
number
答案 27 :(得分:0)
file *|grep directory
O / P(在我的机器上) -
[root@rhel6 ~]# file *|grep directory
mongo-example-master: directory
nostarch: directory
scriptzz: directory
splunk: directory
testdir: directory
使用剪切可以进一步细化上面的输出。
file *|grep directory|cut -d':' -f1
mongo-example-master
nostarch
scriptzz
splunk
testdir
* could be replaced with any path that's permitted
file - determine file type
grep - searches for string named directory
-d - to specify a field delimiter
-f1 - denotes field 1