0

I am working on bash to create a back up system. My code is

  #!/bin/bash
  if [ ! -d "BackUp" ]
  then
    mkdir BackUp
  fi

  echo "enter number of access days you want to take for back up."
  read days

  bak="$(find . -mtime +$days)"

  for file in $bak
  do
    mv $file BackUp
  done

  tar -cvf BackUp.tgz BackUp >> backUp.log

So, currently I am only taking log file from tar. so it does not prints the full path it only takes current working directory for text in log file.My last line of code takes up input for log file.

But the path stored is

 .BackUp/foo1
 .BackUp/foo2
 .BackUp/foo3

instead i want it to be

 home/ubuntu/Downloads/BackUp/foo1
 home/ubuntu/Downloads/BackUp/foo2
 home/ubuntu/Downloads/BackUp/foo3
  • Not really sure I understand your question. You want to get full path of what, to what purpose? https://stackoverflow.com/questions/5265702/how-to-get-full-path-of-a-file ? – mattias Oct 20 '17 at 18:33
  • to store it in log file –  Oct 20 '17 at 18:38
  • 1
    What is _it_? What line of code in your script is it you're not satisfied with? – mattias Oct 20 '17 at 18:40
  • 1
    @Nikul You are only pointing to your local path for finding files and for moving files and for compressing. Can you clarify your question? – Daniel Gale Oct 20 '17 at 18:41
  • @mattias currently I am taking input of log file from last line of code which shows which files are getting compressed. –  Oct 20 '17 at 18:47
  • @Daniel I did some editing for clarification –  Oct 20 '17 at 18:50
  • @Nikul try using the full path in the tar command tar -cvf BackUp.tgz /home/ubuntu/Downloads/BackUp >> backUp.log – Daniel Gale Oct 20 '17 at 18:55

1 Answers1

0

You could store the absolute path in a variable and use it in the tar command:

BackUpDirFullPath=$(cd BackUp && pwd)

As command substitution invokes a subshell you are not leaving the current directory by executing cd.

Update:
In order to make -v output absolute paths (on Mac OS) I had to change to the root directory in a subshell and execute it from there ... something like that:

(cd / && tar -cvf /$OLDPWD/BackUp.tgz $BackUpDirFullPath)

This does output absolute paths ... in order to preserve the leading / you might try -P which preserves path names.

arne
  • 374
  • 1
  • 8