菜鸟的linux成长笔记--文件管理2

一. 文件管理(cp, mv, rm)

cp: 文件复制命令

  • SRC DEST

    • SRC是文件:
      • 如果目标不存在则会新建DEST, 并将SRC中的内容填充至DEST中

        $ ls
        test.txt
        $ cat test.txt
        this is a test
        $ cp test.txt test2
        $ cat test2
        this is a test

  • 如果目标存在:

    • 如果DEST是文件: 将SRC中的内容覆盖至DEST中(此时建议使用-i选项)

      $ echo abc > test.txt
      $ cat test.txt
      abc
      $ cp test.txt test2
      $ cat test2
      abc

    • 如果DEST是目录: 在DEST下新建与源文件同名的文件, 并将SRC中的内容填充至新文件

      $ pwd
      /home/centos/Downloads
      $ cd ../Documents/
      $ cp test.txt ../Downloads/test.txt
      $ cat ../Downloads/test.txt/test.txt
      abc

    • SRC是多个文件

      • DEST必须存在, 且为目录, 其它情形均会出错

        $ cp test* ../Downloads/test.txt/test.txt
        cp: target `../Downloads/test.txt/test.txt' is not a directory

    • SRC是目录: 常用选项为-r(递归复制)
      • 如果DEST不存在: 则创建指定目录, 复制SRC目录中的所有文件至DEST中

        $ cp -r Documents/ Downloads/abc
        $ ls Downloads/abc/
        test2 test.txt

      • 如果DEST存在
        • 如果DEST是文件: 报错
        • 如果DEST是目录, 创建目录并复制源目录

options

  • -i: 交互式

    $ cp -i test2 ../Downloads/abc/test2
    cp: overwrite `../Downloads/abc/test2'? y

  • -R/r: 递归复制

    $ cp Documents/ Downloads/
    cp: omitting directory `Documents/
    $ cp -r Documents/ Downloads/

  • -a: 归档复制 相当于-dR --preserve=all
    -d: --no-deference --preserve=links
    --preserve=[ATTR_LIST]

    mode: 权限  
    ownership  
    timestamp  
    links  
    xattr  
    context  
    all  
    • -p: --preserve=mode, ownership, timestamp保留属主属组及时间戳
    • -v: --verbose
    • -f: --force 强制执行

mv: 移动文件

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
  • 如果SRC是单个文件:

    • 如果DEST不存在: 移动

      $ mv Documents/test2 Music/

    • 如果目标存在:
      • DEST为文件: 覆盖

        $ mv Documents/test.txt Downloads/abc/test.txt

      • DEST为目录: 移动
        * 如果在同一目录下且名字不相同: 重命名
  • options
    • -i: 交互式选项(与cp类似)
    • -f --force: 强制(当有同名文件是直接覆盖不提示)

rm: remove 删除

rm [OPTION]... FILE...
  • options

    • -i: 交互式
    • -f: 强制删除
    • -r: 递归删除

猜你喜欢

转载自blog.51cto.com/13509526/2162054