Linux常用的文件管理类命令以及其使用方法

常见的文件管理有cp(复制)、 mv(移动)、rm(删除)

  • 一、cp命令:copy-复制命令
    命令格式:cp 选项 源目标 目的目标
    cp命令可以分为单一源复制和多源复制两种,格式如下:
    单源复制:cp [OPTION]… [-T] SOURCE DEST
    多源复制:cp [OPTION]… SOURCE… DIRECTORY
    cp [OPTION]… -t DIRECTORY SOURCE…
    其中根据DEST情况分为以下几种情况:
    单源复制:
    1、如果DEST不存在:则事先创建此文件,并复制源文件的数据流至DEST中;
    2、如果DEST存在:
    如果DEST是非目录文件:则覆盖目标文件;
    如果DEST是目录文件:则先在DEST目录下创建一个与源文件同名的文件,并复制其数据流;
    多源复制:
    cp [OPTION]… SOURCE… DIRECTORY
    cp [OPTION]… -t DIRECTORY SOURCE…
    1、如果DEST不存在:错误;
    2、如果DEST存在:
    如果DEST是非目录文件:错误;
    如果DEST是目录文件:分别复制每个文件至目标目录中,并保持原名;
    常用选项:
    -i:交互式复制,即覆盖之前提醒用户确认;
    -f:强制覆盖目标文件;
    -r, -R:递归复制目录;
    -d:复制符号链接文件本身,而非其指向的源文件;
    -a:-dR --preserve=all, archive,用于实现归档;
    -p --preserv=
    mode:权限
    ownership:属主和属组
    timestamps: 时间戳
    context:安全标签
    xattr:扩展属性
    links:符号链接
    all:上述所有属性
    使用示例:
    复制/etc/passwd 到/tmp/testcp.txt
[root@localhost ~]# cp /etc/passwd /tmp/testcp.txt
[root@localhost ~]# ll /tmp/testcp.txt
-rw-r--r--. 1 root root 1199 May 19 16:01 /tmp/testcp.txt
[root@localhost ~]# 

复制/etc/fstab 文件覆盖testcp.txt

[root@localhost ~]# cat /tmp/testcp.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
.....
[root@localhost ~]# cp /etc/fstab /tmp/testcp.txt 
cp: overwrite ‘/tmp/testcp.txt’? y
[root@localhost ~]# cat /tmp/testcp.txt
#
# /etc/fstab
# Created by anaconda on Tue Apr 23 20:55:10 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=e708d7eb-b63a-46e6-ad17-fda76c87af83 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@localhost ~]# 

复制user1家目录下的user1文件至/tmp目录下,并保持原文件的属性。

[root@localhost ~]# cp -a /home/user1/user1.txt  /tmp/
[root@localhost ~]# ll /home/user1/user1.txt /tmp/user1.txt 
-rw-rw-r--. 1 user1 user1 0 May 19 16:07 /home/user1/user1.txt
-rw-rw-r--. 1 user1 user1 0 May 19 16:07 /tmp/user1.txt
[root@localhost ~]#
  • 二、 mv命令:move
    -作用:移动文件(目录)或者用于对文件(目录)的重命名。如果将一个文件移动到一个已存在的目标文件中,则会将目标文件的内容覆盖。
    注意事项:
    1、移动文件或目录时,可以不止一个
    2、mv与cp命令的结果不同,mv好像文件“搬家”,文件个数并未增加。而cp对文件进行复制,文件个数增加了。

    命令格式:
    mv [OPTION]... [-T] SOURCE DEST
    mv [OPTION]... SOURCE... DIRECTORY
    mv [OPTION]... -t DIRECTORY SOURCE...
        常用选项:
        	-i:交互式操作,覆盖前先询问用户确认。
        -f:强制覆盖重复文件或目录;
        -t,--target-directory=<DIR>:指定全部的源文件要移动到的目标目录;
    

    使用示例
    将/tmp/user1.txt文件重名为user2.txt文件

[root@localhost ~]# mv /tmp/user1.txt /tmp/user2.txt 
[root@localhost ~]# ll /tmp/
total 8
drwxr-xr-x. 4 root  root   24 May 19 11:38 a1
drwxr-xr-x. 2 root  root    6 May 19 11:38 a2
drwxr-xr-x. 2 root  root   98 May 19 11:57 mytest1
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_z
-rw-r--r--. 1 root  root  465 May 19 16:04 testcp.txt
-rw-r--r--. 1 root  root   11 Apr 17  2014 test.file
drwxr-xr-x. 2 root  root    6 May 19 11:47 tfile-2019-05-19-11-47-16
-rw-rw-r--. 1 user1 user1   0 May 19 16:07 user2.txt
drwx------. 2 root  root    6 May 19 11:35 vmware-root
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_z
[root@localhost ~]# 

将/home/user1/user1.txt文件移动到/tmp/目录下

[root@localhost ~]# mv /home/user1/user1.txt /tmp/
[root@localhost ~]# ll /tmp/
total 8
drwxr-xr-x. 4 root  root   24 May 19 11:38 a1
drwxr-xr-x. 2 root  root    6 May 19 11:38 a2
drwxr-xr-x. 2 root  root   98 May 19 11:57 mytest1
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_z
-rw-r--r--. 1 root  root  465 May 19 16:04 testcp.txt
-rw-r--r--. 1 root  root   11 Apr 17  2014 test.file
drwxr-xr-x. 2 root  root    6 May 19 11:47 tfile-2019-05-19-11-47-16
-rw-rw-r--. 1 user1 user1   0 May 19 16:07 user1.txt
-rw-rw-r--. 1 user1 user1   0 May 19 16:07 user2.txt
drwx------. 2 root  root    6 May 19 11:35 vmware-root
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_z
[root@localhost ~]#
  • 三、rm命令:remove
    rm命令可以删除一个目录中的一个或多个文件和目录。对于链接文件,只是删除整个链接文件,而原有文件保持不变。
    由于使用rm命令所删除的文件几乎无法恢复,所以建议不用的文件不要直接删除,所有不用的文件建议不要直接删除,而是移动至某个专用目录;(模拟回收站)
    rm [OPTION]… FILE…
    常用选项:
    -i:interactive–交互模式,在删除已有文件或目录前先与用户确认
    -f:force—强制删除文件或目录;
    -r: recursive–递归,将指定的目录下的所有文件与子目录一并处理;:
    -v:显示详细的执行过程;
    -d:删除空目录;
    删除目录:rm -rf /PATH/TO/DIR
    危险操作:rm -rf /*

使用示例
交互式删除

[root@localhost ~]# ll /tmp/
total 8
drwxr-xr-x. 4 root  root   24 May 19 11:38 a1
drwxr-xr-x. 2 root  root    6 May 19 11:38 a2
drwxr-xr-x. 2 root  root   98 May 19 11:57 mytest1
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_z
-rw-r--r--. 1 root  root  465 May 19 16:04 testcp.txt
-rw-r--r--. 1 root  root   11 Apr 17  2014 test.file
drwxr-xr-x. 2 root  root    6 May 19 11:47 tfile-2019-05-19-11-47-16
-rw-rw-r--. 1 user1 user1   0 May 19 16:07 user1.txt
-rw-rw-r--. 1 user1 user1   0 May 19 16:07 user2.txt
drwx------. 2 root  root    6 May 19 11:35 vmware-root
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_z
[root@localhost ~]# rm /tmp/user1.txt 
rm: remove regular empty file ‘/tmp/user1.txt’? y
[root@localhost ~]# 

递归交互删除

[root@localhost ~]# ll /tmp/
total 8
drwxr-xr-x. 4 root  root   24 May 19 11:38 a1
drwxr-xr-x. 2 root  root    6 May 19 11:38 a2
drwxr-xr-x. 2 root  root   98 May 19 11:57 mytest1
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_z
-rw-r--r--. 1 root  root  465 May 19 16:04 testcp.txt
-rw-r--r--. 1 root  root   11 Apr 17  2014 test.file
drwxr-xr-x. 2 root  root    6 May 19 11:47 tfile-2019-05-19-11-47-16
-rw-rw-r--. 1 user1 user1   0 May 19 16:07 user2.txt
drwx------. 2 root  root    6 May 19 11:35 vmware-root
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_z
[root@localhost ~]# rm -r /tmp/a*
rm: descend into directory ‘/tmp/a1’? n
rm: remove directory ‘/tmp/a2’? n
[root@localhost ~]#

递归非交互删除

[root@localhost ~]# ll /tmp/
total 8
drwxr-xr-x. 4 root  root   24 May 19 11:38 a1
drwxr-xr-x. 2 root  root    6 May 19 11:38 a2
drwxr-xr-x. 2 root  root   98 May 19 11:57 mytest1
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_z
-rw-r--r--. 1 root  root  465 May 19 16:04 testcp.txt
-rw-r--r--. 1 root  root   11 Apr 17  2014 test.file
drwxr-xr-x. 2 root  root    6 May 19 11:47 tfile-2019-05-19-11-47-16
-rw-rw-r--. 1 user1 user1   0 May 19 16:07 user2.txt
drwx------. 2 root  root    6 May 19 11:35 vmware-root
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_z
[root@localhost ~]# rm -rf /tmp/a*
[root@localhost ~]# ll /tmp/
total 8
drwxr-xr-x. 2 root  root   98 May 19 11:57 mytest1
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 q_z
-rw-r--r--. 1 root  root  465 May 19 16:04 testcp.txt
-rw-r--r--. 1 root  root   11 Apr 17  2014 test.file
drwxr-xr-x. 2 root  root    6 May 19 11:47 tfile-2019-05-19-11-47-16
-rw-rw-r--. 1 user1 user1   0 May 19 16:07 user2.txt
drwx------. 2 root  root    6 May 19 11:35 vmware-root
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_y
drwxr-xr-x. 2 root  root    6 May 19 11:44 x_z
[root@localhost ~]# 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值