Linux tar all folders

tar: add all files and directories in current directory INCLUDING .svn and so on

The resulting tar includes .svn directories in subdirs but NOT in the current directory (as * gets expanded to only ‘visible’ files before it is passed to tar I tried to tar -czf workspace.tar.gz . instead but then I am getting an error because ‘.’ has changed while reading:

tar: ./workspace.tar.gz: file changed as we read it 

Is there a trick so that * matches all files (including dot-prefixed) in a directory? (using bash on Linux SLES-11 (2.6.27.19)

16 Answers 16

Don’t create the tar file in the directory you are packing up:

tar -czf /tmp/workspace.tar.gz . 

does the trick, except it will extract the files all over the current directory when you unpack. Better to do:

cd .. tar -czf workspace.tar.gz workspace 

or, if you don’t know the name of the directory you were in:

base=$(basename $PWD) cd .. tar -czf $base.tar.gz $base 

(This assumes that you didn’t follow symlinks to get to where you are and that the shell doesn’t try to second guess you by jumping backwards through a symlink — bash is not trustworthy in this respect. If you have to worry about that, use cd -P .. to do a physical change directory. Stupid that it is not the default behaviour in my view — confusing, at least, for those for whom cd .. never had any alternative meaning.)

One comment in the discussion says:

I [. ] need to exclude the top directory and I [. ] need to place the tar in the base directory.

The first part of the comment does not make much sense — if the tar file contains the current directory, it won’t be created when you extract file from that archive because, by definition, the current directory already exists (except in very weird circumstances).

The second part of the comment can be dealt with in one of two ways:

  1. Either: create the file somewhere else — /tmp is one possible location — and then move it back to the original location after it is complete.
  2. Or: if you are using GNU Tar, use the —exclude=workspace.tar.gz option. The string after the = is a pattern — the example is the simplest pattern — an exact match. You might need to specify —exclude=./workspace.tar.gz if you are working in the current directory contrary to recommendations; you might need to specify —exclude=workspace/workspace.tar.gz if you are working up one level as suggested. If you have multiple tar files to exclude, use ‘ * ‘, as in —exclude=./*.gz .
Читайте также:  Настрой локальной сети linux

Источник

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.

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:

Читайте также:  Libreoffice draw установить linux

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.

Источник

How to compress a whole directory (including subdirectories) using TAR in Unix based OS with the CLI

Carlos Delgado

Learn how to quickly compress an entire directory into a single tar file with the command line.

Many modern Unix systems, such as Linux, use GNU tar, a version of tar produced by the Free Software Foundation. TAR stands for tape archiving, the storing of entire file systems onto magnetic tape, which is one use for the command. The most common use for tar is to simply combine a few files into a single file, for easy storage and distribution. If your system uses GNU tar, you can easily use gzip (the GNU file compression program) in conjunction with tar to create compressed files with the command line following the next command syntax:

tar -zcvf [result-filename.tar.gz] [path-of-directory-to-compress]

The -zcvf instruction stand for:

  • -z : Compress the desired file/directory using gzip
  • -c : Stand for create file (output tar.gz file)
  • -v : To display the progress while creating the file
  • -f : Finally the path of the desire file/directory to compress

For example, let’s suppose that you are located in the command line in the /www directory and inside this directory there is a folder that you want to compress into a single file namely «sandbox». To compress the sandbox folder into a tar file you would run the command as:

tar -zcvf sandbox_compressed.tar.gz sandbox

This should start the compression of the directory into a new tar file, as we have our -v argument to display the progress in the console, you would see a list of every filename that is being added to the file.

Extracting generated file

If you want now to extract as well with the command line the generated file, you should use now the following command syntax:

tar -xvzf [your-tar-file.tar.gz]

The -zcvf instruction stands for:

  • -z : Compress the desired file/directory using gzip
  • -x : Stand for extract file (input tar.gz file). If any files are named on the command line, only those files will be extracted from the archive.
  • -v : To display the progress while creating the file

For example, to extract our previously created file you should use the command:

tar -xvzf sandbox_compressed.tar.gz

That will extract the content of the file (sandbox folder) in the current directory.

Extracting content to a custom directory

If you need to extract the content of the generated tar to a custom directory instead of the current directory, you can specify the new directory where the content should be extracted using the -C or —directory option in your extraction command:

tar -xvzf sandbox_compressed.tar.gz -C /target/directory

Источник

Читайте также:  Linux yandex browser codecs

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.

Источник

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