|
|
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已! |
|
恭喜您是本站第 1729362
位訪客!
登入 | 註冊
|
|
|
|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2014/9/23 8:15 |
- 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 @ 符碼記憶
|
|
討論串
|