- [Instructor] Welcome to the next video of section three,
enumerating file type statistics.
In the previous video, we've learned how to find
symbolic links and their targets.
In this video we'll learn how to write scripts
that can enumerate through many files and collect details.
There are many file types.
It will be an interesting exercise to write a script
that can enumerate through all the files inside a directory,
its descendants,
print a report that provides details of file types,
files with different file types,
and the count of each file type present.
The file command can be used to find out the type of file
by looking at the contents of the file.
In Unix/Linux systems file types are not determined
based on the extension of the file,
like the Microsoft Windows platform does.
This section aims at collecting file type statistics
of a number of files.
For storing the count of files of the same type,
we can use an associative array
and the file command can be used
to fetch the file type details from each of the files.
Let's see how to do it.
To print the type of a file use the following command.
Print the file type only
by excluding the filename as follows.
The script for file statistics is as follows.
The usage is as follows.
A sample output is shown as follows.
Now we'll see how the function works.
Here an associative array named statarray is declared,
so that it can take the file type as the file indices
and store the count of each file type in the array.
Let is used to increment the count
each time a file type is encountered.
The find command is used
to get the list of file paths recursively.
A while loop is used to iterate line by line
through the find command's output.
The input line which is on the previous script
is used to find out the file type using the file command.
The -b option specifies the file command
only to print the file type,
without the filename in the output.
The file type output consists of more details,
such as imaging coding used
and resolution in the case of an image file.
But we're not interested in all the details.
We need only the basic information.
Details are comma separated, as in the following example.
We need to extract only elf 32-bit lsb-executable
from the previous details.
Hence we use cut -d, -f1,
which specifies to use comma as the delimitor
and print only the first field.
It's an important bit of code.
The logic is as follows.
Instead of the filename, we use the output of find.
It's equivalent to a filename,
but it substitutes filename with a subprocess output.
Note the first less than is for input redirection
and the second less than
is for converting the subprocess output to a filename.
Also there is a space between these two
so that the shell won't interpret it as the append operator.
In bash 3.x and higher we have a new operator,
less than less than less than,
that lets us use a string output as an input file.
Using this we can write the done line
of the loop as follows.
It's used to return the list of ray indexes.
Great.
We've successfully learned
about enumerating file type statistics.
In the next video, we'll learn about using loopback files.