How do I tar a directory without retaining the directory structure?

LinuxUnixGnuTar

Linux Problem Overview


I'm working on a backup script and want to tar up a file directory:

tar czf ~/backup.tgz /home/username/drupal/sites/default/files

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

Linux Solutions


Solution 1 - Linux

Use the --directory option:

 tar czf ~/backup.tgz --directory=/home/username/drupal/sites/default files 

Solution 2 - Linux

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 !!

Solution 3 - Linux

cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *

Solution 4 - Linux

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/

Solution 5 - Linux

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

Solution 6 - Linux

This worked for me:

gzip -dc "<your_file>.tgz" | tar x -C <location>

Solution 7 - Linux

For me -C or --directory did not work, I use this

cd source/directory/or/file
tar -cvzf destination/packaged-app.tgz *.jar
# this will put your current directory to what it previously was
cd -

Solution 8 - Linux

To build on nbt's and MaikoID's solutions:

tar -czf destination.tar.gz -C source/directory $(ls source/directory)

This solution:

  • 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

the following command:

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.

Solution 9 - Linux

Kindly use the below command to generate tar file without directory structure

tar -C <directoryPath> -cvzf <Path of the tar.gz file> filename1 filename2... filename N

eg:

tar -C /home/project/files -cvzf /home/project/files/test.tar.gz text1.txt text2.txt

Solution 10 - Linux

tar -Cczf ~/backup.tgz /home/username/drupal/sites/default/files

-C does the cd for you

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrock BolandView Question on Stackoverflow
Solution 1 - Linuxuser2100815View Answer on Stackoverflow
Solution 2 - LinuxMaikoIDView Answer on Stackoverflow
Solution 3 - LinuxJohn GibbView Answer on Stackoverflow
Solution 4 - LinuxDeepesh RajpalView Answer on Stackoverflow
Solution 5 - LinuxsangramView Answer on Stackoverflow
Solution 6 - LinuxVishrantView Answer on Stackoverflow
Solution 7 - LinuxAashutosh TaikarView Answer on Stackoverflow
Solution 8 - LinuxDrucklesView Answer on Stackoverflow
Solution 9 - Linuxsathiya rajView Answer on Stackoverflow
Solution 10 - LinuxrafView Answer on Stackoverflow