count the number of files - Unix

This is a discussion on count the number of files - Unix ; Hi, I'm using RedHat, and my question is: How to count the total number of files in one directory and all subdirectories? What commands should I try? Other than this question, I have another one: How to rename a number ...

+ Reply to Thread
Results 1 to 19 of 19

Thread: count the number of files

  1. count the number of files

    Hi,

    I'm using RedHat, and my question is:

    How to count the total number of files in one directory and all
    subdirectories?

    What commands should I try?

    Other than this question, I have another one:

    How to rename a number of files simultaneously?

    Say I have the following files:

    abc_kkk.txt
    efgrtg_kkk.txt
    1tsjd_kkk.txt

    How could I rename them at once to:

    abc.txt
    efgrtg.txt
    1tsjd.txt

    Thank you!

    Jerry


  2. Re: count the number of files

    In article <1131731844.934517.55040@g14g2000cwa.googlegroups.c om>,
    Jerry wrote:

    > Hi,
    >
    > I'm using RedHat, and my question is:
    >
    > How to count the total number of files in one directory and all
    > subdirectories?
    >
    > What commands should I try?


    By running find ./ you get one per line, whether directory or not. By
    pipe'ing that to wc with the option -l, you get a count.

    %find ./ | wc -l
    7752

    This number also includes the directory ./

    >
    > Other than this question, I have another one:
    >
    > How to rename a number of files simultaneously?
    >
    > Say I have the following files:
    >
    > abc_kkk.txt
    > efgrtg_kkk.txt
    > 1tsjd_kkk.txt
    >
    > How could I rename them at once to:
    >
    > abc.txt
    > efgrtg.txt
    > 1tsjd.txt


    Can't help you much with this one; I would write a program, say a
    shell script, and rename one by one by making my decisions after
    seeing each file (in a loop.)

  3. Re: count the number of files

    On Fri, 11 Nov 2005 18:09:38 +0000 (UTC), Daniel C. Bastos wrote:
    [...]
    >> How to count the total number of files in one directory and all
    >> subdirectories?
    >>
    >> What commands should I try?

    >
    > By running find ./ you get one per line, whether directory or not. By
    > pipe'ing that to wc with the option -l, you get a count.
    >
    > %find ./ | wc -l
    > 7752
    >
    > This number also includes the directory ./

    [...]

    But that doesn't include "." and ".." in the subdirectories.

    And that doesn't work if there are multiline file names.

    If using zsh, there's:

    files=(**/*(ND))
    print "there are $#files files in the current directory"

    (that ommits "." and ".." entries altogether).

    With find:

    echo "there are $(
    find .//. \! -name . | grep -c //
    ) files in the current directory"

    ommits "." and ".." as well.

    --
    Stephane

  4. Re: count the number of files

    In article ,
    Stephane Chazelas wrote:

    > On Fri, 11 Nov 2005 18:09:38 +0000 (UTC), Daniel C. Bastos wrote:
    > [...]
    >>> How to count the total number of files in one directory and all
    >>> subdirectories?
    >>>
    >>> What commands should I try?

    >>
    >> By running find ./ you get one per line, whether directory or not. By
    >> pipe'ing that to wc with the option -l, you get a count.
    >>
    >> %find ./ | wc -l
    >> 7752
    >>
    >> This number also includes the directory ./

    > [...]
    >
    > But that doesn't include "." and ".." in the subdirectories.


    It doesn't. I assume he didn't want them. I assume wrong, he speaks of
    all files. But then I get puzzled as to why you didn't include them
    either.

    > And that doesn't work if there are multiline file names.


    Sure, I didn't think of that.

    > If using zsh, there's:
    >
    > files=(**/*(ND))
    > print "there are $#files files in the current directory"
    >
    > (that ommits "." and ".." entries altogether).


    To be shell dependent, I rather use opendir(). :-)

    > With find:
    >
    > echo "there are $(
    > find .//. \! -name . | grep -c //
    > ) files in the current directory"
    >
    > ommits "." and ".." as well.


    I see, this counts multiline. That's nice, but why I thought you were
    going to include dot and dotdot of each subdirectory, since you
    pointed that out above.

  5. Re: count the number of files

    Daniel C. Bastos wrote:
    > It doesn't. I assume he didn't want them. I assume wrong, he speaks of
    > all files. But then I get puzzled as to why you didn't include them
    > either.


    Dan, you are right. I don't intend to count "." and ".." files.

    > > And that doesn't work if there are multiline file names.


    What do you guys mean by "multiline file names"? (Sorry, I'm an linux
    newbie). Does it mean that the file name is so long that it extends to
    next line of screen?

    > > If using zsh, there's:
    > >
    > > files=(**/*(ND))
    > > print "there are $#files files in the current directory"
    > >
    > > (that ommits "." and ".." entries altogether).

    >
    > To be shell dependent, I rather use opendir(). :-)
    >
    > > With find:
    > >
    > > echo "there are $(
    > > find .//. \! -name . | grep -c //
    > > ) files in the current directory"
    > >
    > > ommits "." and ".." as well.

    >
    > I see, this counts multiline. That's nice, but why I thought you were
    > going to include dot and dotdot of each subdirectory, since you
    > pointed that out above.


    I guess Stephane's command meant to omit "." and ".." files

    Thank you guys!

    Jerry


  6. Re: count the number of files

    >>>>> Jerry writes:

    > Say I have the following files:


    > abc_kkk.txt
    > efgrtg_kkk.txt
    > 1tsjd_kkk.txt


    > How could I rename them at once to:


    > abc.txt
    > efgrtg.txt
    > 1tsjd.txt


    One way:

    for i in abc_kkk.txt 1tsjd_kkk.txt efgrtg_kkk.txt
    do
    j=`echo $i | sed s/_kkk.txt/.txt/`
    mv $i $j
    done

    Another way is to use the mmv command:

    mmv '*_kkk.txt' '#1.txt'

    --
    Neil.
    Genius doesn't work on an assembly line basis. You can't simply say,
    "Today I will be brilliant."
    -- Kirk, "The Ultimate Computer", stardate 4731.3

  7. Re: count the number of files



    Jerry wrote:

    > What do you guys mean by "multiline file names"? (Sorry, I'm an linux
    > newbie). Does it mean that the file name is so long that it extends to
    > next line of screen?


    No. Unfortunately, it's perfectly valid (though unusual) to have a
    newline character in a file name, e.g.:

    $ > `printf "abc\ndef"`
    $ ls -l
    total 0
    -rw-r--r-- 1 morton morton 0 Nov 11 13:21 abc
    def

    regards,

    Ed.

  8. Re: count the number of files

    On 11 Nov 2005 10:55:26 -0800, Jerry wrote:
    > Daniel C. Bastos wrote:
    >> It doesn't. I assume he didn't want them. I assume wrong, he speaks of
    >> all files. But then I get puzzled as to why you didn't include them
    >> either.

    >
    > Dan, you are right. I don't intend to count "." and ".." files.
    >
    >> > And that doesn't work if there are multiline file names.

    >
    > What do you guys mean by "multiline file names"? (Sorry, I'm an linux
    > newbie). Does it mean that the file name is so long that it extends to
    > next line of screen?



    A file whose name is made of multiple line. A filename is free
    text on Unix (except for the "/" and NUL characters), so you can
    do:

    touch 'some
    file
    with newline
    characters in it.txt'

    --
    Stephane

  9. Re: count the number of files

    On Fri, 11 Nov 2005 18:38:05 +0000 (UTC), Daniel C. Bastos wrote:
    [...]
    >>> %find ./ | wc -l
    >>> 7752
    >>>
    >>> This number also includes the directory ./

    >> [...]
    >>
    >> But that doesn't include "." and ".." in the subdirectories.

    >
    > It doesn't. I assume he didn't want them. I assume wrong, he speaks of
    > all files. But then I get puzzled as to why you didn't include them
    > either.


    Because that's not very useful. The only command that I know
    reports them is ls, and ls output is not parsable.

    You can also assume that there's always a "." and a ".." in
    every directory.

    Then:

    find .//. \( -name . -o -print \) -type d -print -print |
    grep -c //

    --
    Stephane

  10. Re: count the number of files

    Begin
    On 2005-11-11, Stephane Chazelas wrote:
    > But that doesn't include "." and ".." in the subdirectories.


    Note that those two are counted elsewhere, and counting them would
    distort the count of actual directories by a factor of three. This is
    easily demonstrated, looking at the inodes:

    $ mkdir foo
    $ ls -aid . foo
    23552 . 165014 foo
    $ ls -ai foo
    165014 . 23552 ..

    Altough I'm inclined to think it is unlikely to be desirable to count
    directories thrice[1], for files (both hardlinks and softlinks) a case
    can be made either way, depending on what you want.


    [1] Strictly speaking, one counts foo and foo/. and then foo/*/.. for
    foo, that is foo gets a count of 2+(first level subdirs of foo),
    but in the end it evens out at three per directory, since we are
    not differentiating.

    --
    j p d (at) d s b (dot) t u d e l f t (dot) n l .

  11. Re: count the number of files

    Stephane,

    I tried your following approach.

    > With find:
    >
    > echo "there are $(
    > find .//. \! -name . | grep -c //
    > ) files in the current directory"
    >
    > ommits "." and ".." as well.


    With this approach, the number I got seems include the number of
    subdirectories, which is not what I want to count. Could you or anyone
    else here please modify the command above so that it will gives me ONLY
    the number of FILES in current directory and all subdirectories?

    Thank you very much. I really appreciate it.

    Jerry


  12. Re: count the number of files

    In article <1131739054.319781.235050@g47g2000cwa.googlegroups. com>,
    Jerry wrote:

    > Stephane,
    >
    > I tried your following approach.
    >
    >> With find:
    >>
    >> echo "there are $(
    >> find .//. \! -name . | grep -c //
    >> ) files in the current directory"
    >>
    >> ommits "." and ".." as well.

    >
    > With this approach, the number I got seems include the number of
    > subdirectories, which is not what I want to count. Could you or anyone
    > else here please modify the command above so that it will gives me ONLY
    > the number of FILES in current directory and all subdirectories?


    find ./ -type f | grep -c ./

    From Stephane's idea of counting slashes, I thought of the above, but
    that won't work with directories with . in the name and with at least
    one file inside. Can someone solve this?

    Jerry, if you don't have dots in directory names, it should work for
    you, if I am not missing anything else. I hope not.

  13. Re: count the number of files

    2005-11-11, 11:57(-08), Jerry:
    > Stephane,
    >
    > I tried your following approach.
    >
    >> With find:
    >>
    >> echo "there are $(
    >> find .//. \! -name . | grep -c //
    >> ) files in the current directory"
    >>
    >> ommits "." and ".." as well.

    >
    > With this approach, the number I got seems include the number of
    > subdirectories, which is not what I want to count. Could you or anyone
    > else here please modify the command above so that it will gives me ONLY
    > the number of FILES in current directory and all subdirectories?

    [...]

    find .//. \! -type d -print | grep -c //

    --
    Stéphane

  14. Re: count the number of files

    2005-11-11, 20:20(+00), Daniel C. Bastos:
    [...]
    >> With this approach, the number I got seems include the number of
    >> subdirectories, which is not what I want to count. Could you or anyone
    >> else here please modify the command above so that it will gives me ONLY
    >> the number of FILES in current directory and all subdirectories?

    >
    > find ./ -type f | grep -c ./
    >
    > From Stephane's idea of counting slashes, I thought of the above, but
    > that won't work with directories with . in the name and with at least
    > one file inside. Can someone solve this?


    For grep, "." is any character, not (only) ".".

    find .//. -type f -print | grep -c //

    "//" can't appear anywhere else, even in a multiline file name,
    so it's safe.

    Note that the above doesn't count the symlinks, the fifos, the
    socket, the device files, door...

    --
    Stéphane

  15. Re: count the number of files

    Stephane Chazelas wrote:

    > > %find ./ | wc -l


    > And that doesn't work if there are multiline file names.


    "find . ! -type d -exec echo \; | wc -l" slow but easy ;-)

  16. Re: count the number of files

    Sven Mascheck wrote:
    > Stephane Chazelas wrote:
    >
    >
    >>>%find ./ | wc -l

    >
    >
    >>And that doesn't work if there are multiline file names.

    >
    >
    > "find . ! -type d -exec echo \; | wc -l" slow but easy ;-)


    Why -exec ... \; ?
    -print is much faster. And easy.

    --
    Michael Tosch @ hp : com

  17. Re: count the number of files

    Bill Marcum wrote:

    > I'm not sure if all versions of find support -printf.


    Currently only in GNU find to the best of my knowledge.

  18. Re: count the number of files

    Neil Woods wrote:
    > >>>>> Jerry writes:


    > > How could I rename them at once to:


    > One way:
    > for i in abc_kkk.txt 1tsjd_kkk.txt efgrtg_kkk.txt
    > do


    That will work, but it's not really "at once", is it? There's a
    possible race condition in which one or two files have been moved but
    not all of them. I guess it depends on what the OP meant by "at once",
    and I'm not sure he really understands what he's asking. Renaming a
    file is an atomic command. Renaming three files is not atomic, period.
    OTOH, if he just wants one command, then your solution should be good
    for him.

    --
    Oh to have a lodge in some vast wilderness. Where rumors of oppression
    and deceit, of unsuccessful and successful wars may never reach me
    anymore.
    -- William Cowper

  19. Re: count the number of files

    > How to count the total number of files in one directory and all
    > subdirectories?
    >
    > What commands should I try?


    "man find" and "man wc".

    -- Lars

    --
    Lars Kellogg-Stedman <8273grkci8q8kgt@jetable.net>
    This email address will expire on 2005-11-23.


+ Reply to Thread