"No such file or directory" on simple bash redirection

ACarter picture ACarter · Mar 24, 2013 · Viewed 15k times · Source

Here is some code of mine:

gzip -c $path > /var/www/wiki/backup/$now/$file.gz

I'm gzipping the contents of $path (the path to a directory), and then sending the compressed file to /var/www/wiki/backup/$now/$file.gz. $now contains a directory name, $file is the name I want to write the compressed file to.

However, on running the program, I get this error:

backup.sh: line 20: /var/www/wiki/backup/Sunday/extensions.gz: No such file or directory
                                         ^$now    ^$file

(line 20 is the line given above)

Why is the program breaking? I know Sunday/extensions.gz doesn't exist (although Sunday does), that's why I'm asking you to write to it!

Full program code:

#!/bin/bash

now=$(date +"%A")
mkdir -p /var/www/wiki/backups/$now

databases=(bmshared brickimedia_meta brickimedia_en brickimedia_customs)
locations=("/var/www/wiki/skins" "/var/www/wiki/images" "/var/www/wiki/")

for db in ${databases[*]}
do
#command with passwords and shoodle
:
done

for path in ${locations[*]}
do
#echo "" > var/www/wiki/backup/$now/$file.gz
file=`echo $path | cut -d/ -f5`
echo $path
gzip -c $path > /var/www/wiki/backup/$now/$file.gz
done

Answer

suspectus picture suspectus · Mar 24, 2013

The directory created is backups, the gzip is to backup.

mkdir -p /var/www/wiki/backups/$now

gzip -c $path > /var/www/wiki/backup/$now/$file.gz