Linux 101: How to compress a folder from the command line with tar

3 years ago 448

At immoderate constituent successful your Linux journey, you'll request to beryllium capable to compress and decompress a folder from the bid line. Jack Wallen shows you how.

linuxhero3.jpg

Image: Jack Wallen

The Linux bid enactment is an incredibly almighty tool. From the CLI there's adjacent to thing you can't do. And though a GUI mightiness marque immoderate of the tasks a spot easier, the simplicity of doing your enactment successful a terminal model is astir arsenic elegant a solution arsenic you'll find. 

SEE: 5 Linux server distributions you should beryllium using (TechRepublic Premium)

Take, for instance, the task of compressing a folder. Sure, you could unfastened a record manager, right-click a folder, and prime Compress. But what if you're connected a headless server? A Linux without a GUI isn't going to connection you a point-and-click affair. So what bash you do? 

You crook to the tar command, which makes abbreviated shrift of converting folders into compressed files. 

Let maine amusement you however this is done. Let's accidental you person a folder named TEST and you privation to compress it into a azygous record truthful you tin much easy nonstop it to idiosyncratic oregon prevention it arsenic a backup. To bash this, log into your Linux instrumentality and alteration into the directory lodging the TEST folder. 

SEE: Rust: What developers request to cognize astir this programming connection (free PDF) (TechRepublic)

The bid we'll tally is tar -zcvf TEST.tar.gz TEST. 

The options we utilized are z (for compress), c (for create), v (for verbose output), and f (for force). 

After moving the command, you'll find the recently created TEST.tar.gz. The tar information of the hold means the record is simply a tar archive and the gz indicates it's been compressed. 

You could past decompress that caller record with the bid tar -xvzf TEST.tar.gz. 

If you wanted to presumption the files wrong that archive (without decompressing and extracting), you could contented the communal tar -ztvf TEST.tar.gz and tar would database retired the contents for you. 

And that's beauteous overmuch each you person to bash to compress a folder from the bid enactment with tar.

Open Source Weekly Newsletter

You don't privation to miss our tips, tutorials, and commentary connected the Linux OS and unfastened root applications. Delivered Tuesdays

Sign up today

Also spot

Read Entire Article