newbie ls question - Linux

This is a discussion on newbie ls question - Linux ; 1. How do I list all files in current directory and each of its subdirectories in Linux ? 2. Does anyone have a shell script that counts each unique file in current directory and each subdirectory ? Thanks...

+ Reply to Thread
Results 1 to 9 of 9

Thread: newbie ls question

  1. newbie ls question

    1. How do I list all files in current directory and each of its
    subdirectories in Linux ?

    2. Does anyone have a shell script that counts each unique file in
    current directory and each subdirectory ?

    Thanks


  2. Re: newbie ls question

    Thanks writes:
    > How do I list all files in current directory and each of its
    > subdirectories in Linux ?


    "ls -R". Read the ls man page.

    > Does anyone have a shell script that counts each unique file in current
    > directory and each subdirectory ?


    Including or excluding the directories themselves?
    --
    John Hasler
    john@dhh.gt.org
    Dancing Horse Hill
    Elmwood, WI USA

  3. Re: newbie ls question

    karein_rask@yahoo.com wrote:
    >1. How do I list all files in current directory and each of its
    >subdirectories in Linux ?


    The first thing you need to learn isn't /ls/, it's /man/!

    man ls

    That will tell you far more than you want to know at any
    given time. But when you do need to know something,
    that's how to find it.

    In this instance, browse through the many command line
    options for /ls/, and eventually you'll come to -R,
    which does a recursive listing of all subdirectories.

    >2. Does anyone have a shell script that counts each unique file in
    >current directory and each subdirectory ?


    #!/bin/bash
    find /path/to/directory -type f | wc -l

    Look up the man pages for /find/ and /wc/ to see what
    that is doing.

    --
    Floyd L. Davidson
    Ukpeagvik (Barrow, Alaska) floyd@apaflo.com

  4. Re: newbie ls question

    Floyd L. Davidson writes:
    > find /path/to/directory -type f | wc -l


    He wants _unique_ files.
    find /path/to/directory -type f | sort -u | wc -l

    --
    John Hasler
    john@dhh.gt.org
    Dancing Horse Hill
    Elmwood, WI USA

  5. Re: newbie ls question

    John Hasler wrote:
    >Floyd L. Davidson writes:
    >> find /path/to/directory -type f | wc -l

    >
    >He wants _unique_ files.
    >find /path/to/directory -type f | sort -u | wc -l


    He did not specify "unique files", and "sort -u" won't
    change the listing anyway!

    I'm not sure what you are thinking of... You cannot
    have two files with the same name in the same directory,
    and that would be the only thing "sort -u" would
    eliminate.

    --
    Floyd L. Davidson
    Ukpeagvik (Barrow, Alaska) floyd@apaflo.com

  6. Re: newbie ls question

    Floyd L. Davidson writes:
    > I'm not sure what you are thinking of... You cannot have two files with
    > the same name in the same directory,


    You can have two files with the same name in two directories under the same
    top level directory. He did mention "unique files": that was the only
    thing I could think of that he might mean. You're right that I didn't
    think the command through properly. Try this:

    find /path/to/directory -type f -printf '%f\n' | sort -u | wc -l

    We still don't know what problem he is trying to solve, though. Seems
    unlikely that this is what he really needs.
    --
    John Hasler
    john@dhh.gt.org
    Dancing Horse Hill
    Elmwood, WI USA

  7. Re: newbie ls question

    John Hasler wrote:
    >Floyd L. Davidson writes:
    >> I'm not sure what you are thinking of... You cannot have two files with
    >> the same name in the same directory,

    >
    >You can have two files with the same name in two directories under the same
    >top level directory. He did mention "unique files": that was the only


    I did't think he meant unique file _names_, but
    literally unique _files_, which would include two
    different files, each with the same name but in
    different subdirectories.

    That's the way I read it anyway...

    >thing I could think of that he might mean. You're right that I didn't
    >think the command through properly. Try this:
    >
    >find /path/to/directory -type f -printf '%f\n' | sort -u | wc -l


    Yep, that'll do it.

    >We still don't know what problem he is trying to solve, though. Seems
    >unlikely that this is what he really needs.


    Hard to say... but the OP will definitely get the idea
    that man pages are wonderous things, well worth reading!

    --
    Floyd L. Davidson
    Ukpeagvik (Barrow, Alaska) floyd@apaflo.com

  8. Re: newbie ls question

    floyd@apaflo.com (Floyd L. Davidson) said:
    >John Hasler wrote:
    >>Floyd L. Davidson writes:
    >>> I'm not sure what you are thinking of... You cannot have two files with
    >>> the same name in the same directory,

    >>
    >>You can have two files with the same name in two directories under the same
    >>top level directory. He did mention "unique files": that was the only

    >
    >I did't think he meant unique file _names_, but literally unique
    >_files_, which would include two different files, each with the same
    >name but in different subdirectories.


    The other way to read that would be to count multiple hard links pointing
    to the same inode as one file. "ls -Ri" output with postprocessing in
    awk (memorize and print the first occurrence of each inode number, and omit
    printing re-occurrences of inode numbers) would be quite a fast solution
    for this (unless the ls recursion happens to cross filesystem boundaries,
    which would make the processing somewhat harder).
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)

  9. Re: newbie ls question

    Juha Laiho wrote:

    > floyd@apaflo.com (Floyd L. Davidson) said:
    >>John Hasler wrote:
    >>>Floyd L. Davidson writes:
    >>>> I'm not sure what you are thinking of... You cannot have two files
    >>>> with the same name in the same directory,
    >>>
    >>>You can have two files with the same name in two directories under the
    >>>same
    >>>top level directory. He did mention "unique files": that was the only

    >>
    >>I did't think he meant unique file _names_, but literally unique
    >>_files_, which would include two different files, each with the same
    >>name but in different subdirectories.

    >
    > The other way to read that would be to count multiple hard links pointing
    > to the same inode as one file. "ls -Ri" output with postprocessing in
    > awk (memorize and print the first occurrence of each inode number, and
    > omit printing re-occurrences of inode numbers) would be quite a fast
    > solution for this (unless the ls recursion happens to cross filesystem
    > boundaries, which would make the processing somewhat harder).


    Here's a one liner, assuming that a count is all that is required, which
    seems to be the case from the the original message:

    find -type f -printf "%D:%i\n" | sort | uniq | wc -l

    That works by having find print the device number and inode number as an
    identifying key, then doing a standard sort uniq wc to get the unique
    occurrences.

    To the OP:
    Adjust/remove the "-type f" if count dirs and other funny nodes is required.

    Add -xdev if it is desired not to cross file system boundaries, though the
    above code will function correctly even if it does.

    Cheers

    Tim

+ Reply to Thread