Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
Why do you not want the hidden directories? They're part of the user configuration as well. –  Ignacio Vazquez-Abrams Aug 22 '13 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 Aug 23 '13 at 1:17
    
[this is a very similar question][1]. the chosen answer is quite elegant and concise. [1]: unix.stackexchange.com/questions/83661/… –  sudoman Oct 16 '13 at 19:28
add comment

6 Answers

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.

share|improve this answer
    
At least on cygwin it does pick up /., so you might want to change that asterisk to a plus. –  Bobby Aug 22 '13 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 Aug 22 '13 at 21:43
    
not showing as code in the comments but you get the idea –  bjackfly Aug 22 '13 at 21:44
add comment

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.$$
share|improve this answer
    
This does not work if files have whitespace in their name. –  slhck Aug 22 '13 at 8:32
    
The find seems to include the dot directories when running –  bjackfly Aug 22 '13 at 20:27
    
It works for me here and now –  suspectus Aug 22 '13 at 21:02
add comment

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"
share|improve this answer
add comment

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.

share|improve this answer
    
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. Sep 5 '13 at 8:49
add comment

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.)

share|improve this answer
add comment

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 {} +

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.