Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've received a Unix software distribution as a compressed cpio file. What's the best command to extract the files?

share|improve this question

migrated from stackoverflow.com Aug 28 '09 at 1:28

This question came from our site for professional and enthusiast programmers.

    
When you did man cpio what did you see? – S.Lott Aug 28 '09 at 0:56
    
I see: -i -o -p -t -B -c -C -f -F -H -M -n -v -V -W -b -r -s -S -E -A -O -l -0 -a -I -L -R -d -m -u -? --extract --create --pass-through --list --block-size= --io-size= --force-local --nonmatching --file= --format= --message= --numeric-uid-gid --quiet --rsh-command= --verbose --dot --warning= --swap --rename --swap-bytes --swap-halfwords --to-stdout --pattern-file= --only-verify-crc --append --link --absolute-filenames --no-absolute-filenames --null --reset-access-time --dereference --owner= --make-directories --preserve-modification-time --no-preserve-owner --sparse --unconditional – Mark Harrison Aug 28 '09 at 1:01
up vote 7 down vote accepted
gzip -cd foo.cpio.gz | cpio -idmv
  • i : extract (input)
  • d : create directories
  • m : preserve mtime
  • v : verbose
share|improve this answer
mkdir archive
cd archive
zcat ../archive.cpio.gz | cpio -idmv --no-absolute-filenames

While this is an old question, it shows up high on Google, so I thought I might update it. I agree with the accepted answer in general, but you should add "--no-absolute-filenames" unless you are intending to overwrite important system files on your machine. Also, personally, I prefer "zcat" over "gzip -cd" or "gunzip -c".

Finally, note that you need to run cpio as root (e.g. sudo) if you are extracting a root filesystem that contains device nodes.

share|improve this answer

This Wikipedia page on cpio has some good notes.
For more details, refer to the cpio manual.

A link from the same Wikipedia page discusses comparison with tar archives.
And, here is an example of using cpio with the tar format.

share|improve this answer

For example, to extract the archived contents of /etc/httpd/ to the current directory, creating subdirectories ./etc/httpd/

mkdir restored-etc-httpd
cd restored-etc-httpd    
zcat archive.cpio.gz | cpio -idmv --no-absolute-filenames "*etc/httpd/*"

The accepted answer and Matt's were both helpful to me but I was stumped for a while because of three details:

  1. The matching pattern needs to be quoted to work as a pattern :P
  2. The option --no-absolute-filenames must precede the pattern on the command line
  3. Since that option removes the leading / from filenames, the matching pattern must also omit the leading /
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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