Tar to compress in linux

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 apt get update package

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.

Источник

Compress a folder with tar?

I’m trying to compress a folder ( /var/www/ ) to ~/www_backups/$time.tar where $time is the current date. This is what I have:

cd /var/www && sudo tar -czf ~/www_backups $time" 

I am completely lost and I’ve been at this for hours now. Not sure if -czf is correct. I simply want to copy all of the content in /var/www into a $time.tar file, and I want to maintain the file permissions for all of the files. Can anyone help me out?

Читайте также:  Посмотреть загрузку видеокарты linux

2 Answers 2

To tar and gzip a folder, the syntax is:

tar czf name_of_archive_file.tar.gz name_of_directory_to_tar 

Adding — before the options ( czf ) is optional with tar . The effect of czf is as follows:

  • c — create an archive file (as opposed to extract, which is x )
  • f — filename of the archive file
  • z — filter archive through gzip (remove this option to create a .tar file)

If you want to tar the current directory, use . to designate that.

To construct filenames dynamically, use the date utility (look at its man page for the available format options). For example:

cd /var/www && tar czf ~/www_backups/$(date +%Y%m%d-%H%M%S).tar.gz . 

This will create a file named something like 20120902-185558.tar.gz .

On Linux, chances are your tar also supports BZip2 compression with the j rather than z option. And possibly others. Check the man page on your local system.

This is perfect, thank you. I have one tiny issue though. After creating a tar file of /var/www, it is placed within /var/www directories in the tar file. Here’s the code i’m using now sudo tar -czf ~/www_backups/$time.tar /var/www/» Imagine i have a file called test.txt inside /var/www. After making a tar copy of the file, when i extract it it will be placed inside /var/www directories. Does that make sense? I hope it does, kinda hard to explain. I will check for BZip2 support, thanks for the suggestion!

That’s why you first cd to the directory you want to package, then tar cf file.tar . — that last . instead of specifying the full path will make the paths inside the archive relative to the current directory. You could also use the -C option for tar (look at the man page).

@Qwertylicious -f (from man page) Read the archive from or write the archive to the specified file.The filename can be — for standard input or standard output.

Examples for Most Common Compression Algorithms

The question title for this is simply «Compress a folder with tar?» Since this title is very general, but the question and answer are much more specific, and due to the very large number of views this question has attracted, I felt it would be beneficial to add an up-to-date list of examples of both archiving/compressing and extracting/uncompressing, with various commonly used compression algorithms.

Читайте также:  Uid and euid in linux

These have been tested with Ubuntu 18.04.4. They are very simple for general use, but could easily be integrated into the OP’s more specific question contents using the the techniques in the accepted answer and helpful comments above.

One thing to note for the more general audience is that tar will not add the necessary extensions (like .tar.gz ) automatically — the user has to explicitly add those, as seen in the commands below:

# 1: tar (create uncompressed archive) all files and directories in the current working directory recursively into an uncompressed tarball tar cvf filename.tar * # 2: Untar (extract uncompressed archive) all files and directories in an uncompressed tarball recursively into the current working directory tar xvf filename.tar # 3: tar (create gzipped archive) all files and directories in the current working directory recursively into a tarball compressed with gzip tar cvzf filename.tar.gz * # 4: Untar (extract gzipped archive) all files and directories in a tarball compressed with gzip recursively into the current working directory tar xvf filename.tar.gz # Note: same options as 2 above # 5: tar (create bzip2'ed archive) all files and directories in the current working directory recursively into a tarball compressed with bzip2 tar cvjf filename.tar.bz2 * # Note: little 'j' in options # 6: Untar (extract bzip2'ed archive) all files and directories in an tarball compressed with bzip2 recursively into the current working directory tar xvf filename.tar.bz2 # Note: same options as 2 and 4 above # 7: tar (create xz'ed archive) all files and directories in the current working directory recursively into a tarball compressed with xz tar cvJf filename.tar.xz * # Note: capital 'J' in options # 8: Untar (extract xz'ed archive) all files and directories in an tarball compressed with xz recursively into the current working directory tar xvf filename.tar.xz # Note: same options as 2, 4, and 6 above 

See the tar man page (best to use man tar on your specific machine) for further details. Below I summarize the options used above directly from the man page:

-c, —create
create a new archive

-x, —extract, —get
extract files from an archive

-v, —verbose
verbosely list files processed

-z, —gzip
filter the archive through gzip

-j, —bzip2
filter the archive through bzip2

-J, —xz
filter the archive through xz

-f, —file=ARCHIVE
use archive file or device ARCHIVE

No need to add the — in front of the combined options, or the = sign between the f option and the filename.

I got all this from my recent article, which will be expanded further into a much more comprehensive article as I have time to work on it.

Источник

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