Chinaunix

标题: 请问如何CP的时候不覆盖文件也不出现提示信息 [打印本页]

作者: @sky    时间: 2009-01-07 17:57
标题: 请问如何CP的时候不覆盖文件也不出现提示信息
让它默认不覆盖文件
作者: ttplay    时间: 2009-01-07 18:00
原帖由 @sky 于 2009-1-7 17:57 发表
让它默认不覆盖文件

自己写一个, 简单吧
作者: yezhulin1982    时间: 2009-01-07 19:30

作者: yezhulin1982    时间: 2009-01-07 19:32
原帖由 @sky 于 2009-1-7 17:57 发表
让它默认不覆盖文件


默认不覆盖文件但又不提示,好像有点矛盾。
作者: 我是DBA    时间: 2009-01-07 20:46
  1. cp -i --reply=no
复制代码

作者: 我是DBA    时间: 2009-01-07 20:47
[test@erpdataserver ~]$ ll ttt/*
-rw-rw-r--  1 test test  0 Jan  7 20:41 ttt/1
-rw-rw-r--  1 test test  0 Jan  7 20:43 ttt/2
-rw-rw-r--  1 test test 47 Jan  4 08:11 ttt/file1
-rw-rw-r--  1 test test 46 Jan  4 08:11 ttt/file2
-rw-rw-r--  1 test test 47 Jan  4 08:11 ttt/file3
[test@erpdataserver ~]$ ll ttt2/*
-rw-rw-r--  1 test test  0 Jan  7 20:42 ttt2/1
-rw-rw-r--  1 test test 47 Jan  7 20:39 ttt2/file1
-rw-rw-r--  1 test test 46 Jan  7 20:39 ttt2/file2
-rw-rw-r--  1 test test 47 Jan  7 20:39 ttt2/file3
[test@erpdataserver ~]$ cp -i ttt/* ttt2 --reply=no
[test@erpdataserver ~]$ ll ttt2/*
-rw-rw-r--  1 test test  0 Jan  7 20:42 ttt2/1
-rw-rw-r--  1 test test  0 Jan  7 20:43 ttt2/2
-rw-rw-r--  1 test test 47 Jan  7 20:39 ttt2/file1
-rw-rw-r--  1 test test 46 Jan  7 20:39 ttt2/file2
-rw-rw-r--  1 test test 47 Jan  7 20:39 ttt2/file3
作者: ynchnluiti    时间: 2009-01-07 20:50
原帖由 我是DBA 于 2009-1-7 20:46 发表
cp -i --reply=no

faint, 我这个版本没--reply了。被废弃了
作者: 我是DBA    时间: 2009-01-07 20:52
标题: 回复 #7 ynchnluiti 的帖子
真的啊?什么版本,废了它吧
作者: ynchnluiti    时间: 2009-01-07 20:58
原帖由 我是DBA 于 2009-1-7 20:52 发表
真的啊?什么版本,废了它吧
$ cp --version
cp (GNU coreutils) 6.10
Copyright (C) 2008 Free Software Foundation, Inc.

$ cp -i --reply=no dd a
cp: the --reply option is deprecated; use -i or -f instead

不过是不是可以-i,加expect

没试过,有时间再研究
作者: 我是DBA    时间: 2009-01-07 21:00
标题: 回复 #9 ynchnluiti 的帖子
你版本太高了
[test@erpdataserver ~]$ cp --version
cp (coreutils) 5.2.1
Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering.

Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
作者: biglazybug    时间: 2009-01-07 21:01
我很感兴趣的是楼主为什么要有这样的需求呢
作者: 我是DBA    时间: 2009-01-07 21:11
估计用来作备份吧,我也用到过
作者: welcome008    时间: 2009-01-07 21:15
其实说到这里

我觉得,既然有yes这个命令

那也需要整个no命令出来
作者: 我是DBA    时间: 2009-01-07 21:22
标题: 回复 #13 welcome008 的帖子
你是说什么来着?
--reply么
有yes也有no啊
作者: haimming    时间: 2009-01-07 21:25
标题: 回复 #14 我是DBA 的帖子
yes本身就是个命令
作者: biglazybug    时间: 2009-01-07 21:35
标题: 回复 #15 haimming 的帖子
不是那个yes吧
作者: welcome008    时间: 2009-01-07 21:46
原帖由 biglazybug 于 2009-1-7 21:35 发表
不是那个yes吧



我指的就是那个yes

我觉得有源代码的话
整个no出来应该很简单吧
作者: haimming    时间: 2009-01-07 21:51
标题: 回复 #16 biglazybug 的帖子


你这次的猫比较可爱,没有再虐待它了?
作者: ynchnluiti    时间: 2009-01-07 21:52
原帖由 welcome008 于 2009-1-7 21:46 发表



我指的就是那个yes

我觉得有源代码的话
整个no出来应该很简单吧

应该不难。不知道用的地方多不多
作者: biglazybug    时间: 2009-01-07 22:07
标题: 回复 #18 haimming 的帖子
下一张继续虐待.....
作者: 我是DBA    时间: 2009-01-07 22:22
标题: 回复 #17 welcome008 的帖子
不明白,学习
作者: chenwenming    时间: 2009-01-07 22:28
标题: 回复 #21 我是DBA 的帖子
\cp
作者: haimming    时间: 2009-01-07 22:40
标题: 回复 #21 我是DBA 的帖子

  1. [rockfall@localhost tmp]$ echo "">a.txt
  2. [rockfall@localhost tmp]$ more a.txt

  3. [rockfall@localhost tmp]$ cp -i ../sor ./a.txt
  4. cp: overwrite `./a.txt'? n
  5. [rockfall@localhost tmp]$ yes|cp -i ../sor ./a.txt    <==估计welcome008 是想这样用no吧?
  6. cp: overwrite `./a.txt'? [rockfall@localhost tmp]$ more a.txt
  7. id:0,count:18,doc:10,CHI:9,info:9.3897866898
  8. id:1,count:2813,doc:1186,CHI:8.8059808988,info:9.5329395664
  9. id:2,count:2627,doc:1193,CHI:10.4851413849,info:9.3680302230
复制代码

作者: haimming    时间: 2009-01-07 22:54
刚才看yes时,看到一个等效脚本,改了个no的

  1. #!/bin/bash
  2. # no.sh

  3.   no ()
  4. { # Trivial emulation of "no" ...
  5.    local DEFAULT_TEXT="no"
  6.     while [ true ]     # Endless loop.
  7.     do
  8.        if [ -z "$1" ]
  9.        then
  10.           echo "$DEFAULT_TEXT"
  11.        else            # If argument ...
  12.           echo "$1"    # ... expand and echo it.
  13.        fi
  14.     done               # The only things missing are the
  15.   }

  16. no
复制代码

测试一下

  1. [rockfall@localhost tmp]$ cp -i ../sor ./a.txt
  2. cp: overwrite `./a.txt'? y
  3. [rockfall@localhost tmp]$ more a.txt
  4. id:0,count:18,doc:10,CHI:9,info:9.3897866898
  5. id:1,count:2813,doc:1186,CHI:8.8059808988,info:9.5329395664
  6. id:2,count:2627,doc:1193,CHI:10.4851413849,info:9.3680302230
  7. [rockfall@localhost tmp]$ echo "">a.txt
  8. [rockfall@localhost tmp]$ more a.txt

  9. [rockfall@localhost tmp]$ ./no.sh|cp -i ../sor ./a.txt
  10. cp: overwrite `./a.txt'? [rockfall@localhost tmp]$ more a.txt

  11. [rockfall@localhost tmp]$
复制代码


好像还行

echo "n"|cp -i a/xx b/

[ 本帖最后由 haimming 于 2009-1-8 00:37 编辑 ]
作者: welcome008    时间: 2009-01-08 00:35
说到脚本,猛然想到:

yes|sed 's/y/no/'|cp -i a b

这样应该可以

可是实验结果却是覆盖了,为啥呢?
作者: haimming    时间: 2009-01-08 00:38
标题: 回复 #25 welcome008 的帖子
呵呵,直接echo "n" 就可以了
作者: ynchnluiti    时间: 2009-01-08 00:40
原帖由 welcome008 于 2009-1-8 00:35 发表
说到脚本,猛然想到:

yes|sed 's/y/no/'|cp -i a b

这样应该可以

可是实验结果却是覆盖了,为啥呢?

我测试的没覆盖。
作者: haimming    时间: 2009-01-08 00:41
标题: 回复 #25 welcome008 的帖子
确实没盖!
作者: dayan_he    时间: 2009-01-08 00:41
我测试的也没覆盖

yes no|cp -i  a b
作者: haimming    时间: 2009-01-08 00:46
原帖由 dayan_he 于 2009-1-8 00:41 发表
我测试的也没覆盖

yes no|cp -i  a b


原来忘了yes的最基本应用了,呵呵

so,,,有了yes,就有了no,上面的脚本是多余的了,惭愧惭愧!

[ 本帖最后由 haimming 于 2009-1-8 00:49 编辑 ]
作者: welcome008    时间: 2009-01-08 00:50
靠,太欺侮人了:

bs2:liy\> cat > a
aa
bs2:liy\> cat > b
cc
bs2:liy\> yes | sed 's/y/no/'|cp -i a b
bs2:liy\> cat b
aa
bs2:liy\>
bs2:liy\> yes | sed 's/y/no/'
no
no
no
no
....
作者: welcome008    时间: 2009-01-08 00:50
对了,我的是solaris9
作者: welcome008    时间: 2009-01-08 00:52
原帖由 dayan_he 于 2009-1-8 00:41 发表
我测试的也没覆盖

yes no|cp -i  a b



晕倒,我瞎折腾呢!:em11: :em11: :em11:

不过为啥我那样会覆盖呢?
作者: haimming    时间: 2009-01-08 00:54
你的cp有i参数吗?

没脑,问得太傻了!

[ 本帖最后由 haimming 于 2009-1-8 00:56 编辑 ]
作者: 我是DBA    时间: 2009-01-08 08:24
原帖由 dayan_he 于 2009-1-8 00:41 发表
我测试的也没覆盖

yes no|cp -i  a b

这个我喜欢,学习了。
作者: 我是DBA    时间: 2009-01-08 08:25
标题: 回复 #33 welcome008 的帖子
终于明白你的意思了。
原来是这个意思
谢谢
作者: @sky    时间: 2009-01-08 08:46
谢谢诸位帮助,非常感谢
作者: ritto    时间: 2010-03-05 12:52
网中人的方法  \cp
作者: allway    时间: 2010-08-24 18:29
yes命令好
作者: xiaopan3322    时间: 2010-08-24 20:51
回复 1# @sky

    很奇怪为啥会有这样的需求
    为什么不用rsync呢,




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2