When it comes to creating backups, packaging software source code for distribution, and managing files in Linux, the tar command is no doubt one of the widely used archiving utilities.
Learn more in this guide:
Tar command which is abbreviated as tape archive is used to group files into archives called tarballs and also compress files using popular compression algorithms such as gzip, bzip2, and xz.
Common Options
The picture below lists the most common options you can use with the tar command to perform different operations.
tar Command Operations
Now let's look at some common operations you can do with the tar command:
1. Create
The -c option can be used to combine multiple files into an archive called a tarball. Here is an example of creating archives using the tar command:
$ tar -cf archive.tar file1 file2
2. Compress
Compressing is different from creating an archive in the sense that archiving uses the same amount of disk space as all the individual files and directories combined, whilst compression reduces the size.
The option for compressing files depends on the type of algorithm you want to use. To create and compress an archive using gzip use the -z option:
$ tar -czf archive.tar.gz file1 file2
Tar offers a good option, -a, that intelligently determines the compression algorithm from the specified archive suffix or extension. Compressing an archive using the xz compression algorithm, for example, you would use:
$ tar -caf archive.tar.xz file1 file2
3. Extract
The -x option can be used for extracting or uncompressing archives. Here is an example of extracting an archive in the current working directory:
$ tar -xf archive.tar
If you want to extract an archive to a specific directory use the -C option followed by the directory where you want the files to be extracted:
$ tar -xf archive.tar -C directory-path
4. Concatenate
If you have two or more archives you can concatenate or combine them using the -A option:
$ tar -Af archive.tar archive2.tar
Tar command gives you the ability to add more files to an already existing archive without having to extract and archive the files again. To append files to an archive use the -a option or -r option:
$ tar -rf archive.tar file_to_append
$ tar -af archive.tar file_to_append
5. List
The -T option can be used to list the contents of an archive. This option is very handy if you just want to have a peek at a large archive without extracting it:
tar -tf archive.tar
Summing up!
This thread demonstrated how to use the tar command with practical examples and some of its available options that can help you to perfom different operations.
If you get stuck with this command, feel free to refer to the man pages or the command help menu.
That’s all!
Hope you learn something new from this thread? If so, please let us know by replying in the comments.
If you're new here, do toss us a follow us (@linuxopsys) for more threads, tips and resources on Linux.
Also join 1000+ subscribers inside our newsletter "PenguinChronicles " and never miss any Linux, sysdmin and DevOps content:
In addition to UGO/RWX permissions and ACLs (Access Control Lists), Linux uses file and directory attributes to control the level of access that system programs and users have to files.
Let’s dive in!
File and directory attributes can be set and removed using different commands and utilities. Let's explore some commonly used attributes and how to manage them.
1. Immutable Attribute:
The immutable attribute prevents a file or directory from being modified, renamed, or deleted, even by the root user. It provides an extra layer of security to critical system files or sensitive data.
pushd and popd are very underutilized commands, yet incredibly powerful.
These cmds give you the ability to manage your directory stack and easily switch between directories, making them a must-have tool for any Linux user looking to maximize their efficiency and productivity
In this thread, I'll show you how to use the pushd and popd commands to unlock the power of easy system directory navigation and streamline your workflow.
First let's understand what a Directory Stack is?
The Directory Stack is an ordered list of directories that have been accessed during navigation.
The contents of the Directory Stack can be viewed through the execution of the dirs command.
If you want to learn Linux bash scripting for FREE, open this:
A collection of comprehensive bash scripting guides.
1. What is Shell in Linux
In this guide, let's learn about the shell which is an important part of the Linux operating system which interprets and passes the commands you provide on your keyboard to the operating system to perform some specific tasks
A variable is a named storage location in a program's memory where a value can be stored, retrieved, and manipulated. Like any programming language, Variables are an essential component in the Bash scripts.
If you want to learn Linux for FREE, open this (a thread of our Linux threads):
1. Permissions
The operating system controls file access in Linux by utilizing file permissions, attributes, and ownership. In Linux, file permissions, attributes, and ownership determine the level of access that system programs and users have to files.
Most people who know Git think of it as a software project repository. It's actually more than that. It's useful for any type of file, but especially for config files that are updated regularly.
Git is a valuable tool not for only programmers, but also for sysadmins:
Using git can simplifies the administrator's life in a variety of ways.
Learn these 23 Git commands as you'll be using them 99% of the time as a system administrator:
1. git add
Used to add files to the staging area. Before a file is available to commit to a repository, the file needs to be added to the staging area also know as git index.
The Linux sed command is a stream editor that is used to process text file content like searching for patterns, finding and replacing, insertion, and deletion.
Now that you understand what sed is, let's look at some of the things you can do with it.