Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test for an empty directory 2

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
What I've got so far is
Code:
[[ -z $(ls $dir) ]] && echo $dir is empty
but I'm sure it could be done without invoking ls. Any ideas?

Thanks

Ceci n'est pas une signature
Columb Healy
 
du -s dir

if output = 0 then empty

unless of course it's full of empty files!

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
[ $dir/* = "$dir/*" ] && echo $dir is empty

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Nice one PH

Just the sort of thing I was looking for!

mrn, yes, there could be empty files.

Ceci n'est pas une signature
Columb Healy
 
works only for ksh type shells:

Code:
listfiles() { ls -1 ${1:+"$@"} 2>/dev/null; }
is_dir_empty() { test ! -d $1 -o -z "$(listfiles -A $1)";}

object=./mytmp
is_dir_empty $object && echo $object is empty
 
PHV said:
[ $dir/* = "$dir/*" ] && echo $dir is empty
I thought of that too PHV, but what happens if there are only hidden files in the dir? And won't test complain about missing operators if the $dir/* expands to multiple names?


HTH,

p5wizard
 
Hmm...
It starts with UUOC and UUOG but, once the fever grips, the drive to remove unneccessary use of commands can get obsessive. PHV's code looked good at first, and satisfied my cravings but p5wizard's points are valid and the final version, which allows for hidden and.or mutliple files, looks like
Code:
[ -z "ls -A $dir" ] && echo $dir is empty
which looks pretty bullet proof.

Thanks to all

Ceci n'est pas une signature
Columb Healy
 
Surely you must mean

[tt][ -z `ls -A $dir` ] && echo $dir is empty

or

[ -z $(ls -A $dir) ] && echo $dir is empty[/tt]



HTH,

p5wizard
 
won't test complain about missing operators if the $dir/* expands to multiple names?
Good point.
[ -e $dir/* ] || echo $dir is empty

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PH I've tested your suggestion and
Code:
b01301$ mkdir newtestdir
b01301$ export dir=newtestdir
b01301$ [ -e $dir/* ] || echo $dir is empty
newtestdir is empty
b01301$ touch newtestdir/.hidden
b01301$ [ -e $dir/* ] || echo $dir is empty
newtestdir is empty
b01301$ touch newtestdir/nothidden
b01301$ [ -e $dir/* ] || echo $dir is empty
b01301$ touch newtestdir/nothidden2
b01301$ [ -e $dir/* ] || echo $dir is empty
so it doesn't work for hidden files. However it works fine for multiples and fits my 'No Unneccessary Use of Command' criteria.

p5wizard Yes, I meant
Code:
[ -z "$(ls -A $dir)" ] && echo $dir is empty
(note the quotes)

Ceci n'est pas une signature
Columb Healy
 
it doesn't work for hidden files
And this ?
Code:
[ -e $dir/* ] || [ -f $dir/.[!.]* ] || echo $dir is empty

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PH Thanks - that one fixed it! Tested with
Code:
b01301$ rm -fr newtestdir
b01301$ mkdir newtestdir
b01301$ [ -e $dir/* ] || [ -f $dir/.[!.]* ] || echo $dir is empty
newtestdir is empty
b01301$ touch newtestdir/.hidden
b01301$ [ -e $dir/* ] || [ -f $dir/.[!.]* ] || echo $dir is empty
b01301$ touch newtestdir/.hidden2
b01301$ [ -e $dir/* ] || [ -f $dir/.[!.]* ] || echo $dir is empty
b01301$ touch newtestdir/nothidden
b01301$ [ -e $dir/* ] || [ -f $dir/.[!.]* ] || echo $dir is empty
b01301$ touch newtestdir/nothidden2
b01301$ [ -e $dir/* ] || [ -f $dir/.[!.]* ] || echo $dir is empty

I'm not sure if our combined efforts are worth the tiny improvement in performance in my script by removing the call to ls but it's been interesting and informative. Thanks to all.

Ceci n'est pas une signature
Columb Healy
 
Oops, sorry for the typo:
[ -e $dir/* ] || [ -[!]e[/!] $dir/.[!.]* ] || echo $dir is empty

And now you may even play with an hidden directory ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
if you use echo, one test can do it:

Code:
[ "$(echo $dir/* $dir/.*)" = "$dir/* $dir/. $dir/.." ] && echo $dir is empty


HTH,

p5wizard
 
This might work...
Code:
rmdir $dir && echo The directory $dir [b]WAS[/b] empty
[bigsmile]

OK, a little destructive. But it won't delete it if there are files there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top