茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671804 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
GoodSmile_LuskyStar x StreetFighter_00053.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2016/8/16 2:32:33
Bash scripting: test for empty directory

I want to test if a directory doesn't contain any files. If so, I will skip some processing.

I tried the following:
if [ ./* == "./*" ]; then
    echo "No new file"
    exit 1
fi

That gives the following error:
line 1: [: too many arguments

Is there a solution/alternative?



if [ "$(ls -A /path/to/dir)" ]; then
   echo "Not Empty"
else
   echo "Empty"
fi

Also, it would be cool to check if the directory exists before.



if [ $(find $DIR_TO_CHECK -maxdepth 0 -type d -empty 2>/dev/null) ]; then
    echo "Empty directory"
else
    echo "Not empty or NOT a directory"
fi




No need for counting anything or shell globs. You can also use read in combination with find. If find's output is empty, you'll return false:
if find /some/dir -mindepth 1 | read; then
   echo "dir not empty"
else
   echo "dir empty"
fi

This should be portable.



This will do the job in the current working directory (.):
[ `ls -1A . | wc -l` -eq 0 ] && echo "Current dir is empty." || echo "Current dir has files (or hidden files) in it."

or the same command split on three lines just to be more readable:
[ `ls -1A . | wc -l` -eq 0 ] && \
echo "Current dir is empty." || \
echo "Current dir has files (or hidden files) in it."

Just replace ls -1A . | wc -l with ls -1A | wc -l if you need to run it on a different target folder.
Edit: I replaced -1a with -1A (see @Daniel comment)



#!/bin/bash
if [ -d /path/to/dir ]; then
    # the directory exists
    [ "$(ls -A /path/to/dir)" ] && echo "Not Empty" || echo "Empty"
else
    # You could check here if /path/to/dir is a file with [ -f /path/to/dir]
fi




Use the following:
count="$( find /path -mindepth 1 -maxdepth 1 | wc -l )"
if [ $count -eq 0 ] ; then
   echo "No new file"
   exit 1
fi

This way, you're independent of the output format of ls. -mindepth skips the directory itself, -maxdepth prevents recursively defending into subdirectories to speed things up.



This is all great stuff - just made it into a script so I can check for empty directories below the current one. The below should be put into a file called 'findempty', placed in the path somewhere so bash can find it and then chmod 755 to run. Can easily be amended to your specific needs I guess.
#!/bin/bash
if [ "$#" == "0" ]; then
find . -maxdepth 1 -type d -exec findempty "{}"  \;
exit
fi

COUNT=`ls -1A "$*" | wc -l`
if [ "$COUNT" == "0" ]; then
echo "$* : $COUNT"
fi




I think the best solution is:
files=$(shopt -s nullglob; shopt -s dotglob; echo /MYPATH/*)
[[ "$files" ]] || echo "dir empty"

thanks to http://stackoverflow.com/a/91558/520567
This is an anonymous edit of my answer that might or might not be helpful to somebody: A slight alteration gives the number of files:
files=$(shopt -s nullglob dotglob; s=(MYPATH/*); echo ${s[*]})
echo "MYPATH contains $files files"

This will work correctly even if filenames contains spaces.



Using an array:
files=( * .* )
if (( ${#files[@]} == 2 )); then
    # contents of files array is (. ..)
    echo dir is empty
fi




A hacky, but bash-only, PID-free way:
is_empty() {
    test -e "$1/"* 2>/dev/null
    case $? in;
        1)   return 0 ;;
        *)   return 1 ;;
    esac
}

This takes advantage of the fact that test builtin exits with 2 if given more than one argument after -e: First, "$1"/* glob is expanded by bash. This results in one argument per file. So

if there are no files, test -e "" is run, which obviously always fails, i.e. returns 1 for "no".
If there is one file, test -e "dir/file" is run, which returns 0.
But if there are more files than 1, bash reports it as usage error, i.e. 2.
case wraps the whole logic around so that only the first case, with 1 exit status is reported as success.

Possible problems I haven't checked:

There are more files than number of allowed arguments--I guess this could behave similar to case with 2+ files.
Or there is actually file with an empty name--I'm not sure it's possible on any sane OS/FS.

原文出處:Bash scripting: test for empty directory - Super User*/
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|