Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这是bash bug吗?(用ls列出目录)_Bash_Ls - Fatal编程技术网

这是bash bug吗?(用ls列出目录)

这是bash bug吗?(用ls列出目录),bash,ls,Bash,Ls,如果要列出以下目录中的所有文件和目录: [root@centos2 dir1]# tree . +-- dir2 +-- coucou.txt 1 directory, 1 file 为什么下面没有显示dir2?仅显示该文件 [root@centos2 dir1]# ls -l * total 0 -rwxr--r-- 1 root root 0 Dec 2 18:20 coucou.txt 如果复制目录和列表,它们都会显示: [root@centos2 dir1]# cp -r

如果要列出以下目录中的所有文件和目录:

[root@centos2 dir1]# tree
.
+-- dir2
    +-- coucou.txt

1 directory, 1 file
为什么下面没有显示dir2?仅显示该文件

[root@centos2 dir1]# ls -l *
total 0
-rwxr--r-- 1 root root 0 Dec  2 18:20 coucou.txt
如果复制目录和列表,它们都会显示:

[root@centos2 dir1]# cp -r dir2/ dir3
[root@centos2 dir1]# tree
.
+-- dir2
¦   +-- coucou.txt
+-- dir3
    +-- coucou.txt

2 directories, 2 files

[root@centos2 dir1]# ls -l *
dir2:
total 0
-rwxr--r-- 1 root root 0 Dec  2 18:20 coucou.txt

dir3:
total 0
-rwxr--r-- 1 root root 0 Dec  5 11:34 coucou.txt

不,它不是虫子;shell的预期行为是扩展
*
以匹配所有内容。在运行命令之前,
*
将被一系列参数替换,每个匹配路径对应一个参数

在第一个示例中,
ls-l*
,shell将
*
扩展为
dir2
,因此您的命令是
ls-l dir2
ls
然后只列出该目录的内容,这样就可以得到文件
coucou.txt


在第二个示例中,
*
扩展为两个参数,
dir2
dir3
。在这种情况下,
ls-l
的行为是分别列出每个目录的内容。

如果您使用类似于
ls-l*
的命令,就好像您正在为当前目录中的每个元素编写命令,用元素的名称更改该
*
。当您使用命令
ls
并且参数是一个目录时,它会列出其中的元素,而不是目录本身

我想您正在寻找一个递归的
ls
选项,它是
-R

[root@centos2 dir1]#ls -lR
.:
total 8
drwxr-xr-x 2 root root 4096 Dec  5 11:41 dir2
drwxr-xr-x 2 root root 4096 Dec  5 11:41 dir3

./dir2:
total 0
-rw-r--r-- 1 root root 0 Dec  5 11:41 cocou.txt

./dir3:
total 0
-rw-r--r-- 1 root root 0 Dec  5 11:41 cocou.txt

要列出目录,请使用
-d
开关作为
ls-d*/
尝试
echo*
,然后查看发生了什么。模式(glob)扩展是
bash
ls
是一个外部程序,与
bash
没有任何关系(从20世纪70年代开始,你真的认为你在其中发现了新的bug吗?)。要查看发生的扩展,请使用
set-o xtrace
(或简单的
set-x
)。使用
set+o xtrace
set+x
将其关闭。@cdarke我很想在其中找到一个新的bug!;)你说得对,我问题的标题不太合适。我应该问一下“ls”的行为,而不是谈论bash。。。谢谢你的澄清。