Linux tar directory and files

Tar in Linux – Tar GZ, Tar File, Tar Directory, and Tar Compress Command Examples

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.

Читайте также:  Linux sed replace substring

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Источник

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

Читайте также:  Linux информация процессе pid

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.

Источник

How to Tar a Directory and Subdirectories in Linux

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 11 people, some anonymous, worked to edit and improve it over time.

This article has been viewed 108,031 times.

The most common way to deliver a batch of files from a Linux system to another user is by using the tar command. When you tar a directory, you can easily roll up a group of files into a single file that’s easy to share. The tar file, also known as a tarball, can then be transferred, stored, or compressed to reduce its size.

  • To tar a directory on Linux, use tar -cvf filename.tar /path/to/directory.
  • All subdirectories will be included in the tarball.
  • To tar multiple directories into a single tarball, append other directory names to the command after a space.

Image titled 865895 1 1

  • The tar command will only archive the files. It will not perform any compression, so the archive will be the same size as the original files. You can compress the .tar file using gzip or bzip2 , resulting in a .tar.gz or .tar.bz2 extension. This will be covered at the end of the article.

Image titled 865895 2 1

Create a tarball from a single directory. There are several parts to the tar command when you are creating a tarball from a directory. Below is an example tar command:

tar -cvf tarName.tar /path/to/directory
  • tar — This invokes the tar archiving program.
  • c — This flag signals the «creation» of the .tar file. It should always come first.
  • v — This indicates that the process is «verbose». This will display a readout of all the files that get added to the .tar file as it is being created. This is an optional flag.
  • f — This flag signifies that the next part will be the new .tar file’s file name. It should always be the last flag.
  • tarName.tar — You can choose any name that you’d like. Just make sure that you include the .tar extension at the end. You can add a path to the file name if you want to create the tarball in a different directory than your current working one.
  • /path/to/directory — Enter in the path of the directory that you want to create the .tar file from. The path is relative to your current working directory. For example, if the full path is ~/home/user/Pictures , and you’re currently in the /home directory, you would enter user/Pictures . Note that all subdirectories will be included as well.
Читайте также:  Microsoft teams install linux

Image titled 865895 3 1

Create a tarball that includes multiple directories. Adding multiple directories is pretty much as simple as adding all the paths to the end of the tar command:

tar -cvf tarName.tar /etc/directory1 /var/www/directory2

Image titled 865895 4 1

Add files or directories to an existing tarball. You can continue to add files and directories to your .tar archive files by using the «append» flag: [1] X Research source

tar -rvf tarName.tar textfile.txt path/to/another/directory

Image titled 865895 5 1

Compress an existing .tar file. You can use «gzip» to quickly compress your .tar archive file. If you need more compression (smaller output file), you can use «bzip2» instead. bzip2 will take longer to compress the file than gzip.

gzip tarName.tar bzip2 tarName.tar
  • gzip will add the .gz extension to the file name: tarName.tar.gz
  • bzip2 will add the .bz2 extension to the file name: tarName.tar.bz2

Image titled 865895 6 1

Compress the tarball when you create it. You can use the commands in the step above to compress existing tarballs, but you can also compress them as you are creating them by using the right flags:

tar -czvf tarName.tar.gz /path/to/directory tar -cjvf tarName.tar.bz2 /path/to/directory
  • z — This flag will compress the new .tar file using gzip. Make sure to include the .gz extension at the end of the file name.
  • j — This flag will compress the new .tar file using bzip2. Make sure to include the .bz2 extension at the end of the file name. [2] X Research source

Expert Q&A

A detailed explanation of all of the «Tar» parameters can be obtained at any time by typing «tar —help.»

You Might Also Like

Install Google Chrome Using Terminal on Linux

Can Linux Run Exe

Can Linux Run .exe Files? How to Run Windows Software on Linux

Add or Change the Default Gateway in Linux

Open Ports in Linux Server Firewall

How to Open Linux Firewall Ports: Ubuntu, Debian, & More

Take a Screenshot in Linux

Become Root in Linux

Execute INSTALL.sh Files in Linux Using Terminal

How to Run an INSTALL.sh Script on Linux in 4 Easy Steps

Ping in Linux

Use Ping in Linux: Tutorial, Examples, & Interpreting Results

Boot Linux from a USB on Windows 10

Delete Read Only Files in Linux

How to Delete Read-Only Files in Linux

Use Wine on Linux

Run Files in Linux

Install Linux

How to Install Linux on Your Computer

Install Puppy Linux

How to Install Puppy Linux

Источник

Оцените статью
Adblock
detector