LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2011, 11:35 PM   #1
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Rep: Reputation: Disabled
bash largest folder


Hello! I have a question. Which command can shows largest folder not by size but by amount of files? I can't find this info.
 
Old 10-16-2011, 12:24 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
1) Make a list of directories.
2) Determine the number of files in each directory.
3) Sort the list by that number.
4) The top entry will be your answer.
 
Old 10-16-2011, 12:30 AM   #3
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
But every time new directory can be created and again list need to be updated.
 
Old 10-16-2011, 01:31 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yeah, so? That's true no matter what command you use.

Set it up as a script or function and tell it to count the files that exist at the time you run it. Even if there was a single command that did just what you want, it would just be doing the same thing internally.

Also, you probably need to define your criteria in more detail. What about sub-directories? Do they count as files, and do you want to recurse through their contents too? What about dotfiles? Symlinks?
 
Old 10-16-2011, 01:40 AM   #5
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Sub directories does not count dotfiles to.
 
Old 10-16-2011, 05:25 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Ok then. I look forward to seeing what kind of solution you come up with.
 
Old 10-17-2011, 02:21 AM   #7
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
I don't know what to do
Quote:
#!/bin/bash

for i in `find /home/ -type d`
do
find -type f | wc -l
done
Trying something like this.

Last edited by trintukaz; 10-18-2011 at 11:37 AM.
 
Old 10-18-2011, 08:17 AM   #8
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
anyone?
 
Old 10-18-2011, 10:29 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by trintukaz View Post
I don't know what to do

Code:
#!/bin/bash

for i in `find /home/arturas -type d`
do
find -type f | wc -l
done
Trying something like this.
The for i in `find /home/arturas -type d` implements David the H's first step, "Make a list of directories". It will bork on directory names with spaces in their names etc. but let's not get distracted by that yet.

The second step is "Determine the number of files in each directory". find -type f | wc -l is going in the right direction but you need to tell it the starting directory (otherwise it starts from the current directory) and to tell it not to look in any subdirectories. find's -maxdepth option is what you need.

BTW, when not familiar with the commands to put in a script it's helpful to run them at the command prompt to explore what they do.

The script will need to keep the number of files in each directory and the directory name. It could keep them in a variable but if there are a lot of directories the script will run slowly and writing them to a temporary file would allow the script to run faster. The >> redirection operator is good for that.

When scripts become large, it is very helpful to have meaningful variable names so for dir in `find /home/arturas -type d` would be a good choice.

Please post back how you got on with these hints.
 
Old 10-18-2011, 12:01 PM   #10
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
I decided to look in subdirectories.
Quote:
#!/bin/bash

for i in `find /home -type d`
do
find "$i" -type f | wc -l
done
Now my script looks like this. It is i little bit better than previous but i think i need make somehow to show only largest directory and also show the name.
 
Old 10-22-2011, 08:54 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by trintukaz View Post
I decided to look in subdirectories.
That will count files multiple times. OK if that's OK for you.

Quote:
Originally Posted by trintukaz View Post
Now my script looks like this. It is i little bit better than previous but i think i need make somehow to show only largest directory and also show the name.
Code:
#!/bin/bash

for i in `find /home -type d`
do
find "$i" -type f | wc -l
done
True. You've got as far as David the H's steps 1 and 2. Now you need the rest. Step 3 is "Sort the list by that number".

So you need a list. Assuming you want to use a temporary file then you can write the data to file as you generate it. $i already has the directory name. The echo command followed by the >> redirection operator is good for appending to temporary files. The sort command is good for sorting files.

Those are basic techniques explained in bash introductions such as:BTW if you use CODE tags instead of quote tags then your code will be shown when people quote your post.
 
  


Reply



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
Print folder name in BASH waqasdaar Programming 10 02-27-2009 06:50 PM
bash check folder exists zerocool22 Programming 22 06-01-2008 07:53 AM
How to issue "ls" command start with the largest file size in a folder? fjkum Linux - Newbie 5 07-27-2006 03:28 AM
Find 100 largest files in folder tree tyreth Linux - General 2 02-07-2006 08:18 AM
bash scripting - largest # file extension brian0918 Programming 3 06-16-2003 12:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 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