LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 09-07-2010, 12:17 AM   #1
JoatMan
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Rep: Reputation: 0
du style directory listing with file numbers


Is there a command to return a recursive listing of sub-directories and the number of fils in them?

I have found plenty of ways to give me the total number of files in a directory structure, but none that gives a list of the sub-directories with the number of files in them. "du" gives me a listing of directories with their sizes, but I couldn't find an option (or any other way) to give me the number of files as well.

Ideally, I'd like to get list with "Size" "Files" "Dir name" - but I don't want to be greedy, just the last two would be a great help. And the order of the columns doesn't matter.

Is there a "simple" command line solution or do I need a shell script for that?
Thanks
 
Old 09-07-2010, 12:58 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
You can use this Python snippet
Code:
import os
root="/home"
for r,d,f in os.walk( os.path.join(root,"path1","path2") ):
    print "In directory: %s, there are %d files" %(r,len(f))
    for files in f:
        path=os.path.join(r,files)
        print "File: %s, size: %d bytes" %( os.path.join(r,files) , os.path.getsize(path ) )

Last edited by ghostdog74; 09-07-2010 at 01:00 AM.
 
Old 09-07-2010, 02:07 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Here is an alternative:
Code:
find <path> -type f -printf "%s %h\n" | awk '{c[$2]++;s[$2]+=$1}END{for(x in c)print s[x],c[x],x}'
Obviously enter the <path> you wish to look at.
 
1 members found this post helpful.
Old 09-09-2010, 01:12 AM   #4
JoatMan
LQ Newbie
 
Registered: Sep 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Many thanks to ghostdog74 and grail

ghosdog74:

Unfortunately, your snippet returned the following error.

./dirlist.py: line 3: syntax error near unexpected token `('
./dirlist.py: line 3: `for r,d,f in os.walk( os.path.join(root,"path1","path2") ):'

I copied your snippet into a file "dirlist.py",
then: chmod +x dirlist.py
followed by ./dirlist.py

Python has been on my "wish-to-learn" list for some time, but I don't know enough (or anything) of it to fix the problem. Hopefully, in the not too distant future, I will find the time to explore Python and understand what's gone wrong here.

grail:
Your "oneliner" worked a treat, just what the doctor ordered. I will try to pick it appart and see how the individual bits and pieces work together.

Again, Thanks a lot for your help and support!!
 
Old 09-09-2010, 01:42 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by JoatMan View Post
Unfortunately, your snippet returned the following error.
I do not have the problem. Make sure when you change the paths to use your own paths, don't forget about the quotes. (and the open/closen braces).
 
Old 09-09-2010, 02:07 AM   #6
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,508
Blog Entries: 35

Rep: Reputation: 384Reputation: 384Reputation: 384Reputation: 384
Quote:
Originally Posted by ghostdog74 View Post
I do not have the problem. Make sure when you change the paths to use your own paths, don't forget about the quotes. (and the open/closen braces).
Don't forget to specify the interpreter at the beginning of the file (#!/usr/local/bin/python). Run "whereis python" to figure out the correct interpreter path because the one I gave was just a guess.

Or run "python ./dirlist.py"

Last edited by sag47; 09-09-2010 at 02:10 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting File Names from Long Listing of Directory in UNIX Hi_This_is_Dev Programming 15 09-01-2010 12:32 AM
[SOLVED] RHEL5 Apache Directory Listing for a directory thorinn Red Hat 8 02-02-2010 10:01 AM
Linux directory file listing after a particular interval ratul_11 Linux - General 2 02-20-2008 09:33 AM
Save ftp directory listing into a file Vitalie Ciubotaru Linux - Software 1 06-30-2007 07:37 PM
Getting a DOS-style sorted directory listing KimVette Linux - General 2 01-29-2006 12:22 PM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration