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

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
Brock Boland
  • 15,870
  • 11
  • 35
  • 36

11 Answers11

193

Use the --directory option:

 tar czf ~/backup.tgz --directory=/home/username/drupal/sites/default files 
Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
  • 6
    What if I want to put 2 directories here? – Shanpei Zhou Feb 07 '15 at 00: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 07 '15 at 07:52
  • 8
    **-C DIR** is same as **--directory=DIR**, changes directory to DIR – Markus Apr 28 '16 at 07: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`. – Chris Stryczynski Apr 10 '17 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 '17 at 19:03
  • I'm not able to use wildcards in files. How to use wildcards using directory? – Prashant Pokhriyal May 02 '18 at 11:35
  • @ChrisStryczynski That's because `~/backup.tgz` is the option argument for the `-f` option. I imagine that it's fine to do `tar czC /home/username/drupal/sites/default -f ~/backup.tgz files` – JoL May 23 '19 at 23:19
  • @PrashantPokhriyal `cd /home/username/drupal/sites/default; tar czf ~/backup.tgz files/*; cd -` – JoL May 23 '19 at 23:24
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 !!

Danvil
  • 22,240
  • 19
  • 65
  • 88
MaikoID
  • 4,359
  • 4
  • 25
  • 27
  • 1
    **-C DIR** is same as **--directory=DIR**, changes directory to DIR – Markus Apr 28 '16 at 07:12
  • 6
    What does the dot at the end do? – Bruce Sun Jul 05 '17 at 05: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 '17 at 00:52
  • 2
    If you omit the dot at the end, you likely will receive the warning: [tar: Cowardly refusing to create an empty archive](https://stackoverflow.com/questions/44380154/tar-cowardly-refusing-to-create-an-empty-archive) – user12345 Oct 20 '17 at 00: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 02 '18 at 14:07
  • 19
    This still leaves you with a `.` folder in the root of the tar – derpedy-doo Aug 01 '19 at 00:10
  • This is the only recipe that actually worked. Thanks! – mvp Jan 15 '20 at 01:01
  • 1
    This does not correctly tar only the contents of the directory "directory/contents/to/be/compressed". The dot is causing a directory "." to be created and that directory contains the contents. Does anyone have a way to only tar the contents of the source directory? – user3477889 Nov 11 '22 at 01:44
45
cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *
John Gibb
  • 10,603
  • 2
  • 37
  • 48
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/
upe
  • 1,862
  • 1
  • 19
  • 33
3

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.

Druckles
  • 3,161
  • 2
  • 41
  • 65
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
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
sangram
  • 377
  • 4
  • 5
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>
Vishrant
  • 15,456
  • 11
  • 71
  • 120
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
atline
  • 28,355
  • 16
  • 77
  • 113
sathiya raj
  • 35
  • 1
  • 5
-3
tar -Cczf ~/backup.tgz /home/username/drupal/sites/default/files

-C does the cd for you

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
raf
  • 1,007
  • 2
  • 10
  • 13