Shell - install

今天在看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

install 命令,Linux install 命令详解:安装或升级软件或备份数据 - Linux 命令搜索引擎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值