113

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

11 Answers 11

194

Use the --directory option:

 tar czf ~/backup.tgz --directory=/home/username/drupal/sites/default files 
8
  • 6
    What if I want to put 2 directories here? Feb 7, 2015 at 0:18
  • @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
    – Anonymous
    Aug 7, 2015 at 7:52
  • 8
    -C DIR is same as --directory=DIR, changes directory to DIR
    – Markus
    Apr 28, 2016 at 7:12
  • 1
    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. Apr 10, 2017 at 19:10
  • 6
    @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.
    – Chap
    Dec 29, 2017 at 19:03
69

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

8
  • 6
    What does the dot at the end do?
    – Bruce Sun
    Jul 5, 2017 at 5:36
  • 3
    @BruceSun The -C changes your working directory. The dot tells tar which directory you want to archive (i.e the current working directory).
    – user12345
    Oct 20, 2017 at 0:52
  • 2
    If you omit the dot at the end, you likely will receive the warning: tar: Cowardly refusing to create an empty archive
    – user12345
    Oct 20, 2017 at 0:55
  • 3
    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.
    – aks
    Nov 2, 2018 at 14:07
  • 19
    This still leaves you with a . folder in the root of the tar Aug 1, 2019 at 0:10
45
cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *
4
  • 22
    You can also use the -C option of tar.
    – GuruM
    Jul 30, 2013 at 12:16
  • 10
    cd is not recommended and error prone, e.g. big scripts or makefiles. The answer below is more generally usable and correct.
    – Gregor
    Nov 5, 2015 at 21:54
  • 1
    Tries -C and --directory didn't work for me on Ubuntu 16.04. This is only option that works.
    – haxpor
    Sep 23, 2018 at 18:28
  • 2
    This is the only option that works -C add a . root folder, which is not what we want. Nov 23, 2019 at 9:01
7

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/
4

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.

2

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
0
2

If you want to tar files while keeping the structure but ignore it partially or completely when extracting, use the --strip-components argument when extracting.

In this case, where the full path is /home/username/drupal/sites/default/files, the following command would extract the tar.gz content without the full parent directory structure, keeping only the last directory of the path (e.g. files/file1).

tar -xzv --strip-components=5 -f backup.tgz

I've found this tip on https://www.baeldung.com/linux/tar-archive-without-directory-structure#5-using-the---strip-components-option.

0

This worked for me:

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

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

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
-3
tar -Cczf ~/backup.tgz /home/username/drupal/sites/default/files

-C does the cd for you

2
  • This will not work. See MaikoID's and Neil Butterworth's solutions.
    – Vincent
    Mar 26, 2017 at 20:31
  • 4
    -C works, but has to be used like -C <dir> file1 file2 ...
    – Ani
    May 4, 2017 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.