At some point in your Linux journey, you’ll need to be able to compress and decompress a folder from the command line. Jack Wallen shows you how.
The Linux command line is an incredibly powerful tool. From the CLI there’s next to nothing you can’t do. And although a GUI might make some of the tasks a bit easier, the simplicity of doing your work in a terminal window is about as elegant a solution as you’ll find.
SEE: 5 Linux server distributions you should be using (TechRepublic Premium)
Take, for instance, the task of compressing a folder. Sure, you could open a file manager, right-click a folder, and select Compress. But what if you’re on a headless server? A Linux without a GUI isn’t going to offer you a point-and-click affair. So what do you do?
You turn to the tar command, which makes short shrift of converting folders into compressed files.
Let me show you how this is done. Let’s say you have a folder named TEST and you want to compress it into a single file so you can more easily send it to someone or save it as a backup. To do this, log into your Linux machine and change into the directory housing the TEST folder.
SEE: Rust: What developers need to know about this programming language (free PDF) (TechRepublic)
The command we’ll run is tar -zcvf TEST.tar.gz TEST.
The options we used are z (for compress), c (for create), v (for verbose output), and f (for force).
After running the command, you’ll find the newly created TEST.tar.gz. The tar portion of the extension means the file is a tar archive and the gz indicates it’s been compressed.
You could then decompress that new file with the command tar -xvzf TEST.tar.gz.
If you wanted to view the files within that archive (without decompressing and extracting), you could issue the common tar -ztvf TEST.tar.gz and tar would list out the contents for you.
And that’s pretty much all you have to do to compress a folder from the command line with tar.