0

I'm trying to finish a little bash project and I got stuck while trying to tar a folder given a path. For example, the program receives the route /home/folderx. Then, it looks for every folder inside that directory with a for i in ~/folderx/*; do... loop. And here's the problem: I'm trying to tar every folder inside that route with tar -czvf $i.tgz $i -P, but it compress the entire route that was given to the program. At the end, I obtain a .tgz with the correct name (the name of the directory that I wanted to tar) but inside it there's /home/folderx/name_of_the_folder/contents instead of just name_of_the_folder contents.

What's the problem? Thank you very much.

  • > Then, it looks for every folder inside that directory with a for i in ~/folderx/*; do... : If they exist, that will also match other types of files. To match only directories (excluding hidden directories, whose name begins with a .): for i in ~/folderx/*/; do.... Note the trailing slash. – Barefoot IO Mar 9 '16 at 22:01
0

you need to be in the directory which you need to do tar.

In for i in ~/folderx/*; do...loop do

cd $i
tar -czvf $i.tgz . -P
cd -

This works for me.

  • That worked more or less for me. If I use your code, the script creates a folder with the correct name but, when decompressing, it leaves all content inside the directory. I finally used: cd ~/directory_that_contents_the_folders_to_compress final_name=$(basename $i)"_"$(date +"%Y%m%d").tgz tar -czf $final_name $(basename $i) -P && mv $final_name /detination_folder cd - – Erwol Mar 10 '16 at 16:00
0

Create the archive file first and then run the command.

For example:

destination__folder = "name_of_folder.tgz"

tar czvf $path/to/destination_folder $i

In the above example $i is the one that you are already using in your script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.