Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a script which searching the given path(first parameter) and prints number of dirs, files etc.Update table is just a function which sorts my datas. My problem is how can i insert here the depth of searching. The depth will be the second parameter and the script will search my path until reach depth.
User will run the program as ./script.sh /Desktop/folder 2

What I have tried:

PowerShell
function check_dir {

      echo Checking dir : $1
           for f in `ls $1`
           do

            if [ -d $1/$f ]
           then

             dirs_num=$(($dirs_num+1))
             check_dir $1/$f

            else    

               files_num=$(($files_num+1))
               size=`stat -c%s $1/$f`
               echo $1/$f - $size
               update_table $1/$f $size
       
  fi

     done

}

    files_num=0
    dirs_num=0
    files_names=(a a a)
    files_sizes=(0 0 0)

    check_dir $1 

 echo Found $files_num files and $dirs_num dirs.
Posted
Updated 2-Jun-20 22:16pm
v4

1 solution

You just need to capture the depth value and reduce it by 1 each time you call check_dir. Something like:
JavaScript
function check_dir {
    depth = $($2 - 1) // reduce the depth count by 1
    echo Checking dir : $1
    for f in `ls $1`
    do
        
        if [ -d $1/$f ]
        then
        
            dirs_num=$(($dirs_num+1))
            if [ $depth -gt 0 ] // if the depth count is greater than 0
            then
                check_dir $1/$f  $depth// do a recursive check
            fi        
        else    
        
            files_num=$(($files_num+1))
            size=`stat -c%s $1/$f`
            echo $1/$f - $size
            update_table $1/$f $size
        
        fi
        
    done
    
}

NB apologies if some of my syntax is wrong, it's a few years since I was writing shell scripts.
 
Share this answer
 
v2
Comments
Member 14825085 3-Jun-20 7:47am    
Τhanks for this i will keep the idea

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900