In this post I will be showing you how to pack/compress and unpack/decompress files in Linux using a variety of utilities such as the tar command as-well as others.
Lets start by creating a simple .tar file, please keep in mind that this doesn’t really compress your files, rather it puts them all into a package. You can specify files and directories, either will work. To pack all the files in your directory you can use wildcards as-well, here is an example of packing files below.
root@teachmelinux:~/tutorial# tar -cvf files.tar ~/tutorial/file* tar: Removing leading `/' from member names /root/tutorial/file1.txt /root/tutorial/file2.txt
If you want to actually compress your files into a .tar.gz archive, then you can simply add a -z argument to the tar command. Here is an example of compressing files using the tar command.
root@teachmelinux:~/tutorial# tar cvzf files.tar.gz ~/tutorial/file* tar: Removing leading `/' from member names /root/tutorial/file1.txt /root/tutorial/file2.txt
Next lets create a simple zip file, you can do this by running the following zip command and substituting my information for what you want to zip.
root@teachmelinux:~/tutorial# zip -r files.zip ~/tutorial/file* adding: root/tutorial/file1.txt (stored 0%) adding: root/tutorial/file2.txt (stored 0%)
To extract the files out of a .tar.gz file you can use the tar command and substitute the file name for your desired file name.
root@teachmelinux:~/tutorial# tar xvzf files.tar.gz root/tutorial/file1.txt root/tutorial/file2.txt
To extract a simple tar file (not compressed, simply archived), you will also use the tar command except you will drop the z argument.
root@teachmelinux:~/tutorial# tar xvf files.tar root/tutorial/file1.txt root/tutorial/file2.txt
For zip files you can simply run the unzip command followed by the archives name, there are more options which can be viewed by running unzip –help.
root@teachmelinux:~/tutorial# unzip files.zip Archive: files.zip extracting: root/tutorial/file1.txt extracting: root/tutorial/file2.txt
I hope this post has helped you learn how to pack and unpack files in Linux, please don’t forget to like/comment/share. Thanks!