Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to count number of files in each directory

Tags:

shell

I need to count the number of files on a large number of directories. Is there an easy way to do this with a shell script (using find, wc, sed, awk or similar)? Just to avoid having to write a proper script in python.

The output would be something like this:

$ <magic_command>
dir1  2
dir2 12
dir3  5

The number after the dir name would be the number of files. A plus would be able to turn counting of dot/hidden files on and off.

Thanks!

like image 895
tiago Avatar asked Dec 07 '12 06:12

tiago


People also ask

How do I count the number of files in a directory?

To determine how many files there are in the current directory, put in ls -1 | wc -l. This uses wc to do a count of the number of lines (-l) in the output of ls -1.

How do I count the number of files in multiple folders?

Browse to the folder containing the files you want to count. Highlight one of the files in that folder and press the keyboard shortcut Ctrl + A to highlight all files and folders in that folder. In the Explorer status bar, you'll see how many files and folders are highlighted, as shown in the picture below.

How do I count the number of files in a directory in Linux?

Use the tree command. It will give you the tree and at the bottom tell you how many files and directories there are. If you want hidden files also use tree -a .

How do I count files in CMD?

How to count the files in a folder, using Command Prompt (cmd) You can also use the Command Prompt. To count the folders and files in a folder, open the Command Prompt and run the following command: dir /a:-d /s /b "Folder Path" | find /c ":".


1 Answers

Try the below one:

du -a | cut -d/ -f2 | sort | uniq -c | sort -nr

from http://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-the-total-number-of-files-in-a-folder-510009/#post3466477

like image 77
Mehdi Karamosly Avatar answered Oct 02 '22 22:10

Mehdi Karamosly