- How to tar a Folder in Linux?
- How to tar a Folder in Linux?
- Append a tar Folder in Linux
- View a tar Folder in Linux
- Extract a tar Folder in Linux
- Conclusion
- Tar in Linux – Tar GZ, Tar File, Tar Directory, and Tar Compress Command Examples
- Syntax
- Usage Examples
- How to compress a whole directory (including subdirectories) using TAR in Unix based OS with the CLI
- Extracting generated file
- Extracting content to a custom directory
How to tar a Folder in Linux?
Linux offers a pre-installed ‘tar (tape archive)’ command utility tool. It is used for the compression and extraction of “.xz”, “gz”, “.tbz”, and “.bz2” extensions. It is beneficial to compress large folders and files into an archive file. The compressed folders take less storage to transfer from one system to another.
This post pens down brief details to tar a folder in Linux with the following highlights:
How to tar a Folder in Linux?
The creation of a tar folder depends on its basic syntax which is defined below:
The syntax contains the following parameters:
- tar: Represents the “tar” command line utility.
- c: Creates the required tar file.
- f: Specifies the created archive file name.
- tar: Identifies the compressed tar archive.
- Directory/Folder to Archive: Shows folder name that needs to be compressed.
By following the above syntax, tar the “Extra” directory having the following files and subdirectories:
Example 1: Create a “.tar” Folder
Create the “ComExtra.tar” simple compressed tar archive of “Extra” directory using the above “tar” command syntax:
The “Extra” has been successfully compressed into a “ComExtra.tar” file.
Example 2: Create a “tar.gz” Folder
The “tar” command also supports the commonly used gzip compression format having “tar.gz”, “tgz”, and “tar.gzip” extensions. For this purpose, “tar” offers the “-z (gzip format)” flag.
Now we compressed the “Extra” folder into the “tar.gz” compression format using the command defined below:
Append a tar Folder in Linux
Once the “tar” folder is created, it can easily be customized or extended without extraction.
Use the “r(append)” flag of the “tar” command to append the “Extra.tar” folder by adding three files on its end:
The “three” desired files have been added into the “ComExtra.tar” folder that can be seen using the “-v(verbose)” flag.
View a tar Folder in Linux
All the tar files and folders can be displayed in the terminal by using the “-t(list tar content)” flag in this way:
The output is the same as the output from the “-v” flag.
Extract a tar Folder in Linux
The “tar” command also supports the extraction of “tar” files/folders into the current working directory or the specific directory.
The basic syntax to extract the “tar” file is written here:
The “x” flag denotes the extraction of the tar archive.
Example 1: Extract “.tar” Folder in PWD
To extract the “tar” folder in the current working directory simply utilize the above syntax and specify the compressed “ComExtra.tar” folder in the terminal:
The compressed “ComExtra.tar” folder has been extracted.
Example 2: Extract the “tar.gz” Folder in a Specific Directory
The “-C(–directory)” flag is beneficial to extract the desired tar folder in the particular location where the user wants.
In this example, the “GzipExtra.tar.gz” compressed archive located in the “Home” is extracted into the “Sample” directory in this way:
The output verifies that “GzipExtra.tar.gz” has been completely extracted into the “Sample” directory.
Example 3: Extract Specific File From “tar” Folder
Simple type the name of the specified file/subdirectory that you want to extract from the “tar” folder with the “tar” utility in the following way:
The “Dir1” directory has been extracted from the “GzipExtra.tar.gz” compressed file.
Conclusion
To tar a folder in Linux, users can execute the “tar cf [File_name.tar] [Directory/Folder to Archive]” command. Using this, you can compress and decompress the tar folder. Additionally, users can append, view, and extract a tar Folder in Linux. This post has demonstrated a set of practical examples that illustrates the creation and extraction of a tar folder in Linux.
Tar in Linux – Tar GZ, Tar File, Tar Directory, and Tar Compress Command Examples
Do you want to combine a bunch of files and directories into a single file? The tar command in Linux is what you’re looking for!
The tar command is used to compress a group of files into an archive. The command is also used to extract, maintain, or modify tar archives.
Tar archives combine multiple files and/or directories together into a single file. Tar archives are not necessarily compressed but they can be. Permissions are preserved and it supports many compression formats.
Learn how to use tar in this quick article.
Syntax
tar [options] [archive-file] [file or directory to be archived]
Options:
-c : Creates archive
-x : Extracts the archive
-f : creates archive with given filename
-t : displays or lists files in archived file
-u : archives and adds to an existing archive file
-v : Displays verbose information
-A : Concatenates the archive files
-z : compresses the tar file using gzip
-j : compresses the tar file using bzip2
-W : Verifies an archive file
-r : updates or adds file or directory in already existing .tar file
Usage Examples
Extract an archive:
tar xfv archive.tar
(Options: x = extract, f = file, v = verbose)
Create an archive with files or folder:
tar cfv archive.tar file1 file2 file3
(Options: c = create)
Create compressed archives:
tar cfzv archive.tar file1 file2 file3
(Options: z = compress with gzip)
Show all files of an archive:
tar tvf archive.tar
Create an uncompressed archive of all .txt files in current directory:
tar cfv archive.tar *.txt
Extract files from gzip tar Archive archive.tar.gz:
tar xvzf archive.tar.gz
Create a compressed tar archive file using bzip2:
tar cvfj archive.tar.tbz example.cpp
(Options: j = compress with bzip2, smaller file size but takes longer than -z )
Update existing tar file by adding todo.txt file to archive:
tar rvf archive.tar todo.txt
(Options: r = add file)
List contents of tar file:
tar tf file.tar
(Options: t = display, f = file)
Create a compressed archive of current directory but exclude certain directories:
tar —exclude=’./folder’ —exclude=’./upload/folder2′ cfzv archive.tar . («folder» and «folder2» are excluded)
I’m a teacher and developer with freeCodeCamp.org. I run the freeCodeCamp.org YouTube channel.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
How to compress a whole directory (including subdirectories) using TAR in Unix based OS with the CLI
Learn how to quickly compress an entire directory into a single tar file with the command line.
Many modern Unix systems, such as Linux, use GNU tar, a version of tar produced by the Free Software Foundation. TAR stands for tape archiving, the storing of entire file systems onto magnetic tape, which is one use for the command. The most common use for tar is to simply combine a few files into a single file, for easy storage and distribution. If your system uses GNU tar, you can easily use gzip (the GNU file compression program) in conjunction with tar to create compressed files with the command line following the next command syntax:
tar -zcvf [result-filename.tar.gz] [path-of-directory-to-compress]
The -zcvf instruction stand for:
- -z : Compress the desired file/directory using gzip
- -c : Stand for create file (output tar.gz file)
- -v : To display the progress while creating the file
- -f : Finally the path of the desire file/directory to compress
For example, let’s suppose that you are located in the command line in the /www directory and inside this directory there is a folder that you want to compress into a single file namely «sandbox». To compress the sandbox folder into a tar file you would run the command as:
tar -zcvf sandbox_compressed.tar.gz sandbox
This should start the compression of the directory into a new tar file, as we have our -v argument to display the progress in the console, you would see a list of every filename that is being added to the file.
Extracting generated file
If you want now to extract as well with the command line the generated file, you should use now the following command syntax:
tar -xvzf [your-tar-file.tar.gz]
The -zcvf instruction stands for:
- -z : Compress the desired file/directory using gzip
- -x : Stand for extract file (input tar.gz file). If any files are named on the command line, only those files will be extracted from the archive.
- -v : To display the progress while creating the file
For example, to extract our previously created file you should use the command:
tar -xvzf sandbox_compressed.tar.gz
That will extract the content of the file (sandbox folder) in the current directory.
Extracting content to a custom directory
If you need to extract the content of the generated tar to a custom directory instead of the current directory, you can specify the new directory where the content should be extracted using the -C or —directory option in your extraction command:
tar -xvzf sandbox_compressed.tar.gz -C /target/directory