Linux add directory to archive

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.

Читайте также:  Linux где файлы virtualbox

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 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 107,821 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.

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
Читайте также:  Управление питанием процессора linux

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 Software on Linux

How to Install Software on Linux: Packages, Compiling, & More

Источник

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