LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Command to list total number of files. (https://www.linuxquestions.org/questions/linux-general-1/command-to-list-total-number-of-files-43023/)

WillieB_72 01-27-2003 10:31 PM

Command to list total number of files.
 
Hi all,

I'm looking for a command with a certain output. I've searched high and low for two days. Maybe you can help me.

I would like to list the total number of files including subdirectories and total amount of space the files are taking up. To be specific as in /var/ftp/pub/mp3. As in windows when you right-click on the folder and click properties. Or for command line, a command in Linux equal to Windows' "dir/w/s". I would like that info. Any ideas would be great. Thanks...

-WillieB
CCNA/CCAI, CCNP
Cable ISP Admin

rnturn 01-27-2003 10:58 PM

Re: Command to list total number of files.
 
Quote:

Originally posted by WillieB_72
[snip]

I would like to list the total number of files including subdirectories and total amount of space the files are taking up. To be specific as in /var/ftp/pub/mp3. As in windows when you right-click on the folder and click properties. Or for command line, a command in Linux equal to Windows' "dir/w/s". I would like that info. Any ideas would be great.

[snip]

What about:

find . | wc -l ; du -sk .

which: finds all the files under the current directory (.) and pipes the output to wc which counts the number of lines (files). The subsequent du command gives a summary, in 1K blocks, of all the space in use under the current directory. Wrapping this in a bit of shell script would give it a prettier output:

#!/bin/bash

FILES=`find $1 2>/dev/null | wc -l`
BLOCKS=`du -sk $1 2>/dev/null | awk '{ print $1 }'`
echo "$1 : ${FILES} files in ${BLOCKS}"

Or something like that. If there's a way to do it in either the Gnome or KDE GUIs, I haven't stumbled across it yet.

Hope this helps...

WillieB_72 01-28-2003 10:44 AM

Command to list total number of files.
 
Thanks a lot for the reply. I have been playing with that command and it's really close to what I want but I can't seem to get the output the same as dir/w/s. See below and maybe that will help.

*******************************
On Windows Box:

C:\Server\mp3s>dir/w/s
Volume in drive C has no label.
Volume Serial Number is 1094-4F2B

Directory of C:\Server\mp3s

**Output Omitted**

Total Files Listed:
146 File(s) 530,461,824 bytes
32 Dir(s) 4,025,344,000 bytes free

C:\Server\mp3s>

This shows that I have 146 mp3s in 32 Folders which is 1 full cd each. So I know that I have 146 mp3s in 32 CD's.

Here's the output from the Red Hat Box for the exact same data underneath the current folder.

[root@server01 mp3s]# find . | wc -l ; du -sk .
157
519316 .
[root@server01 mp3s]#

*******************************

Any idea on how I could get the same data on Linux as in Windows above? The most important info to me is the # of files and directories, preferably seperate. Thanks again for your reply..:-)

-WillieB
CCNA/CCAI, CCNP
Cable ISP Admin

rnturn 01-29-2003 09:25 PM

Re: Command to list total number of files.
 
Quote:

Originally posted by WillieB_72

Directory of C:\Server\mp3s

**Output Omitted**

Total Files Listed:
146 File(s) 530,461,824 bytes
32 Dir(s) 4,025,344,000 bytes free

This shows that I have 146 mp3s in 32 Folders which is 1 full cd each. So I know that I have 146 mp3s in 32 CD's.

Here's the output from the Red Hat Box for the exact same data underneath the current folder.

[root@server01 mp3s]# find . | wc -l ; du -sk .
157
519316 .

The only thing I can think of right now to account for the difference in the number of reported directories is that the `find' command may be counting some hidden files and/or directories that DOS isn't. The difference in the number of bytes (519316 * 1024 = 531779584) is undoubtedly due to du counting whole blocks whereas DIR is returning only the actual number of bytes in the files. Remember that you can ``fill'' up a disk with a relatively small number of 1-byte files since the filesystem allocates 4096 or 8192 (or whatever the cluster size is) bytes to a file at a time. Try:

echo "a" > a.file
ls -l a.file
du -sk a.file
du -sb a.file

Anyway, so how about:

#!/bin/bash

FILES=`find $1 -type f 2>/dev/null | wc -l | awk '{ print $1 }'`
DIRS=`find $1 -type d 2>/dev/null | wc -l | awk '{ print $1 }'`
BYTES=`du -sb $1 2>/dev/null | awk '{ print $1 }'`
echo "$1 : ${FILES} files in ${DIRS} directories using ${BYTES} bytes."

And if you want commas in the output... the only way I know how to pull that one off is using Perl.

Have fun...


All times are GMT -5. The time now is 06:55 PM.