在RedHat中cp覆蓋時,無論加什麼參數-f之類的還是提示是否覆蓋,這在編寫腳本或者大量cp覆蓋操作的時候是不能忍受的。
做個試驗說明這個問題
創建一個實驗環境:
[[email protected] ~]# mkdir -p test/1111
[[email protected] ~]# mkdir -p test/2222
[[email protected] ~]# touch test/1111/aaa.txt
目錄結構如下:
[[email protected] ~]# ls -R test/
test/:
1111 2222
test/1111:
aaa.txt
【注】test下有兩個文件夾1111,2222。1111中有個aaa.txt,現在要把aaa.txt拷貝到2222下。
第一次拷貝因為2222文件夾下沒有文件,執行成功。
[[email protected] ~]# cp -R test/1111/aaa.txt test/2222/
同樣再執行一遍這個命令,提示:
[[email protected] ~]# cp -R test/1111/aaa.txt test/2222/
cp:是否覆蓋‘test/2222/aaa.txt’?
[這是因為2222下已經有一個aaa.txt了,看一下:
[[email protected] ~]# ls -R test/
test/:
1111 2222
test/1111:
aaa.txt
test/2222:
aaa.txt
要是大量文件已經存在而且必須覆蓋,想要不提示該怎麼做呢?
[[email protected] ~]# cp -R -f test/1111/aaa.txt test/2222/
cp:是否覆蓋‘test/2222/aaa.txt’?
加-f也不管用。。。
這是因為redhat給cp建了一個alias:
[[email protected] ~]# 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 vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
當你執行cp時,其實執行的是cp -i
可以這樣解決
[[email protected] ~]# vi ~/.bashrc
在alias cp='cp -i'前加上"#"注釋掉這行,:wq!保存推出
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
#alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
重新登陸後生效