I have to a create directory backups of selected directories; each backup should have only one root directory; if multiple directories are selected, then each of them should be stored in a separate backup. Backups should use tar and gzip. I wrote a script that should somewhat achieve this but I keep getting an error that cannot stat:

no such file name or directory


DATE=$(date +"%d-%b-%Y")
cd ~/comp232/lab5
tar zcvf lab5-$DATE.tgz lab5
mv *.tgz ~/Documents

I have Been looking up the problem online but the solutions I've tried such removing the hyphen between tar and zcvf does not work.

Any help would be appreciated.

  • Are you sure you are running the script correctly, and not mis-typing the filename or something? Try running the script with a ./ in front of the script name, for example ./script.sh
    – user533208
    May 10, 2016 at 12:19

1 Answer 1


This should fix your Script but this long time not already does what you want to achieve:

DATE=$(date +"%d-%b-%Y")
cd ~/comp232/
tar -caf lab5-$DATE.tgz lab5
mv *.tgz ~/Documents

However assuming all directories you want to .tgzare in the same parent folder you can do something like this:

DATE=$(date +"%d-%b-%Y")
cd ~/comp232/
for f in ./* # for each entry in directory do
    if [ -d "$f" ] # making sure $f is a directory
        tar -caf "$f-$DATE".tgz $f
mv *.tgz ~/Documents

Hope this will help you :)

  • Oh i just saw my script proposal will aswell put single files in the parent directory into a tgz file let me edit my answer then you can accept it. There you go.
    – Videonauth
    May 8, 2016 at 18:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.