LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-27-2003, 10:31 PM   #1
WillieB_72
LQ Newbie
 
Registered: Jan 2003
Posts: 6

Rep: Reputation: 0
Smile 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
 
Old 01-27-2003, 10:58 PM   #2
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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...
 
Old 01-28-2003, 10:44 AM   #3
WillieB_72
LQ Newbie
 
Registered: Jan 2003
Posts: 6

Original Poster
Rep: Reputation: 0
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

Last edited by WillieB_72; 01-28-2003 at 12:22 PM.
 
Old 01-29-2003, 09:25 PM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
count total number of lines in several files xushi Programming 5 11-12-2005 04:42 PM
Command to run another command against a list of files psweetma Linux - General 3 11-09-2005 05:29 PM
Command Line Help: Remove a List of Files lukasbradley Linux - Newbie 2 08-17-2005 04:21 AM
List total number of directories paraiso Linux - Newbie 5 04-18-2005 04:04 AM
Command to list some files? AoiSora Programming 13 10-29-2003 04:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration