Small question regarding a Shell command please.
I have a top level folder, which contains files of different types of extensions. For instance, the folder contains files .java, .xml, etc...
Under this top level folder, it contains also underlying folders, child folders, which themselves contains same and/or other types of files, and possibly other child folders.
I am trying just to list all the types of files under the top level folder, all the way down.
I tried using the command find, but it requires me to know in advance all the types, which I do not know.
May I ask, what is the correct command to get all types of files please?
An expected result output would be like:
- .java
- .xml
- .png
(if possible, with the count, but totally ok if not)
- .java 86
- .xml 23
- .png 42
CodePudding user response:
Or you could write a script in, say, Perl to do it all.
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
use File::Find;
# Takes directory/directories to scan as a command line argument
# or current directory if none given
@ARGV = qw/./ unless @ARGV;
my %extensions;
find(sub {
if ($File::Find::name =~ /\.([^.] )$/) {
$extensions{$1} = 1;
}
}, @ARGV);
for my $ext (sort { $a cmp $b } keys %extensions) {
say "$ext\t$extensions{$ext}";
}
or using bash:
#!/usr/bin/env bash
shopt -s dotglob globstar
declare -A extensions
# Scans the current directory
allfiles=( **/*.* )
for ext in "${allfiles[@]##*.}"; do
extensions["$ext"]=$(( ${extensions["$ext"]:-0} 1))
done
for ext in "${!extensions[@]}"; do
printf "%s\t%d\n" "$ext" "${extensions[$ext]}"
done | sort -k1,1
or any shell (Won't work well with filenames with newlines; there are ways around that if using, say, GNU userland tools, though):
find . -name "*.*" | sed 's/.*\.\([^.]\{1,\}\)$/\1/' | sort | uniq -c