cpio命令详解(3)

张开发
2026/4/5 9:49:43 15 分钟阅读

分享文章

cpio命令详解(3)
接前一篇文章cpio命令详解24. cpio命令实例1基础类命令cpio {-?|--help|--usage|--version}1提供简短帮助摘要并退出命令为cpio -? 或 cpio --help实际命令及结果如下$ cpio -? Usage: cpio [OPTION...] [destination-directory] GNU cpio copies files to and from archives Examples: # Copy files named in name-list to the archive cpio -o name-list [ archive] # Extract files from the archive cpio -i [ archive] # Copy files named in name-list to destination-directory cpio -p destination-directory name-list Main operation mode: -i, --extract Extract files from an archive (run in copy-in mode) -o, --create Create the archive (run in copy-out mode) -p, --pass-through Run in copy-pass mode -t, --list Print a table of contents of the input Operation modifiers valid in any mode: --block-sizeBLOCK-SIZE Set the I/O block size to BLOCK-SIZE * 512 bytes -B Set the I/O block size to 5120 bytes -c Use the old portable (ASCII) archive format -C, --io-sizeNUMBER Set the I/O block size to the given NUMBER of bytes -D, --directoryDIR Change to directory DIR --force-local Archive file is local, even if its name contains colons -H, --formatFORMAT Use given archive FORMAT --quiet Do not print the number of blocks copied -R, --owner[USER][:.][GROUP] Set the ownership of all files created to the specified USER and/or GROUP -v, --verbose Verbosely list the files processed -V, --dot Print a . for each file processed -W, --warningFLAG Control warning display. Currently FLAG is one of none, truncate, all. Multiple options accumulate. Operation modifiers valid in copy-in and copy-out modes -F, --file[[USER]HOST:]FILE-NAME Use this FILE-NAME instead of standard input or output. Optional USER and HOST specify the user and host names in case of a remote archive -M, --messageSTRING Print STRING when the end of a volume of the backup media is reached --rsh-commandCOMMAND Use COMMAND instead of rsh Operation modifiers valid only in copy-in mode: -b, --swap Swap both halfwords of words and bytes of halfwords in the data. Equivalent to -sS -f, --nonmatching Only copy files that do not match any of the given patterns -I [[USER]HOST:]FILE-NAME Archive filename to use instead of standard input. Optional USER and HOST specify the user and host names in case of a remote archive -n, --numeric-uid-gid In the verbose table of contents listing, show numeric UID and GID -r, --rename Interactively rename files -s, --swap-bytes Swap the bytes of each halfword in the files -S, --swap-halfwords Swap the halfwords of each word (4 bytes) in the files --to-stdout Extract files to standard output -E, --pattern-fileFILE Read additional patterns specifying filenames to extract or list from FILE --only-verify-crc When reading a CRC format archive, only verify the CRCs of each file in the archive, dont actually extract the files Operation modifiers valid only in copy-out mode: -A, --append Append to an existing archive. --device-independent, --reproducible Create device-independent (reproducible) archives --ignore-devno Dont store device numbers -O [[USER]HOST:]FILE-NAME Archive filename to use instead of standard output. Optional USER and HOST specify the user and host names in case of a remote archive --renumber-inodes Renumber inodes Operation modifiers valid only in copy-pass mode: -l, --link Link files instead of copying them, when possible Operation modifiers valid in copy-in and copy-out modes: --absolute-filenames Do not strip file system prefix components from the file names --no-absolute-filenames Create all files relative to the current directory Operation modifiers valid in copy-out and copy-pass modes: -0, --null Filenames in the list are delimited by null characters instead of newlines -a, --reset-access-time Reset the access times of files after reading them -L, --dereference Dereference symbolic links (copy the files that they point to instead of copying the links). Operation modifiers valid in copy-in and copy-pass modes: -d, --make-directories Create leading directories where needed -m, --preserve-modification-time Retain previous file modification times when creating files --no-preserve-owner Do not change the ownership of the files --sparse Write files with large blocks of zeros as sparse files -u, --unconditional Replace all files unconditionally -?, --help give this help list --usage give a short usage message --version print program version Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options. Report bugs to bug-cpiognu.org.cpio --help命令也是同样结果。2打印一条简短的使用说明并退出命令为cpio --usage实际命令及结果如下$ cpio --usage Usage: cpio [-ioptBcvVbfnrsSAl0aLdmu?] [-C NUMBER] [-D DIR] [-H FORMAT] [-R [USER][:.][GROUP]] [-W FLAG] [-F [[USER]HOST:]FILE-NAME] [-M STRING] [-I [[USER]HOST:]FILE-NAME] [-E FILE] [-O [[USER]HOST:]FILE-NAME] [--extract] [--create] [--pass-through] [--list] [--block-sizeBLOCK-SIZE] [--io-sizeNUMBER] [--directoryDIR] [--force-local] [--formatFORMAT] [--quiet] [--owner[USER][:.][GROUP]] [--verbose] [--dot] [--warningFLAG] [--file[[USER]HOST:]FILE-NAME] [--messageSTRING] [--rsh-commandCOMMAND] [--swap] [--nonmatching] [--numeric-uid-gid] [--rename] [--swap-bytes] [--swap-halfwords] [--to-stdout] [--pattern-fileFILE] [--only-verify-crc] [--append] [--device-independent] [--reproducible] [--ignore-devno] [--renumber-inodes] [--link] [--absolute-filenames] [--no-absolute-filenames] [--null] [--reset-access-time] [--dereference] [--make-directories] [--preserve-modification-time] [--no-preserve-owner] [--sparse] [--unconditional] [--help]3打印程序版本号并退出命令为cpio --version实际命令及结果如下$ cpio --version cpio (GNU cpio) 2.13 Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Phil Nelson, David MacKenzie, John Oleynick, and Sergey Poznyakoff.2打包Copy-out类命令准备工作创建实验文件夹及其下文件$ cd cpio_test/ habitubuntu:~/work/shiyans/cpio_test$ mkdir bin conf etc lib habitubuntu:~/work/shiyans/cpio_test$ touch etc/test.txt habitubuntu:~/work/shiyans/cpio_test$ habitubuntu:~/work/shiyans/cpio_test$ tree . ├── bin ├── conf ├── etc │ └── test.txt └── lib 4 directories, 1 file habitubuntu:~/work/shiyans/cpio_test$$ cat init #!/bin/bash1基础打包当前目录命令为ls | cpio -ov backup.cpio # 仅文件不含子目录 find . -depth | cpio -ov all.cpio # 递归全目录推荐实际命令及结果如下$ ls | cpio -ov backup.cpio backup.cpio bin conf etc init lib 1 block$ find . -depth | cpio -ov all.cpio ./all.cpio ./backup.cpio ./init ./etc/test.txt ./etc ./bin ./conf ./lib . 2 blocks涉及参数-o或--create执行copy-out模式建立备份档。-v或--verbose详细显示指令的执行过程。如果不加-v参数则实际结果为$ ls | cpio -o backup.cpio 1 block $ find . -depth | cpio -o all.cpio 2 blocks2安全打包含空格命令为find . -print0 | cpio -0ov safe.cpio # -print0 -0 防空格/特殊字符实际命令及结果如下$ find . -print0 | cpio -0ov safe.cpio . ./init ./etc ./etc/test.txt ./bin ./conf ./safe.cpio ./lib 1 block $ ls bin conf etc init lib safe.cpio涉及参数-0或--null接受新增列控制字符通常配合find指令的-print0参数使用。分解动作$ find . -print0 ../init./etc./etc/test.txt./bin./conf./lib$3initramfs 专用强制newc命令为find . | cpio -ov -H newc initramfs.cpio # 内核initrd标准格式实际命令及结果如下$ find . | cpio -ov -H newc initramfs.cpio . ./init ./etc ./etc/test.txt cpio: File ./initramfs.cpio grew, 512 new bytes not copied ./initramfs.cpio ./bin ./conf ./lib 3 blocks涉及参数-H 备份格式指定备份时欲使用的文件格式new/crc/tar/ustar。-H参数详细说明指定格式initramfs/initrd必须用newcbin旧二进制废弃odc旧ASCIInewcSVR4便携格式initramfs专用crc带校验newc CRCtar/ustar兼容tarfind命令带/不带-print0的区别如下$ find . . ./init ./etc ./etc/test.txt ./bin ./conf ./lib$ find . -print0 ../init./etc./etc/test.txt./bin./conf./lib$4指定文件/排除命令为find . -name *.c | cpio -ov src.cpio # 仅C文件 find . -path ./tmp -prune -o -print | cpio -ov no_tmp.cpio # 排除tmp在原有测试文件夹下添加test.c以及tmp文件夹$ ls bin conf etc init lib $ touch test.c $ mkdir tmp $ ls bin conf etc init lib test.c tmp $ tree . ├── bin ├── conf ├── etc │ └── test.txt ├── init ├── lib ├── test.c └── tmp 5 directories, 3 files实际命令及结果如下$ find . -name *.c | cpio -ov src.cpio ./test.c 1 block$ find . -path ./tmp -prune -o -print | cpio -ov no_tmp.cpio . ./init ./etc ./etc/test.txt ./no_tmp.cpio ./bin ./conf ./test.c ./lib 1 block更多内容请看下回。

更多文章