350

I have the following command in the part of a backup shell script:

tar -cjf site1.bz2 /var/www/site1/

When I list the contents of the archive, I get:

tar -tf site1.bz2
var/www/site1/style.css
var/www/site1/index.html
var/www/site1/page2.html
var/www/site1/page3.html
var/www/site1/images/img1.png
var/www/site1/images/img2.png
var/www/site1/subdir/index.html

But I would like to remove the part /var/www/site1 from directory and file names within the archive, in order to simplify extraction and avoid useless constant directory structure. Never know, in case I would extract backuped websites in a place where web data weren't stored under /var/www.

For the example above, I would like to have :

tar -tf site1.bz2
style.css
index.html
page2.html
page3.html
images/img1.png
images/img2.png
subdir/index.html

So, that when I extract, files are extracted in the current directory and I don't need to move extracted files afterwards, and so that sub-directory structures is preserved.

There are already many questions about tar and backuping in stackoverflow and at other places on the web, but most of them ask for dropping the entire sub-directory structure (flattening), or just add or remove the initial / in the names (I don't know what it changes exactly when extracting), but no more.

After having read some of the solutions found here and there as well as the manual, I tried :

tar -cjf site1.bz2 -C . /var/www/site1/
tar -cjf site1.bz2 -C / /var/www/site1/
tar -cjf site1.bz2 -C /var/www/site1/ /var/www/site1/
tar -cjf site1.bz2 --strip-components=3 /var/www/site1/

But none of them worked the way I want. Some do nothing, some others don't archive sub-directories anymore.

It's inside a backup shell script launched by a Cron, so I don't know well, which user runs it, what is the path and the current directory, so always writing absolute path is required for everything, and would prefer not changing current directory to avoid breaking something further in the script (because it doesn't only backup websites, but also databases, then send all that to FTP etc.)

How to achieve this?

Have I just misunderstood how the option -C works?

Alex.K.
  • 3,342
  • 13
  • 38
  • 44
QuentinC
  • 7,473
  • 3
  • 16
  • 28
  • related: https://unix.stackexchange.com/questions/22842/tar-with-relative-paths | https://serverfault.com/questions/339762/how-to-tarball-a-directory-without-carrying-over-path-to-folder – Ciro Santilli新疆棉花TRUMP BAN BAD May 09 '19 at 12:54
  • Well, `-C` just means "change directory", while replacing a path (or prefixing) can be only done by `--transform`. rif. https://superuser.com/questions/595510/prepend-prefix-in-tar/595512 you can simple -C (change directory) and --transform it: ``` tar cjf site1.bz2 --transform "s/^\.\//$targetbase/" -C /var/www/site1 . ``` – Daniele Cruciani Jan 02 '20 at 11:54
  • This is a very good question and sadly none of the answers to this date is satisfactory. We are yet to hear from some wise person how we could possibly extract just the single file style.css (example above) into the current directory without any reference to the original location or directory tree? I do not want to clutter up my current directory with unwanted new tree structure. Sounds like a serious shortcoming of tarball that has been ignored for years. – elmclose Jun 11 '20 at 13:22

9 Answers9

462
tar -cjf site1.tar.bz2 -C /var/www/site1 .

In the above example, tar will change to directory /var/www/site1 before doing its thing because the option -C /var/www/site1 was given.

From man tar:

OTHER OPTIONS

  -C, --directory DIR
       change to directory DIR
SHernandez
  • 950
  • 1
  • 9
  • 21
Lars Brinkhoff
  • 11,729
  • 1
  • 24
  • 42
  • 178
    Don't miss the dot at the end, that's important ;-) – Freedom_Ben Jun 02 '14 at 16:14
  • 12
    how about if you also want to select the files to backup based on a wildcard? -C /var/www/site1 *.dat doesn't work :( – Andy Lorenz Nov 05 '14 at 10:30
  • 1
    `(d=$PWD && cd /var/www/site1 && tar -cjf $d/site1.tar.bz2 *.dat)` – Lars Brinkhoff Nov 05 '14 at 11:38
  • 19
    The dot tells `tar` to archive everything in the current directory. And `-C` sets the current directory. – Lars Brinkhoff May 23 '15 at 18:12
  • 25
    This works great. I find it useful to preserve the directory name (just not the full path), so I did the following: `tar -czvf site1.tar.gz -C /var/www/ site1` (Note the space, I'm still using the -C, to cd to the parent dir, and specifying the dir to tar instead of dot) – jorfus Dec 09 '15 at 23:46
  • 1
    The trailing dot refers to the current directory after `tar` has changed it, not the directory you are in when you run the command. So, if you're in `/home/user1` and you do `tar -cf mine.tar -C /var/www/site1 .` it will tar up `/var/www/site1`, not `/home/user1`. – Christian Long Jul 14 '16 at 20:39
  • 1
    Do not use a `*` instead of the `.` in the end. This does not work as it means something different ;-) – thutt Apr 06 '17 at 08:55
  • 18
    I get a leading dot in the path of the tar e.g. `./folders` how can this be removed? – Mika571 Dec 19 '17 at 22:57
  • or `tar cvjf name.tar.bz2 -C "$p/../" $(basename $p)` to pack `$p` directory – Alex Dec 02 '18 at 15:31
  • Why does this not work: `tar -cjf site1.tar.bz2 -C /var/www/site1 /var/www/site1` ? – MrCalvin Dec 16 '18 at 10:40
  • @Mika571 you can use `find` to specify the files without the leading `./` like: `find "$DIR" -type f -printf '%P\0' |tar -cjvf "$OUTFILE" -C "$DIR" --null -T -` (replace `$DIR` and `$OUTFILE` as appropriate.) See: https://stackoverflow.com/a/2596736/213983 – thom_nic Dec 10 '20 at 15:40
48

The option -C works; just for clarification I'll post 2 examples:

  1. creation of a tarball without the full path: full path /home/testuser/workspace/project/application.war and what we want is just project/application.war so:

    tar -cvf output_filename.tar  -C /home/testuser/workspace project
    

    Note: there is a space between workspace and project; tar will replace full path with just project .

  2. extraction of tarball with changing the target path (default to ., i.e current directory)

    tar -xvf output_filename.tar -C /home/deploy/
    

    tar will extract tarball based on given path and preserving the creation path; in our example the file application.war will be extracted to /home/deploy/project/application.war.

    /home/deploy: given on extract
    project: given on creation of tarball

Note : if you want to place the created tarball in a target directory, you just add the target path before tarball name. e.g.:

tar -cvf /path/to/place/output_filename.tar  -C /home/testuser/workspace project
Toby Speight
  • 23,550
  • 47
  • 57
  • 84
Grizli
  • 497
  • 4
  • 3
  • 2
    how to add wildcard for file selection in the last example? – Siva Apr 30 '15 at 19:14
  • The problem with wildcards is that the shell expands them to the matching filenames and that tar doesn't expand them if they are quoted... – Gert van den Berg Oct 18 '17 at 10:16
  • I tried this on Ubuntu 18.04 and no luck. I'm not sure what I am missing. My stdout is displaying it correctly when I package it, but when I untar it, it still has the full path – sdc Feb 25 '20 at 03:55
21

Seems -C option upto tar v2.8.3 does not work consistently on all the platforms (OSes). -C option is said to add directory to the archive but on Mac and Ubuntu it adds absolute path prefix inside generated tar.gz file.

tar target_path/file.tar.gz -C source_path/source_dir

Therefore the consistent and robust solution is to cd in to source_path (parent directory of source_dir) and run

tar target_path/file.tar.gz source_dir

or

tar -cf target_path/file.tar.gz source_dir

in your script. This will remove absolute path prefix in your generated tar.gz file's directory structure.

  • 1
    Use of the -C option *DID* remove absolute path prefixes inside the generated tar.gz file on fedora 29. Is your answer specific to some system? – EL_DON Feb 08 '19 at 18:33
  • @EL_DON: I did not test -C option on Fedora, but ideally tar application software should work consistently on every platform unless it is a bug in tar application. -C option, I tested on Mac 10.8 and Mac 10.13 and Ubuntu (version I cannot remember). But as of tar v2.8.3, the command has been changed to tar -cf target_path/file.tar.gz source_dir and still if you add -C option it will not remove absolute path prefix inside generated tar.gz file. – Chinthaka Senanayaka Feb 24 '19 at 18:01
  • I tested again on a centOS system. After creating all the paths in the example and running the command (with `-cvf` added after `tar`), I find the resulting tar.gz file does not have absolute paths inside of it, which is consistent with several other answers. If you think tar is broken or outdated on both of the systems I've used for testing, please link to some documentation that would support your answer. I think the `-C` option changes directory before executing (as in other answers). When I omit it, tar tries to add junk from `./`, including paths from starting from `./`. – EL_DON Feb 26 '19 at 19:37
  • I used this doc: https://linux.die.net/man/1/tar Yes, the doc says -C would do the path change, but on my Mac 10.13 it is not working. this can be an inconsistent behavior of tar app. That means this is a bug. If you are writing a shell script to run on all unix platforms then better be safe with running code that will work on all OSes. – Chinthaka Senanayaka Feb 28 '19 at 06:58
  • Your answer doesn't say that there may be a bug and the more robust solution for cross-platform compatibility is to `cd` first. Your answer says the tool works in the opposite way of how the docs say it works and how it works on my system, so it's a wrong answer. You could easily fix it. – EL_DON Feb 28 '19 at 18:24
  • @EL_DON: Fixed the answer for platform consistent tar command, thanks – Chinthaka Senanayaka Mar 01 '19 at 06:02
8

The following command will create a root directory "." and put all the files from the specified directory into it.

tar -cjf site1.tar.bz2 -C /var/www/site1 .

If you want to put all files in root of the tar file, @chinthaka is right. Just cd in to the directory and do:

tar -cjf target_path/file.tar.gz *

This will put all the files in the cwd to the tar file as root files.

WLatif
  • 996
  • 9
  • 18
  • 2
    Using the * doesn't save any "hidden" .files or .folders. (fyi, using -C together with * fails, the shell expands the current dir, not the -C dir) – Xen2050 Apr 20 '17 at 05:29
4

Using the "point" leads to the creation of a folder named "point" (on Ubuntu 16).

tar -tf site1.bz2 -C /var/www/site1/ .

I dealt with this in more detail and prepared an example. Multi-line recording, plus an exception.

tar -tf site1.bz2\
    -C /var/www/site1/ style.css\
    -C /var/www/site1/ index.html\
    -C /var/www/site1/ page2.html\
    -C /var/www/site1/ page3.html\
    --exclude=images/*.zip\
    -C /var/www/site1/ images/
    -C /var/www/site1/ subdir/
/
Rod
  • 44,422
  • 3
  • 32
  • 51
  • 1
    Why are you calling it "point"? It's just `.`, which is the current directory. In the context of the `tar.gz`'s structure, that's just the base/root/top level, right? – EL_DON Feb 08 '19 at 18:36
  • See the snapshot for details [image](https://i.ibb.co/jkcnRjL/test1.png). My way is more correct to use, It's my opinion. – Sergey Asachev Feb 13 '19 at 12:53
3

One minor detail:

tar -cjf site1.tar.bz2 -C /var/www/site1 .

adds the files as

tar -tf site1.tar.bz2
./style.css
./index.html
./page2.html
./page3.html
./images/img1.png
./images/img2.png
./subdir/index.html

If you really want

tar -tf site1.tar.bz2
style.css
index.html
page2.html
page3.html
images/img1.png
images/img2.png
subdir/index.html

You should either cd into the directory first or run

tar -cjf site1.tar.bz2 -C /var/www/site1 $(ls /var/www/site1)
laktak
  • 47,916
  • 15
  • 112
  • 150
  • 1
    If you use `ls -A` you get hidden files too, WITHOUT trying to traverse the `..` and `.` files which is a common source of confusing if doing a tar or rsync where it tries to resolve symlinks. – dragon788 Nov 01 '20 at 18:31
0

If you want to archive a subdirectory and trim subdirectory path this command will be useful:

tar -cjf site1.bz2 -C /var/www/ site1
Hirurg103
  • 3,515
  • 1
  • 21
  • 44
0

Found tar -cvf site1-$seqNumber.tar -C /var/www/ site1 as more friendlier solution than tar -cvf site1-$seqNumber.tar -C /var/www/site1 . (notice the . in the second solution) for the following reasons

  • Tar file name can be insignificant as the original folder is now an archive entry
  • Tar file name being insignificant to the content can now be used for other purposes like sequence numbers, periodical backup etc.
dexter2305
  • 726
  • 8
  • 20
0

Low reputation (too many years of lurking, sigh) so I can't yet comment inline, but I found the answer from @laktak to be the only one that worked as intended on Ubuntu 18.04 -- using tar -cjf site1.tar.bz2 -C /var/www/site1 . on my machine resulted in all the files I wanted being under ./ inside the tar.bz2 file, which is probably ok but there is some risk of inconsistent behavior across OSs when un-tarring.

Jacob
  • 51
  • 4