count the number of files
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix questions > count the number of files




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    count the number of files  
Jerry


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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






[ Post a follow-up to this message ]



    Re: count the number of files  
Daniel C. Bastos


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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.)





[ Post a follow-up to this message ]



    Re: count the number of files  
Stephane Chazelas


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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





[ Post a follow-up to this message ]



    Re: count the number of files  
Daniel C. Bastos


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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.





[ Post a follow-up to this message ]



    Re: count the number of files  
Jerry


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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






[ Post a follow-up to this message ]



    Re: count the number of files  
Neil Woods


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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





[ Post a follow-up to this message ]



    Re: count the number of files  
Ed Morton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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.





[ Post a follow-up to this message ]



    Re: count the number of files  
Stephane Chazelas


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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





[ Post a follow-up to this message ]



    Re: count the number of files  
Stephane Chazelas


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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





[ Post a follow-up to this message ]



    Re: count the number of files  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-11-05 11: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:52 PM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register