cpio cannot do that, unless you use the -r option to rename files interactively. It could be very annoying, anyway. Use
pax instead that works similarly to cpio but in addition has an option to rename the archived files based on
ed regexp. Here we go:
1. to list the content of the archive use pax without options
Code:
$ pax < archive.cpio
folder1
folder1/file1
folder1/file2
folder2
folder2/subfolder2
folder2/subfolder2/file5
folder2/subfolder1
folder2/subfolder1/file4
folder2/subfolder1/file3
this is just to show the archive I previously created in order to resemble your example. The output is similar to what you can obtain by running
cpio -t < archive.cpo.
2. to list a partial content based on a pattern:
Code:
$ pax < archive.cpio folder2/*
folder2/subfolder2
folder2/subfolder2/file5
folder2/subfolder1
folder2/subfolder1/file4
folder2/subfolder1/file3
this is similar to
cpio -t < archive.cpio folder2/*.
3. extract the content of folder2 in the current working directory (and this actually answers your question):
Code:
$ pax -r -s '/\<folder2\>\///' < archive.cpio folder2/*
$ ls
archive.cpio subfolder1 subfolder2
$ ls -R
.:
archive.cpio subfolder1 subfolder2
./subfolder1:
file3 file4
./subfolder2:
file5
and the trick is done!