0

I am running the following snippet in a bash script in a folder. This creates an archive where there is no root folder since I am running the script within the folder whose contents I am archiving.

tar -pczf $ARCHIVE_NAME --exclude=${ARCHIVE_NAME} --exclude=$(basename ${0}) *

Archive Contents:
/a/
/b/
/c/
/a_normal_file

works great, but since I am using the * it is not archiving hidden files in the immediate directory. I did some searching and found archiving (ubuntu tar) hidden directories I have then changed it to:

tar -pczf $ARCHIVE_NAME --exclude=${ARCHIVE_NAME} --exclude=$(basename ${0}) .

Archive Contents:
/./a/
/./b/
/./c/
/./.a_hidden_file
/./a_normal_file

still works, but now there is a root folder being created with the name being the '.' character. The contents are then within that. How do I get my previous result, but where it still archives hidden files in the immediate directory?

Thanks in advance!

Community
  • 1
  • 1
kittycat
  • 14,983
  • 9
  • 55
  • 80

2 Answers2

4

Use -C, --directory DIR

--directory=$(dirname ${0})

Example:

$ ls -a xxx
.  ..  aaa  .bbb
$ tar cvf test.tar --exclude test.tar -C xxx .
./
./.bbb
./aaa
csgwro
  • 181
  • 2
  • 5
  • some explanation will help this answer – dove Nov 08 '12 at 10:31
  • I edited the answer adding an example. "-C" parameter changes the current working directory before listing files to archive (so the files have relative paths). "$(dirname ${0})" is a directory in which the script was run (I think author wanted to archive files inside that directory). – csgwro Nov 09 '12 at 09:38
1

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.

Joakim Nohlgård
  • 1,832
  • 16
  • 16