在bash中仅使用ls列出目录:检查

问题描述 投票:827回答:25

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

*/的模式究竟是什么?

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

bash directory ls
25个回答
890
投票

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


7
投票

显示没有/的文件夹列表

ls -d */|sed 's|[/]||g'

7
投票

Actual ls solution, including symlinks to directories

这里的许多答案实际上并没有使用ls(或者仅在ls -d的简单意义上使用它,而使用通配符进行实际的子目录匹配。真正的ls解决方案很有用,因为它允许使用ls选项来排序顺序等。

Excluding symlinks

已经给出了使用ls的一种解决方案,但它与其他解决方案有所不同,因为它将符号链接排除在目录之外:

ls -l | grep '^d'

(可能通过sedawk来隔离文件名)

Including symlinks

在(可能更常见)应该包含目录的符号链接的情况下,我们可以使用-pls选项,这使得它为目录名称(包括符号链接的)添加斜杠字符:

ls -1p | grep '/$'

或者,摆脱尾随斜杠:

ls -1p | grep '/$' | sed 's/\/$//'

我们可以根据需要为ls添加选项(如果使用长列表,则不再需要-1)。

注意:如果我们想要尾随斜杠,但不希望它们被grep突出显示,我们可以通过使该行的实际匹配部分为空来删除突出显示:

ls -1p | grep -P '(?=/$)'

6
投票

仅列出目录:

ls -l | grep ^d

仅列出文件:

ls -l | grep -v ^d 

或者你也可以这样做:ls -ld * /


5
投票

这是我正在使用的

ls -d1 /Directory/Path/*;


4
投票

测试该项是否是test -d目录:

for i in $(ls); do test -d $i && echo $i ; done

2
投票

单行列表仅从“此处”列出目录。

随着文件数量。

for i in `ls -d */`; do g=`find ./$i -type f -print| wc -l`; echo "Directory $i contains $g files."; done

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'
}

2
投票

仅供参考,如果您想要以多行打印所有文件,您可以执行ls -1,它将在单独的行中打印每个文件。 file1 file2 file3


2
投票

使用Perl:

ls | perl -nle 'print if -d;'

2
投票

当前目录的简单列表,它是:

ls -1d ./*/

你想要它整理和清洁?

ls -1d ./*/ | cut -c 3- | rev | cut -c 2- | rev | sort

请记住:大写字符在排序中具有不同的行为


442
投票

完成此操作的四种方法,每种方式都有不同的输出格式

1. Using echo

示例:echo */echo */*/ 这是我得到的:

cs/ draft/ files/ hacks/ masters/ static/  
cs/code/ files/images/ static/images/ static/stylesheets/  

2. Using ls only

示例:ls -d */ 这正是我得到的:

cs/     files/      masters/  
draft/  hacks/      static/  

或者作为列表(详细信息):ls -dl */

3. Using ls and 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  

4. Bash Script (Not recommended for filename containing spaces)

示例: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/

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

好吧,这减少了我,市长部分:)


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/

1
投票

试试这个吧。这将适用于所有发行版ls -ltr | grep drw


1
投票

回答原来的问题,*/ls本身无关;它由shell / Bash完成,在一个称为globbing的过程中。

这就是echo */ls -d */输出相同元素的原因。 (-d标志使ls输出目录名而不是目录的内容。)


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

0
投票

这是我用于仅列出目录名称的内容:

ls -1d /some/folder/*/ | awk -F "/" "{print \$(NF-1)}"

85
投票

我用:

ls -d */ | cut -f1 -d'/'

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

我的两分钱。


53
投票

对于没有子文件夹的所有文件夹:

find /home/alice/Documents -maxdepth 1 -type d

对于包含子文件夹的所有文件夹:

find /home/alice/Documents -type d

33
投票

4 (more) Reliable Options.

一个不带引号的星号*将被shell解释为一个模式(glob)。 shell将在路径名扩展中使用它。 然后,它将生成与模式匹配的文件名列表。 一个简单的星号将匹配PWD(当前工作目录)中的所有文件名。 一个更复杂的模式,如*/将匹配所有以/结尾的文件名。 因此,所有目录。这就是为什么命令:

1.- echo.

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中目录列表的原因。 -dls选项使其列出当前目录条目而不是每个目录的内容(默认情况下显示)。

ls -d */

但是,这个命令(有些)不太可靠。它将失败,上面列出了奇怪的命名文件。它会扼杀几个名字。你需要逐个擦除,直到找到有问题的那些。

2.- ls

GNU ls将接受“选项结束”(--)键。

ls -d ./*/                     ### more reliable BSD ls
ls -d -- */                    ### more reliable GNU ls

3.-printf

要列出其自己行中的每个目录(在一列中,类似于ls -1),请使用:

$ 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价值的影响。
  • 将在空格和制表符上拆分名称(默认为IFS)。
  • 名称中的每个换行符将启动一个新的echo命令。

4.- Function

最后,使用函数内的参数列表不会影响当前运行的shell的参数列表。只是:

$ listdirs(){ set -- */; printf "%s\n" "${@%/}"; }
$ listdirs

列出这个清单:

--
-
*
cs
dir with spaces
draft
files
-h
masters
-n
static
-v var

这些选项对于几种类型的奇数文件名是安全的。


18
投票

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可以看到更多的论点


12
投票

如果你想知道为什么来自'ls -d * /'的输出给你两个尾部斜杠,比如:

[prompt]$ ls -d */    
app//  cgi-bin//  lib//        pub//

这可能是因为你的shell或会话配置文件的某个地方将ls命令别名为包含-F标志的ls版本。该标志在每个输出名称(不是普通文件)上附加一个字符,表示它的类型。因此,一个斜杠是匹配模式'* /',另一个斜杠是附加的类型指示符。

要摆脱这个问题,你当然可以为ls定义一个不同的别名。但是,要暂时不调用别名,可以在命令前加上反斜杠:

\ ls -d * /


9
投票

我只是将它添加到我的.bashrc文件中(如果你只需要/想要一个会话,你也可以在命令行输入它)

alias lsd='ls -ld */'

然后lsd将产生所需的结果。


8
投票

如果不需要列出隐藏目录,我提供:

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}'
© www.soinside.com 2019 - 2024. All rights reserved.