Tar one folder in linux

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?

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).

Читайте также:  Find all symbolic links linux

@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.

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.

Читайте также:  Linux and dynamic disk

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.

Источник

How to compress and tar a folder in Linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

I am new to Linux, I have a folder called stacey . How can I create a compressed tarball from this? I can tar the folder with tar -cvzf stacey.tar * . But can I add the 7zip at the same time as so I have a compressed tarball called stacey.tar.gz ?

It’s already gzip compressed thanks to your -z . You can verify this with file stacey.tar , and then simply rename it to add the missing extension

This sort of question is best asked on Super User, since it is essentially a consumer-level computing problem. It could go onto Ubuntu or Unix & Linux.

2 Answers 2

To make a compressed tar ball of the current directory

tar -cvz -f path_to_the_archive_to_be_created . 
[user@machine temp]$ tar -cvz -f ~/dir1/temp.tar.gz . 

The current directory (temp) is archived into ~/dir1/temp.tar.gz

To make a compressed tar ball of a remote directory

tar -cvz -f path_to_the_archive_to_be_created -C path_to_the_remote_directory . 
[user@machine ~]$ tar -cvz -f ~/dir1/temp.tar.gz -C ~/temp . 

The directory ~/temp is archived into ~/dir1/temp.tar.gz .

-c, --create create a new archive -v, --verbose verbosely list files processed -z, --gzip, --gunzip, --ungzip filter the archive through gzip -f, --file=ARCHIVE use archive file or device ARCHIVE -C, --directory=DIR change to directory DIR 

Passing -z on the tar command will gzip the file, so you should name your file stacey.tar.gz (or stacey.tgz ), not stacey.tar :

Читайте также:  Linux edit files in console

If you want to 7zip the file (instead of Gzip), remove the -z , keep stacey.tar , and run 7z stacey.tar after the tar command completes:

tar -cvf stacey.tar * 7z a stacey.tar.7z stacey.tar 

Or you can use a pipeline to do it in one step:

tar -cvf - * | 7z a -si stacey.tar.7z 

7zip is more like tar in that it keeps an index of files in the archive. You can actually skip the tar step entirely and just use 7zip:

Источник

How do you extract a single folder from a large tar.gz archive?

Did you use an absolute or relative path? Should be relative, other than that it looks right. How long did you let it run, and how good is your system? It will take at least several minutes on a fast machine. Potentially an hour or more? I’m running a test now.

On Linux, you can watch how far into the archive tar is with cat /proc/$(pidof tar)/fdinfo/0 (adapt the command if you have more than one tar process running).

1 Answer 1

tar stores relative paths by default. GNU tar even says so if you try to store an absolute path:

tar -cf foo.tar /home/foo tar: Removing leading `/' from member names 

If you need to extract a particular folder, have a look at what’s in the tar file:

And note the exact filename. In the case of my foo.tar file, I could extract /home/foo/bar by saying:

tar -xvf foo.tar home/foo/bar # Note: no leading slash 

So no, the way you posted isn’t (necessarily) the correct way to do it. You have to leave out the leading slash. If you want to simulate absolute paths, do cd / first and make sure you’re the superuser. Also, this does the same:

tar -C / -xvf foo.tar home/foo/bar # -C is the ‘change directory’ option 

There are very obvious, good reasons why tar converts paths to relative ones. One is the ability to restore an archive in places other than its original source. The other is security. You could extract an archive, expect its files to appear in your current working directory, and instead overwrite system files (or your own work) elsewhere by mistake.

Note: if you use the -P option, tar will archive absolute paths. So it always pays to check the contents of big archives before extracting.

Источник

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