Linux - Software This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
04-26-2009, 06:42 PM
|
#1
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Rep:
|
How to list & sort directories by number of items they contain?
I need something similar to this:
Code:
du --max-depth=1 /home/ | sort -n -r
But I want to check for the total number of items in each directory rather than the size.
|
|
|
04-26-2009, 08:09 PM
|
#2
|
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
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.
|
|
|
04-26-2009, 08:14 PM
|
#3
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214
Original Poster
Rep:
|
Quote:
Originally Posted by choogendyk
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.)
|
|
|
04-27-2009, 07:34 AM
|
#4
|
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
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
|
|
|
04-27-2009, 07:58 AM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
You can try also the action -exec of find, using the command "sh -c" to manage the pipe and printf to specify the output format:
Code:
find /home -maxdepth 1 -type d -exec sh -c 'ls {} | printf "%5s\t%s\n" $(wc -l) {}' \; | sort -nr
|
|
|
04-27-2009, 08:10 AM
|
#6
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
if you have Python, you can use os.walk
Code:
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)
|
|
|
All times are GMT -5. The time now is 08:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|