I typically do:
tar -czvf my_directory.tar.gz 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
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
This will also work on files with spaces or other special characters. Good job!
Not perfect - tar file contains '.' and also ./file1 instead of just file1. I like the solution by mateusza below to use --strip-components when un-tarring.
@Ivan if you replace
.
with*
so the command will becd my_directory/ && tar -zcvf ../my_dir.tgz * && cd ..
then it will work as you expected.@jmathew You can also use a subshell so your current shell's working directory doesn't change:
$ (cd my_directory/ && tar -zcvf ../my_dir.tgz .)
I know it's an old answer but
cd
ing into directories and out is pretty lame. Could at least usepushd
andpopd
iftar
didn't have any flags like-C
.