0

How to find which folder in home directory is largest by the number of files? (excluding directories)

0

4 Answers 4

0
find -type f -printf '%h\n'|uniq -c|sort -rn|head -1

You can substitute "head -<N>" for "head -1" to see the top <N>, or remove it altogether to see the whole list from most to least number of files

From "man find":

   -printf format
     .....
          %h     Leading directories of file's name (all but the last element).  If the file name contains no
                 slashes (since it is in the current directory) the %h specifier expands to ".".
0
2
(cd $HOME && find . -type f) | grep '/.*/' | cut --delimiter=/ --field=1,2 \
 | uniq --count | sort --numeric-sort --reverse \
 | head -1 | cut --delimiter=/ --field=2

i.e. print every file path in home directory, use only the first 2 directories level (the first being .), skipping files at the top level, group and count the occurences, then sort, then take the first entry, and print the name of the directory.

0

If you simply mean which directory has the greatest number of files, ignoreing their size, you can do

find ~/ -type d -print0 | 
    while IFS= read -r -d '' dir; do 
        files=$(find "$dir" -maxdepth 1 | wc -l); 
        echo -e "$files\t$dir"; 
    done | sort -n | tail -n 1

That, however, also counts subdirectories as "files". To count only files, use this one instead:

find ~/ -type d -print0 | 
    while IFS= read -r -d '' dir; do 
        files=$(find "$dir" -maxdepth 1 -type -f | wc -l); 
        echo -e "$files\t$dir"; 
    done | sort -n | tail -n 1
-4

Use the

$ ls

command, or list directory contents. Try something like:

$ cd /your_directory
$ ls -lA

that should work. Check the

 man ls 

to tweak it for exactly what you'd like to see.

Not the answer you're looking for? Browse other questions tagged .