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.
Let’s 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.
[email protected]:~/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.
[email protected]:~/tutorial# tar cvzf files.tar.gz ~/tutorial/file* tar: Removing leading `/' from member names /root/tutorial/file1.txt /root/tutorial/file2.txt
Next let’s 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.
[email protected]:~/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.
[email protected]:~/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.
[email protected]:~/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.
[email protected]:~/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 comment/share. Thanks!