Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi all, Im trying to see a list of the top level items in a directory without getting a list of every single file inside those directories. I want to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    11

    How do I see a list of the directories without a list of every file?


    Hi all,
    Im trying to see a list of the top level items in a directory without getting a list of every single file inside those directories. I want to use this command:

    find /path/to/files -mtime +1 -exec ls {} \;

    The directory I want to see has two directories and a .tar file inside it. I just want to have my command find those three items, not list every file inside the two directories as well. How could I modify my command? I tried adding the -d argument to ls, no dice.

    Thanks,
    Erin

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Why can't you just do "ls /path/to/directory"? That should only show you the direct contents of that directory:
    Code:
    [alex@niamh ~]$ ls ~/test
    c  perl  ruby
    Each of {c, perl, ruby} is itself a directory, but they are not expanded.

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,288
    Hi.

    I often like tree, which is fairly flexible, as in this demo:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate flexible structure listing of "tree".
    
    # Section 1, setup, pre-solution, $Revision: 1.23 $".
    # Infrastructure details, environment, debug commands for forum posts. 
    # Uncomment export command to run script as external user.
    # export PATH="/usr/local/bin:/usr/bin:/bin" HOME=""
    set +o nounset
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    edges() { _n="$1" _f="$2";head -n $_n $_f; pe "  ---";tail -n $_n $_f ; }
    db() { : ; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    C=$HOME/bin/context && [ -f $C ] && $C tree
    set -o nounset
    pe
    
    pl " Setup, create structure of directories and files:"
    db " Section 1: Create directory / file structure."
    rm -rf d
    mkdir -p d/d1/d11 d/d2/d21
    touch d/f d/d1/f1 d/d1/d11/f11 d/d2/f2 d/d2/d21/f21
    tree -a d
    
    # Section 2, solution.
    pl " Results, all directories:"
    db " Section 2: solution."
    tree -d d
    
    pl " Results, one level:"
    tree -L 1 -d d
    
    pl " Results, one level, full path, no indent:"
    tree -fi -L 1 -d d
    
    pl " Results, one level, no indent, permissions, size :"
    tree -psh -i -L 1 -d d
    
    pl " Results, pretty ascii graphic lines:"
    LC_ALL="" LANG=en_US.UTF-8 tree -L 1 -d d
    
    exit 0
    producing:
    Code:
    %  ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.32-5-686, i686
    Distribution        : Debian GNU/Linux 6.0 (squeeze) 
    GNU bash 4.1.5
    tree v1.5.3 (c) 1996 - 2009 by Steve Baker, Thomas Moore, Francesc Rocher, Kyosuke Tokoro 
    
    
    -----
     Setup, create structure of directories and files:
     db,  Section 1: Create directory / file structure.
    d
    |-- d1
    |   |-- d11
    |   |   `-- f11
    |   `-- f1
    |-- d2
    |   |-- d21
    |   |   `-- f21
    |   `-- f2
    `-- f
    
    4 directories, 5 files
    
    -----
     Results, all directories:
     db,  Section 2: solution.
    d
    |-- d1
    |   `-- d11
    `-- d2
        `-- d21
    
    4 directories
    
    -----
     Results, one level:
    d
    |-- d1
    `-- d2
    
    2 directories
    
    -----
     Results, one level, full path, no indent:
    d
    d/d1
    d/d2
    
    2 directories
    
    -----
     Results, one level, no indent, permissions, size :
    d
    [drwxr-x--- 4.0K]  d1
    [drwxr-x--- 4.0K]  d2
    
    2 directories
    
    -----
     Results, pretty ascii graphic lines:
    d
    ├── d1
    └── d2
    
    2 directories
    See man tree for details ... cheers, drl

    PS If you run it and get somewhat different ouput, it may be due to the Google speller, which wants to interpret some sequences as markup, and changed the output somewhat. I think I caught most of the flaws.
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,639
    Code:
    # to just list one directory level, including hidden files that start with a dot.
    ls -a /path/to/files
    
    # to list all the sub-directories
    ls -aR /path/to/files
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Oct 2011
    Location
    STL, USA
    Posts
    8
    Yes, why does...
    ls
    ...produce a listing while...
    ls -d
    only gives me "."

    ???

    ls -d * works but ls -d fails...

  6. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,288
    Hi.

    First line in man ls:
    Code:
    List information about the FILEs (the current directory by default).
    So an
    Code:
    ls -d
    is the same as
    Code:
    ls -d .
    The man pages are your friends. Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,639
    Quote Originally Posted by davejjj View Post
    Yes, why does...
    ls
    ...produce a listing while...
    ls -d
    only gives me "."

    ???

    ls -d * works but ls -d fails...
    From the ls man page:
    Code:
           -d, --directory
                  list directory entries instead of contents, and do not dereference symbolic links
    So, the only directory entry in the current directory is its self-reference, denoted by the dot '.' - so don't use the -d argument. The suggestions I made previously show you what to do.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Just Joined!
    Join Date
    Oct 2011
    Location
    STL, USA
    Posts
    8
    Quote Originally Posted by Rubberman View Post
    From the ls man page:
    Code:
           -d, --directory
                  list directory entries instead of contents, and do not dereference symbolic links
    So, the only directory entry in the current directory is its self-reference, denoted by the dot '.' - so don't use the -d argument. The suggestions I made previously show you what to do.
    But this directory has three subdirectories in it. If I do...
    ls
    ...I get...
    dir1 dir2 dir3 file1 file2 file3

    Let me just ask then, what is the best way to filter results from ls? Say I want to only see directories or only see links? Thanks.

  9. #9
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,600
    Maybe not the best way but
    Code:
    ls -l | grep '^d'
    will get you directories only. The others, I'll leave as an exercise for the reader
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  10. #10
    Just Joined!
    Join Date
    Oct 2011
    Location
    STL, USA
    Posts
    8
    Quote Originally Posted by elija View Post
    Maybe not the best way but
    Code:
    ls -l | grep '^d'
    will get you directories only. The others, I'll leave as an exercise for the reader
    That is what I want! This seems to work without the single-quotes on SUSE. I would have thought that ls itself would have had something like this built in.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •