Tuesday, January 30, 2024

[SOLVED] Using cpio, is it possible to change filepaths when extracting?

Issue

generally, I'd execute rpm2cpio $rpm | cpio -idmv but this extracts the exact file paths into the local folder

i.e

./usr/local/mycompany/myapp/bin/app
./usr/local/mycompany/myapp/resource/...

Is it possible to rewrite the file paths during the extract so that they instead extract to

./myapp/bin/app
./myapp/resource/...

Solution

Is it possible to rewrite the file paths during the extract

GNU cpio offers the -r option to interactively rename files during extraction ("copy in"). But I suppose you want something automatic and / or scriptable, and there is no documented cpio feature providing for that.

But it should be fast and straightforward to just move the subtree you want after the extraction, and then remove the extra, presumably empty parent directories:

mv ./usr/local/mycompany/myapp .
test "$(realpath .)" != / && rm -rf ./usr

That adds a little protection against catastrophe arising from running those commands from the filesystem root as a privileged user, but overall, you should not run it with privilege.



Answered By - John Bollinger
Answer Checked By - Katrina (WPSolving Volunteer)