Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am attempting to tar up the contents of a root file system, located at $rootfs. The problem is, when I try to tar -c $rootfs > $dest, the directories between my current directory and the actual file system remain. So instead of etc/fstab, I have fun/rootfs/path/etc/fstab when rootfs=fun/rootfs/path, for every single file.

I know about --strip, but the problem is that $rootfs is a relative-path variable, so I don't know how many directories are between the file system and my current directory. I can't cd for a similar reason; $dest is a relative-path variable and I have to stay in the current directory to create the archive in the proper location.

Is there a way I can ignore the rootfs path when creating the archive?

share|improve this question

You could generate the listing of files relative to the $rootfs path and then use -C $rootfs -T <file-of-filenames> to create the archive. You cannot use a wildcard to generate the list of files on the tar command line as that will not complete correctly. You could use something like find or ls to do it though.

share|improve this answer

I’m not sure I understand the question, but how about

(cd "$rootfs"; tar -c .) > "$dest"

so the tar runs in the $rootfs directory, but $dest is created relative to the current directory.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .