why ls -d can't get the directories

L

loryliu

Guest
in the man page of ls, -d means list the dorectories
when I issue ls -d ,I get nothing.
Anyone has an idea
 


where are you performing the command? Lets say that you are in /home/username. And for some reason your homefolder is empty. It won't show anything. Also, sometimes if you are performing the command in a directory that you do not have read permission, you won't see anything either.
 
Hi lorylui,

The -d option to the ls command will work the way you expect it to if you provide it a directory name.
Eg:
Code:
$ ls -ld scripts
drwxr-xr-x 2 user user 4096 Jul 17 21:12 scripts

This will show you the the directory itself rather than it's contents.

A work around to this would be:
Code:
ls -la|grep ^d

Hope this helps.

McPhee
 
Hi loryliu try this:

Code:
ls -F | grep /

I know that exist a better way, but it's another form to list all directories.
 
My lsd function will probably give you the results you're looking for. I find it so handy that I keep it in my ~/.bashrc on every machine I have access to.

The trick is ${D:+/}. If you specify a directory, it expands to a slash. If not, then it does not add a leading slash. So lsd shows you all the subdirectories in the current directory, or the directory you specify.

Code:
function lsd { local D="${1:-}"; command ls -d "${D%/}${D:+/}"*/; }
 
in my terminal "ls" shows my directories as blue, and executables as Green, and normal files as light blue.
 
in the man page of ls, -d means list the dorectories
when I issue ls -d ,I get nothing.
Anyone has an idea

The ls -d command requires a filename to list. to make this work, you try: ls -d */.

This will list only the directories by themselves. If you drop the -d, the directories are listed separately with the files located within them.
 


Top