I have a tar archive xyz.tar containing lots of .sh files like:


Every .sh file has some string content, e.g. 123.sh contains welcome to Hollywood, 456.sh contains I love Hollywood, and 789.sh contains Hollywood movies.

Now, my objective is to replace the string Hollywood by Oscar in all the .sh files using a loop.

I tried this command:

for i in $(tar -xvf xyz.tar); do sed -e 's/Hollywood/Oscar/' $i; done

I'm getting output on command window, but o/p is not reflecting in xyz.tar archive because unable to recompress the tar archive, after extraction and replacement command.

1 Answer 1


First of all you should realise that a .tar file is normally not compressed (it would more likely have an extension .tar.gz or .tar.xz if it is).

And your tar -xvf xyz.tar extracts the files and lists the names, sed works on the list of filenames, not even the file contents.

If you have exact same length substitutions as the originals, then you could theoretically (as tar files have no checksum) replace the strings in the .tar. However better is to first extract all the files in a temporary directory, then walk over the tree using

find . -name "*.sh" -print0 | xargs -0 sed -i -e 's/Hollywood/Oscar/'

and afterwards re-create the tar file from this temporary directory.

This assumes your sed supports -i (replacing of the input file with the edited contents), if it doesn't you should make a small script that calls sed and writes the output to a temporary file, then copies the file back when sed is finished.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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