LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 04-26-2009, 06:42 PM   #1
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Rep: Reputation: 41
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.
 
Old 04-26-2009, 08:09 PM   #2
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
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.
 
Old 04-26-2009, 08:14 PM   #3
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by choogendyk View Post
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.)
 
Old 04-27-2009, 07:34 AM   #4
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
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
 
Old 04-27-2009, 07:58 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
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
 
Old 04-27-2009, 08:10 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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)
 
  


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
C++ List iteration and updating list items x_terminat_or_3 Programming 7 03-24-2009 03:02 PM
counting the number of unique items in a list UnixKiwi Programming 4 11-29-2008 02:49 PM
How to limit number of items in .recently-used neo_in_matrix Linux - Newbie 2 06-09-2005 11:04 AM
List total number of directories paraiso Linux - Newbie 5 04-18-2005 04:04 AM
File Input, how to get number of items? ftgow Programming 1 03-11-2005 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:22 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
Open Source Consulting | Domain Registration