LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-19-2011, 03:00 PM   #1
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
List directories and number of files inside them.


I'm looking for a way to produce a list of all the directories in the current working directory sorted by the total number of files that are contained with them.

Initially I though that Nautilus could be used for this, but then I realised it doesn't count files in the sub directories.

The best I've got for a command line solution so far is this

Code:
$ find . -type d -maxdepth 1  | while read thing;do  find "$thing" | wc -l | tr '\n' ' ' ; echo  "$thing"; done  | sort -nr

Anyone get a better solution?

The use case for this is a situation where a user has a quota applied to their home directory which limits the number of files they are allowed to have and they have exceeded that limit.

I assure this is not homework, I'm far too old

Last edited by arizonagroovejet; 03-19-2011 at 04:34 PM.
 
Old 03-19-2011, 04:24 PM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
I would do that in the following way:

Code:
find . -type f | cut -d "/" -f 1 | sort | uniq -c | sort -n
Let me know if that's not what you were thinking.

Regards,

Alunduil
 
Old 03-19-2011, 04:34 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Original Poster
Rep: Reputation: 198Reputation: 198
Quote:
Originally Posted by alunduil View Post
Code:
find . -type f | cut -d "/" -f 1 | sort | uniq -c | sort -n
That just provides the total number of files in the current directory and it's sub-directories. Also it seems overly complex for what it does...

Code:
mike@continuity:~$ find . -type f | cut -d "/" -f 1 | sort | uniq -c | sort -n
 408953 .
mike@continuity:~$ find . -type f | wc -l
408953

I have just realised there's a typo at the start of my post. "...in the current directories..." should, and will shortly, read ...in the current working directory...
 
Old 03-19-2011, 04:49 PM   #4
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
I realize what happened. The typo I placed in my script was selecting the wrong column. If you use the following it will behave as you expect:

Code:
find . -type f | cut -d "/" -f 2 | sort | uniq -c | sort -n
Let me know if that's more of what you were expecting.

Regards,

Alunduil
 
1 members found this post helpful.
Old 03-20-2011, 10:33 AM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Original Poster
Rep: Reputation: 198Reputation: 198
I like yours more than mine. It's shorter, doesn't give a number for the current directory and the output is more aesthetically pleasing. Except it's only counting regular files and I actually need to count directories and symbolic links and such too as they all count towards the quota.

It turns out the quota is actually inodes(?) not files. Although the output of the quota command labels the relevant number as 'files', creating a directory increases that number by one. So you could use up your quota by creating lots and lots of directories none of which contain any files.

But I can just remove the '-type f' from your command and add the r option to sort it gives the desired numbers in the desired order. Thanks.

Now, can anyone produce something more elegant than alunduil?
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to list the number of files in serveral sub-directories cy163 Linux - Newbie 2 05-16-2009 11:29 AM
How to list & sort directories by number of items they contain? Mountain Linux - Software 5 04-27-2009 08:10 AM
sticky bit: how to protect directories but not files inside alexandrusa Linux - Server 8 03-27-2008 10:35 AM
Getting a list of directories with certain files inside... Banacek Linux - Newbie 7 11-27-2006 05:43 PM
List total number of directories paraiso Linux - Newbie 5 04-18-2005 04:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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