Tar: add all files and directories to the current directory. INCLUDING .svn etc.

I am trying to use tar.gz in a directory and use

tar -czf workspace.tar.gz * 

As a result, tar includes .svn directories in sub-directories, but NOT in the current directory (since * applies only to "visible" files before tar is passed

I tried

tar -czf workspace.tar.gz . but then I get an error because '.' while reading:

 tar: ./workspace.tar.gz: file changed as we read it 

Is there a trick for * match all files (including the prefix with the dot prefix) in the directory?

(using bash on Linux SLES-11 (2.6.27.19)

+46
linux bash design-patterns tar
Sep 06 '10 at 13:23
source share
14 answers

If you really don't want to include the top directory in the tarball (and this is generally a bad idea):

 tar czf workspace.tar.gz -C /path/to/workspace . 
+37
Sep 06 '10 at 13:30
source share

Do not create the tar file in the directory you are building:

 tar -czf /tmp/workspace.tar.gz . 

does the trick, except that it will extract files all over the current directory when unpacking. Better to do:

 cd .. tar -czf workspace.tar.gz workspace 

or, if you do not know the name of the directory in which you were:

 base=$(basename $PWD) cd .. tar -czf $base.tar.gz $base 

(It is assumed that you did not follow symbolic links to get to where you are, and that the shell does not try to guess you by jumping back over a symbolic link - bash not trustworthy in this regard. You need to worry about this, use cd -P .. to create a directory of physical changes. It's silly that this is not the default behavior on my view - confusing, at least for those for whom cd .. has never been an alternative value.)




One comment in the discussion says:

I [...] need to exclude the top directory, and I [...] need to put tar in the base directory.

The first part of the comment does not make much sense - if the tar file contains the current directory, it will not be created when the file is extracted from this archive, because by definition the current directory already exists (except for very strange circumstances).

The second part of the comment can be considered in one of two ways:

  • Or: create a file somewhere else - /tmp - one of the possible places - and then return it to its original location after its completion.
  • Or: if you use GNU Tar, use the --exclude=workspace.tar.gz option. The line after = is a pattern - an example is the simplest pattern - an exact match. You may need to specify --exclude=./workspace.tar.gz if you work in the current directory contrary to recommendations; you may need to specify --exclude=workspace/workspace.tar.gz if you work on the same level as suggested. If you have several tar files for exclusion, use ' * ', as in --exclude=./*.gz .
+37
Sep 06 '10 at 16:01
source share

in the directory you want to compress (current directory) try this:

 tar -czf workspace.tar.gz . --exclude=./*.gz 
+14
Oct 02 '14 at 2:13 on
source share

You can correct the form . using --exclude :

 tar -czf workspace.tar.gz --exclude=workspace.tar.gz . 
+6
06 Sep 2018-10-06T00:
source share

You can include hidden directories by returning the directory and doing:

 cd .. tar -czf workspace.tar.gz workspace 

Assuming the directory you want to use gzip was called the workspace.

+5
Sep 06 '10 at 13:26
source share

There are several steps:

  • Replace * with . to include hidden files.
  • To create an archive in the same directory, you can use --exclude=workspace.tar.gz to exclude the archive itself.
  • To prevent the tar: .: file changed as we read it error when the archive has not yet been created, make sure it exists (for example, using touch ), so -exclude matches the name of the archive file. (This does not match a file that does not exist)

Combined, this leads to the following script:

 touch workspace.tar.gz tar -czf workspace.tar.gz --exclude=workspace.tar.gz . 
+5
Sep 19 '16 at 9:21
source share

Actually, the problem is related to compression parameters. The trick is that the pipe results in a compressor instead of using the built-in options. By the way, this can also improve compression, as you can set additional compression options.

Minimum Resin:

 tar --exclude=*.tar* -cf workspace.tar . 

A pipe to the compressor of your choice. This example is detailed and uses xz with maximum compression:

 tar --exclude=*.tar* -cv . | xz -9v >workspace.tar.xz 

The solution was tested on Ubuntu 14.04 and Cygwin on Windows 7. This is a response from the wiki community, so feel free to edit if you notice an error.

+4
Aug 24 '14 at 17:12
source share

Update: I added a fix for the OP comment.

 tar -czf workspace.tar.gz . 

will really change the current directory, but why not put the file somewhere else?

 tar -czf somewhereelse/workspace.tar.gz . mv somewhereelse/workspace.tar.gz . # Update 
+3
Sep 06 '10 at 13:43
source share

Good question. In ZSH, you can use the globing (D) modifier, which stands for "dotfiles". For comparison:

 ls $HOME/* 

and

 ls $HOME/*(D) 

This correctly excludes special entries in the directory . and .. In Bash, you can use .* To enable dotfiles explicitly:

 ls $HOME/* $HOME/.* 

But that includes . and .. so this is not what you were looking for. I am sure there is a way to make * match dotfiles in bash too.

+1
Sep 06 '10 at 13:34 on
source share
 tar -czf workspace.tar.gz .??* * 

The .??* task will include β€œpoint” files and directories that have at least two characters after the period. The downside is that it will not include files / directories with one character after the .a , for example .a , if any.

0
Sep 06 '10 at 14:15
source share

If disk space is not a problem, it can also be very simple:

 mkdir backup cp -r ./* backup tar -zcvf backup.tar.gz ./backup 
0
01 Oct '15 at 9:54 on
source share

The problem with most of the solutions presented here is that tar contains ./ for begging each entry. Thus, this leads to the presence of a directory . when opening it through the GUI compressor. So what I ended up doing:

ls -1A | xargs -d "\n" tar cfz my.tar.gz

If you already have my.tar.gz in the current directory, you might want to do this:

ls -1A | grep -v my.tar.gz | xargs -d "\n" tar cfz my.tar.gz

Remember that xargs has a certain limit (see xargs --show-limits ). Therefore, this solution will not work if you are trying to create a package with a large number of entries (directories and files) in the directory that you are trying to use tar.

0
Mar 03 '16 at 12:41
source share

Another solution, assuming the number of items in the folder is not huge:

 tar -czf workspace.tar.gz `ls -A` 

( ls -A prints normal and hidden files, but not "." and "..", as ls -A does.)

0
May 24 '17 at 14:49
source share

Using find is probably the easiest way:

 find . -maxdepth 1 -exec tar zcvf workspace.tar.gz {} \+ 

find . -maxdepth 1 find . -maxdepth 1 will find all files / directories / symbolic links / etc in the current directory and run the command specified in -exec . {} in the list of command files means that here and \+ means that the command will be executed as:

 tar zcvf workspace.tar.gz .file1 .file2 .dir3 

instead

 tar zcvf workspace.tar.gz .file1 tar zcvf workspace.tar.gz .file2 tar zcvf workspace.tar.gz .dir3 
-one
May 23 '13 at 12:39
source share



All Articles