Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No such file or directory" on simple bash redirection

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
like image 923
ACarter Avatar asked Mar 24 '13 15:03

ACarter


2 Answers

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
like image 133
suspectus Avatar answered Nov 19 '22 21:11

suspectus


One of your locations is "/var/www/wiki/". Then you have

file=`echo $path | cut -d/ -f5`
gzip -c $path > /var/www/wiki/backup/$now/$file.gz

Since $file contains the empty string, you're attempting to write to /var/www/wiki/backup/Sunday/.gz. That's a problem but it's not the error you're reporting.

When I try to gzip a directory, I get this error

$ gzip -c ./subdir/ > subdir.gz
gzip: ./subdir/ is a directory -- ignored

That's a problem but it's not the error you're reporting.

@suspectus solved your reported problem.

like image 38
glenn jackman Avatar answered Nov 19 '22 22:11

glenn jackman