Linux Basics for the Aspiring Hacker: Archiving & Compressing Files « Null Byte :: WonderHowTo

When usingLinux, we often need to installnew software, a script, or numerous large files. To make things easier on us, these files are usually compressed and combined together into a single file with a .tar extension, which makes them easier to download, since it's one smaller file.As a Linux user, we need to understand how to decompress .tar files and extract what is in them. At the same time, we need to understand how to compress and combine these files into a .tar when we need to send files.Those of you who come from the Windows world (most of you), are probably familiar withthe .zip format. The .zip utility built into Windows lets you take several files and combine them into a single folder, then compress that folder to make it smaller for transferring over the internet or removable storage. The process we are addressing here in this tutorial is the equivalent of zipping and unzipping in the Windows world.Previously:Configuring Apache in LinuxWhat Is Compression?Compression is an interesting subject that I will have to expand upon in another tutorial at a later time. Basically, there are at least two categories of compression: lossy and lossless.Lossy compression loses the integrity of the information. In other words, the compressed/decompressed file is not exactly the same as the original. This works great for graphics, video, and audio files where a smaller difference in the file is hardly noticed (.mp3 and .jpg are both lossy compression algorithms).When sending files or software, lossy compression is not acceptable. We must have integrity with the original file when it is decompressed. That's the type of compression we are discussing here (lossless), and it is available from a number of different utilities and algorithms.(More on all of this in a later tutorial.)Step 1: Tarring Files TogetherIn most cases, when archiving or combining files, thetarcommand is used in Linux/Unix. Tar stands fortape archive, a reference to the prehistoric days of computing when systems used tape to store data. Tar is used to create one single file from many files. These files are then often referred to as an archive, tar file, or tarball.For instance, say we had three files, nullbyte1, nullbyte2, and nullbyte3. We can clearly see them below when we do along listing.kali > ls -lLet's say we want to send all three of these files to a friend. We can combine them together and create a single archive file by using the following command:kali > tar -cvf NB.tar nullbyte1 nullbyte2 nullbyte3Let's break down that command to better understand:taris the archiving command-cmeans create-vmeans verbose (optional)-fwrite or read from the following fileNB.taris the new file name we wantThis will take all three files and create a single file, NB.tar, as seen below, when we do another long listing of the directory.Please note the size of the tarball. When the three files are archived, tar uses significant overhead to do so. The sum of the three files before archiving was 72 bytes. After archiving, the tarball has grown to 10,240 bytes. The archiving process has added over 1,000 bytes. Although this overhead can be significant with small files, this overhead becomes less and less significant with larger and larger files.We can then display those files from the tarball by using the tar command, then the-tswitch to display the files, as seen below.kali > -tvf NB.tarWe can then extract those files from the tarball by using the tar command and then the-xswitch to extract the files, as seen above.kali > tar -xvf NB.tarFinally, if we want to extract the files and do so "silently," we can remove the-vswitch, and tar extracts the files without showing us any output.kali > tar -xf NB.tarStep 2: Compressing FilesTar is capable of taking many files and making them into one archived file, but what if want to compress those files, as well? We have three commands in Linux capable of creating compressed files:gzip(.tar.gz or .tgz)bzip2(tar.bz2)compress(.tar.Z)Theyallare capable of compressing our files, but they use different compression algorithms and have different compression ratios (the amount they can compress the files).Using GzipLet's try gzip (GNU zip) first, as it is the most commonly used compression utility in Linux. We can compress our NB.tar file by typing:kali > gzip NB.*Notice that I used the wild card (*) for my file extension meaning that the command should apply to any file that begins with "NB" with any file extension. I will use similar notation for the following examples. When we do a long listing on the directory, we can see that it has changed the file extension to .tar.gz, and the file size has been compressed to just 231 bytes!We can then decompress that same file by using thegunzip(GNU unzip) command.kali > gunzip NB.*When we do, the file is no longer saved with the .tar.gz extension, and has now returned to its original size of 10,240 bytes.Using Bzip2One of the other widely used compression utilities in Linux is bzip2. It works similarly to gzip, but with better compression ratios. We can compress our NB.tar file by typing:kali > bzip2 NB.*As you can see in the screenshot above, bzip2 has compressed the file down to just 222 bytes! Also note that the file extension now is .tar.bz2.To uncompress the compressed file, usebunzip2(b unzip 2).kali > bunzip2 NB.*When we do, the file returns to its original size and its file extension returns to .tar.Using CompressFinally, we can use the commandcompressto compress the file. This is probably the least commonly used compression utility, but it is easy to remember.kali > compress NB.*Note in the screenshot above that the compress utility reduced the size of the file to 395 bytes, almost twice the size of bzip2. Also note that the file extension now is .tar.Z (with a capital Z).To decompress the same file, useuncompressor thegunzipcommand.kali > uncompress NB.*Step 3: Untarring & Uncompressing VMware ToolsNow that we have a basic understanding of these tools, let's use them in a real world example. Many of you, including myself, usingVMware Workstationas a virtualization system. It allows you to run many operating systems on a single physical machine. No one does this better thanVMWare.If you are using VMware Workstation, you probably know that VMware encourages you to download and install itsVMware tools. When you install VMware tools, your guest operating system integrates much better into your host operating system, which includes better graphics performance, drag-and-drop capability, shared folders, and better mouse performance, among other things.Now that we have downloaded VMware tools, you can see that it is a tarball and compressed with gzip. We know this because it has file extension of .tar.gz. So, to decompress it and separate each of the files so that we can use them, we can use the following command:kali > tar -xvzf VMwareTools-.9.6.2-1688356.tar.gzLet's break down that command to better understand:taris the archiving command-xmeans extract-vmeans verbose output-zis used to decompress-fdirects the command to the file that followsWhen we hit enter, the compressed tarball is decompressed and extracted. Hundreds of files are extracted and decompressed from this VMware tarball. Now, we need to only run the script to install these tools.Archiving and compressing are key Linux skills that any hacker/administrator must understand when using Linux. We will continue to explorethe Linux basics in this series, so keep coming back, my aspiring hackers!Next Up:Managing Hard Drives in LinuxFollow Null Byte onTwitter,Flipboard, andYouTubeFollow WonderHowTo onFacebook,Twitter,Pinterest, andFlipboardWant to start making money as a white hat hacker?Jump-start your hacking career with our2020 Premium Ethical Hacking Certification Training Bundlefrom the newNull Byte Shopand get over 60 hours of training from cybersecurity professionals.Buy Now (90% off) >Other worthwhile deals to check out:97% off The Ultimate 2021 White Hat Hacker Certification Bundle99% off The 2021 All-in-One Data Scientist Mega Bundle98% off The 2021 Premium Learn To Code Certification Bundle62% off MindMaster Mind Mapping Software: Perpetual LicenseCover image byMcCarony/Shutterstock; Screenshots by OTW/Null ByteRelatedHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 5 (Installing New Software)How To:Create a ZIP Archive Using the Files App on Your iPhoneHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 11 (Apache Web Servers)News:How to Study for the White Hat Hacker Associate Certification (CWA)How To:Linux Basics for the Aspiring Hacker: Managing Hard DrivesHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 6 (Networking Basics)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 1 (Getting Started)How To:The Essential Skills to Becoming a Master HackerHow To:Linux Basics for the Aspiring Hacker: Configuring ApacheHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 2 (Creating Directories & Files)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 12 (Loadable Kernel Modules)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 22 (Samba)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 17 (Client DNS)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 21 (GRUB Bootloader)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 4 (Finding Files)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 10 (Manipulating Text)Hack Like a Pro:Linux Basics for the Aspiring Hacker, Part 25 (Inetd, the Super Daemon)How To:Archive and compress files with 7-zip softwareHack Like a Pro:Linux Basics for the Aspiring Hacker, Part 13 (Mounting Drives & Devices)How To:Linux Basics for the Aspiring Hacker: Using Start-Up ScriptsHack Like a Pro:Windows CMD Remote Commands for the Aspiring Hacker, Part 1Hack Like a Pro:Metasploit for the Aspiring Hacker, Part 10 (Finding Deleted Webpages)How To:Recover WinRAR and Zip PasswordsGoodnight Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingHow To:A Guide to Steganography, Part 2: How to Hide Files and Archives in Text or Image FilesCommunity Byte:HackThisSite Walkthrough, Part 6 - Legal Hacker TrainingHow To:Recover Deleted Files in LinuxGoodnight Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingGoodnight Byte:HackThisSite Walkthrough, Part 7 - Legal HackerGoodnight Byte:HackThisSite Walkthrough, Part 8 - Legal Hacker TrainingGoodnight Byte:HackThisSite, Realistic 3 - Real Hacking SimulationsHow To:Use Cygwin to Run Linux Apps on WindowsGoodnight Byte:HackThisSite Walkthrough, Part 9 - Legal Hacker TrainingNews:First Steps of Compiling a Program in LinuxCommunity Byte:HackThisSite Walkthrough, Part 10 - Legal Hacker TrainingCommunity Byte:HackThisSite Walkthrough, Part 5 - Legal Hacker TrainingHow To:How Hackers Take Your Encrypted Passwords & Crack Them