tags:

views:

2872

answers:

6

I typically do:

tar -czvf my_directory.tar my_directory

What if I just want to include everything (including any hidden system files) in my_directory, but not the directory itself? I don't want:

my_directory
   --- my_file
   --- my_file
   --- my_file

I want:

my_file
my_file
my_file
+2  A: 
cd my_directory
tar zcvf ../my_directory.tar.gz *
mateusza
Hal explicitly asked about hidden files. You also need .??*.
PanCrit
-1: This doesn't add the hidden files to the tar. See tbman's answer.
dubek
A: 

You can also create archive as usual and extract it with:

tar --strip-components 1 -xvf my_directory.tar.gz
mateusza
A: 
tar -C my_dir -zcvf my_dir.tar.gz `ls my_dir`
mateusza
-1 The ls will fail when there are spaces in the file names.
Aaron Digulla
A: 

If it's a unix/linux system, and you care about hidden files (which will be missed by *), you need to do

cd my_directory
tar zcvf ../my_directory.tar.gz * .??*

(I don't know what hidden files look like under windows.)

PanCrit
+3  A: 
cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..

sould 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
tbman
This will also work on files with spaces or other special characters. Good job!
PanCrit
+1  A: 

Use the -C switch of tar:

tar -czvf my_directory.tar.gz -C my_directory .

The -C my_directory tells tar to change the current directory to my_directory, and then . means "add the entire current directory" (including hidden files and sub-directories).

dubek