今天在看yocto里的脚本,使用shell的install命令用来进行文件拷贝操作,平时一般不用,就记录了一下。
在Linux生态系统中,有不同的方法将数据从源位置复制到目标位置,install命令行工具是其中之一。install命令与Linux的cp命令类似。
根据用户的要求,install命令被用来将数据复制到目标位置,同时还能进行更改文件权限、变更所有者等操作,所以install常用在makefile等场景种,用于拷贝程序或文件。
这个install命令工具的作者是David MacKenzie。
注意,install命令不能用来安装应用或包,和apt-get的功能是不同的,此命令是随着Linux发行版一起的。
语法:
install [OPTION]... [-T] SOURCE DEST
install [OPTION]... SOURCE... DIRECTORY
install [OPTION]... -t DIRECTORY SOURCE...
install [OPTION]... -d DIRECTORY...
前三行,用于将数据从源位置复制到目标位置,或将数据从多个源位置复制到目标位置,可以设置权限和改变文件所有权。
使用-T选项时,DEST是一个文件,而不是文件夹,这时只能拷贝一个文件,并且文件名会被改为DEST参数所指定的名字。如果不使用-T选项,则根据DEST参数来判断,如果是一个文件夹,就将文件拷贝进去,如果不是文件夹,就当作一个文件名来处理,将源文件拷贝成这个文件。
当时用-t选项时,第一个DIRECTORY参数是目标位置,否则目标位置都是最后一个参数。在使用-T选项时,如果DEST的文件路径不存在,可以使用-D选项,将路径创建出来,参见下面-D选项的帮助说明。
最后一行,用于创建所提供的目录。
install命令的帮助信息:
Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-c (ignored)
-C, --compare compare each pair of source and destination files, and
in some cases, do not modify the destination at all
-d, --directory treat all arguments as directory names; create all
components of the specified directories
-D create all leading components of DEST except the last,
or all components of --target-directory,
then copy SOURCE to DEST
-g, --group=GROUP set group ownership, instead of process' current group
-m, --mode=MODE set permission mode (as in chmod), instead of rwxr-xr-x
-o, --owner=OWNER set ownership (super-user only)
-p, --preserve-timestamps apply access/modification times of SOURCE files
to corresponding destination files
-s, --strip strip symbol tables
--strip-program=PROGRAM program used to strip binaries
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-v, --verbose print the name of each directory as it is created
--preserve-context preserve SELinux security context
-Z set SELinux security context of destination
file and each created directory to default type
--context[=CTX] like -Z, or if CTX is specified then set the
SELinux or SMACK security context to CTX
--help display this help and exit
--version output version information and exit
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:
none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/install>
or available locally via: info '(coreutils) install invocation'
例子
1,拷贝一个文件成另一个文件,并创建路径:
install -T -D abc.txt ~/top/sub/test.txt
注意,这时-T是可以省略的。
2,将一个文件拷贝到一个文件夹中
install test.txt copydata/
多个文件:
install test1.txt test2.txt copydata/
3,改变ownership,文件所有者
install -D -o usr file1 /folder/
使用-o选项,将文件的所有者设置为usr。
4,改变文件权限
install -D -m 777 file2 /owner/
使用-m选项,将文件权限设置为777.
5,创建一个或多个文件夹
install -d myname/myfolder
在当前目录创建myname目录,里面创建myfolder子目录。
-d选项表示所有的参数都是文件夹,并进行创建。
创建多个文件夹:
install -d myname/myfolder myname1/myfolder1
这种使用方式类似于mkdir -p。
6,使用通配符拷贝多个文件
install folder/* directory/
参考:
Linux install Command | A Quick Glance of Linux Install Command