Unix questions - count the number of files

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > November 2005 > count the number of files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author count the number of files
Jerry

2005-11-11, 6:00 pm

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

Daniel C. Bastos

2005-11-11, 6:00 pm

In article <1131731844.934517.55040@g14g2000cwa.googlegroups.com>,
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.)
Stephane Chazelas

2005-11-11, 6:00 pm

On Fri, 11 Nov 2005 18:09:38 +0000 (UTC), Daniel C. Bastos wrote:
[...]
>
> 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
Daniel C. Bastos

2005-11-11, 6:00 pm

In article <slrndn9nuf.6in.stephane_chazelas@duey.spider.com>,
Stephane Chazelas wrote:

> On Fri, 11 Nov 2005 18:09:38 +0000 (UTC), Daniel C. Bastos wrote:
> [...]
> [...]
>
> 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.
Jerry

2005-11-11, 6:00 pm

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.


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?
[vbcol=seagreen]
>
> To be shell dependent, I rather use opendir(). :-)
>
>
> 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

Neil Woods

2005-11-11, 6:00 pm

>>>>> 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
Ed Morton

2005-11-11, 6:00 pm



Jerry wrote:
<snip>
> 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.
Stephane Chazelas

2005-11-11, 6:00 pm

Mail-Copies-To: nobody
Message-ID: <slrndn9s02.748.stephane_chazelas@duey.spider.com>
User-Agent: slrn/0.9.8.1 (SunOS)
Date: 11 Nov 2005 19:24:50 GMT
Lines: 26
Organization: Guest of ProXad - France
NNTP-Posting-Date: 11 Nov 2005 20:24:50 MET
NNTP-Posting-Host: 194.217.109.13
X-Trace: 1131737090 news10-e.free.fr 11132 194.217.109.13:56189
X-Complaints-To: abuse@proxad.net
Xref: number1.nntp.dca.giganews.com comp.unix.questions:103521

On 11 Nov 2005 10:55:26 -0800, Jerry wrote:
> Daniel C. Bastos wrote:
>
> Dan, you are right. I don't intend to count "." and ".." files.
>
>
> 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
Stephane Chazelas

2005-11-11, 6:00 pm

On Fri, 11 Nov 2005 18:38:05 +0000 (UTC), 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.


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
jpd

2005-11-11, 6:00 pm

Begin <slrndn9nuf.6in.stephane_chazelas@duey.spider.com>
On 2005-11-11, Stephane Chazelas <stephane_chazelas@yahoo.fr> 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 .
Jerry

2005-11-11, 6:00 pm

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

Daniel C. Bastos

2005-11-11, 6:00 pm

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

> Stephane,
>
> I tried your following approach.
>
>
> 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.
Stephane CHAZELAS

2005-11-11, 6:00 pm

2005-11-11, 11:57(-08), Jerry:
> Stephane,
>
> I tried your following approach.
>
>
> 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
Stephane CHAZELAS

2005-11-12, 7:49 am

Mail-Copies-To: nobody
Message-ID: <slrndnbbni.58p.stephane.chazelas@spam.is.invalid>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit
User-Agent: slrn/0.9.8.1pl1 (Debian)
Date: Sat, 12 Nov 2005 08:59:30 +0000
Lines: 25
Organization: Guest of ProXad - France
NNTP-Posting-Date: 12 Nov 2005 10:00:02 MET
NNTP-Posting-Host: 82.26.171.113
X-Trace: 1131786002 nnrp10-1.free.fr 4299 82.26.171.113:45355
X-Complaints-To: abuse@proxad.net
Xref: number1.nntp.dca.giganews.com comp.unix.questions:103530

2005-11-11, 20:20(+00), Daniel C. Bastos:
[...]
>
> 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
Sven Mascheck

2005-11-12, 5:51 pm

Stephane Chazelas wrote:

[vbcol=seagreen]
> And that doesn't work if there are multiline file names.


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

2005-11-12, 5:51 pm

Sven Mascheck wrote:
> Stephane Chazelas wrote:
>
>
>
>
>
>
> "find . ! -type d -exec echo \; | wc -l" slow but easy ;-)


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

--
Michael Tosch @ hp : com
Bill Marcum

2005-11-12, 5:51 pm

On Sat, 12 Nov 2005 19:55:21 +0100, Michael Tosch
<eedmit@NO.eed.SPAM.ericsson.PLS.se> wrote:
> Sven Mascheck wrote:
>
> Why -exec ... \; ?
> -print is much faster. And easy.
>

-exec echo \; prints only a newline, not the name of each file. This
prevents miscounting filenames which might contain newlines. You could
also use -printf '\n' , but I'm not sure if all versions of find support
-printf.

--
To err is human, but when the eraser wears out before the pencil,
you're overdoing it a little.
Sven Mascheck

2005-11-13, 7:49 am

X-Trace: news.in-ulm.de 0D7740EEC231D750556AC02462ADF700
User-agent: tin/1.5.18-mod "Elizium" (UNIX)
Xref: number1.nntp.dca.giganews.com comp.unix.questions:103536

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.
Jeremiah DeWitt Weiner

2005-11-16, 6:03 pm

Neil Woods <cnw+usenet@pobox.com> wrote:
[vbcol=seagreen]
[vbcol=seagreen]
> 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
Lars Kellogg-Stedman

2005-11-16, 8:48 pm

> 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.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com