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
Re: count the number of files
In article <1131731844.934517.55040@g14g2000cwa.googlegroups.com>,
Jerry wrote:
[color=blue]
> 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?[/color]
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 ./
[color=blue]
>
> 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[/color]
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.)
Re: count the number of files
On Fri, 11 Nov 2005 18:09:38 +0000 (UTC), Daniel C. Bastos wrote:
[...][color=blue][color=green]
>> How to count the total number of files in one directory and all
>> subdirectories?
>>
>> What commands should I try?[/color]
>
> 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 ./[/color]
[...]
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
Re: count the number of files
In article <slrndn9nuf.6in.stephane_chazelas@duey.spider.com>,
Stephane Chazelas wrote:
[color=blue]
> On Fri, 11 Nov 2005 18:09:38 +0000 (UTC), Daniel C. Bastos wrote:
> [...][color=green][color=darkred]
>>> How to count the total number of files in one directory and all
>>> subdirectories?
>>>
>>> What commands should I try?[/color]
>>
>> 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 ./[/color]
> [...]
>
> But that doesn't include "." and ".." in the subdirectories.[/color]
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.
[color=blue]
> And that doesn't work if there are multiline file names.[/color]
Sure, I didn't think of that.
[color=blue]
> If using zsh, there's:
>
> files=(**/*(ND))
> print "there are $#files files in the current directory"
>
> (that ommits "." and ".." entries altogether).[/color]
To be shell dependent, I rather use opendir(). :-)
[color=blue]
> With find:
>
> echo "there are $(
> find .//. \! -name . | grep -c //
> ) files in the current directory"
>
> ommits "." and ".." as well.[/color]
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.
Re: count the number of files
Daniel C. Bastos wrote:[color=blue]
> 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.[/color]
Dan, you are right. I don't intend to count "." and ".." files.
[color=blue][color=green]
> > And that doesn't work if there are multiline file names.[/color][/color]
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?
[color=blue][color=green]
> > If using zsh, there's:
> >
> > files=(**/*(ND))
> > print "there are $#files files in the current directory"
> >
> > (that ommits "." and ".." entries altogether).[/color]
>
> To be shell dependent, I rather use opendir(). :-)
>[color=green]
> > With find:
> >
> > echo "there are $(
> > find .//. \! -name . | grep -c //
> > ) files in the current directory"
> >
> > ommits "." and ".." as well.[/color]
>
> 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.[/color]
I guess Stephane's command meant to omit "." and ".." files
Thank you guys!
Jerry
Re: count the number of files
>>>>> Jerry writes:
[color=blue]
> Say I have the following files:[/color]
[color=blue]
> abc_kkk.txt
> efgrtg_kkk.txt
> 1tsjd_kkk.txt[/color]
[color=blue]
> How could I rename them at once to:[/color]
[color=blue]
> abc.txt
> efgrtg.txt
> 1tsjd.txt[/color]
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
Re: count the number of files
Jerry wrote:
<snip>[color=blue]
> 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?[/color]
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.
Re: count the number of files
On 11 Nov 2005 10:55:26 -0800, Jerry wrote:[color=blue]
> Daniel C. Bastos wrote:[color=green]
>> 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.[/color]
>
> Dan, you are right. I don't intend to count "." and ".." files.
>[color=green][color=darkred]
>> > And that doesn't work if there are multiline file names.[/color][/color]
>
> 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?[/color]
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
Re: count the number of files
On Fri, 11 Nov 2005 18:38:05 +0000 (UTC), Daniel C. Bastos wrote:
[...][color=blue][color=green][color=darkred]
>>> %find ./ | wc -l
>>> 7752
>>>
>>> This number also includes the directory ./[/color]
>> [...]
>>
>> But that doesn't include "." and ".." in the subdirectories.[/color]
>
> 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.[/color]
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
Re: count the number of files
Begin <slrndn9nuf.6in.stephane_chazelas@duey.spider.com>
On 2005-11-11, Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:[color=blue]
> But that doesn't include "." and ".." in the subdirectories.[/color]
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 .
Re: count the number of files
Stephane,
I tried your following approach.
[color=blue]
> With find:
>
> echo "there are $(
> find .//. \! -name . | grep -c //
> ) files in the current directory"
>
> ommits "." and ".." as well.[/color]
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
Re: count the number of files
In article <1131739054.319781.235050@g47g2000cwa.googlegroups.com>,
Jerry wrote:
[color=blue]
> Stephane,
>
> I tried your following approach.
>[color=green]
>> With find:
>>
>> echo "there are $(
>> find .//. \! -name . | grep -c //
>> ) files in the current directory"
>>
>> ommits "." and ".." as well.[/color]
>
> 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?[/color]
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.
Re: count the number of files
2005-11-11, 11:57(-08), Jerry:[color=blue]
> Stephane,
>
> I tried your following approach.
>[color=green]
>> With find:
>>
>> echo "there are $(
>> find .//. \! -name . | grep -c //
>> ) files in the current directory"
>>
>> ommits "." and ".." as well.[/color]
>
> 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?[/color]
[...]
find .//. \! -type d -print | grep -c //
--
Stéphane
Re: count the number of files
2005-11-11, 20:20(+00), Daniel C. Bastos:
[...][color=blue][color=green]
>> 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?[/color]
>
> 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?[/color]
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
Re: count the number of files
Stephane Chazelas wrote:
[color=blue][color=green]
> > %find ./ | wc -l[/color][/color]
[color=blue]
> And that doesn't work if there are multiline file names.[/color]
"find . ! -type d -exec echo \; | wc -l" slow but easy ;-)
Re: count the number of files
Sven Mascheck wrote:[color=blue]
> Stephane Chazelas wrote:
>
>[color=green][color=darkred]
>>>%find ./ | wc -l[/color][/color]
>
>[color=green]
>>And that doesn't work if there are multiline file names.[/color]
>
>
> "find . ! -type d -exec echo \; | wc -l" slow but easy ;-)[/color]
Why -exec ... \; ?
-print is much faster. And easy.
--
Michael Tosch @ hp : com
Re: count the number of files
Bill Marcum wrote:
[color=blue]
> I'm not sure if all versions of find support -printf.[/color]
Currently only in GNU find to the best of my knowledge.
Re: count the number of files
Neil Woods <cnw+usenet@pobox.com> wrote:[color=blue][color=green][color=darkred]
> >>>>> Jerry writes:[/color][/color][/color]
[color=blue][color=green]
> > How could I rename them at once to:[/color][/color]
[color=blue]
> One way:
> for i in abc_kkk.txt 1tsjd_kkk.txt efgrtg_kkk.txt
> do[/color]
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
Re: count the number of files
> How to count the total number of files in one directory and all[color=blue]
> subdirectories?
>
> What commands should I try?[/color]
"man find" and "man wc".
-- Lars
--
Lars Kellogg-Stedman <8273grkci8q8kgt@jetable.net>
This email address will expire on 2005-11-23.