We often need to tar all the files under a directory into a zip file. This post gives two examples to show how to tar all the contents under a directory into a tgz file.
Suppose we have directory structures like
|
my_folder --- file1 --- file2 --- file3 |
By running the following command, we can tar all the files of my_folder into the tgz file.
|
tar -czvf my_folder.tgz my_folder |
However, you will find that you actually include the my_folder in the zip file. The structure of the tgz file will looks like this.
|
my_folder --- file1 --- file2 --- file3 |
What if you want the file structures like this:
Two methods tar all the files under a directory into a tgz file
You can use the following command to include only the files under my_folder to the zip file:
|
cd my_folder tar -zcvf ../my_folder.tgz . cd .. or cd my_folder/ && tar -zcvf ../my_folder.tgz . && cd .. |
Using the -C option to tar all the files under a directory into a tgz file
You can also using the -C option to include all the files under your directory into a zip file:
|
tar -czvf my_folder.tar.gz -C my_folder . |
The -C my_folder tells tar to change the current directory to my_folder,
[Read More...]