timothepoznanski
timothepoznanski

Reputation: 1112

Is this a bash bug? (list directories with ls)

If I want to list all files and directories in the following directory :

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

1 directory, 1 file

Why dir2 is not shown below? Only the file is shown.

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

If I copy the directory and list, they are both shown :

[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

Upvotes: 0

Views: 64

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74685

No, it's not a bug; it's the expected behaviour of the shell to expand * to match everything. The * is replaced by a list of arguments, one for each matching path, before the command is run.

In your first example, ls -l *, the shell expands * to dir2, so your command is ls -l dir2. ls then just lists the contents of that directory, so you get your file coucou.txt.

In your second example, * is expanded to two arguments, dir2 and dir3. In this case, the behaviour of ls -l is to separately list the contents of each directory.

Upvotes: 5

FcoRodr
FcoRodr

Reputation: 1633

If you use the command like that ls -l * it's like you are writing the command for every element in your current directory, changing that * with the name of your element. And when you use the command ls and the argument is a directory, it lists the elements inside, not the directory itself.

I think you are looking for a recursive ls option, which is -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

Upvotes: 2

Related Questions