Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to test if a directory. If so, i will skip some processing.

I tried the following:

if [ ./* == "./*" ]; then
        echo "No new file"
            exit 1
            fi

but it gives the following error:

line 1: [: too many arguments

Is there a solution/alternative?

share|improve this question

8 Answers 8

up vote 6 down vote accepted
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.

share|improve this answer
1  
Don't use && and || simultaneously! If echo "Not Empty" fails, echo "Empty" will run! Try echo "test" && false || echo "fail"! Yes, I know echo will not fail but if you change any other command, you will be suprise! –  uzsolt Oct 31 '11 at 9:27
1  
Please, provide at least one example when the code above won't work. Concretely this code is absolutely correct. I hope that asker is able to adapt this for his own purposes –  Andrey Atapin Oct 31 '11 at 9:56
1  
[ "$(ls -A /)" ] && touch /non-empty || echo "Empty" - if you want "mark" the non-empty dirs with a created file named non-empty, will fails and prints "Empty". –  uzsolt Oct 31 '11 at 10:00
    
where's touch /empty in my line? –  Andrey Atapin Oct 31 '11 at 10:01
1  
Yes, you didn't wrote touch. But if you want more (other) than an echo "Not Empty", your script may be wrong! I've written this in my first comment, please read again (maybe you've read before my edit). –  uzsolt Oct 31 '11 at 10:04

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.

share|improve this answer

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.

share|improve this answer
    
Of course, you're now dependent on wc -l and find output format (which is reasonably plain though). –  Daniel Beck Oct 31 '11 at 10:25

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

share|improve this answer

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
share|improve this answer
#!/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
share|improve this answer
    
That must be it, no need for parsing ls output, just see if it is empty or not. Using find just feels like an overkill to me. –  akostadinov Sep 18 '13 at 12:34
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
share|improve this answer
    
Correct and fast. Nice! –  l0b0 Feb 24 '12 at 11:00

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 <target-directory> | wc -l if you need to run it on a different target folder.

Edit: I replaced -1a with -1A (see @Daniel comment)

share|improve this answer
1  
use ls -A instead. Some file systems don't have . and .. symbolic links according to the docs. –  Daniel Beck Oct 31 '11 at 10:12
    
Thanks @Daniel, I edited my answer after your suggestion. I know the "1" might be removed too. –  ztank1013 Oct 31 '11 at 10:21
1  
It doesn't hurt, but it's implied if output it not to a terminal. Since you pipe it to another program, it's redundant. –  Daniel Beck Oct 31 '11 at 10:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.