Score:3

tar command with and without --absolute-names option

cw flag

I am currently using ubuntu 20.04 machine. I was trying to create compressed archives using tar command using the below command,

sudo tar -caf /opt/archive.tar.bz2 /home/bob/databases/

my current working directory is /home/bob. when using with -P (--absolute-names) option, the content of the compressed archive is coming as below,

/home/bob/databases/
/home/bob/databases/file00
/home/bob/databases/file01
/home/bob/databases/file02

and without the -P option, the content is coming as below,

home/bob/databases/
home/bob/databases/file00
home/bob/databases/file01
home/bob/databases/file02

How actually this option works? and what if I want to change the base directory to bob/databases/ ?

Score:4
us flag

If you want to omit /home from the paths, then change to that directory (-C, --directory):

tar -caf /opt/archive.tar.bz2 -C /home bob/databases

Or:

cd /home
tar -caf /opt/archive.tar.bz2 bob/databases

The -P option is rarely needed (in fact, I'd say this one of those options where you'll already know why you need it if you need it at all). What it does is obvious from the output in the question - it keeps the leading slash in paths to make them absolute, whereas tar archives usually have relative paths. Then, if you extract an archive containing absolute paths (while using this option), it will extract the files to those paths instead of relative to the current directory.

Score:3
jp flag

In addition to @muru's answer

Saving the absolute path/names is particularly handy when backing up and restoring whole system mount points(the home partition for example) ... This a one use case for the option -P.

what if I want to change the base directory to bob/databases/?

tar as well has a --transform option to change filenames/paths using sed style s/match/substitute/ ... So you can always do something like this:

$ ls -l /home/ubuntu/test/
total 0
-rw-r--r-- 1 ubuntu ubuntu 0 May  1 08:11 file1
-rw-r--r-- 1 ubuntu ubuntu 0 May  1 08:11 file2
-rw-r--r-- 1 ubuntu ubuntu 0 May  1 08:11 file3
$
$ tar -caf test.tar --transform 's|^home\/||' /home/ubuntu/test/
$
$ tar -tf test.tar
ubuntu/test/
ubuntu/test/file3
ubuntu/test/file2
ubuntu/test/file1
muru avatar
us flag
Even for whole system backups, I'd suggest just changing directory to `/`. The absolute pathnames feature isn't portable, it won't work with other `tar`s (and even on Ubuntu you have Busybox `tar` as an alternative to consider). Also `--strip-components` is more commonly supported than `--transform`, I think.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.