Compressing with Tar
Tar files place several files or the contents of a directory or directories in one file. This is a good way to create backups and archives. Usually, tar files end with the .tar extension.
To create a tar file, type:
tar -cvf filename.tar files/directories |
In this example, filename.tar represents the file you are creating and files/directories represents the files or directories you want to put in the new file.
You can use absolute or relative pathnames for these files and directories (for more on pathnames. Separate the names of files and directories with a space.
The following input would create a tar file using absolute pathnames:
tar -cvf foo.tar /home/mine/work /home/mine/school |
The above command would place all the files in the /work subdirectory and the /school subdirectory in a new file called foo.tar in the current working directory.
The command tar -cvf foo.tar file1.txt file2.txt file3.txt would place file1.txt, file2.txt and file3.txt in a new file called foo.tar.
To list the contents of a tar file, type:
tar -tvf foo.tar |
To extract the contents of a tar file, type:
tar -xvf foo.tar |
This command does not remove the .tar file, but it places copies of the .tar contents in the current working directory.
The tar command does not compress files automatically. You can compress tar files with:
tar -czvf foo.tar |
Compressed tar files are conventionally given the extension .tgz and are compressed with gzip.
To expand a compressed tar file type:
tar -xzvf foo.tgz
Compressing with Gzip and Zip
Gzip
Type the following command to compress a file at a shell prompt
gzip filename.ext -> The file will be compressed and saved as filename.ext.gz
Type the following command to expand a compressed file
gunzip filename.ext.gz -> The filename.ext.gz is deleted and replaced with filename.ext
Type the following command to compress multiple files at a shell prompt
gzip filename.gz file1 file2 file3 -> will compress file1, file2, file3 and put them in filename.gzip.
Zip
Type the following command to compress a file at a shell prompt
zip -r filename.zip files
filename represents the file you are creating and files represents the files you want to put in the new file
Type the following command to expand a compressed file
unzip filename.zip
Type the following command to compress multiple files at a shell prompt
zip filename.zip file1 file2 file3
will compress file1, file2, file3 and put them in filename.zip
If you add directory to multiple files
zip filename.zip file1 file2 file3 /user/work/school
will compress file1, file2, file3, the contents of the /user/work/school directory and put them in filename.zip
How To Tar.gz
If you are in the same directory as where your files / folders are placed, then you dont have to specify the file location. i.e. /usr/home/bleh/bleh
This is how to make a .tar.gz from a Folder
tar czf /path/to/output/folder/filename.tar.gz /path/to/folder
i.e if you are in /path/to/output/ and you want to put the file in /path/to/output/ you can use the following command.
tar czf filename.tar.gz folder
This is how to extract a .tar.gz file
gunzip -c /path/to/folder/filename.tar.gz
or like i do…
tar -zxvf /path/to/folder/filename.tar.gz
i.e if you are in /path/to/folder/ You can do the following…
tar -zxvf filename.tar.gz
View a list of all files in a .tar.gz archive
gunzip -c /path/to/folder/filename.tar.gz | tar -tvf -
i.e if you are in /path/to/folder/ You can do the following…
gunzip -c filename.tar.gz | tar -tvf -
Extract a single file from a .tar.gz file
gunzip -c /path/to/folder/filename.tar.gz | tar -xvf - path/within/archive/filename.php
i.e if you are in /path/to/folder/ You can do the following…
gunzip -c filename.tar.gz | tar -xvf - path/within/archive/filename.php
You can also add a date stamp to the tarball by using the following example below…
tar -czf public_html-`date +%d%m%y`.tar.gz public_html
Bagi Yang Baru belajar /