0

I'm trying to clean up my files and I'm struggling with some command line stuff.

With the command

du -sh *

I can get a nice list of all folders where I use the command. How can I also get the number of files of each folder? i try to explain better:

Let's say I have the following structure:

macrofolder
|
+--folderA
|------file1
|------file2
+--folderB
|--file1
+--folderC
|------file1

How can I get something like this?

2MB     .          1 file
30MB    folderA    2 files
0MB     folderB    0 files
4MB     folderC    1 file

Is there a way to achieve something similar in a simple way?

0

3 Answers 3

2

Well, from command line tools, something like this

find . -type d -exec stat -f '%z %N %l' {} +

Where %z is the size of the file in bytes but not the size of the blocks allocated to the files in the directory. %N the name of the file and %l the link count to the directory.

0

DISCLAIMER: despite put here, this is at the moment more a comment - but within these the script flow/line breaks are IMHOcompletely confusing. So I put it here (because I do not know better).

To get e.g. the number of files as well as the total size in a directory you can use:

    NUMBER=$(find . -type f -name '*' -exec du -ch {} +)
    N=$(echo "$NUMBER" | head -n 1 | wc -l)
    SIZE=$(echo "$NUMBER" | tail -n 1)
    echo "Number of files: $N"
    echo $SIZE

this will list the number of files as well as the total size for the selected folder.

Editing the -name pattern for find you can get this information for specific file types like e.g *.PNGs etc..

2
  • it doesn't work :( gives me an error head: illegal line count -- -1 Number of files: 0
    – rita1989
    Jul 31 at 11:25
  • Aaah, sorry - it has to be head -n 1 somehow “-1” slipped in, corrected. Jul 31 at 11:39
-1

Open a Finder Window, then select View>Show View Options, then tick the checkbox for "Calculate All Sizes", then click use as defaults.

You must log in to answer this question.

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