"No such file or directory" on simple bash redirection

16,611

Solution 1

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

Solution 2

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.

Share:
16,611
ACarter
Author by

ACarter

I flit around the SE network, catch me if you can...

Updated on July 26, 2022

Comments

  • ACarter
    ACarter almost 2 years

    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
    
  • Jordan
    Jordan almost 7 years
    This answer actually helped me... turns out I typo'ed too!