So I am a making a simple backup script, it will be run every hour. All this script does, is make a backup directory and copy files:


backup structure

- backup
    - '$HOME'
        - Documents
             - todo.txt
        - Pictures
    - etc
        - ....

I keep the home variable in the name of the folder so it's not bound to one user.

Now, I am trying to make a restore script, which copies all the files in the backup folder, but my problem is, when I echo the structure, the $HOME part is not parsed and echoed directly:

What it echos as:


What I want it to echo as


The code I am using to loop over each file in the backup file and try and copy the file to the location:

for file in $(find backup -type f); do
    echo "$file" | cut -c 7- | xargs -I {} cp $file {} 

But, of course, this won't work, because the $HOME folder in my backup folder is not parsed to a variable. So is it possible to parse variable in a string to make them act like variables?

1 Answer 1


Some hints:

  • Use eval to interpret the variable.
  • Don't loop over find results, rather use find -exec.
  • Use bash substring removal instead of cut.
  • Use rsync instead of cp.
  • Create directories before copy.
find backup -type f -exec sh -c '
    mkdir -p "$(eval dirname "${1#*/}")";
    rsync -av "$1" "$(eval printf "${1#*/}")"
' find-sh {} \;

Or better just use rsync in the first place which does exactly what you want:

rsync -av backup/\$HOME/ $HOME/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .