M HYPE SPLASH
// news

bash script - tar Cannot stat: No such file or directory, exiting with failure status due to previous errors

By Emma Terry

I'm trying a simple bash script to backup my source files from my website root directory to my /home/backups/files directory.

script2.sh

#!/bin/bash
#START
TIME=`date +%b-%d-%y`
FILENAME=backup-$TIME.tar.gz
SRCDIR="var/www/html/My_Site"
DESDIR="/home/backups/files"
tar cpzfP $DESDIR/$FILENAME $SRCDIR
#END

Error:

tar: /var/www/html/My_Site\r: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
  1. I cross checked for Whitespaces in each line of code, there are none.

  2. Cross checked the folder permission for both SRC and DES, its 0777.

I'm not getting any idea as to why I getCannot stat: No such file or directory.

3

2 Answers

It looks to me like you script has Windows carriage returns in it, which are causing the failure. The clue to this is the \r at the end of the path in the SRCDIR variable, which is shown in the error code as /var/www/html/My_Site\r. Obviously that path does no exist with the added \r on the end.

Here's a sed command which you can run on your script to remove the carriage returns, it will make a backup of the original script, named script2.sh.bak.

sed -i.bak 's/\r//g' script2.sh

The script should work correctly now, you can delete the backup version once you've verified this.

Another method to get rid of those unwanted carriage returns would be to use tr -d '\r' script2.sh, but you'd need to direct the output to a new file, and copy that in place of the old one.

One other thing that would be useful would be to ensure that you quote your variables when you use them in your script. It's a good practice to get into.

1

Do you have rights to write into DESDIR folder?

Also, to avoid "tar: Removing leading `/' from member names" try this:

tar cpzfP $DESDIR/$FILENAME $SRCDIR

I don't know why, but this works for me.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy