Archiving Hidden Directories with Tar

archiving hidden directories with tar

The answer is that the * wildcard is handled by the shell and it just does not expand to things that start with a dot. The other wildcard ? also does not expand to things that start with a dot. Thanks to Keith for pointing out it is the shell that does the expansion and so it has nothing to do with tar.

If you use shopt -s dotglob then expansion will include things like .filename. Thanks to Andy.

Use shopt -u dotglob to turn it off.

Switching the dotglob option does not change ls itself. Rather it just changes expansion behaviour as exhibited in something like ls *.

Edit: My recommendations are in a comment below.

How do I tar a directory of files and folders without including the directory itself?

cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd - 

should do the job in one line. It works well for hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:

$ mkdir my_directory
$ touch my_directory/file1
$ touch my_directory/file2
$ touch my_directory/.hiddenfile1
$ touch my_directory/.hiddenfile2
$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
$ tar ztf my_dir.tgz
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2

Tar Directory Contents Without Creating A Root Folder In Archive

While it doesn't affect the unpacking of the archive (since . just refers to the same dir), if it bothers you, you can change the wildcard from * to .[^.]* * to include all the hidden files as well.

In addition, if you have hidden files beginning with .., such as ..a, you'll need to add ..?* to the list as well.

Tar a directory, but don't store full absolute paths in the archive

tar -cjf site1.tar.bz2 -C /var/www/site1 .

In the above example, tar will change to directory /var/www/site1 before doing its thing because the option -C /var/www/site1 was given.

From man tar:

OTHER OPTIONS

-C, --directory DIR
change to directory DIR

Exclude hidden dot files with tar

Order matters with tar apparently!

While the command in the question didn't work, rearranging --exclude to be at the front did. Many of the guides I found online were either wrong in the example commands they gave or didn't specify, so I thought I would answer my own question when I figured it out.

tar --exclude=".*" -zcvf dist.tar.gz Foo/ Bar/ Buzz/

R tar() Method Excluding Hidden Files and Folders

This is the method I used to find out if a directory has hidden files and folders or not:

filesPresent <- function(object){
if(length(list.files(path = object@ExpRootDir, recursive = FALSE, all.files = TRUE)) - 2 != length(list.dirs(path = object@ExpRootDir, recursive = FALSE))) # all.files = TRUE for Checking Hidden Files, -2 for Excluding . and ..
return(TRUE)
else
return(FALSE)
}


Related Topics



Leave a reply



Submit