Linux基础之文件管理三兄弟(cp、mv、rm)

  我们日常工作中对于文件的操作用到最多的几个我觉得应该是是复制、剪切、移动、重命名、删除这五个。因此今天主要介绍关于上面五个操作在Linux中实现涉及到的三个命令:cp、mv、rm。


    cp是copy的简写,从名字我们大致就能知道它的作用,它主要是用来复制文件的。虽然这个命令很简单,可是它有很多细节需要我们注意,如果稍有忽略那么命令很有可能无法达到我们想要的结果。

以下信息来自man手册

NAME

       cp - copy files and directories


单源复制(源文件为文本文件)  

其命令格式为:

       cp [OPTION]... [-T] SOURCE DEST

如果DEST不存在:则事先创建此文件,并复制源文件的数据流至DEST中;

如果DEST存在:1、如果DEST是非目录文件,则覆盖目标文件;2、如果DEST是目录文件,则先在DEST目录下创建同名文件并复制数据流


多源复制(源文件为目录文件,且其下有多个文件)

其命令格式:

       cp [OPTION]... SOURCE... DIRECTORY

       cp [OPTION]... -t DIRECTORY SOURCE...

如果DEST不存在:报错

如果DEST存在:1、如果DEST是非目录文件,报错;2、如果DEST是目录文件,则分别复制每个文件至目标目录中,并保持原名。


常用选项:

     -i, --interactive  

              prompt before overwrite (overrides a previous -n option)

                     交互式复制,覆盖之前提醒用户确认

    -R, -r, --recursive

              copy directories recursively

                           递归复制目录

    -d     same as --no-dereference --preserve=links

                  复制符号链接文件本身,而非其指向的源文件

    -a, --archive

              same as -dR --preserve=all

                          用于实现归档

    -f, --force

              if  an existing destination file cannot be opened, remove it and try again (this  ignored when the -n option is also used)

                      强制覆盖目标文件

    --preserve[=ATTR_LIST]

              preserve the specified attributes (default: mode,ownership,timestamps), if possible  additional attributes: context, links, xattr, all

            mode:权限                       ownership:属主和属组

            timestamps:时间戳               context:安全标签

            xattr:扩展属性                  links:符号链接

            all:上述所有属性

    -v, --verbose

              explain what is being done

              详细展示命令的运行过程


看了上面的内容下面通过几个例子来对cp命令及其选项有一个更具体的认识

1、使用别名命令,每日将/etc/目录下所有文件,备份到/testdir/下独立的新目录下,并要求新目录格式为backupYYYY-mm-dd,备份过程可见

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~] # mkdir /testdir
[root@localhost ~] # cp -rdv /etc/ /testdir/bakup$(date +%F)
"/etc/rsyslog.conf"  ->  "/testdir/bakup2016-08-01/rsyslog.conf"
"/etc/vimrc"  ->  "/testdir/bakup2016-08-01/vimrc"
"/etc/pulse"  ->  "/testdir/bakup2016-08-01/pulse"
"/etc/pulse/client.conf"  ->  "/testdir/bakup2016-08-01/pulse/client.conf"
"/etc/pulse/daemon.conf"  ->  "/testdir/bakup2016-08-01/pulse/daemon.conf"
"/etc/pulse/default.pa"  ->  "/testdir/bakup2016-08-01/pulse/default.pa"
"/etc/pulse/system.pa"  ->  "/testdir/bakup2016-08-01/pulse/system.pa"
...
..
.
"/etc/screenrc"  ->  "/testdir/bakup2016-08-01/screenrc"

因为/etc/下文件很多,因此需要等一会。

1
2
[root@localhost ~] # ls /testdir/
bakup2016-08-01

结果满足要求,下面解释下使用cp那三个选项的原因,给出的要求是首先要将/etc目录下的所有文件都备份,因此需要使用递归选项-r,后面要求备份过程可见,于是又加上-v,至于-d是因为要保持备份文件与源文件的一致性,虽然没有明确要求,不过加上-d显得更好点。


2、先创建/testdir/rootdir目录,再复制/root所有下文件到该目录内,并要求保留原有权限。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@localhost ~] # mkdir /testdir/rootdir
[root@localhost ~] # cp -r --preserve=mode,ownership /root /testdir/rootdir/
[root@localhost ~] #  ll /root /testdir/rootdir/root/ 
/root :
总用量 20
-rw-------. 1 root root 1172 7月  20 00:38 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 7月  21 18:52 CST
-rw-r--r--. 1 root root   14 8月   1 10:49  file
-rw-------. 1 root root 1220 7月  19 16:46 initial-setup-ks.cfg
-rw-r--r--. 1 root root 4282 8月   1 12:17  test .txt
-rw-r--r--. 1 root root    0 8月   1 12:20  tr
drwxr-xr-x. 2 root root    6 7月  22 09:52 公共
drwxr-xr-x. 2 root root    6 7月  22 09:52 模板
drwxr-xr-x. 2 root root    6 7月  22 09:52 视频
drwxr-xr-x. 2 root root    6 7月  22 09:52 图片
drwxr-xr-x. 2 root root    6 7月  22 09:52 文档
drwxr-xr-x. 2 root root    6 7月  22 09:52 下载
drwxr-xr-x. 2 root root    6 7月  22 09:52 音乐
drwxr-xr-x. 2 root root    6 7月  22 09:52 桌面
/testdir/rootdir/root/ :
总用量 20
-rw-------. 1 root root 1172 8月   1 22:06 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 8月   1 22:06 CST
-rw-r--r--. 1 root root   14 8月   1 22:06  file
-rw-------. 1 root root 1220 8月   1 22:06 initial-setup-ks.cfg
-rw-r--r--. 1 root root 4282 8月   1 22:06  test .txt
-rw-r--r--. 1 root root    0 8月   1 22:06  tr
drwxr-xr-x. 2 root root    6 8月   1 22:06 公共
drwxr-xr-x. 2 root root    6 8月   1 22:06 模板
drwxr-xr-x. 2 root root    6 8月   1 22:06 视频
drwxr-xr-x. 2 root root    6 8月   1 22:06 图片
drwxr-xr-x. 2 root root    6 8月   1 22:06 文档
drwxr-xr-x. 2 root root    6 8月   1 22:06 下载
drwxr-xr-x. 2 root root    6 8月   1 22:06 音乐
drwxr-xr-x. 2 root root    6 8月   1 22:06 桌面

依然是复制目录依然是使用递归选项-r,这次使用--preserve主要是为了满足保留权限这一要求。由结果可知满足要求。

3、复制/etc/system-release文件,比较加-d与不加-d有何区别

wKiom1efWu-DvvLBAAAwN0xmI5M864.gif

1
2
3
4
5
6
7
8
9
10
[root@localhost ~] # cp /etc/system-release /test/t1
[root@localhost ~] # cp -d /etc/system-release /test/t2
[root@localhost ~] # ll /test/
总用量 2
-rw-r--r--. 1 root root    38 8月   1 22:22 t1
lrwxrwxrwx. 1 root root    14 8月   1 22:22 t2 -> centos-release
[root@localhost  test ] # cat t1 
CentOS Linux release 7.2.1511 (Core) 
[root@localhost  test ] # cat t2
cat : t2: 没有那个文件或目录

    由结果的比较我们可以知道加-d我们复制的是原始文件,若目标是链接文件我们复制的只是链接文件,而不是原始文件。



mv 是move缩写,主要作用是移动和重命名文件

  mv [OPTION]... [-T] SOURCE DEST

 mv [OPTION]... SOURCE... DIRECTORY

  mv [OPTION]... -t DIRECTORY SOURCE...

在同一路径下,mv作用为重命名

在不同路径下,mv作用为移动文件或剪切

常用选项:

      -v, --verbose

              explain what is being done

      -i     prompt before every removal  

以上两种选项上文已有例子说明,这里不再具体演示。


下面以实验说明mv的具体作用

1、在/test/目录下更改t1文件名为t3

1
2
3
4
5
6
7
[root@localhost  test ] # ll
总用量 0
-rw-r--r--. 1 root root 0 8月   1 22:36 t1
[root@localhost  test ] # mv t1 t3
[root@localhost  test ] # ll
总用量 0
-rw-r--r--. 1 root root 0 8月   1 22:36 t3

2、将/test/目录下的t3文件移动至/testdir/目录下

1
2
3
4
5
6
7
8
[root@localhost  test ] # ll /testdir/
总用量 0
[root@localhost  test ] # mv t3 /testdir/
[root@localhost  test ] # ll
总用量 0
[root@localhost  test ] # ll /testdir/
总用量 0
-rw-r--r--. 1 root root 0 8月   1 22:36 t3



rm - remove files or directories 移除文件或目录

格式: rm [OPTION]... FILE...

常用选项:

    -f, --force   强制删除

              ignore nonexistent files and arguments, never prompt

    -i     prompt before every removal   交互模式

    -r, -R, --recursive    递归删除

              remove directories and their contents recursively

    -v, --verbose          显示删除详细过程

              explain what is being done


下面以实验来具体演示rm的作用

1、删除/test/目录下t1文件

1
2
3
4
5
[root@localhost  test ] # rm t1
rm :是否删除普通空文件  "t1" ?y
[root@localhost  test ] # ll
总用量 0
drwxr-xr-x. 2 root root 18 8月   1 22:46  file

在root用户下其默认情况下“cp=cp -i”

2、删除/test/目录下的file文件夹

1
2
3
4
[root@localhost  test ] # rm file/
rm : 无法删除 "file/" : 是一个目录
[root@localhost  test ] # rm -f file/
rm : 无法删除 "file/" : 是一个目录

我们发现仅仅使用rm且加上强制删除选项也无法将目录给删除。

删除目录需要加上-r递归,删除目录连带将其下面的所有文件都给删掉。

1
2
3
[root@localhost  test ] # rm -rfv file/
已删除 "file/file1"
已删除目录: "file/"

其删除过程是先从最底层的文件一个个删除之后向上再次删除一直删到最上层为止。










本文转自 紫色的茶碗 51CTO博客,原文链接:http://blog.51cto.com/chawan/1833275,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值