BASH copy to another directory not working

5,759

The path separator character / is strictly forbidden in filenames (imagine the hell that would be unleashed if it weren't so!)

Otherwise... backticks for command substitution is kind of deprecated, so I changed it... And adjusted your quoting a bit

If you really want the script to copy itself to the target...

#!/bin/bash
fullname=$(basename "$0")
now=$(date +"%H:%M:%S-%d.%m.%Y")
backupname="${fullname%.*}"."$now.sh"
cp -v "$fullname" ../Logs/"$backupname"

So the function could be something like

backup() {
fullname=$(basename "$1")
now=$(date +"%H:%M:%S-%d.%m.%Y")
backupname="${fullname%.*}"."$now.sh"
cp -v "$fullname" /path/to/wherever/"$backupname"
}
Share:
5,759

Related videos on Youtube

Jamjar91
Author by

Jamjar91

Updated on September 18, 2022

Comments

  • Jamjar91
    Jamjar91 almost 2 years

    Firstly, I'll admit that I am new to UNIX based systems, I am currently learning at college.

    I'm trying to create a function that automates a backup of an existing file to an existing directory. However, I am being returned a message in the CLI that quotes:

    cp: cannot create regular file '../Logs/bashmenu.22:52:28-06/12/2016.sh': No such file or directory
    

    I've done a fair amount of self-research, but I cannot find a working solution to my issue.

    The directory path to the file that contains the function, the one I want to back up is:

    /Home/Scripts/bashmenu.sh
    

    The directory path to the location I wish to back up to is:

    /Home/Logs/TARGET
    

    When backing the file up, I'd like it to be date stamped. So I've used a couple variables to obtain the naming convention for the file when backed up. I believe this is what's causing an issue. I do not know if it causes an issue, but the file and the directories, it's all on a USB, I just created a similar directory path for it.

    Here is part of my script, the parts that I believe make a difference:

    #### #!/bin/bash
    fullname=`basename "$0"`
    now=`date +"%H:%M:%S-%d/%m/%Y"`;
    backupname="${fullname%.*}"".""$now.sh"
    cp -v $fullname ../Logs/$backupname
    
    • Jamjar91
      Jamjar91 over 7 years
      Thank you for the edit Zanna, it's tidied the post up. :)
    • Jamjar91
      Jamjar91 over 7 years
      Steeldriver, That fixed it, thank you for the quick assistance. I thought if the date was being saved as a variable, it'd just accept it as a string of text. Can you change your comment to an answer so I can accept it please. Thanks. :)
    • Terrance
      Terrance over 7 years
      Oops, last comment was incorrect characters. Fixed it. You might want to change your $now also to be for Chronological order listing by date as %Y-%m-%d_%H:%M:%S
  • Jamjar91
    Jamjar91 over 7 years
    Thanks Zanna, I thought as the date was stored as a variable, the data would just be accepted as text, I've since changed it and it's working. That function is pretty much how mines looks in my full script, I just never enclosed it as one in the original question. :)