Thread: Urgent help on ls- ld

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    40

    Urgent help on ls- ld

    Hi all,

    I have a quick doubt on ls utility
    Kindly clarify the same

    please let me know the difference between ls -ld and ls -lrt | grep '^d'

    ls -ld also shows directory but weird output.
    ls -lrt | grep '^d' shows actual output.

    what ls -ld actually does?

    Thanks in advance,
    Vaibhav.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "ls -d" is sort of a silly switch. Look in the man page ("man ls").
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    could not understand! :-(

    can somebody explain with an exact example?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Both methods are not correct to find all directories under the current directory. The "ls -ld" will not recurse, and the usage of grep sucks.

    Do this:

    Code:
    find . -type d
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    40
    I understand urs example, but
    i wanted to know about ls -ld.
    ls -ld is telling about the home directory and nothing else.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vaibhavs17 View Post
    I understand urs example, but
    i wanted to know about ls -ld.
    ls -ld is telling about the home directory and nothing else.
    Yes... What else did you expect?

    To get an actual list of the directores in the current directory, you must do:

    Code:
    ls -ld *
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vaibhavs17 View Post
    I understand urs example, but
    i wanted to know about ls -ld.
    ls -ld is telling about the home directory and nothing else.

    Like I said, -d is a stupid switch. Look:
    Code:
    [root~] ls -d
    .
    [root~] ls -d /
    /
    [root~] cd /
    [root/] ls -d
    .
    [root/] ls -ld
    drwxr-xr-x 25 root root 4096 2009-05-18 10:28 .
    What brewbuck is claiming in the last post would be nice, but is experientially false. ls could be broken, or this could just be a feature most sane people would forget.
    Last edited by MK27; 05-18-2009 at 12:57 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    Like I said, -d is a stupid switch. Look:
    Well, it's definitely non-intuitive, for historical reasons. What it actually means is "among the filenames specifically given to ls, list only the ones which are directories." So you have to do

    Code:
    ls -d *
    Instead of just:
    Code:
    ls -d
    Remember that if no filenames are given to ls, it uses an implicit filename of "." so "ls -d" is doing exactly what you (implicitly) told it -- print any directory in its list of filenames. "." is a directory, so it prints it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by brewbuck View Post
    Well, it's definitely non-intuitive, for historical reasons.
    The description of what it is suppose to do is intuitive enough, if it means what you are saying. But it obviously does not, because when I try "ls -d *" I do not get a list consisting only of directories. I get *the exact same* output as if I had just used "ls".
    Code:
    [root~/C] ls
    answerone.txt  experimental   header  orig_test.c  studentsmark.txt  test.txt
    a.out          fork-return.c  input   programs     test              text.file
    bin2dec.c      functions      kernel  RECURSION.c  test2.c           this
    diff.patch     gtk            models  SCRAPS.c     test3.c           tmp.txt
    diff.patch2    gtk-app        old     see          test.c
    example-usage  hacking.h      openGL  Sphere       testfile.txt
    [root~/C] ls -d *
    answerone.txt  experimental   header  orig_test.c  studentsmark.txt  test.txt
    a.out          fork-return.c  input   programs     test              text.file
    bin2dec.c      functions      kernel  RECURSION.c  test2.c           this
    diff.patch     gtk            models  SCRAPS.c     test3.c           tmp.txt
    diff.patch2    gtk-app        old     see          test.c
    example-usage  hacking.h      openGL  Sphere       testfile.txt
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MK27 View Post
    The description of what it is suppose to do is intuitive enough, if it means what you are saying. But it obviously does not, because when I try "ls -d *" I do not get a list consisting only of directories. I get *the exact same* output as if I had just used "ls".
    Ugh. Even I can misinterpret the manual, it seems.

    "-d" just means, "If it's a directory, list its name, not its contents."

    So to finally get to what we're trying to do, you need to use the shell globbing to limit the list to only directories:

    Code:
    ls -d */
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    SHELL GLOBBING! I don't know what these *nix types expect. They have made me afraid of my own computer! It's laughing at me right now and dancing back and forth, satanically!

    Thanks brewbuck. I will never sneer at "ls -d" again.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. huh? ld can't find library that is in /usr/lib
    By zoubidoo in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2008, 08:18 AM
  2. version of ls -l bug, only takes cwd :(
    By chl12 in forum C Programming
    Replies: 2
    Last Post: 06-12-2007, 02:44 AM
  3. Undefined symbols from ld? ; making bundle; gcc 3.3
    By profxtjb in forum C Programming
    Replies: 5
    Last Post: 06-15-2004, 01:31 AM
  4. Turtle CVS and sourceforge, no 'ls'
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 03:19 AM
  5. ld: linking
    By ilear_01 in forum C++ Programming
    Replies: 0
    Last Post: 06-10-2003, 05:40 AM