7

I have a cpio archive with lots of files and I need to extract only one file, not all. With tar I could just use tar -xf archive.tar path/to/file, but that does not work with cpio:

cpio -i < archive.cpio path/to/file
bash: path/to/file: No such file or directory

Does anyone know how to extract just a single file from a cpio archive?

2 Answers 2

9

You should use the -d option to let cpio create the leading directories (path/to) if they don't exist:

cpio -id < archive.cpio path/to/file

Also, bsdtar (the regular tar on FreeBSD) knows how to extract cpio archives, whether compressed or not.

1
  • Make sure to OMIT the leading forward slash on the /path/to/file... I mean... path/to/file... or it will not work! Also, you can include more than one file with spaces in between them, just in case you want to extract TWO files. Commented Oct 22, 2020 at 4:58
1

If you want to consume the file right away in a pipe you can skip the -d and add --to--stdout

here is an overkill example - checking the MD5SUM of an executable in an initramfs cpio

$ objdump -sj '.init.ramfs' ./usr/initramfs_data.o | \
    tail -n +5 | \
    cut -b1-44 | \
    xxd -r | \
    gunzip | \
    cpio --to-stdout -i usr/bin/mount 2>/dev/null | \
    md5sum 
bffe60496ce15be471e7cfc04c14fac5  -
  • the tail kills the lines before the hexdump from objdump
  • The cut removes the ASCII porion of the hexdump
  • xxd -r un-hexdumps (hex back to binary)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.