Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.