[SOLVED] du style directory listing with file numbers
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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 12:00 AM.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.