美文网首页Linux
Linux 目录处理

Linux 目录处理

作者: Levid_GC | 来源:发表于2018-08-23 21:47 被阅读11次

    创建目录

    mkdir

    • 创建单个目录

      $ mkdir New_Dir
      $ ls -ld New_Dir
      drwxrwxrwx 1 levid_gc levid_gc 512 Aug 23 18:48 New_Dir/
      
    • 创建多级目录

      $ mkdir -p New_Dir/Sub_Dir/Under_Dir
      $ ls -R New_Dir
      New_Dir/:
      Sub_Dir
      
      New_Dir/Sub_Dir:
      Under_Dir
      
      New_Dir/Sub_Dir/Under_Dir:
      

    删除目录

    rmdir / rm

    rmdir 默认情况下只能删除空目录,如果待删除的目录下面存在文件则无法删除:

    $ rmdir New_Dir
    rmdir: failed to remove 'New_Dir': Directory not empty
    

    这时就得借助 rm 命令:

    $ rm -ir New_Dir
    rm: descend into directory 'New_Dir'? y
    rm: descend into directory 'New_Dir/Sub_Dir'? y
    rm: remove directory 'New_Dir/Sub_Dir/Under_Dir'? y
    rm: remove directory 'New_Dir/Sub_Dir'? y
    rm: remove directory 'New_Dir'? y
    $ ls -l New_Dir
    ls: cannot access 'New_Dir': No such file or directory
    

    提示:对于 rm 命令来说, 参数 -r-R 的效果是一样的,-R 参数同样可以递归地删除目录中的文件。

    但是,如果某个待删除的目录下面存在很多文件,那么再使用上面的方法将会出现很多的确认提示,就会显得异常繁琐,这时就可能需要借助于强制删除参数 -f

    比如,存在下面的一个目录结构:

    $ tree publish
    publish
    ├── appsettings.Development.json
    ├── appsettings.json
    ├── aspnetcoreapp.Views.dll
    ├── aspnetcoreapp.Views.pdb
    ├── aspnetcoreapp.deps.json
    ├── aspnetcoreapp.dll
    ├── aspnetcoreapp.pdb
    ├── aspnetcoreapp.runtimeconfig.json
    ├── web.config
    └── wwwroot
        ├── css
        │   ├── site.css
        │   └── site.min.css
        ├── favicon.ico
        ├── images
        │   ├── banner1.svg
        │   ├── banner2.svg
        │   └── banner3.svg
        ├── js
        │   ├── site.js
        │   └── site.min.js
        └── lib
            ├── bootstrap
            │   ├── LICENSE
            │   └── dist
            │       ├── css
            │       │   ├── bootstrap-theme.css
            │       │   ├── bootstrap-theme.css.map
            │       │   ├── bootstrap-theme.min.css
            │       │   ├── bootstrap-theme.min.css.map
            │       │   ├── bootstrap.css
            │       │   ├── bootstrap.css.map
            │       │   ├── bootstrap.min.css
            │       │   └── bootstrap.min.css.map
            │       ├── fonts
            │       │   ├── glyphicons-halflings-regular.eot
            │       │   ├── glyphicons-halflings-regular.svg
            │       │   ├── glyphicons-halflings-regular.ttf
            │       │   ├── glyphicons-halflings-regular.woff
            │       │   └── glyphicons-halflings-regular.woff2
            │       └── js
            │           ├── bootstrap.js
            │           ├── bootstrap.min.js
            │           └── npm.js
            ├── jquery
            │   ├── LICENSE.txt
            │   └── dist
            │       ├── jquery.js
            │       ├── jquery.min.js
            │       └── jquery.min.map
            ├── jquery-validation
            │   ├── LICENSE.md
            │   └── dist
            │       ├── additional-methods.js
            │       ├── additional-methods.min.js
            │       ├── jquery.validate.js
            │       └── jquery.validate.min.js
            └── jquery-validation-unobtrusive
                ├── LICENSE.txt
                ├── jquery.validate.unobtrusive.js
                └── jquery.validate.unobtrusive.min.js
    
    15 directories, 46 files
    $
    $ rm -rf publish
    $ tree publish
    publish [error opening dir]
    
    0 directories, 0 files
    

    执行 rm -rf 命令的时候没有任何警告,所以务必谨慎使用,特别是在拥有 root 用户权限的时候。

    说明:tree 是一个非常好用的可视化工具,能够很直观地展示目录层级,默认情况是没有安装的,在 Ubuntu 系统中,可以使用 sudo apt install tree 命令来安装它。

    参考资料

    相关文章

      网友评论

        本文标题:Linux 目录处理

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