Tar command in linux with directory

tar command in Linux with examples

The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the Archive files. tar command in Linux is one of the important command which provides archiving functionality in Linux. We can use Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them.

tar [options] [archive-file] [file or directory to be archived]

Options:
-c : Creates Archive
-x : Extract 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 : zip, tells tar command that creates tar file using gzip
-j : filter archive tar file using tbzip
-W : Verify a archive file
-r : update or add file or directory in already existed .tar file

What is an Archive file?
An Archive file is a file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space.

Examples:
1. Creating an uncompressed tar Archive using option -cvf : This command creates a tar file called file.tar which is the Archive of all .c files in current directory.

2. Extracting files from Archive using option -xvf : This command extracts files from Archives.

3. gzip compression on the tar Archive, using option -z : This command creates a tar file called file.tar.gz which is the Archive of .c files.

4. Extracting a gzip tar Archive *.tar.gz using option -xvzf : This command extracts files from tar archived file.tar.gz files.

5. Creating compressed tar archive file in Linux using option -j : This command compresses and creates archive file less than the size of the gzip. Both compress and decompress takes more time then gzip.

$ tar cvfj file.tar.tbz example.cpp
$tar cvfj file.tar.tbz example.cpp example.cpp $tar tvf file.tar.tbz -rwxrwxrwx root/root 94 2017-09-17 02:47 example.cpp

6. Untar single tar file or specified directory in Linux : This command will Untar a file in current directory or in a specified directory using -C option.

$ tar xvfj file.tar or $ tar xvfj file.tar -C path of file in directory 

7. Untar multiple .tar, .tar.gz, .tar.tbz file in Linux : This command will extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the above command will extract “fileA” “fileB” from the archive files.

$ tar xvf file.tar "fileA" "fileB" or $ tar zxvf file1.tar.gz "fileA" "fileB" or $ tar jxvf file2.tar.tbz "fileA" "fileB"

8. Check size of existing tar, tar.gz, tar.tbz file in Linux : The above command will display the size of archive file in Kilobytes(KB).

$ tar czf file.tar | wc -c or $ tar czf file1.tar.gz | wc -c or $ tar czf file2.tar.tbz | wc -c

9. Update existing tar file in Linux

Читайте также:  Linux checking cpu cores

10. list the contents and specify the tarfile using option -tf : This command will list the entire list of archived file. We can also list for specific content in a tarfile

11. Applying pipe to through ‘grep command’ to find what we are looking for : This command will list only for the mentioned text or image in grep from archived file.

$ tar tvf file.tar | grep "text to find" or $ tar tvf file.tar | grep "filename.file extension"

12. We can pass a file name as an argument to search a tarfile : This command views the archived files along with their details.

13. Viewing the Archive using option -tvf

-rwxrwxrwx root/root 191 2017-09-17 02:20 os2.c -rwxrwxrwx root/root 218 2017-09-17 02:20 os3.c -rwxrwxrwx root/root 493 2017-09-17 02:20 os4.c

What are wildcards in Linux
Alternatively referred to as a ‘wild character’ or ‘wildcard character’, a wildcard is a symbol used to replace or represent one or more characters. Wildcards are typically either an asterisk (*), which represents one or more characters or question mark (?),which represents a single character.

14. To search for an image in .png format : This will extract only files with the extension .png from the archive file.tar. The –wildcards option tells tar to interpret wildcards in the name of the files
to be extracted; the filename (*.png) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell.

$ tar tvf file.tar --wildcards '*.png'

Note: In above commands ” * ” is used in place of file name to take all the files present in that particular directory.

?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L
This article is contributed by Akansh Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Источник

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.
Читайте также:  Imax b6 mini linux

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.

Читайте также:  Linux export for all

Источник

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.

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

Источник

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