美文网首页
Linux 目录的常用操作命令

Linux 目录的常用操作命令

作者: 峤桓 | 来源:发表于2019-06-02 16:00 被阅读0次

    1.查看目录中的内容:ls

    2.进入目录:cd

    cd .. 表示回到上一级目录,类似 Windows 的「向上」

    cd ..
    

    cd - 表示回到上一次所在的目录,类似 Windows 的「后退」。

    cd -
    

    cd ~ 表示回到当前用户的 home 目录,类似 Windows 的「回到桌面」(但并非桌面)。

    cd ~
    

    cd / 表示进入根目录,它是一切目录的父目录

    cd /
    

    3.使用 mkdir 命令可创建目录

    在 mkdir 后加入 -p 参数,一次性创建多级目录

    mkdir -p one/two/three
    
    创建多级目录

    4.新建空白文档

    使用 touch 命令可以新建文件,比如我想在 /home/shiyanlou 目录下新建一个名为 “hello” 的文件

    shiyanlou:~/ $ cd /home/shiyanlou
    shiyanlou:~/ $ touch hello
    
    touch

    5.复制

    使用 cp 命令(Copy)复制文件到指定目录下,比如要把 hello 文件复制到 one/two 这个目录下:

    shiyanlou:~/ $ cp hello one/two/  
    shiyanlou:~/ $ tree one   
    one
     └── two
          ├── hello
          └── three
    
    2 directories, 1 file
    shiyanlou:~/ $ 
    

    如果要复制目录,需要在 cp 后加上 -r ,然后接上 目录名 目标目录名:

    shiyanlou:~/ $ mkdir test
    shiyanlou:~/ $ cp -r test one/two 
    shiyanlou:~/ $ tree one 
    one
     └── two
          ├── hello
          ├── test
          └── three
    
    3 directories, 1 file
    

    上面的操作中,我们先新建了一个 test 目录,然后把它复制进了 one/two 这个目录中,再通过tree one 直接查看 one 的目录结构。

    6.💡 删除

    使用 rm 命令删除文件:

    shiyanlou:~/ $ ls
    Code  Desktop  hello  one
    shiyanlou:~/ $ rm hello
    shiyanlou:~/ $ ls
    Code  Desktop  one
    

    删除目录要加上 -r 选项,类似 cp -r 拷贝目录,会删除目录和目录下的所有内容:

    shiyanlou:~/ $ mkdir test
    shiyanlou:~/ $ ls
    Code  Desktop  one  test
    shiyanlou:~/ $ rm -r test
    shiyanlou:~/ $ ls
    Code  Desktop  one
    

    7.💡 移动文件 / 目录与重命名

    使用 mv 命令可以移动文件或目录。

    首先,我们进入到 /home/shiyanlou 目录,使用 touch 创建空文件 test1:

    shiyanlou:~/ $ cd ~
    shiyanlou:~/ $ touch test1
    

    然后,我们创建一个新目录 dir1,ls 查看一下

    shiyanlou:~/ $ mkdir dir1
    shiyanlou:~/ $ ls
    Code  Desktop  dir1  one  test1
    

    然后使用 mv 命令 将 test1 移动到 dir1 目录,然后进入 dir1 目录查看一下:

    shiyanlou:~/ $ mv test1 dir1
    shiyanlou:~/ $ cd dir1
    shiyanlou:dir1/ $ ls
    test1
    

    使用 mv 移动文件或目录时,如果第二个参数不存在,系统会判断为你在 重命名 该目录或文件。

    如 mv test1 test2,如果 test2 不存在,该命令就会把 test1 重命名为 test2:

    shiyanlou:dir1/ $ ls
    test1
    shiyanlou:dir1/ $ mv test1 test2
    shiyanlou:dir1/ $ ls
    test2
    

    相关文章

      网友评论

          本文标题:Linux 目录的常用操作命令

          本文链接:https://www.haomeiwen.com/subject/ljexxctx.html