0

I have this cronjob.

50 15 * * * cd /var/www/html; for i in $(ls -d */); do tar zcvf ${i%%/}.tar.gz $i; done

But it shows me this.

/bin/sh: 1: Syntax error: Missing '}'

But when I run this command from command line directly, it works.

Where is the problem?

Thanks.

genderbee
  • 830
  • 1
  • 14
  • 31
  • BTW don't do `for i in $(ls -d */)` - see for example [Bash pitfall #1](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29) – steeldriver Jan 31 '20 at 15:03
  • @steeldriver Yeah, it works, thank you. So `%` is special char for cron and there must be \ before this char, so `\%` is correct, just for info. And about `for`, what is better way to do it? It is only backup for all folders in `/var/www/html`, every folder to separated file, for example folder `aa` to `aa.tar.gz`, folder `bb` to `bb.tar.gz, etc`. – genderbee Jan 31 '20 at 15:08
  • 1
    `for i in */;` should work fine. – Terrance Jan 31 '20 at 15:10
  • 1
    ... and quotes in the expansions `tar zcvf "${i%%/}.tar.gz" "$i"` – steeldriver Jan 31 '20 at 15:30
  • @steeldriver Ok, thank you, it works perfect. – genderbee Jan 31 '20 at 18:30

0 Answers0