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

Brock Boland picture Brock Boland · Apr 17, 2011 · Viewed 93.6k times · Source

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

Answer

user2100815 picture user2100815 · Apr 17, 2011

Use the --directory option:

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