Linux shell大量刪除指定目錄下的所有目錄的編程思路

來源:互聯網
上載者:User

標籤:inode   檔案操作   字串操作   linux shell編程   inode應用   

應用情境:某個目錄中存放著許多軟體的源碼壓縮包,在部署完這些軟體後,一定會產生許多沒用的臨時目錄,可以通過指令碼的方式刪除該目錄下所有的目錄。當這個目錄中存在著一些特殊名字的目錄時,例如這些目錄中有Linux系統所不支援的特殊字元“/”(這些檔案通常有可能是Windows系統所支援的)或有目錄名中有空格,那大量刪除這些目錄就會變得困難。

問題分析與解決辦法:

問題1.當前工作目錄中有一些重要的目錄可能不想被刪除

解決辦法:排除這些目錄即可

問題2.需要考慮一些特殊的目錄名稱,諸如“test dir”,“2015/03/11-log”,“下載”

解決辦法:利用inode消除使用者命名空間所帶來的特殊性

背景知識:

inode是index node或information node的縮寫,是一種特殊的結構用於存放檔案的基本資料,這些資訊包括檔案的所有者、類型、存取權限和存放在磁碟中實際資料的位置。inode存放在每一個檔案系統的inode表(inode table)中。

Index or Information Node. This is a structure containing the basic information about a file such as owner, type, access permissions and pointers to the actual data on the disk. Inodes are stored in the per filesystem inode table and are referenced through inode numbers. --http://www.linux-tutorial.info/modules.php?name=MContent&obj=glossary&term=inode

如果Linux系統經常與Windows系統中的磁碟或檔案進行互動,建議安裝mtools,此工具能在不掛載Windows磁碟格式磁碟的情況下,進行讀寫等操作。

Summary     : Programs for accessing MS-DOS disks without mounting the disks

Description : Mtools is a collection of utilities for accessing MS-DOS files.  
            : Mtools allow you to read, write and move around MS-DOS filesystem    
            : files (normally on MS-DOS floppy disks).  Mtools supports Windows95    
            : style long file names, OS/2 XDF disks, and 2m disks    
            :    
            : Mtools should be installed if you need to use MS-DOS disks

shell執行個體(核心步驟):

#!/bin/bash   WORK_DIRECTORY_NAME=.    EXCLUDE_DIRECTORY_NAME=python    INODE_OF_EXCLUDE_DIRECTORY_NAME=$(ls -id $EXCLUDE_DIRECTORY_NAME |awk ‘{print $1}‘)    INODE_OF_CURRENT_DIRECTORY_NAME=$(ls -Fi $WORK_DIRECTORY_NAME | grep \/ | awk ‘{print $1}‘)    INODE_OF_REMOVE_DIRECTORY_NAME=$(echo $INODE_OF_CURRENT_DIRECTORY_NAME | sed "s/$INODE_OF_EXCLUDE_DIRECTORY_NAME//g")    for INODE in $INODE_OF_REMOVE_DIRECTORY_NAME; do        find $WORK_DIRECTORY_NAME -inum $INODE -exec rm -rf {} \;    done


實際操作結果(僅示範效果不做具體操作):

650) this.width=650;" title="image" style="border-top:0px;border-right:0px;border-bottom:0px;border-left:0px;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/5B/21/wKiom1T_1PLz5yyoAAKsa4VXG2k526.jpg" height="462" />

--END--

本文出自 “通訊,我的最愛” 部落格,請務必保留此出處http://dgd2010.blog.51cto.com/1539422/1619310

Linux shell大量刪除指定目錄下的所有目錄的編程思路

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.