Linux 目錄處理

創(chuàng)建目錄

mkdir

  • 創(chuàng)建單個目錄

    $ mkdir New_Dir
    $ ls -ld New_Dir
    drwxrwxrwx 1 levid_gc levid_gc 512 Aug 23 18:48 New_Dir/
    
  • 創(chuàng)建多級目錄

    $ 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 默認(rèn)情況下只能刪除空目錄,如果待刪除的目錄下面存在文件則無法刪除:

$ 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 命令來說, 參數(shù) -r-R 的效果是一樣的,-R 參數(shù)同樣可以遞歸地刪除目錄中的文件。

但是,如果某個待刪除的目錄下面存在很多文件,那么再使用上面的方法將會出現(xiàn)很多的確認(rèn)提示,就會顯得異常繁瑣,這時就可能需要借助于強制刪除參數(shù) -f

比如,存在下面的一個目錄結(jié)構(gòu):

$ 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

執(zhí)行 rm -rf 命令的時候沒有任何警告,所以務(wù)必謹(jǐn)慎使用,特別是在擁有 root 用戶權(quán)限的時候。

說明:tree 是一個非常好用的可視化工具,能夠很直觀地展示目錄層級,默認(rèn)情況是沒有安裝的,在 Ubuntu 系統(tǒng)中,可以使用 sudo apt install tree 命令來安裝它。

參考資料

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容