-2

I'm new to Bash and am trying to compress subfolders, but it's not working.

|-- Folder
|   |-- SubFolder1
|   |-- SubFolder2
|   |-- SubFolder3

after tar command

|-- Folder
|   |-- SubFolder1
|   |-- SubFolder2
|   |-- SubFolder3
|   |-- SubFolder1.tar.gz
|   |-- SubFolder2.tar.gz
|   |-- SubFolder3.tar.gz

Script:

#!/bin/bash
DEST=/home/pc/Desktop


for folder in $DEST
do

      tar -czvf "$folder.tar.gz"  $DEST
      #rm -rf "$folder"

done

How do I do this?

slm
  • 15,396
  • 12
  • 109
  • 124
  • please clarify which result you expect. – user803422 Sep 04 '18 at 11:30
  • `cd Folder && echo SubFolder* | tr ' ' '\n' | xargs -I {} tar -cvzf {}.tar.gz {}` – Cyrus Sep 04 '18 at 11:56
  • 1
    *"but it's not working..."* is not a good problem statement. State how it is not working for you. Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Sep 04 '18 at 13:00
  • Also see [Tar a directory, but don't store full absolute paths in the archive](https://stackoverflow.com/q/18681595/608639) and [How to loop over directories in Linux?](https://stackoverflow.com/q/2107945/608639) – jww Sep 04 '18 at 13:02

2 Answers2

0

Your for loop needs to be told what to iterate over. You're merely telling it the top level directory:

...
DEST=/home/pc/Desktop

for folder in $DEST
...

This needs to include a wildcard so that Bash knows you want all the directories underneath it:

...
DEST=/home/pc/Desktop

for folder in "$DEST"/*
...

Example

$ mkdir -p Folder/SubFolder{1,2,3}
$ tree Folder/
Folder/
├── SubFolder1
├── SubFolder2
└── SubFolder3

3 directories, 0 files

Your command:

$ for i in Folder/;do echo "$i";done
Folder/

With the star (*):

$ for i in Folder/*;do echo "$i";done
Folder/SubFolder1
Folder/SubFolder2
Folder/SubFolder3

Using find

Instead of using for to do the generation of directories to tar, you can use find instead like this:

$ find Folder/ -mindepth 1 -maxdepth 1 -type d | xargs -I {} tar zcvf {}.tar.gz {}
Folder/SubFolder3/
Folder/SubFolder1/
Folder/SubFolder2/

$ ll Folder/
total 24
drwxr-xr-x 2 root root 4096 Sep  4 07:30 SubFolder1
-rw-r--r-- 1 root root  122 Sep  4 08:01 SubFolder1.tar.gz
drwxr-xr-x 2 root root 4096 Sep  4 07:30 SubFolder2
-rw-r--r-- 1 root root  122 Sep  4 08:01 SubFolder2.tar.gz
drwxr-xr-x 2 root root 4096 Sep  4 07:30 SubFolder3
-rw-r--r-- 1 root root  122 Sep  4 08:01 SubFolder3.tar.gz
slm
  • 15,396
  • 12
  • 109
  • 124
0

Please check the comments in code block:

#!/bin/bash
DEST=/whatever/whatever/*/  #Note the ending "/" let you get only dirs

for folder in $DEST
do
    #the basename gets the name of current sub-dir
    #instead of fullpath
    tar -czvf "$(basename "$folder").tar.gz"  "$folder"
done

You may get something like "tar:removing leading "/" or slash..." err message, I cannot remember exact wording. You can add -P to your tar to ignore it.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Kent
  • 189,393
  • 32
  • 233
  • 301