[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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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?
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:
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.
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.