How to list & sort directories by number of items they contain?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
If you do an `ls -l` the second item is the number of links to a file or directory. For a directory that would include all the items contained in it, since they link back to it. So it is a reasonable approximation.
$ ls -l /home/ | grep '^d' | sort -n -r -k 2
the second command in the pipe sequence just eliminates everything that is not a directory.
If you do an `ls -l` the second item is the number of links to a file or directory. For a directory that would include all the items contained in it, since they link back to it. So it is a reasonable approximation.
$ ls -l /home/ | grep '^d' | sort -n -r -k 2
the second command in the pipe sequence just eliminates everything that is not a directory.
I need something that is recursive. Any ideas for that? Thanks.
(I mean for each subdirectory 1 level down, I would like to see a value for all items it contains including all subdirectories in it, recursively.)
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
I can't think of a simple command line that would do it.
find can tell you what is in one directory recursively, so
$ find /home/particular-one -type f | wc -l
will tell you how many files are in that particular-one. Starting from that idea, you could script something. Have the output go to a file and include the directory name. Then cat the file to sort and output that. So, something like
Code:
#!/bin/ksh
for DIR in `ls /home/`
do
echo "`find $DIR -type f | wc -l` $DIR" >> temp-file
done
cat temp-file | sort -n -r
for root, d ,files in os.walk("/home"):
print "root: ",root ,"number of items: ", len(root)
print "d: ",d ,"number of directories: ", len(d)
print "files: ",files ,"number of items: ", len(files)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.