Linux 使用 cp 命令强制覆盖功能

我们平时在 Linux 中使用 cp 命令时,当把文件从一个目录复制到另一个目录,且目录中具有同名文件时,系统会提示输入 y 来确认是否覆盖同名文件。

如果文件少的话,也无关紧要,但文件多的话,要一个一个确认简直太累了。更要命的是,即使我们加了 -rf 参数,还是会提示。

为什么会这样呢?

原因就是 cp 命令被系统设置了 别名

我们可以查看一下:

[root@localhost]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

也就是说,我们平时使用的 cp 命令,实际上是 cp -i

-i 参数是什么意思呢?

[root@localhost]# cp --help
-i, --interactive            prompt before overwrite (overrides a previous -n
                                  option)

也就是说,在覆盖之前会给出提示,这应该被视为系统的一种保险措施。

[En]

That is, it will give a hint before overwriting, which should be regarded as an insurance measure of the system.

那如果希望进行强制复制,不想一个一个输入 y,有什么办法呢?

使用原生命令:

[root@localhost]# /bin/cp -rf xxx

取消别名:

[root@localhost]# unalias cp

这样再使用 cp -rf 的时候就不会提示确认了。

但是,重要的是要记住在使用别名后恢复它。

[En]

However, it is important to remember to restore the alias after using it.

[root@localhost]# alias cp='cp -i'

更推荐使用方法1,因为该命令简单,不会导致忘记恢复别名的风险。

[En]

It is more recommended to use method 1, because the command is simple and does not cause the risk of forgetting to restore the alias.

以上就是本文的全部内容。如果您觉得不错,欢迎点赞转发关注。谢谢您一直鼓励我。

[En]

The above is the whole content of this article. If you think it is good, welcome * like * , * forward * and * follow * . Thank you for your support.

推荐阅读:

  • 计算机经典必读书籍
  • 技术博客 硬核后端开发技术干货,内容包括 Python、Django、Docker、Go、Redis、ElasticSearch、Kafka、Linux 等。
  • Go 程序员 Go 学习路线图,包括基础专栏,进阶专栏,源码阅读,实战开发,面试刷题,必读书单等一系列资源。
  • 面试题汇总 包括 Python、Go、Redis、MySQL、Kafka、数据结构、算法、编程、网络等各种常考题。

Original: https://www.cnblogs.com/alwaysbeta/p/16306688.html
Author: yongxinz
Title: Linux 使用 cp 命令强制覆盖功能

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523511/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球