在linux中install命令和cp命令的区别
基本上,在Makefile里会用到install,其他地方会用cp命令。
它们完成同样的任务——拷贝文件,它们之间的区别主要如下:
1、最重要的一点,如果目标文件存在,cp会先清空文件后往里写入新文件,而install则会先删除掉原先的文件然后写入新文件。这是因为往正在使用的文件中写入内容可能会导致一些问题,比如说写入正在执行的文件可能会失败,比如说往已经在持续写入的文件句柄中写入新文件会产生错误的文件。而使用install先删除后写入(会生成新的文件句柄)的方式去安装就能避免这些问题了;
2、install命令会恰当地处理文件权限的问题。比如说,install-c会把目标文件的权限设置为rwxr-xr-x;
3、install命令可以打印出更多更合适的debug信息,还会自动处理SElinux上下文的问题。
转:http://blog.csdn.NET/stevenliyong/article/details/4663583
install-copyfilesandsetattributes
install在做拷贝的同时,设置attributes.
因此Makefile中尽量使用install命令。
例如
@install-d/usr/bin
@install-p-D-m0755targets/usr/bin
相当于
@mkdir-p/usr/bin
@cptargets/usr/bin
@chmod755/usr/bin/targets
@touch/usr/bin/tagets<----更新文件时间戳
install命令好强大啊。
另外@前缀的意思是不在控制台输出结果。
转载:http://www.cnblogs.com/wwwsinagogogo/archive/2011/08/15/2139124.html
【概述】
Install和cp类似,都可以将文件/目录拷贝到指定的地点。但是,install允许你控制目标文件的属性。install通常用于程序的makefile,使用它来将程序拷贝到目标(安装)目录。
【语法】
install[OPTION]...[-T]SOURCEDEST
install[OPTION]...SOURCE...DIRECTORY
install[OPTION]...-tDIRECTORYSOURCE...
install[OPTION]...-dDIRECTORY...
如果指定了两个文件名,`install''将第一个文件拷贝到第二个
如果使用了`--target-directory''(`-t'')选项,或者如果最后一个文件是一个目录并且没有使用`--no-target-directory''(`-T'')选项,`install''将每一个源文件拷贝到指定的目录,目标文件名与SOURCE文件名相同。
如果使用了`--directory''(`-d'')选项,`install''将逐级创建缺失的目标目录
【常用选项】
-s:对待拷贝的可执行文件进行strip操作,取出文件中的符号表。(一般在做成nandrom时去除符号表,NFS时为了调试方便,一般不会使用此选项)
-d(--directory):创建制定的目录结构(逐级创建)。如,指定安装位置为/usr/local/aaa/bbb,/usr/loacal已存在,install会帮助我们创建aaa和bbb目录,并把程序安装到指定位置
IfwehandwriteaMakefile,weshouldalwayssticktoinstallinsteadofusingcpfortheinstallationcommands.Notonlyisitmoreconvenient,butitdoesthingsright(cpdoesthingswrong).
Forexample,ifweattempttoupdate/bin/bash,whichiscurrentlyrunning,with“cp.../bin/bash”,wegeta“textbusy”error.Ifweattempttoupdate/lib/libc.so.6with“cp.../lib/libc.so.6”,thenweeitherget“textbusy”(inancientversionsofLinux)orbreakseachandeveryrunningprogramwithinafractionofasecond(inrecentversionsofLinux).installdoesthethingrightinbothsituations.
Thereasonwhycpfailsisthatitsimplyattemptstoopenthedestinationfileinwrite-onlymodeandwritethenewcontents.ThiscausesproblembecauseLinux(andallcontemporaryUnicesaswellasMicrosoftWindows)usesmemorymapping(mmap)toloadexecutablesanddynamiclibraries.
Thecontentsofanexecutableordynamiclibraryaremmap’dintothelinearaddressspaceofrelevantprocesses.Therefore,anychangeintheunderlyingfileaffectsthemmap’dmemoryregionsandcanpotentiallybreakprograms.(MAP_PRIVATEguaranteeschangesbyprocessestothosememoryregionsarehandledbyCOWwithoutaffectingtheunderlyingfile.Onthecontrary,POSIXleavestoimplementationswhetherCOWshouldbeusedifwww.baiyuewang.nettheunderlyingfileismodified.Infact,forpurposeofefficiency,inLinux,suchmodificationsarevisibletoprocesseseventhoughMAP_PRIVATEmayhavebeused.)
ThereisanoptionMAP_DENWRITEwhichdisallowsanymodificationtotheunderlyingfile,designedtoavoidsituationsdescribedabove.Executablesanddynamiclibrariesareallmmap’dwiththisoption.Unfortunately,itturnedoutMAP_DENYWRITEbecameasourceofDoSattacks,forcingLinuxtoignorethisoptioninrecentversions.
Executablesaremmap’dbythekernel(intheexecvesyscall).Forkernelcodes,MAP_DENYWRITEstillworks,andthereforeweget“textbusy”errorsifweattempttomodifytheexecutable.
Ontheotherhand,dynamiclibrariesaremmap’dbyuserspacecodes(forexample,byloaderslike/lib/ld-linux.so).ThesecodesstillpassMAP_DENYWRITEtothekernel,butnewerkernelssilentlyignoresthisoption.Thebadconsequenceisthatyoucanbreakthewholesystemifyouthinkyou’reonlyupgradingtheCruntimelibrary.
Then,howdoesinstallsolvethisproblem?Verysimple–unlinkingthefilebeforewritingthenewone.Thentheoldfile(nolongerpresentindirectoryentriesbutstillindiskuntilthelastprogramreferringtoitexits)andthenewfilehavedifferentinodes.Programsstartedbeforetheupgrading(continuingusingtheoldfile)andthoseaftertheupgrading(usingthenewversion)willbothbehappy.
记得在大学的时候在编译LFS6的时候,一直搞不懂install的命令和cp以及和chmod,chgrp的区别?
工作之后才明白一个Running的进程不能随便进行cp,经常会提示"textbusy",运维部的前辈们给的建议是采用mv来替代cp,今天看起来前辈好像不知道install这个命令啊.
今天就简单介绍一下install命令.
installcopy文件列表且同时能够设置文件的属性(包括owner,group),通常用在Makefiles中用来copy程序到指定的目录.
常见的用法有以下3中形式:
1:install-d[option]DIRECTORY[DIRECTORY...]支持多个.类似mkdir-p支持递归.
例如:install-da/b/ce/f结果和mkdir-pa/b/ce/f一样.
2:install[option]SOURCEDEST
复制SOURCE文件(测试不能是目录)到DESTfile(文件).
installa/ec结果类似cpa/ec#注意c必须是文件.
有用选项-D
install-Dxa/b/c#效果类似mkdir-pa/b&&cpxa/b/c
3:install[option]SOURCE[SOURCE...]DIRECTORY
复制多个SOURCE文件到目的目录.
installa/d其中d是目录.
有用选项
-b:自动备份.
-m:设置安装文件的权限
-p:保留文件的timestamps.也就是说文件的timestaamps和source文件一样.当我们想要利用安装文件的mtime来跟踪文件的build时间而不是安装时间.
-s:Stripthesymboltablesfrominstalledbinaryexecutables.
-S:备份文件的后缀.
install-S.baknewold#old文件自动被mv为old.bak.
-v:verbose,打印install的文件的详细信息.
`-c''
Ignored;forcompatibilitywitholdUnixversionsof`install''.#用来兼容旧版的unix.
-C:(大写)
安装文件,但是如果目标文件和源文件一样(判断方法需要看看代码确认)就跳过,这样的好处是能够保持一样文件的mtime.
|
|