0

backup/ has the following files.

ubuntu@ip-172-31-8-46:~/$ ls backup/
itrpl_dsm_10_1.tif  itrpl_dsm_12_3.tif  itrpl_dsm_2_3.tif  itrpl_dsm_4_3.tif  itrpl_dsm_6_3.tif  itrpl_dsm_8_3.tif
itrpl_dsm_10_2.tif  itrpl_dsm_12_4.tif  itrpl_dsm_2_4.tif  itrpl_dsm_4_4.tif  itrpl_dsm_6_4.tif  itrpl_dsm_8_4.tif
itrpl_dsm_10_3.tif  itrpl_dsm_12_5.tif  itrpl_dsm_2_5.tif  itrpl_dsm_4_5.tif  itrpl_dsm_6_5.tif  itrpl_dsm_8_5.tif
itrpl_dsm_10_4.tif  itrpl_dsm_12_6.tif  itrpl_dsm_2_6.tif  itrpl_dsm_4_6.tif  itrpl_dsm_6_6.tif  itrpl_dsm_8_6.tif
itrpl_dsm_10_5.tif  itrpl_dsm_12_7.tif  itrpl_dsm_2_7.tif  itrpl_dsm_4_7.tif  itrpl_dsm_6_7.tif  itrpl_dsm_8_7.tif
itrpl_dsm_10_6.tif  itrpl_dsm_12_8.tif  itrpl_dsm_2_8.tif  itrpl_dsm_4_8.tif  itrpl_dsm_6_8.tif  itrpl_dsm_8_8.tif
itrpl_dsm_10_7.tif  itrpl_dsm_1_1.tif   itrpl_dsm_3_1.tif  itrpl_dsm_5_1.tif  itrpl_dsm_7_1.tif  itrpl_dsm_9_1.tif
itrpl_dsm_10_8.tif  itrpl_dsm_1_2.tif   itrpl_dsm_3_2.tif  itrpl_dsm_5_2.tif  itrpl_dsm_7_2.tif  itrpl_dsm_9_2.tif
itrpl_dsm_11_1.tif  itrpl_dsm_1_3.tif   itrpl_dsm_3_3.tif  itrpl_dsm_5_3.tif  itrpl_dsm_7_3.tif  itrpl_dsm_9_3.tif
itrpl_dsm_11_2.tif  itrpl_dsm_1_4.tif   itrpl_dsm_3_4.tif  itrpl_dsm_5_4.tif  itrpl_dsm_7_4.tif  itrpl_dsm_9_4.tif
itrpl_dsm_11_3.tif  itrpl_dsm_1_5.tif   itrpl_dsm_3_5.tif  itrpl_dsm_5_5.tif  itrpl_dsm_7_5.tif  itrpl_dsm_9_5.tif
itrpl_dsm_11_4.tif  itrpl_dsm_1_6.tif   itrpl_dsm_3_6.tif  itrpl_dsm_5_6.tif  itrpl_dsm_7_6.tif  itrpl_dsm_9_6.tif
itrpl_dsm_11_5.tif  itrpl_dsm_1_7.tif   itrpl_dsm_3_7.tif  itrpl_dsm_5_7.tif  itrpl_dsm_7_7.tif  itrpl_dsm_9_7.tif
itrpl_dsm_11_6.tif  itrpl_dsm_1_8.tif   itrpl_dsm_3_8.tif  itrpl_dsm_5_8.tif  itrpl_dsm_7_8.tif  itrpl_dsm_9_8.tif
itrpl_dsm_11_7.tif  itrpl_dsm_2_1.tif   itrpl_dsm_4_1.tif  itrpl_dsm_6_1.tif  itrpl_dsm_8_1.tif
itrpl_dsm_11_8.tif  itrpl_dsm_2_2.tif   itrpl_dsm_4_2.tif  itrpl_dsm_6_2.tif  itrpl_dsm_8_2.tif

When executing

ubuntu@ip-172-31-8-46:~/backup$ for (( i=1; i<=12; i++ )); do tar -czvf itrpl_dsm_$i.tar.gz itrpl_dsm_$i_*.tif; done

Output:

itrpl_dsm_10_1.tif
itrpl_dsm_10_2.tif
itrpl_dsm_10_3.tif
itrpl_dsm_10_4.tif
itrpl_dsm_10_5.tif
itrpl_dsm_10_6.tif
itrpl_dsm_10_7.tif
itrpl_dsm_10_8.tif
itrpl_dsm_11_1.tif
itrpl_dsm_11_2.tif
itrpl_dsm_11_3.tif
itrpl_dsm_11_4.tif
itrpl_dsm_11_5.tif
itrpl_dsm_11_6.tif
itrpl_dsm_11_7.tif
itrpl_dsm_11_8.tif
itrpl_dsm_12_3.tif
itrpl_dsm_12_4.tif
itrpl_dsm_12_5.tif
itrpl_dsm_12_6.tif
itrpl_dsm_12_7.tif
itrpl_dsm_12_8.tif

My intention was generating itrpl_dsm_1.tar.gz from itrpl_dsm_1_*.tif files, but apparently it didn't do what I want.

3

You should use ${i} not $i. i.e.

for (( i=1; i<=12; i++ )); do tar -czvf itrpl_dsm_$i.tar.gz itrpl_dsm_${i}_*.tif; done

Because bash will look for variable name i_, but there is no variable with this name, so it will replaced with nothing.

So the pattern will become itrpl_dsm_*.tif and then *, wildcard will expand and it will be replaced by every file matching the pattern.

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.