茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672593 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
PIMG_00235.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]Linux 檔案複製強制覆寫, cp force overwrite

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Linux 檔案複製強制覆寫, cp force overwrite
Linux:檔案複製強制覆寫, cp force overwrite

相信大家若在使用 Linux,其中一個常用到的指令就是 copy, cp。
而若要複製到的地方已有相同名稱的檔案,
cp 會跳出訊息詢問是否要覆蓋 overwrite 也是很正常的。

但!如果我們就是要直接強制覆蓋,不想要確認訊息怎麼辦呢?
對 Linux 有概念的人大概會直覺地回答,那就加個 -f (force) 就好啦!
嗯,以上就是我遭遇到這個問題的整個思考流程,
結果…像下面那樣,還是會硬是跳出確認訊息!orz
[root@centos demo]# clear
[root@centos demo]# touch toBeOverwrite.txt
[root@centos demo]# touch toBeCopy.txt
[root@centos demo]# tree
.
|-- toBeCopy.txt
`-- toBeOverwrite.txt

0 directories, 2 files
[root@centos demo]# cp toBeCopy.txt toBeOverwrite.txt
cp:是否覆寫 ‘toBeOverwrite.txt’? n
[root@centos demo]# cp -f toBeCopy.txt toBeOverwrite.txt
cp:是否覆寫 ‘toBeOverwrite.txt’? n


那到底為什麼 cp 會一直出現是不是要覆寫的確認訊息呢?
明明 cp --help 裡面也提到 -f 的參數使用方式:
-f, --force if an existing destination file cannot be opened, remove it and try again。
可是即使加上了這個參數仍舊無法直接覆蓋 @@

後來查了一下資料發現,原來 cp 預設會被設定成別名:alias cp='cp -i',
也就是使用了參數 cp 時都會自動改用 cp -i,-i 是 interactive 互動的縮寫,
所以不管你說了什麼,他就是要來跟我們「互動」一下 orz。
察看 alias 中有放了哪些別名:
[root@centos demo]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@centos demo]#


解決方法很簡單,避開別名 cp,直接使用 /bin/cp 就可以囉!
[root@centos demo]# /bin/cp toBeCopy.txt toBeOverwrite.txt
[root@centos demo]#


或者用更直接的方式,取消 cp 的別名:
[root@centos demo]# unalias cp
[root@centos demo]# alias
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@centos demo]# cp toBeCopy.txt toBeOverwrite.txt
[root@centos demo]#



原文出處:Linux:檔案複製強制覆寫, cp force overwrite @ 符碼記憶
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]cp 不再詢問是否覆蓋的方式
cp 不再詢問是否覆蓋的方式

分類: 技術分享
作者: admin
31 七月 2008

在 Linux 使用 cp 遇到檔案覆蓋時,預設不會詢問並且直接覆蓋。但為了要達到詢問是否覆蓋的功能,大部份的 Linux 在~/.bashrc 都有設定 alias cp='cp -i' (prompt before overwrite)

但是問題來了,cp -i 只能回答 y 或 n,並沒有類似 unzip 有 [A]ll, [N]one (全部覆蓋或全部不覆蓋)的選項,所以每個檔案要回答,也造成了不少困擾,即使下了 cp -f ,也因為 alias 的設定自動變成了 cp -i -f 而失效。

如果要強制全部覆蓋有幾種方式:

1. 忽略 alias
/bin/cp  ....

2.全部自動回答 yes
cp ..... --reply=yes

3.取消 cp 的 alias
unalias cp
cp ....



原文出處: cp 不再詢問是否覆蓋的方式 - 哇寶部落格
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|