list directories with more than X files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers list directories with more than X files
# 1  
Old 11-02-2011
list directories with more than X files

I want to search a server beginning at /home and list directories with more than X files
I found a hack that injects tons of files into a directory

How can I search the server recursively and list directories with more than X files?
Thank you!

like,
find /home (directories, that meet the condition that # of files directly under is is > X)


results might be like;

/home/user1/public_html/dir1/dir2

and maybe list the # of files next to it

like 432234 /home/user1/public_html/dir1/dir2

even if there are more directories under dir2, it counts just the number of files directly under it, not the total number of files which are nested inside of it
# 2  
Old 11-02-2011
Code:
#!/usr/bin/ksh
find . -type d  | while read mDir; do
  mTot=$(find ${mDir} -type f | wc -l)
  echo "Directory <${mDir}> has <${mTot}> files"
done

# 3  
Old 11-02-2011
thanks, but there are a lot of directories and when i try to run it in /home it doesnt work

the /home directory has about 1000 accounts
and when i start the program it doesnt load

each user has probably over a thousand directories
i tried running it on one directory and it listed a thousand of them
is there a way to only list it if its more than X number of files in that directory?

and a way to speed it up in the beginning?
otherwise it will list like a million files on the screen
thanks

---------- Post updated at 03:48 PM ---------- Previous update was at 03:44 PM ----------

ok i modified it a bit

what does the {'s do around the variable?

#!/usr/bin/ksh
find /home/bbweb -type d | while read mDir; do
mTot=$(find ${mDir} -type f | wc -l)
if test $mTot -gt 1000
then
echo "${mDir} ${mTot}"
fi
done


out put is something like
/home/bbweb 2470
/home/bbweb/public_html 2372
/home/bbweb/public_html/videos 1209
find: /home/bbweb/public_html/media/categories/Signatures: No such file or directory


im not sure why the last line says it cant find the directory?

thanks!
# 4  
Old 11-02-2011
When you don't quote things, they split on spaces, causing find to look for "/home/whatever/directory" "name" "with" "spaces" "in" "it" instead of "/home/whatever/directory name with spaces in it". Unless you want your variables to split/expand, you should always put them in double quotes.

Here's the difference between $var and ${var}

Code:
$ var="1234"
$ echo $var
1234
$ echo $var_

$ echo ${var}_
1234_
$

# 5  
Old 11-02-2011
do you mean it should be find "${mDir}" and not find ${mDir}?

does the {} mean you can attach other things to it?
thanks!

#!/usr/bin/ksh
find /home -type d | while read mDir; do
mTot=$(find "${mDir}" -type f | wc -l)
if test $mTot -gt 2000
then
echo "${mDir}:${mTot}"
fi
done


i think this works
it just takes a little while to get started (but psosibly due to a lot of files, i guess it cant be any faster than this)

thanks Smilie

---------- Post updated at 04:18 PM ---------- Previous update was at 04:16 PM ----------

sometimes it says that a directory cant be found, strange
find: /home/bbweb/public_html/media/categories/Signatures: No such file or directory


also when counting files,

can it only count what is directly under it? (either files or files + directories)

for example, it shows a lot of files under /home/vashleyb even though there are only 20 files + directories under it
/home/vashleyb should show a small number like 20 (either # of files or directories directly under it), and not the total # of files of everything under it

thanks

/home/vashleyb:16678
/home/vashleyb/public_html:16490
/home/vashleyb/public_html/gallery:12825
/home/vashleyb/public_html/gallery/albums:10569
/home/vashleyb/public_html/gallery/albums/userpics:5452
/home/vashleyb/public_html/gallery/albums/userpics:5452

---------- Post updated at 04:37 PM ---------- Previous update was at 04:18 PM ----------

i put in -maxdepth 1

mTot=$(find "${mDir}" -type f -maxdepth 1 | wc -l)

which is ok now

its trying to find some directories that dont exist though, not sure why
find: /home/ultbrit/public_html/photos/albums/Ontour/Babyonemoretime/July 06 1999 - Washington DC: No such file or directory
find: /home/ultbrit/public_html/photos/albums/Ontour/House of Blues: No such file or directory
# 6  
Old 11-02-2011
Quote:
Originally Posted by vanessafan99
can it only count what is directly under it? (either files or files + directories)

for example, it shows a lot of files under /home/vashleyb even though there are only 20 files + directories under it
That's a bit tricky if you don't have GNU find, but:

Code:
find /path/to/dir '(' -type d -a '!' -path /path/to/dir -prune ')' -o -type f

This tells it to prune any directories which aren't /path/to/dir, which stops it from recursing deeper.

Last edited by Corona688; 11-03-2011 at 11:51 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

A way to list directories that contain specific files.

Hi everyone My issue is this, I need to list all the sub directories in a directory that contains files that have the extension *.log, *.dat and *.out . After reviewing the output i need to delete those directories i do not need. I am running Solaris 10 in a bash shell. I have a script that I... (2 Replies)
Discussion started by: jsabo40
2 Replies

2. UNIX for Dummies Questions & Answers

Find Files In A List with known Partial Directories

First I'm new to Linux and have used the find command pretty often but this is where I've hit a snag. I have a file that contains 3500 files that I want to find and then eventually copy to my own directory (these files are all on a shared directory at work atm). Our work computer are huge and... (2 Replies)
Discussion started by: Myrona
2 Replies

3. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

4. UNIX for Dummies Questions & Answers

List biggest files (Not Directories)

Hello, can you please help me writing a command that would output the biggest files on my system from biggest to smallest? I want this to print only the files, not the directories. I have tried du -a ~ | sort -nr | head -10 However, this also prints out all the directories - which I do... (8 Replies)
Discussion started by: tonydaniels1980
8 Replies

5. UNIX for Dummies Questions & Answers

List directories, subs and files

Hi, I try to list all files in a folder, including all the subdirs (and their subdirs) and all files contained in each of these folders. I then print it to a simple txt file. I use ls -R -1 >test.txt This sort of does what I need, yet, the result is something like: It reasonably comes... (53 Replies)
Discussion started by: dakke
53 Replies

6. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

7. Shell Programming and Scripting

How to get a list of files in a dir w/o sub-directories?

Hi I am looking for the correct syntax to find all files in the current directory without listing sub-directoris. I was using the following command, but it still returns subdirectoris and files inside them: $ ls -laR | grep -v ^./ Any idea? Thanks PS I am in ksh88 (4 Replies)
Discussion started by: aoussenko
4 Replies

8. Shell Programming and Scripting

Command to list only files omit directories.

Hi All I am writting a script that does a comparison between files in 2 diffectent directories. To do this I need a command that will list out only the files in a give directory and omit any sub dorectories with that directory. But I am unable to find it. Please Help. I tried ls... (5 Replies)
Discussion started by: Veenak15
5 Replies

9. UNIX for Dummies Questions & Answers

List directories and files

I want to count how many levels there are under a directory. I repeat level. Also how i count only all the files in a directoy ( all files of all directories of all leves down!) and how can i count only all the directories under a directory (including subdirectories, all levels down) ... (2 Replies)
Discussion started by: psalas
2 Replies

10. Shell Programming and Scripting

List specific files from directories

Hello, I would like to list the files from all directories that has been modified more than 1 month ago, and whose name is like '*risk*log'. I think a script like this should work : ls -R | find -name '*risk*.log' -mtime 30 -type f But it tells me "no file found" though I can see some. ... (4 Replies)
Discussion started by: Filippo
4 Replies
Login or Register to Ask a Question