Skip to content

cpio command in MacOS

The MacOS cpio command is a powerful tool for creating, extracting, and manipulating archives. It allows users to easily manage large sets of files by archiving them into a single file or extracting them from an existing archive. With cpio, you can preserve file permissions, timestamps, and ownership when creating or extracting archives. Additionally, you can specify various options to customize the behavior of cpio according to your needs.

Terminal window
cpio [options] [parameters]
OptionDescription
-iRestore archive
-oCreate archive
-tList archive contents
-vVerbose mode (print file names as processed)
-dCreate leading directories where needed
-BSet the block size to 5120 bytes
ParameterDescription
directorySpecify the directory to archive or extract files from
fileSpecify the file to archive or extract
archive_fileSpecify the archive file to create or extract from

Create a cpio archive from a list of files

Section titled “Create a cpio archive from a list of files”
Terminal window
ls | cpio -o > archive.cpio

Create a cpio archive named “archive.cpio” from the list of files in the current directory.

Terminal window
cpio -i < archive.cpio

Extract files from the cpio archive “archive.cpio” in the current directory.

Terminal window
ls | cpio -o | gzip > archive.cpio.gz

Create a compressed cpio archive named “archive.cpio.gz” from the list of files in the current directory.

Extract files from a compressed cpio archive

Section titled “Extract files from a compressed cpio archive”
Terminal window
gunzip -c archive.cpio.gz | cpio -i

Extract files from the compressed cpio archive “archive.cpio.gz” in the current directory.

Copy files and directories into a cpio archive

Section titled “Copy files and directories into a cpio archive”
Terminal window
find . -print | cpio -oBv > archive.cpio

Copy files and directories from the current directory into a cpio archive named “archive.cpio” with verbose output.

Extract specific files from a cpio archive

Section titled “Extract specific files from a cpio archive”
Terminal window
cpio -i -F archive.cpio 'file1.txt' 'directory1/file2.txt'

Extract specific files “file1.txt” and “file2.txt” from the cpio archive “archive.cpio”.

Copy files with extended attributes into a cpio archive

Section titled “Copy files with extended attributes into a cpio archive”
Terminal window
find . -print | cpio -oX@ > archive.cpio

Copy files from the current directory into a cpio archive “archive.cpio” preserving extended attributes.

Extract files with extended attributes from a cpio archive

Section titled “Extract files with extended attributes from a cpio archive”
Terminal window
cpio -i -dX@ < archive.cpio

Extract files from the cpio archive “archive.cpio” while preserving extended attributes.

To use the cpio command in MacOS, execute the following command:

Terminal window
cpio --create < files.txt

What is the syntax for creating a cpio archive in MacOS?

Section titled “What is the syntax for creating a cpio archive in MacOS?”

To create a cpio archive in MacOS, use the following command syntax:

Terminal window
find . -depth -print0 | cpio --null --create --format=newc > archive.cpio

How can I extract files from a cpio archive in MacOS?

Section titled “How can I extract files from a cpio archive in MacOS?”

To extract files from a cpio archive in MacOS, use the following command:

Terminal window
cpio --extract < archive.cpio

How do I list the contents of a cpio archive in MacOS?

Section titled “How do I list the contents of a cpio archive in MacOS?”

To list the contents of a cpio archive in MacOS, use the following command:

Terminal window
cpio --list < archive.cpio

How can I create a compressed cpio archive in MacOS?

Section titled “How can I create a compressed cpio archive in MacOS?”

To create a compressed cpio archive in MacOS, use the following command:

Terminal window
find . -depth -print0 | cpio --null --create --file=archive.cpio.gz --format=newc | gzip

What is the command for adding files to an existing cpio archive in MacOS?

Section titled “What is the command for adding files to an existing cpio archive in MacOS?”

To add files to an existing cpio archive in MacOS, use the following command:

Terminal window
find additional_files -depth -print0 | cpio --null --append < archive.cpio

How can I extract a specific file from a cpio archive in MacOS?

Section titled “How can I extract a specific file from a cpio archive in MacOS?”

To extract a specific file from a cpio archive in MacOS, use the following command:

Terminal window
cpio --extract --pattern="specific_file.txt" < archive.cpio

What is the command to create an incremental cpio archive in MacOS?

Section titled “What is the command to create an incremental cpio archive in MacOS?”

To create an incremental cpio archive in MacOS, use the following command:

Terminal window
find . -depth -print0 | cpio --null --create --format=newc --reset-access-time > incremental.cpio

How can I extract files interactively from a cpio archive in MacOS?

Section titled “How can I extract files interactively from a cpio archive in MacOS?”

To extract files interactively from a cpio archive in MacOS, use the following command:

Terminal window
cpio --extract --verbose < archive.cpio
  • Creating archives
  • Extracting files from archives
  • Copying files between directories
  • Preserving file permissions and ownership
  • Incremental backups
  • Verifying data integrity
  • Listing contents of archives