接前一篇文章:cpio命令详解(2)
4. cpio命令实例
(1)基础类命令
cpio {-?|--help|--usage|--version}
1)提供简短帮助摘要并退出
命令为:
bash
cpio -?
或
cpio --help
实际命令及结果如下:
bash
$ 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-size=BLOCK-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-size=NUMBER Set the I/O block size to the given NUMBER of
bytes
-D, --directory=DIR Change to directory DIR
--force-local Archive file is local, even if its name contains
colons
-H, --format=FORMAT 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, --warning=FLAG 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, --message=STRING Print STRING when the end of a volume of the
backup media is reached
--rsh-command=COMMAND 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-file=FILE Read additional patterns specifying filenames to
extract or list from FILE
--only-verify-crc When reading a CRC format archive, only verify the
CRC's of each file in the archive, don't 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 Don't 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-cpio@gnu.org>.
cpio --help命令也是同样结果。
2)打印一条简短的使用说明并退出
命令为:
bash
cpio --usage
实际命令及结果如下:
bash
$ 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-size=BLOCK-SIZE]
[--io-size=NUMBER] [--directory=DIR] [--force-local]
[--format=FORMAT] [--quiet] [--owner=[USER][:.][GROUP]] [--verbose]
[--dot] [--warning=FLAG] [--file=[[USER@]HOST:]FILE-NAME]
[--message=STRING] [--rsh-command=COMMAND] [--swap] [--nonmatching]
[--numeric-uid-gid] [--rename] [--swap-bytes] [--swap-halfwords]
[--to-stdout] [--pattern-file=FILE] [--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)打印程序版本号并退出
命令为:
bash
cpio --version
实际命令及结果如下:
bash
$ 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)类命令
准备工作:创建实验文件夹及其下文件:
bash
$ cd cpio_test/
habit@ubuntu:~/work/shiyans/cpio_test$ mkdir bin conf etc lib
habit@ubuntu:~/work/shiyans/cpio_test$ touch etc/test.txt
habit@ubuntu:~/work/shiyans/cpio_test$
habit@ubuntu:~/work/shiyans/cpio_test$ tree
.
├── bin
├── conf
├── etc
│ └── test.txt
└── lib
4 directories, 1 file
habit@ubuntu:~/work/shiyans/cpio_test$
bash
$ cat init
#!/bin/bash
1)基础打包(当前目录)
命令为:
bash
ls | cpio -ov > backup.cpio # 仅文件(不含子目录)
find . -depth | cpio -ov > all.cpio # 递归全目录(推荐)
实际命令及结果如下:
bash
$ ls | cpio -ov > backup.cpio
backup.cpio
bin
conf
etc
init
lib
1 block
bash
$ 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参数,则实际结果为:
bash
$ ls | cpio -o > backup.cpio
1 block
$ find . -depth | cpio -o > all.cpio
2 blocks
2)安全打包(含空格)
命令为:
bash
find . -print0 | cpio -0ov > safe.cpio # -print0 + -0 防空格/特殊字符
实际命令及结果如下:
bash
$ 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"参数使用。
分解动作:
bash
$ find . -print0
../init./etc./etc/test.txt./bin./conf./lib$
3)initramfs 专用(强制newc)
命令为:
bash
find . | cpio -ov -H newc > initramfs.cpio # 内核initrd标准格式
实际命令及结果如下:
bash
$ 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必须用newc:
bin:旧二进制(废弃)odc:旧ASCIInewc:SVR4便携格式(initramfs专用)crc:带校验(newc+ CRC)tar/ustar:兼容tar
find命令带/不带-print0的区别如下:
bash
$ find .
.
./init
./etc
./etc/test.txt
./bin
./conf
./lib
bash
$ find . -print0
../init./etc./etc/test.txt./bin./conf./lib$
4)指定文件/排除
命令为:
bash
find . -name "*.c" | cpio -ov > src.cpio # 仅C文件
find . -path "./tmp" -prune -o -print | cpio -ov > no_tmp.cpio # 排除tmp
在原有测试文件夹下添加test.c以及tmp文件夹:
bash
$ 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
实际命令及结果如下:
bash
$ find . -name "*.c" | cpio -ov > src.cpio
./test.c
1 block
bash
$ 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
更多内容请看下回。