Shell脚本在某些情况下不删除空目录

时间:2017-01-21 05:29:24

标签: bash shell unix

我正在编写一个shell脚本来组织文件。截至目前,它不支持大量类型,只是足以测试它。工作流程如下:

  1. 为某类商品制作文件夹
  2. 使用通配符移动所有项目
  3. 删除所有空文件夹

    对其他类型的文件重复此操作。

  4. 截至目前,如果目录中没有任何内容存储,则目录删除部分可以正常工作。因此,如果我运行脚本并且没有任何内容被移动到文件夹,它将在第一次运行期间被删除。但是,如果创建了文件夹并将项目移动到该文件夹​​,如果我手动删除这些项目(使文件夹为空)并再次运行该脚本,则不会删除这些目录。

    任何人都有任何想法是什么导致这个?我声称对shell脚本知之甚少,甚至在SO上获得了检查空目录的语法。

    #!/bin/bash
    
    # Add support for other files and formats
    # Figure out how to move all rest of files to MISC without moving the script
    # Figure out why folders are deleted, unless they have an item stored at some point
    # Figure out how to copy files from existing folders out and make them empty
    # so they can be sorted, and they will be deleted in the end
    
    ########################################## AUDIO ##########################################
    
    # Make folders for audio
    mkdir Audio
    mkdir Audio/AIFF
    mkdir Audio/FLAC
    mkdir Audio/MP3
    mkdir Audio/WAVE
    
    # Move all audio files to audio folder
    mv *.aiff Audio/AIFF
    mv *.flac Audio/FLAC
    mv *.mp3 Audio/MP3
    mv *.wav Audio/WAVE
    
    # Delete any empty folders related to audio folder - delete parent folder last
    # Because it will only ever be empty at the end of deleting all other subfolders
    if [ "$(ls Audio/AIFF 2> /dev/null)" == "" ];
    then
        rmdir Audio/AIFF
    fi
    
    if [ "$(ls Audio/FLAC 2> /dev/null)" == "" ];
    then
        rmdir Audio/FLAC
    fi
    
    if [ "$(ls Audio/MP3 2> /dev/null)" == "" ];
    then
        rmdir Audio/MP3
    fi
    
    if [ "$(ls Audio/WAVE 2> /dev/null)" == "" ];
    then
        rmdir Audio/WAVE
    fi
    
    if [ "$(ls Audio 2> /dev/null)" == "" ];
    then
        rmdir Audio
    fi
    
    ########################################## DOCUMENTS ##########################################
    
    # Make folders for documents
    mkdir Documents
    mkdir Documents/DOC
    mkdir Documents/DOCX
    mkdir Documents/HTML
    mkdir Documents/RTF
    mkdir Documents/TXT
    
    # Move all docs to documents folder
    mv *.doc Documents/DOC
    mv *.docx Documents/DOCX
    mv *.html Documents/HTML
    mv *.rtf Documents/RTF
    mv *.txt Documents/TXT
    
    # Delete any empty folders related to documents folder - delete parent folder last
    # Because it will only ever be empty at the end of deleting all other subfolders
    if [ "$(ls Documents/DOC 2> /dev/null)" == "" ];
    then
        rmdir Documents/DOC
    fi
    
    if [ "$(ls Documents/DOCX 2> /dev/null)" == "" ];
    then
        rmdir Documents/DOCX
    fi
    
    if [ "$(ls Documents/HTML 2> /dev/null)" == "" ];
    then
        rmdir Documents/HTML
    fi
    
    if [ "$(ls Documents/RTF 2> /dev/null)" == "" ];
    then
        rmdir Documents/RTF
    fi
    
    if [ "$(ls Documents/TXT 2> /dev/null)" == "" ];
    then
        rmdir Documents/TXT
    fi
    
    if [ "$(ls Documents 2> /dev/null)" == "" ];
    then
        rmdir Documents
    fi
    
    ########################################## IMAGES ##########################################
    
    # Make folders for images
    mkdir Images
    mkdir Images/GIF
    mkdir Images/JPEG
    mkdir Images/JPG
    mkdir Images/PNG
    
    # Move all Images to folder - Split up files into specific file extension folders?
    mv *.gif Images/GIF
    mv *.jpeg Images/JPEG
    mv *.jpg Images/JPG
    mv *.png Images/PNG
    
    # Delete any empty folders related to images folder - delete parent folder last
    # Because it will only ever be empty at the end of deleting all other subfolders
    if [ "$(ls Images/GIF 2> /dev/null)" == "" ];
    then
        rmdir Images/GIF
    fi
    
    if [ "$(ls Images/JPEG 2> /dev/null)" == "" ];
    then
        rmdir Images/JPEG
    fi
    
    if [ "$(ls Images/JPG 2> /dev/null)" == "" ];
    then
        rmdir Images/JPG
    fi
    
    if [ "$(ls Images/PNG 2> /dev/null)" == "" ];
    then
        rmdir Images/PNG
    fi
    
    if [ "$(ls Images 2> /dev/null)" == "" ];
    then
        rmdir Images
    fi
    
    ########################################## VIDEOS ##########################################
    
    # Make folders for videos
    mkdir Videos
    mkdir Videos/AVI
    mkdir Videos/FLV
    mkdir Videos/MP4
    mkdir Videos/MPEG
    mkdir Videos/MOV
    mkdir Videos/WMV
    
    # Move all videos to folder - Split up files into specific file extension folders?
    mv *.avi Videos/AVI
    mv *.flv Videos/FLV
    mv *.mp4 Videos/MP4
    mv *.mpeg Videos/MPEG
    mv *.mov Videos/MOV
    mv *.wmv Videos/WMV
    
    # Delete any empty folders related to images folder - delete parent folder last
    # Because it will only ever be empty at the end of deleting all other subfolders
    if [ "$(ls Videos/AVI 2> /dev/null)" == "" ];
    then
        rmdir Videos/AVI
    fi
    
    if [ "$(ls Videos/FLV 2> /dev/null)" == "" ];
    then
        rmdir Videos/FLV
    fi
    
    if [ "$(ls Videos/MP4 2> /dev/null)" == "" ];
    then
        rmdir Videos/MP4
    fi
    
    if [ "$(ls Videos/MPEG 2> /dev/null)" == "" ];
    then
        rmdir Videos/MPEG
    fi
    
    if [ "$(ls Videos/MOV 2> /dev/null)" == "" ];
    then
        rmdir Videos/MOV
    fi
    
    if [ "$(ls Videos/WMV 2> /dev/null)" == "" ];
    then
        rmdir Videos/WMV
    fi
    
    if [ "$(ls Videos 2> /dev/null)" == "" ];
    then
        rmdir Videos
    fi
    
    ########################################## ZIP ##########################################
    
    # Make folder for zip
    mkdir Zip
    
    # Move all zip files to folder
    mv *.zip Zip
    
    # Delete zip folder
    if [ "$(ls Zip 2> /dev/null)" == "" ];
    then
        rmdir Zip
    fi
    
    ########################################## MISCELLANEOUS ##########################################
    
    # Make folder for Miscellaneous
    mkdir Miscellaneous
    
    # Delete Miscellaneous folder
    if [ "$(ls Miscellaneous 2> /dev/null)" == "" ];
    then
        rmdir Miscellaneous
    fi
    
    # Move rest of items to Miscellaneous Folder
    #mv *.* Miscellaneous
    

1 个答案:

答案 0 :(得分:3)

您可以取消保护rmdir的条件,因为它不会删除非空的目录。但是,它将返回非零退出代码,并生成标准错误消息。

你能做什么:

rmdir "$directory_that_may_be_empty_or_not" 2>/dev/null || :

|| :“吃掉”来自rmdir的任何非零返回码(根据您处理或忽略错误的方式可能会或可能不会重要)。

换句话说,让您的脚本尝试删除所有目录,如果它们不为空,则忽略失败(或者,如果由于任何其他原因(例如访问权限)导致删除失败)。

如果这不能解决您的问题,那么您的目录可能不是空的。它们可能包含隐藏文件(以.句号开头),除非您添加ls选项,否则-a不会列出这些文件。但是不要在代码中添加-a,因为它还指示ls列出.(当前目录)和..(父目录)特殊文件,并且会导致您当前的代码永远不会尝试删除。

作为补充说明,||是一个逻辑运算符,允许两个语句“ORed”。 Bash使用延迟评估,因此只有左侧的语句失败才会执行右边的语句(成功意味着返回代码= 0,失败=任何其他代码)。 :是一个不带参数的shell内置函数,除了提供零返回代码(如true命令)之外什么都不做。在这种情况下,它保证整个构造的状态为0。