1

On ls man page, I saw this

 -d, --directory

    list directory entries instead of contents, and do not 
    dereference symbolic links

on my current folder:

drwxr-xr-x 2 oracle oinstall 4.0K Jul 13 11:52 folderd
drwxr-xr-x 2 oracle oinstall 4.0K Jul 13 11:52 folderc
drwxr-xr-x 2 oracle oinstall 4.0K Jul 13 11:52 folderb
drwxr-xr-x 2 oracle oinstall 4.0K Jul 13 11:52 foldera
-rw-r--r-- 1 oracle oinstall    0 Jul 13 11:52 filed
-rw-r--r-- 1 oracle oinstall    0 Jul 13 11:52 filec
-rw-r--r-- 1 oracle oinstall    0 Jul 13 11:52 fileb
-rw-r--r-- 1 oracle oinstall    0 Jul 13 11:52 filea

q1) Doing a ls -ld show me a . - why?

[oracle@centos1 script]$ ls -ld
drwxr-xr-x 6 oracle oinstall 4096 Jul 13 11:52 .

doing a ls -l *, give me everything including the files in the folder:

[oracle@centos1 script]$ ls -l *
-rw-r--r-- 1 oracle oinstall    0 Jul 13 11:52 filea
-rw-r--r-- 1 oracle oinstall    0 Jul 13 11:52 fileb
-rw-r--r-- 1 oracle oinstall    0 Jul 13 11:52 filec
-rw-r--r-- 1 oracle oinstall    0 Jul 13 11:52 filed

foldera:
total 0
-rw-r--r-- 1 oracle oinstall 0 Jul 13 11:57 fileafolda
-rw-r--r-- 1 oracle oinstall 0 Jul 13 11:55 fileainfolda

folderb:
total 0

folderc:
total 0

folderd:
total 0

Doing a ls -ld */ and it shows me the right result, only folders.

[oracle@centos1 script]$ ls -ld */
drwxr-xr-x 2 oracle oinstall 4096 Jul 13 11:57 foldera/
drwxr-xr-x 2 oracle oinstall 4096 Jul 13 11:52 folderb/
drwxr-xr-x 2 oracle oinstall 4096 Jul 13 11:52 folderc/
drwxr-xr-x 2 oracle oinstall 4096 Jul 13 11:52 folderd/

q3) what does ls actually take in as a parameter? * here means everything including folder. and */ means folder only, and -d here removes folder content.

If that's the case, why doesn't ls -ld show any result at all. only a . at q1?

In short, does it means that:

  1. ls -l = ls -l . = list all contents in current directory
  2. ls -l * = list all contents in current directory + the contents in the sub -directories
  3. ls -ld = ls -ld . = list the current directory but not its content (hence the .)
  4. ls -l */ = list all sub-directories + its content in this current directory
  5. ls -ld */ = list all sub-directories but not its content in this current directory

q1) But I do not understand the following below

Assuming I am inside /script.

ls -l * → list all contents inside the current directory, including it subdirectories's content.

ls -ld * → list all contents inside the current directory, excluding subdirectories's content.

Here the -d is taking effect on the subdirectories inside the current directory.

But ls -ld . → the "-d" is not meant to be use for the subdirectories, but actually for the current directory itself.

I don't understand why is the -d affecting the current directory and not its contents.

Noob
  • 303
  • I do not understand your new question "q1)" after the edit. Are you mixing up -d with -r? -r will recursively descend and show the contents of the contents of directories, and so on. But without -r ls only lists the directories it has been asked to list and stops there. -d does not change that. – Celada Jul 13 '15 at 07:59
  • @Celada thanks for the reply. nope , it is nothing to do with -r, let me rephrase my question again. – Noob Jul 13 '15 at 08:08
  • "But ls -ld . -> the "-d" is not meant to be use for the subdirectories, but actually for the current directory itself." — well, yes, that's what you asked for by asking ls to list . (the current directory). So it shows the current directory but not its contents, which is the point of -d. – Celada Jul 13 '15 at 08:09
  • "I don't understand why is the "-d" refering/affecting the current directory and not its contents." — it affects its contents when what's what you ask ls to list. – Celada Jul 13 '15 at 08:10
  • In conclusion, I'm afraid I must still be missing something in your explanation. – Celada Jul 13 '15 at 08:11
  • ls -l * is also listing everything in the current directory ls -l . is listing everything in the current directory, just without the subdirectories 'content

    but ls -ld * is affecting the subdirectories, not the current directory

    – Noob Jul 13 '15 at 08:12
  • Yes, this is all expected and correct, and exactly what you asked ls to do. For example, for the last: ls -ld* yes of course it is affecting subdirectories and not the current directory, because subdirectories (and other files) are exactly what you supplied to ls to list. – Celada Jul 13 '15 at 08:15

1 Answers1

4

q1) Doing a ls -ld show me a . - why ?

When you give no arguments to ls, the default is to run the command on the current directory, also known as .. Normally that means listing the contents of the directory, but you have used the -d option which requests listing the directory itself, not its contents. So you get the information for ., the current directory.

doing a ls -ld *, give me everything including the files in the folder

No, it doesn't. ls -ld * will list each file in the current directory matching * (which is all of them excet hidden ones) but not the contents of those that are directories, again because you used -d. Note that your example output does something different because it does not use -d.

doing a ls -ld */ and it shows me the right result, only folders.

Only directories match the pattern */, because regular files cannot have pathnames ending in /.

q3) what does "ls" actually take in as a parameter ? * here means everything including folder. and */ means folder only, and -d here remove folder content.

All basically correct. To expand: * matches everything except hidden files, */ is the same but only matches directories, and you have the meaning of the -d option just right.

if that's the case, why doesn't ls -ld show any result at all. only a . at q1

It does show a result: it shows the current directory, which is known as .!

Celada
  • 44,132