- How do I tar a directory without retaining the directory structure?
- 11 Answers 11
- Create a tar archive
- Un-tar files on a local machine
- Upload to a server
- Un-tar on server
- How to compress and tar a folder in Linux [closed]
- 2 Answers 2
- To make a compressed tar ball of the current directory
- To make a compressed tar ball of a remote directory
- How do I compress a directory?
- 3 Answers 3
- Compress a folder with tar?
- 2 Answers 2
How do I tar a directory without retaining the directory structure?
This tars it up, but when I untar the resulting file, it includes the full file structure: the files are in home/username/drupal/sites/default/files . Is there a way to exclude the parent directories, so that the resulting tar just knows about the last directory ( files )?
11 Answers 11
tar czf ~/backup.tgz --directory=/home/username/drupal/sites/default files
@user3352668 simply add two directories. E.g. if default contains «files» and «morefiles», simply add «morefiles» to the string of nbt. Btw: Best solution is nbt’s, should be the accepted answer
Just thought I’d mention that the order here is significant. You can’t have the —directory=»/home/username/drupal/sites/default files» infront of the destination ~/backup.tgz .
@ChrisStryczynski Note that the path given by the —directory option is not «/home/username/drupal/sites/default files» . It is /home/username/drupal/sites/default , followed by a positional argument specifying the name of the directory to be tar’ed, files .
Hi I’ve a better solution when enter in the specified directory it’s impossible (Makefiles,etc)
tar -cjvf files.tar.bz2 -C directory/contents/to/be/compressed .
Do not forget the dot (.) at the end !!
@BruceSun The -C changes your working directory. The dot tells tar which directory you want to archive (i.e the current working directory).
If you omit the dot at the end, you likely will receive the warning: tar: Cowardly refusing to create an empty archive
Everyone talks about the -C option or —directory but without the little ‘.’ at the end as @MaikolD mentioned it, it does not work — at least on my environment.
cd /home/username/drupal/sites/default/files tar czf ~/backup.tgz *
cd is not recommended and error prone, e.g. big scripts or makefiles. The answer below is more generally usable and correct.
Create a tar archive
tar czf $sourcedir/$backup_dir.tar --directory=$sourcedir WEB-INF en
Un-tar files on a local machine
tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/
Upload to a server
scp -r -i $privatekey $sourcedir/$backup_dir.tar $server:$deploydir/med365/ echo "File uploaded.. deployment folders"
Un-tar on server
ssh -i $privatekey $server tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/
To gunzip all txt (*.txt) files from /home/myuser/workspace/zip_from/ to /home/myuser/workspace/zip_to/ without directory structure of source files use following command:
tar -P -cvzf /home/myuser/workspace/zip_to/mydoc.tar.gz --directory="/home/myuser/workspace/zip_from/" *.txt
To build on nbt’s and MaikoID’s solutions:
tar -czf destination.tar.gz -C source/directory $(ls source/directory)
- Includes all files and folders in the directory
- Does not include any of the directory structure (or . ) in the final product
- Does not require you to change directories.
However, it requires the directory to be given twice, so it may be most useful in another script. It may also be less efficient if there are a lot of files/folders in source/directory . Adjust the subcommand as necessary.
So for instance for the following structure:
|- source | |- one | `- two `- working
working$ tar -czf destination.tar.gz -C ../source $(ls ../source)
will produce destination.tar.gz where both one and two (and sub-files/-folders) are the first items.
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 :
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 I compress a directory?
I’m trying to compress a directory and ftp it to a windows ftp. I have tried every tar command I can find to compress a directory. It appears to be ok. Then I transfer it and view it’s contents using Winrar. Winrar keeps telling me the file is corrupted. I have viewed other .gz or .bz2 files using winrar but for some odd reason I can’t get it to work. I would prefer just to have it zip the files so they have a .zip extension but even then when i try to browse it’s contents both windows and winrar claim it’s corrupt. Does anyone else have a suggestion as to something else to try?
3 Answers 3
Well, most probably your files are perfectly fine before FTP transmission.
Unfortunately, probably you are transferring your files using wrong FTP mode.
FTP do have two modes: binary and ASCII. By default most clients use ASCII mode, which breaks your binary files completely. I don’t know which FTP client you are using, but for example in ncftp you can use command «binary» to switch to binary mode.
If you want to create ZIP files using Ubuntu (or almost any other Linux), use zip . You can install it to Ubuntu by running
Then you can create zip file by running
zip -r compressed_filename.zip foldername
On related note, you should know that FTP is insecure transmission protocol. Consider switching to sftp, for example. There is many free ssh servers for Windows, including minimal OpenSSH port.
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).
@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.
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.