此命令列出当前路径中的目录:ls -d */
模式究竟是*/
做什么的?
我们如何在上述命令(例如ls -d /home/alice/Documents
)中给出绝对路径以仅列出该路径中的目录?
*/
是匹配当前目录中所有子目录的模式(*
将匹配所有文件和子目录;将其/
限制为目录)。同样,要列出 /home/alice/Documents 下的所有子目录,请使用ls -d /home/alice/Documents/*/
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/
我用:
ls -d */ | cut -f1 -d'/'
这会创建一个没有斜杠的单列 - 在脚本中很有用。
对于所有没有子文件夹的文件夹:
find /home/alice/Documents -maxdepth 1 -type d
对于所有带有子文件夹的文件夹:
find /home/alice/Documents -type d
未加引号的星号*
将被 shell 解释为模式(glob)。shell 将在路径名扩展中使用它。然后它将生成与该模式匹配的文件名列表。
一个简单的星号将匹配 PWD(当前工作目录)中的所有文件名。一个更复杂的模式 as*/
将匹配所有以 . 结尾的文件名/
。因此,所有目录。这就是为什么命令:
echo */
echo ./*/ ### Avoid misinterpreting filenames like "-e dir"
将(通过 shell)扩展到echo
PWD 中的所有目录。
要对此进行测试:创建一个mkdir
名为 test-dir 的目录 ( ),然后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 是在 PWD 中获取目录列表的原因。使其列出当前目录条目而不是每个目录的内容的选项(默认情况下)-d
。ls
ls -d */
但是,此命令(有点)不太可靠。它会因上面列出的奇怪的命名文件而失败。它会被几个名字窒息。您需要一一擦除,直到找到有问题的。
GNUls
将接受“选项结束”( --
) 键。
ls -d ./*/ ### More reliable BSD ls
ls -d -- */ ### More reliable GNU ls
要在自己的行中列出每个目录(在一列中,类似于 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
)。最后,在函数中使用参数列表不会影响当前运行的 shell 的参数列表。简单地
$ listdirs(){ set -- */; printf "%s\n" "${@%/}"; }
$ listdirs
提供此列表:
--
-
*
cs
dir with spaces
draft
files
-h
masters
-n
static
-v var
这些选项对于几种类型的奇数文件名是安全的。
该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
。
如果您想知道为什么 'ls -d */' 的输出会给您两个斜杠,例如:
[prompt]$ ls -d */
app// cgi-bin// lib// pub//
这可能是因为您的外壳或会话配置文件将ls命令别名为包含该标志的ls版本。-F
该标志将一个字符附加到每个输出名称(不是纯文件),指示它的类型。所以一个斜线来自匹配模式'*/',另一个斜线是附加的类型指示符。
为了摆脱这个问题,您当然可以为ls定义一个不同的别名。但是,要暂时不调用别名,您可以在命令前加上反斜杠:
\ls -d */
ls
解决方案,包括目录的符号链接这里的许多答案实际上并没有使用ls
(或仅在微不足道的意义上使用它ls -d
,而使用通配符进行实际的子目录匹配。真正的ls
解决方案很有用,因为它允许使用ls
排序选项等选项。
ls
已经给出了一种使用的解决方案,但它与其他解决方案的不同之处在于它排除了指向目录的符号链接:
ls -l | grep '^d'
(可能通过管道sed
或awk
隔离文件名)
在应该包含指向目录的符号链接的(可能更常见的)情况下,我们可以使用-p
选项 of ls
,这使得它在目录名称(包括符号链接的)后面附加一个斜杠字符:
ls -1p | grep '/$'
或者,去掉尾部的斜杠:
ls -1p | grep '/$' | sed 's/\/$//'
我们可以ls
根据需要添加选项(如果使用长列表,-1
则不再需要)。
注意:如果我们想要尾部斜杠,但不希望它们被 突出显示grep
,我们可以通过使该行的实际匹配部分为空来粗略地删除突出显示:
ls -1p | grep -P '(?=/$)'
当前目录的简单列表,它将是:
ls -1d */
如果你想要它排序和清洁:
ls -1d */ | cut -c 1- | rev | cut -c 2- | rev | sort
请记住:大写字符在排序中具有不同的行为
如果不需要列出隐藏目录,我提供:
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}'
我只是将它添加到我的.bashrc
文件中(如果您只需要/想要一个会话,您也可以在命令行上键入它):
alias lsd='ls -ld */'
然后lsd将产生所需的结果。
仅列出目录:
ls -l | grep ^d
仅列出文件:
ls -l | grep -v ^d
或者你也可以这样做:
ls -ld */
要显示不带的文件夹列表/
:
ls -d */|sed 's|[/]||g'
试试这个。它适用于所有 Linux 发行版。
ls -ltr | grep drw
这是我正在使用的
ls -d1 /Directory/Path/*;
测试该项目是否是一个目录test -d
:
for i in $(ls); do test -d $i && echo $i ; done
*/
是匹配当前目录中目录的文件名匹配模式。
只列出目录,我喜欢这个功能:
# 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'
}
仅供参考,如果您想在多行中打印所有文件,您可以执行一个ls -1
将每个文件打印在单独行中的操作。文件 1 文件 2 文件 3
file * | grep directory
输出(在我的机器上)——
[root@rhel6 ~]# file * | grep directory
mongo-example-master: directory
nostarch: directory
scriptzz: directory
splunk: directory
testdir: directory
使用cut可以进一步细化上述输出:
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
ls -l | awk '/^d/ {print $9}'
where ls -l
list files with permisions
awk
filter output
'/^d/'
regularexpresion 仅搜索以字母开头的行d
(作为目录)查看第一行 - 权限
{print}
将打印所有列
{print $9}
将仅打印输出中的第 9 列(名称)ls -l
非常简单干净
我部分解决了它:
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
好吧,这减少了我的主要部分:)
仅从“此处”列出目录的单行代码。
带文件数。
for i in `ls -d */`; do g=`find ./$i -type f -print| wc -l`; echo "Directory $i contains $g files."; done
使用 Perl:
ls | perl -nle 'print if -d;'
这是一个使用树的变体,它仅在单独的行上输出目录名称,是的,它很难看,但是嘿,它可以工作。
tree -d | grep -E '^[├|└]' | cut -d ' ' -f2
或使用 awk
tree -d | grep -E '^[├|└]' | awk '{print $2}'
但是,这可能会更好,并且会保留/
之后的目录名称。
ls -l | awk '/^d/{print $9}'
添加使其完整循环,检索每个文件夹的路径,使用 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 -1d /some/folder/*/ | awk -F "/" "{print \$(NF-1)}"
回答原来的问题,与本身*/
无关;ls
它是由 shell/Bash 在称为globbing的过程中完成的。
这就是为什么echo */
和ls -d */
输出相同的元素。(该-d
标志ls
输出目录名称而不是目录的内容。)
如果您的文件夹名称中有空间 $9 print 将不起作用,请尝试以下命令
ls -l yourfolder/alldata/ | grep '^d' | awk '{print $9" " $10}'
输出
ls -l yourfolder/alldata/ | grep '^d' | awk '{print $9" " $10}'
testing_Data
Folder 1