2

The following tar command will exclude all dot files and dot directories.

tar -cvzf /media/bjackfly/bkup/bkup.gz --exclude '.*' --one-file-system /home/bjackfly

In my case I want the dot files to be backed up in the home directory (.vimrc, .bashrc) etc. but not the dot directories /.config /.cache /.eclipse etc.

Any Linux gurus with a command for this, or do I need to run a find into a tar or do two different tar commands which is non-ideal? One for dot files in the home directory and one for everything else?

6
  • Why do you not want the hidden directories? They're part of the user configuration as well. Commented Aug 22, 2013 at 6:12
  • Mainly because I don't know how to ghost or restore a linux image. I would like to learn though. In the past I just back up all my data files and dot files like .vimrc etc and then usually have to go through the pain of reinstalling the OS the apps and all the tools like eclipse etc. which is never fun. If you have some advice on how to restore ubuntu linux as an image that would be great
    – bjackfly
    Commented Aug 23, 2013 at 1:17
  • [this is a very similar question][1]. the chosen answer is quite elegant and concise. [1]: unix.stackexchange.com/questions/83661/…
    – andrew-e
    Commented Oct 16, 2013 at 19:28
  • @IgnacioVazquez-Abrams That's not relevant to his question.
    – felwithe
    Commented Mar 24, 2018 at 11:38
  • @felwithe: It is if it will leave 90% of their profile behind. Commented Mar 24, 2018 at 13:20

6 Answers 6

2

To zip up dot files in your home directory:

$ cd
$ find . -maxdepth 1 -type f -name ".?*" -exec tar czfv dotfiles.tgz {} +

If you want to include directories:

$ find . -maxdepth 1 -name ".?*" -exec tar czfv dotfiles.tgz {} +

The ? is optional in the first command, but not in the second, otherwise, find will include the '.' directory.

(inspiration taken from here.)

0
1

I don't know how to do this in one go, but you can run your command and create a tar file, then use the find command to append the missing . directories.

Something like $ find -type d -regex '\..+' -exec tar -r {} /media/bjackfly/bkup/bkup.gz \;

I have not tested this command as I am sitting on a Windows machine and does not have access to a shell, but you should be able to do this using this command or a variance of it. I am not sure if this command will pick up the . and .. directories to.

Cheers.

3
  • At least on cygwin it does pick up /., so you might want to change that asterisk to a plus.
    – Bobby
    Commented Aug 22, 2013 at 7:04
  • Thanks I ended up creating 2 separate files but I am going to try your way to just append. # back up other files exluding dot dir tar -cvzf "$BACKUPDIR/$BKUPFILE" --exclude '.*' --one-file-system "$SRCDIR"` # back up other files exluding tar -cvzf "$BACKUPDIR/$BKUPFILE" --exclude '.*' --one-file-system "$SRCDIR"
    – bjackfly
    Commented Aug 22, 2013 at 21:43
  • not showing as code in the comments but you get the idea
    – bjackfly
    Commented Aug 22, 2013 at 21:44
1

Using find logical operators -and and -or to select all files and directories which do not begin with '.':

tar -cvzf /media/bjackfly/bkup/bkup.gz --one-file-system $(find /home/bjackfly -type f -or -type d -and -not -name '.*' )

Edit slhck commented the above solution does not handle file names containing whitespace. This is one solution, perhaps someone knows a more elegant technique?

find . -type f -or -type d -and -not -name '.*' >/tmp/tar.$$
tar -cvzf /media/bjackfly/bkup/bkup.gz --one-file-system /home/bjackfly -T /tmp/tar.$$
rm /tmp/tar.$$
3
  • This does not work if files have whitespace in their name.
    – slhck
    Commented Aug 22, 2013 at 8:32
  • The find seems to include the dot directories when running
    – bjackfly
    Commented Aug 22, 2013 at 20:27
  • It works for me here and now
    – suspectus
    Commented Aug 22, 2013 at 21:02
0

so the real trick here is to create a regular .tar file and then use a find piped into another tar using the -u option to add or update the .tar and then just zip it up afterwards. Thanks to Mogget who had the basic idea.

#!/bin/bash
BKUPDIR="/media/bjackfly/backupDisk/bkup"
BKUPFILE=$(/bin/date +"%Y%m%d").bkup
SRCDIR=/home/bjackfly


tar -cvf "$BKUPDIR/${BKUPFILE}.tar" --exclude '.*'  --one-file-system "$SRCDIR"
find $SRCDIR -maxdepth 1 -mindepth 1 -type f -name '.*'  ! \( -name .Xauthority.* \) | tar -uvf "$BKUPDIR/$BKUPFILE.tar" -T -
gzip "$BKUPDIR/$BKUPFILE.tar"
0

No, not that. If you want backup all files in the flat directory structure without recursion, use:

tar cvf backupfile.tar -C /specify/dir --no-recursion '.*'

unfortunately this backup dotted files with dotted directories. good news is, directory contents will not be backed up.

Otherwise if file names does not have new line chars you can use:

cd /some/dir
find ./ -minlevel 1 -maxlevel 1 -type f -name '.*'|tar cvf backupfile.tar -T - --no-recursion

look for -T option. with argument '-' this get file list from his stdin. list is generated by find command. This approach is more better because you are not limited by maxumum command length, like examples given by others, for example tar ....something $(find) or find ...| xargs ... tar ..something... {} that put full file list as tar command args. this can overflow command size limit.

option --no-recursion is in this case only for security. this cause, when tar get some directory, tar doesn't step for this directory structure. it's very useful in conjunction with find.

In this approach you can run (as dry without backup making) command find and you will see what files it takes.

1
  • Note that you could use the options -print0 on find and --null on tar to allow newlines in file names in the second command.
    – Dan D.
    Commented Sep 5, 2013 at 8:49
0

1.
find ./target-dir -type d -name '.?*' -prune -o -print0 | tar --null --no-recursion -cvzf backup.tar.gz -T -

OR

2.
find ./target-dir -type d -name '.?*' -prune -o -print0 -exec tar --null --no-recursion -cvzf backup.tar.gz -T {} +

You must log in to answer this question.

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