Today i am going to show you how can we make a deep scanner by which we can scan whole directory including subdirectories and fetch all files of given extension.
Here is Code :)
Explanation :
- In Line 3, i declared a array
databasewhich will save our fetched files name. - In Line 5, introduced a new function with the name of
scannerwhich expects two values as arguments i.e PATH to be searched & EXTENSION of required files. - Line 27, take input from user (path & extension)
- Line 29, function
scannercalled with required arguments - Line 6 to 8, inside
scannerfunction, twolocalvariables are declared & initialised. One ispath_to_be_scannedwhich is initialised to$1(path to be scanned) and Second isfileinitialised to emptyString. Now third one isextinitialised to EXTENSION of file. - Line 10,
cdinto given directory. - Line 12, so now we are looping through all files & directories in current directory. (
*is just like a universal set which consists all elements )if you are more curious guy then just search
regexon google - Line 13, so now check if
fileis real file or directory.# -d returns true if given argument is directory otherwise false if [ -d DIR ]; then echo "DIR is a directory & exists" else echo "DIR is a not a directory or not exists" fi - Line 15, i am skipping one folder for scanning to save our time but you can scan it too if you want.
- Line 16, ok so if folder name is not Android then call
scanneragain with current value offileandext. Alwayscdback for each recursive call. - Line 21, now i am checking if
filecontains givenextor not. If yes then append it todatabasearray.if you never heard about recursion then you should keep reading this post :)
Example For Better Understanding
- Content of directory
$HOME/bash( See Attached Screenshot )

- After running script with required inputs i got output like below

- As you can see i got my all
.shfiles within given directory (including subdirectories test1, test2, test3, test4). But how ?
Behind The Scene
- So after taking input,
scannercalled with$HOME/bashaspathandshas extension of file to be searched. - Now after
cdinto$HOME/bash, magic begins with maggy masala magic ;) - Ok so first i got a list of all things within current directory i.e
$HOME/bashby*a.k.a asterisk and feed that list toforloop.# for loop now looks like for file in debug.sh sort.sh test1 test2 test3 test4 wget.sh; do ... done - Now i checked if
$fileis a file or a directory. So in first run of loop$fileisdebug.sh.# Offcourse debug.sh is not a directory if [ -d debug.sh ]; then ... # will not run. fi - Now i am checking if extension of
debug.shisshor not by simply trimming$filename using${file##*.}. It will return anything after.fromdebug.shi.eshif [ sh = sh ]; then database+=(debug.sh) fi - Now in second run of loop,
$fileissort.shand the same process will be done. - In third run of loop when
$fileistest1which is a folder,[ -d test1 ]will return0ortrue. Then if its not a folder with name of Android then call againscannerwith current value offilevariable i.etest1. - Now inside
scannerfunctionpath_to_be_scannedassigned totest1&extis same as before i.esh. - After
cdintotest1folder,forloop looks likefor file in t1 t2 t3 t4; do ... done - They all are files, but not with
shextension, so nothing happens. Neither[ -d t1 ]nor[ * = sh ]will be execute. - Now after looping four times (t1,t2,t3,t4) function ends and will return to it’s previous state where execution was stopped. (See below diagram)


- So program was on line 16 when
scannerwas called second time. And one more thing, first calledscannerfunction comes in paused state while executingscannersecond time. And when secondscannerwill do all his work then currentscannerfunction will be wiped out from memory ( stack ) and then our pausedscannerwill resume from Line 17yes your Operating system ( more specifically CPU ) maintains a stack where it stores recursive functions along with data like local variables. You should learn about stack if you are confused :)
- We are on Line 17 now, it’s time to
cdback fromtest1folder and then check iftest1hasshextension but it does not make sense because we know thattest1is a folder not file, so it’s your homework to not check folders for extension. Well soifpart will not execute. - Now fourth run of
forloop,$fileistest2, which is a folder and same thing will happen now ( recursive call) - Fifth run of loop,
$fileistest3, which is also a folder or directory, ok so it’s time to call our hero againscanner test3 shand now insidescanner,forloop looks likefor file in my.sh; do ... done my.shis a file so no recursive call takes place and we shifted to secondifconditionif [ sh = sh ]; then database+=(my.sh) done- Now
forloop ends and there is nothing to execute so secondscannerwiped out again and control returns to our first initial called functionscanner. - At last when initial called
scannerends ( means there is nothing to do ) , this also wiped out from stack and stack is empty and we are ready to go for next instruction in script which isecho ${database[@]}
Ok so this was my best ( i think 😬). Thank you for reading.