Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tar: add all files and directories in current directory INCLUDING .svn and so on

I try to tar.gz a directory and use

tar -czf workspace.tar.gz * 

The resulting tar includes .svn directories in subdirs but NOT in the current directory (as * gets expanded to only 'visible' files before it is passed to tar

I tried to

tar -czf workspace.tar.gz . instead but then I am getting an error because '.' has changed while reading:

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

Is there a trick so that * matches all files (including dot-prefixed) in a directory?

(using bash on Linux SLES-11 (2.6.27.19)

like image 578
Micha Avatar asked Sep 06 '10 13:09

Micha


People also ask

How do I change a directory to a tar file?

To change the working directory in the middle of a list of file names, either on the command line or in a file specified using ' --files-from ' (' -T '), use ' --directory ' (' -C '). This will change the working directory to the specified directory after that point in the list.


1 Answers

Don't create the tar file in the directory you are packing up:

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

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

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

or, if you don't know the name of the directory you were in:

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

(This assumes that you didn't follow symlinks to get to where you are and that the shell doesn't try to second guess you by jumping backwards through a symlink - bash is not trustworthy in this respect. If you have to worry about that, use cd -P .. to do a physical change directory. Stupid that it is not the default behaviour in my view - confusing, at least, for those for whom cd .. never had any alternative meaning.)


One comment in the discussion says:

I [...] need to exclude the top directory and I [...] need to place the 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 won't be created when you extract file from that archive because, by definition, the current directory already exists (except in very weird circumstances).

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

  1. Either: create the file somewhere else - /tmp is one possible location - and then move it back to the original location after it is complete.
  2. Or: if you are using GNU Tar, use the --exclude=workspace.tar.gz option. The string after the = is a pattern - the example is the simplest pattern - an exact match. You might need to specify --exclude=./workspace.tar.gz if you are working in the current directory contrary to recommendations; you might need to specify --exclude=workspace/workspace.tar.gz if you are working up one level as suggested. If you have multiple tar files to exclude, use '*', as in --exclude=./*.gz.
like image 54
Jonathan Leffler Avatar answered Oct 10 '22 19:10

Jonathan Leffler