0

I want to back up home directories including the usual array of dotfiles, but I do not want the directory itself, or its permissions to be backed up or even mentioned. So, if I have a structure like

homedir
    .dotfile
    somesubdir
        subfile
    outerfile

I want a tar that looks like this:

.dotfile
somesubdir
    subfile
outerfile

Note the absense of any reference to homedir, and not even a ./ to be seen.

I actually have a solution (posted below) but when I tried to tack it onto the question that made me think of it, some moderator said it needed its own question. So here it is, and I hope it's useful.

2 Answers 2

1

Assuming you are in the parent directory of homedir:

tar -c -f homedir.tar -C homedir .

This would make tar treat . as relative to homedir. The -C option basically acts like a cd into the given directory. The files and directories would be stored in the archive with a ./ path prefix.

To get rid of the ./ path prefix, using BSD tar,

tar -c -f homedir.tar -C homedir -s '/^\.\//' .

or, with GNU tar,

tar -c -f homedir.tar -C homedir --transform 's/^\.\//' .
0

Using Bash 4.4.19 and GNU tar 1.29, the following works. 'dotglob' makes * recognize dots as well as letters, while 'GLOBIGNORE' is set to ignore "." and "..". The curly braces allow me to direct the output to the current directory, while the pushd and popd make the tar happen where the stuff is. I don't use tar's -C option because that messes up the "*" glob.

shopt -s dotglob
GLOBIGNORE=".:.."
{
  pushd homedir >/dev/null
  tar --create --file=- *
  popd >/dev/null
} >inner.tar
shopt -u dotglob

note that the output file is set to "-" which means standard output which winds up going to the file inner.tar. The redirections to /dev/null silence the mention of the directory changes.

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.