Count multiple directory files from shell
In one of my projects I had train an ML model to classify documents. The customer provided some samples for each category to identify. To have an homogeneous training set the number of files for each category should be the same.
To quickly check if the number of files in each directory is the same I created a simple shell script.
Imagine a directory structure like this one:
1root directory
2├── Category-A
3│ ├── File_1
4│ ├── File_2
5│ ├── File_3
6│ ├── File_4
7│ ├── File_5
8├── Category-B
9│ ├── File_1
10│ ├── File_2
11│ ├── File_3
12│ ├── File_4
13├── Category-C
14│ ├── File_1
15│ ├── File_2
16│ ├── File_3
17│ ├── File_4
18│ ├── File_5
19│ ├── File_6
20│ ├── File_7
I wanted to obtain something like this:
1 3 files in directory .
2 5 files in directory ./Category-A
3 4 files in directory ./Category-B
4 7 files in directory ./Category-C
Below there's a script that does exactly this. I made two versions one for bash
and one for fish
bash version
This is the bash version of the command
1#! /bin/bash
2
3find . -type d -print0 | while read -d '' -r dir; do
4 files=("$dir"/*)
5 printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
6done
fish version
This is the fish version.
1#! /bin/fish
2
3for x in (find {$1} -maxdepth 1 -type d)
4 set files (ls {$x} | wc -l)
5 printf "%5d files in directory %s\n" {$files} {$x}
6end
If you want to make this command part of your fish
commands you can place the following version in ~/.config/fish/functions
1function file_count --description "Count files in each directory from a give root"
2
3 for x in (find {$argv} -maxdepth 1 -type d)
4 set files (ls {$x} | wc -l)
5 printf "%5d files in directory %s\n" {$files} {$x}
6 end
7
8end
At this point you can execute the file_count
command wherever you want:
1$> file_count .
2
3 3 files in directory .
4 5 files in directory ./Category-A
5 4 files in directory ./Category-B
6 7 files in directory ./Category-C
7
8$> _