I am on a MacOS machine (I think tar command is BSD). From I have read the -s switch on BSD tar, is the same thing as the --transform switch for GNU tar.

I have this command:

tar -zcf foo.tgz -s /^"$(pwd)"/"$(pwd)/package"/ "$(pwd)"

what I am trying to do is archive everything into a file called foo.tgz, and put everything into a subdirectory called "package".

I get this error:

tar: Invalid replacement flag a

I believe what's past to the -s option is not correct, but not sure what it might be.

Ultimately, I am looking to write the output to stdout, not to a file called foo.tgz, so if you feel like updating that too, please do, thanks.


1 Answer 1


Note the implicit clash between the slash used as the separator for the substitution pattern and replacement, and the slash used as the directory separator. If the current directory is, e.g. /foo/bar, the string you're giving to tar is


where ^ is taken as the pattern, foo as the replacement, and the rest as flags. Here, I get an error for the b:

tar: Invalid replacement flag b

Luckily, the tar on my Mac accepts other characters in place of the slash separator, so you can use, say ~, which makes it easier to put full pathnames in the pattern. e.g. -s "~^a$~x/y~" works and renames a to x/y.

Backslash-escapes (\/, \057 or \x2f) don't seem to work, so you'll need to choose as separator some character you don't need in the patterns themselves.

You must log in to answer this question.

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