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


Reply
  Search this Thread
Old 06-11-2009, 06:04 AM   #1
basher400
Member
 
Registered: Mar 2005
Posts: 54

Rep: Reputation: 15
sorting and displaying dirs and subdirs by the amount of files they contain


I have thousands of files and directories under a certain path.
/home/derk/
each directory under that path may have thousands of sub-directories as well.

I want to find out which directories/subdirectories have the most amount of files
I wish to do that by sorting those directories by the amount of files they have in them and display them.

what's the command for that ?
I tried all kinds of combinations of "find" and "grep" and "xargs" and i'm getting no where.
please help
 
Old 06-11-2009, 06:11 AM   #2
saavik
Member
 
Registered: Nov 2001
Location: NRW, Germany
Distribution: SLES / FC/ OES / CentOS
Posts: 612

Rep: Reputation: 32
find . -type f | awk -F / {'print$2'} | sort -r | uniq -c
 
Old 06-11-2009, 06:23 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python
Code:
#!/usr/bin/env python
import os,operator
path=os.path.join("/home")
di={}
for r,d,f in os.walk(path):        
    di.setdefault(r,len(f))
for i in sorted(di.iteritems(), key=operator.itemgetter(1), reverse=True):
    print i

Last edited by ghostdog74; 06-11-2009 at 07:23 AM. Reason: thanks to colucix for pointing out shebang bug
 
Old 06-11-2009, 06:30 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
A little bug in the sha-bang, it should be:
Code:
#!/usr/bin/env python
 
Old 06-11-2009, 08:28 AM   #5
basher400
Member
 
Registered: Mar 2005
Posts: 54

Original Poster
Rep: Reputation: 15
thanks but i don't have python, I need it an a bash command
 
Old 06-11-2009, 08:33 AM   #6
basher400
Member
 
Registered: Mar 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by saavik View Post
find . -type f | awk -F / {'print$2'} | sort -r | uniq -c
this command seems to go only 1 level deep and doesn't seem to be sorted.
 
Old 06-11-2009, 08:36 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
for dir in $(find /home/derk -type d)
do
  count=$(find $dir -maxdepth 1 -type f | wc -l)
  printf "%6d   %s\n" $count $dir
done | sort -nr
 
Old 06-11-2009, 10:25 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
Code:
for dir in $(find /home/derk -type d)
do
  count=$(find $dir -maxdepth 1 -type f | wc -l)
  printf "%6d   %s\n" $count $dir
done | sort -nr
for loop may break on files with white spaces. Also using 2 finds is inefficient.
Code:
ls -lRa /home/derk| awk '
/total/{next}
/^\// && f {f=0;print d, i;}
/^\//{
    f=1
    d=$0
    i=0
}
f && /^-/{
 i++
}'
 
Old 06-11-2009, 10:40 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Your code does not work if you don't specify an absolute path or even no arguments at all in the ls command. Here is a slightly modified version:
Code:
ls -lRa | awk '
/total/{next}
/^[./]/ && f {f=0;printf "%6d   %s\n",i,gensub(/:$/,"",1,d)}
/^[./]/{
    f=1
    d=$0
    i=0
}
f && /^-/{
 i++
}' | sort -nr
I added the sorting part as per OP's requirement.
 
Old 06-15-2009, 04:00 AM   #10
basher400
Member
 
Registered: Mar 2005
Posts: 54

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
Code:
for dir in $(find /home/derk -type d)
do
  count=$(find $dir -maxdepth 1 -type f | wc -l)
  printf "%6d   %s\n" $count $dir
done | sort -nr
this seems to work for me thanks
 
  


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
hash all files in subdirs madivad Solaris / OpenSolaris 6 05-01-2008 10:37 AM
Copy files in subdirs to one dir Rotwang Linux - General 4 12-14-2007 02:30 PM
xgettext: How to parse entire directory structure (dirs/subdirs) Muffe Linux - Software 1 04-28-2006 10:52 AM
invisible files and dirs karmine Slackware - Installation 8 12-26-2004 04:05 PM
ls, dirs first, files later TroelsSmit Linux - Newbie 4 05-31-2004 12:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:36 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration