0

Hello I am trying to copy all files from Documents directory to the backup directory that has a timestamp. So I have created a directory called bk$( the time stamp of the directory) and I am trying to copy files from the Documents directory to the new created directory that is unique. This will be in a crontab backing up files from documents and when the backup will kick in, it will create new directory for each backup that is uniquely identified by the directory timestamp. For some reason I cannot get the cp or cpio -mdp.

bkdate="date +%Y_%m_%d_%H_%M_%S"
PATH=/home/user/backup/
bksource="/home/user/Documents/"

mkdir /home/user/backup/"bk$(date +%Y_%m_%d_%H_%M_%S)"
cp $bksource * ls | tail -l | $PATH

I could of went with the ctime but unfortunately it does not work with the directory creation date.

This was my approach but with the latest created directory and not file

find $HOME -type d -daystart ctime 0

If someone could please help me out to copy to that new directory, I would really appreciate it.

Solution: This is one solution using target. I am open to other ways that could be used for this purpose.

bkdest=/home/user/backup
bksource=/home/user/Documents
target=${bkdest}/bk.$(date +%Y_%m_%d_%H_%M_%S)
mkdir -p $target
cp ${bksource}/* ${target}/
0

You set the location in a variable named $PATH. This variable has a special meaning as the search path where the shell will look for commands to execute. By setting it to a directory which you created (and which therefore is empty), you just made sure that the shell can't find any command anymore.

Rename that variable to something else. All will be fine then.

| improve this answer | |
  • So do you mean to leave $PATH variable as is and then just copy files "cp $bksource * $PATH" and that will work and copy to that particular created folder that has been created previously? Thanks! – Dmitriy Jul 29 '15 at 6:31
  • Nonono. rename the PATH variable to something else. You will still need a variable where you store that. Just don't call it PATH, – Wouter Verhelst Jul 29 '15 at 6:36
  • Thanks, I'm still a bit confused, then how is $PATH variable being used? it doesn't know the location to which it is assigned. Where am I calling the path, to the "cp" destination? – Dmitriy Jul 29 '15 at 6:39
  • If you could please provide an example of how this should work, I would really appreciate it. Thanks! – Dmitriy Jul 29 '15 at 6:44

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.