user9540477
user9540477

Reputation:

Shell, For loop into directories and sub directory not working as intended

When trying to do a simple script that tars the files and moves them to another directory i'am having problems implementing the recursive loop.

#!/bin/bash
for directorio in $1; do #my thought process: start for loop goes into dir
    for file in $directorio; do #enter for loop for files
        fwe="${file%.*}" #file_without_extension    
        tar -czPf "$fwe.tar.bz2" "$(pwd)" && mv "$fwe.tar.bz2" /home/cunha/teste
    done
done

the script seems to be doing nothing ... when the script is called like: ./script.sh /home/blablabla

How could i get this fixed?

Upvotes: 0

Views: 71

Answers (1)

Abhijit Pritam Dutta
Abhijit Pritam Dutta

Reputation: 5591

You can better follow below option. How will it work? First it will list all the directories in short_list.txt. In while loop it will read each directory name and zip it in location /home/cunha/teste

find $1 -type d -name "*" > short_list.txt
cdr=$(pwd)

while read line
do
   cd $line
   base=$(basename $PWD)
   cd ..
   tar -cf /home/cunha/teste/$base.tar.gz  $base
done < short_list.txt
cd $cdr
rm -rf short_list.txt

Upvotes: 3

Related Questions