which directory has the most files?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 11-08-2011
which directory has the most files?

I need to write a simple bash shell script that will recurse a directory and print out the top 2-3 directories with the most files in it.

I know how to use find & wc, but cannot seem to figure out how to store the count in each directory found, sort the count by the number and print out the top few directories that have the most files.

I would pass the script a single directory and it would make a list of all directories below it and count the number of files in each directory, store the count and then sort by descending order & print out the top 2-3 directories with the most files in it (excluding subdirectories in that directory - just want a count of files).

If anyone has an idea, that would be great,
# 2  
Old 11-08-2011
Code:
find /path -type f |awk  '{$NF="";a[$0]++}END{for (i in a) print a[i],i }' FS=\/ OFS=\/ |sort -rn|head -3

This User Gave Thanks to rdcwayx For This Post:
figaro (11-23-2011)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Directory containing files,Print names of the files in the directory that are exactly same content. anuragpgtgerman Shell Programming and Scripting 7 03-23-2017 08:58 AM
How to move gz files from one source directory to destination directory? venkat918 UNIX for Dummies Questions & Answers 11 04-06-2016 04:29 AM
Moving Files one directory to another directory shell script venkat918 Shell Programming and Scripting 4 04-03-2016 02:32 PM
List files with date, create directory, move to the created directory charli1 Shell Programming and Scripting 6 01-26-2016 02:00 PM
How to set owner and permission for files/directory in directory in this case? bobochacha29 AIX 8 05-26-2015 11:40 PM
Grepping file names, comparing them to a directory of files, and moving them into a new directory sHockz Shell Programming and Scripting 1 02-02-2012 01:53 AM
FTP files from different directory from remote server to one directory in local dassv Shell Programming and Scripting 3 12-16-2011 10:01 AM
Script which removes files from the first directory if there is a file in the second directory SLAMUL Shell Programming and Scripting 6 10-26-2011 08:12 PM
How to rsync or tar directory trees, with hidden directory, but without files? fld2007 UNIX for Advanced & Expert Users 4 09-19-2010 12:54 PM
Zip files inside the directory, but not the directory itself Thales.Claro UNIX for Dummies Questions & Answers 2 07-20-2010 05:40 AM
Finding files in current directory when 100,000's files in current directory kewong007 Shell Programming and Scripting 2 02-26-2009 02:13 PM
Creating date directory and moving files into that directory ravi030 Shell Programming and Scripting 3 12-05-2008 03:18 AM
Move all files in a directory tree to a signal directory? briandanielz UNIX for Dummies Questions & Answers 2 06-15-2008 05:20 PM
moving files from a unix directory to a windows directory gleads UNIX for Dummies Questions & Answers 2 08-29-2002 08:42 PM