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

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



Solution 1:[1]

Use the --directory option:

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

Solution 2:[2]

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:[3]

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 4:[4]

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 5:[5]

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 6:[6]

This worked for me:

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

Solution 7:[7]

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:[8]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tom Aranda
Solution 2 Danvil
Solution 3 upe
Solution 4 Manos Nikolaidis
Solution 5 Druckles
Solution 6 Vishrant
Solution 7 Aashutosh Taikar
Solution 8 atline