Advertisement
abrar1

Untitled

Feb 18th, 2022
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.41 KB | None | 0 0
  1. #!/usr/bin/bash
  2.  
  3. directory_explore(){
  4.     #moved to src dir
  5.     local path_dir=$1
  6.     echo $path_dir           +++++++++++++++++++++++++++++
  7.     echo before pwd jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
  8.     pwd
  9.    
  10.     ## CHAMGED HERE ;;; NEED TO GO FROM ROOT DIR TO ACCUMULATED PATH
  11.     #ACCUMULATED PATH = PATHDIR
  12.    
  13.     cd "$root_dir"      # absolute path of root
  14.     cd "$path_dir"      # relative path from root to current
  15.    
  16.    
  17.     echo after pwd jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
  18.     pwd
  19.     #cd "$1"
  20.     echo INTO NEW DIRECTORY DONE
  21.     for f in *
  22.     do
  23.         if [ -d "$f" ]; then  # if this is a directory
  24.             local new_path_dir="$path_dir/$f"
  25.             # directory_explore $1/$f
  26.             echo WILL BE ENTERING NEW PATH $new_path_dir
  27.             directory_explore $new_path_dir
  28.            
  29.         elif [ -f "$f" ]; then #if this is a file
  30.             # now check if th file matches our array
  31.            
  32.             local file_name=$(basename -- "$f")
  33.             local extension="${file_name##*.}"
  34.             #file_name="${filename%.*}"
  35.  
  36.             #extracted file name and extension
  37.             pwd
  38.             echo "file name $file_name"
  39.             echo "extension $extension"
  40.  
  41.             bool=0
  42.             for i in $arr
  43.             do
  44.                 echo "$extension"
  45.                 echo "$i"
  46.                
  47.                 if [ $extension = $i ]
  48.                     then
  49.                         echo ++++++++++++++++++++++++++++++
  50.                         bool=1
  51.                         break;
  52.                 fi
  53.             done
  54.  
  55.             #if bool becomes 1, means this $f is a desired file, dont do anything
  56.             #else make a directory named after $extension in the sub and move this $f there
  57.  
  58.             echo $bool
  59.             echo 0000000000000000000000000000
  60.  
  61.             if [ $file_name = $extension ]; then
  62.                 mkdir -p $cp_dir/others
  63.                 cp $f  $cp_dir/others
  64.                 echo "$file_name CREATED IN oTHER"
  65.  
  66.  
  67.                 #went to dir
  68.                 cd $cp_dir/others
  69.                 touch desc_others.txt
  70.  
  71.  
  72.                 echo "$path_dir/$file_name/" >> desc_others.txt   #writing in file done
  73.  
  74.  
  75.                 #came back
  76.                 cd -
  77.                 echo "after writing logfile HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH"
  78.                 pwd
  79.  
  80.  
  81.             elif [ $bool -eq 0 ]; then
  82.                 #   echo $cp_dir
  83.                 mkdir -p $cp_dir/$extension
  84.                
  85.                 cp $f $cp_dir/$extension/$file_name
  86.  
  87.                 echo "$file_name CREATED"
  88.  
  89.  
  90.                 #went to dir
  91.                 cd $cp_dir/$extension   #went to write logfile
  92.                
  93.                 #local filenaam="desc_${file_name}.txt"
  94.                 #touch "desc_${file_name}.txt"    
  95.                 touch log.txt
  96.                 echo "$path_dir/$file_name/" >> "log.txt"   #writing in file done
  97.  
  98.                 echo WRITE DONE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  99.                 cd -  # came back
  100.  
  101.  
  102.             fi
  103.         fi
  104.     done
  105.    
  106.     cd ..
  107.     echo "EXITIG DIRECTORYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY DONEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE PWD"
  108.     pwd
  109. }
  110.  
  111.  
  112. csv_file_write(){
  113.     cd $root_dir
  114.     cd ../output
  115.     pwd
  116.     touch A.csv
  117.  
  118.     echo "Filetype,No_of_files" >> A.csv
  119.  
  120.     count=-1
  121.     for f in *     #f is folder name
  122.     do
  123.         if [ -d "$f" ]; then   #this is a folder
  124.             cd ./$f
  125.             count=-1
  126.             for f1 in *
  127.             do
  128.                 count=$((count+1));
  129.                 echo $count
  130.             done
  131.             cd ..
  132.             echo "$f,$count" >> A.csv
  133.         elif [ -f "$f" ]; then   #if this is a file
  134.             echo hahavodox
  135.         fi
  136.     done
  137.  
  138.     cd .. #exited from output folder
  139.     mv ./output/A.csv $root_dir/A.csv
  140. }
  141.  
  142. root_dir=$(pwd)
  143.  
  144. # S1   S2
  145. #    -----------------------make user give input
  146. if [ $# -eq 0 ];then
  147.     echo "you have to input a filename"
  148.     read filename
  149.     until [ -f "$filename" ]
  150.     do
  151.         echo "wrong filename. input a valid one"
  152.         read filename
  153.     done
  154.  
  155.    
  156. else
  157.     if [ $# -eq 1 ];then
  158.         filename=$1
  159.     else
  160.         filename=$2
  161.         dirr=$1
  162.     fi
  163. fi
  164.  
  165. #   ---------------------------checking if file exists
  166.  
  167. until [ -f "$filename" ]
  168. do
  169.     echo "wrong filename. input a valid one"
  170.     read filename
  171. done
  172.  
  173. echo "you inputed this filename $filename"
  174.  
  175.  
  176.  
  177. #     ----------------------checking done, trying to read file and put it in arr ARRAY
  178.  
  179.  
  180.  
  181. #readarray -t arr < $filename
  182. arr=$(<$filename)
  183.  
  184. for i in $arr
  185. do
  186.     echo $i
  187. done
  188.  
  189. #     ------------------------ stroing the destination directory
  190. cd ..
  191. mkdir -p output
  192. cd output
  193. cp_dir=$(pwd)            #this is the path to copy a file
  194. cd $root_dir
  195.  
  196. #     -----------------------  steoing the src directory
  197.  
  198. if [ $# -eq 1 ];then
  199.     #explore_dir=$(pwd)     # source is currnt working directory
  200.     path=.      # current directory is working directory
  201. else
  202.     # cd $1       # source is given
  203.     # explore_dir=$(pwd)
  204.     # cd ..
  205.     path=./$1
  206. fi
  207.  
  208. echo $path
  209.  
  210. directory_explore $path
  211.  
  212. csv_file_write
  213.  
  214. # echo $explore_dir
  215. # echo $cp_dir
  216. # cd $explore_dir
  217. # pwd
  218.  
  219. # all directories are good and
  220. #I am in cexplore directory
  221.  
  222. # directory_explore $explore_dir
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement