shell命令学习(cp cp-i cp-n cp -r mv)

正确的 cp 命令的语法是 cp 源文件 目标文件
下面是正确的命令示例:

cp 源文件 目标文件

你需要将 “源文件” 替换为你要复制的实际文件名,并将 “目标文件” 替换为你要复制到的实际文件名。

如果 “目标文件” 是一个已存在的文件,它将被覆盖并复制为 “源文件” 的内容。如果 “目标文件” 是一个不存在的文件,将会创建一个新的目标文件,并将 “源文件” 的内容复制到其中。

linux@ubuntu:~/code$ mkdir test
linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test
linux@ubuntu:~/code$ cp hello.c test/
linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test
linux@ubuntu:~/code$ cd test
linux@ubuntu:~/code/test$ ls
hello.c
linux@ubuntu:~/code/test$ 

需要注意的是,如果目标文件已经存在,使用 cp 命令会覆盖目标文件。如果你希望在覆盖目标文件时给出提示或避免覆盖,可以使用 -i-n 选项。例如,cp -i 源文件 目标文件 将在覆盖目标文件时给出提示,而 cp -n 源文件 目标文件 将不会覆盖已存在的目标文件。

cp -icp 命令的一个选项,用于在复制文件时给出覆盖确认提示。当目标文件已经存在时,使用 cp -i 会提示是否覆盖已存在的目标文件。

下面是使用 cp -i 的正确命令示例:

cp -i 源文件 目标文件

将上述命令中的 “源文件” 替换为要复制的实际文件名,“目标文件” 替换为要复制到的实际文件名。

例如,如果要将文件 example.txt 复制到目标文件 backup.txt,但如果 backup.txt 文件已经存在,要先确认是否覆盖,可以执行以下命令:

cp -i example.txt backup.txt

在这种情况下,如果 backup.txt 已经存在,系统会提示你是否要覆盖。

希望这解答了你的问题。如果还有其他问题,请随时提问。

linux@ubuntu:~/code/test$ cd ..
linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test
linux@ubuntu:~/code$  cp -i hello.c test/
cp: overwrite `test/hello.c'? y
linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test
linux@ubuntu:~/code$ cd test
linux@ubuntu:~/code/test$ ls
hello.c
linux@ubuntu:~/code/test$ 

cp -ncp 命令的一个选项,用于在复制文件时避免覆盖已存在的目标文件。如果目标文件已经存在,使用 cp -n 将不会复制源文件,并且保留原有的目标文件。

下面是使用 cp -n 的正确命令示例:

cp -n 源文件 目标文件

将上述命令中的 “源文件” 替换为要复制的实际文件名,“目标文件” 替换为要复制到的实际文件名。

例如,如果要将文件 example.txt 复制到目标文件 backup.txt,但如果 backup.txt 文件已经存在,不想覆盖原有的目标文件,可以执行以下命令:

cp -n example.txt backup.txt

在这种情况下,如果 backup.txt 已经存在,系统将不会复制 example.txt,并保留原有的 backup.txt

linux@ubuntu:~/code$ cp -n hello1.c test/hello.c
linux@ubuntu:~/code$  ls
hello  hello1.c  hello.c  test
linux@ubuntu:~/code$ cd test
linux@ubuntu:~/code/test$ ls
hello.c
linux@ubuntu:~/code/test$ 

命令 cp -r 源文件 to 目标文件 是用于复制整个文件夹的正确命令。其中,-r 选项表示递归复制,可以复制整个文件夹及其子文件夹和文件。

以下是具体的命令示例:

cp -r 源文件夹 目标文件夹

你需要将 “源文件夹” 替换为你要复制的实际文件夹路径,将 “目标文件夹” 替换为你要复制到的目标路径。

例如,如果要将文件夹 folder1 复制到目标文件夹 folder2 中,可以执行以下命令:

cp -r folder1 folder2

这将递归复制 folder1 文件夹中的所有文件和子文件夹到 folder2 文件夹中。

请注意,在使用 cp -r 复制文件夹时,目标文件夹必须是一个不存在的目录,或者是已存在的目录名。如果目标文件夹不存在,复制操作将会创建一个新的目标文件夹,并将源文件夹的内容复制到其中。

linux@ubuntu:~/code/test$ cd ..
linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test
linux@ubuntu:~/code$ cp -r test test1
linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test  test1
linux@ubuntu:~/code$ cd test1
linux@ubuntu:~/code/test1$ ls
hello.c
linux@ubuntu:~/code/test1$ 

命令 mv 源文件 to 目标文件 是用于移动文件的正确命令。这个命令将源文件移动到目标文件,同时也可以用于对文件的重命名操作。

以下是具体的命令示例:

mv 源文件 目标文件
linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test  test1
linux@ubuntu:~/code$ mv test1 test2
linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test  test2
linux@ubuntu:~/code$ 

你需要将 “源文件” 替换为你要移动的实际文件名或文件路径,将 “目标文件” 替换为你要移动到的实际文件名或文件路径。

例如,如果要将文件 file.txt 移动到目标文件 folder/ 中,可以执行以下命令:

mv file.txt folder/

这将把 file.txt 移动到 folder/ 目录中。如果目标文件是一个已经存在的目录,源文件会被移动到该目录中,并保留原文件名。如果目标文件是一个不存在的文件,那么源文件会被移动并重命名为目标文件。

linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test  test2
linux@ubuntu:~/code$ mv test2 test
linux@ubuntu:~/code$ ls
hello  hello1.c  hello.c  test
linux@ubuntu:~/code$ cd test
linux@ubuntu:~/code/test$ ls
hello.c  test2
linux@ubuntu:~/code/test$ cd test2
linux@ubuntu:~/code/test/test2$ ls
hello.c
linux@ubuntu:~/code/test/test2$ 

如果你只是要对文件进行重命名,可以使用相同的命令,只需在目标文件中提供新的文件名。例如,如果要将文件 oldname.txt 重命名为 newname.txt,可以执行以下命令:

mv oldname.txt newname.txt

这将重命名 oldname.txtnewname.txt

需要注意的是,mv 命令既可以在同一个文件系统内移动文件,也可以在不同的文件系统之间移动文件。

  • 23
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一杯烟火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值