How do I tar a directory of files and folders without including the directory itself?
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).
Make sure you do -C my_directory
before you do .
or else you'll get the files in the current directory.
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
You can also create archive as usual and extract it with:
tar --strip-components 1 -xvf my_directory.tar.gz