RE: Bash problems?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2006-06-27 at 13:23 +0200, Tomas Larsson wrote:
> > -----Original Message-----
> > From: fedora-list-bounces@xxxxxxxxxx 
> > [mailto:fedora-list-bounces@xxxxxxxxxx] On Behalf Of Paul Howarth
> > Sent: Tuesday, June 27, 2006 12:34 PM
> > To: For users of Fedora Core releases
> > Subject: Re: Bash problems?
> > 
> > Ben Stringer wrote:
> > > On Tue, 2006-06-27 at 12:03 +0200, Tomas Larsson wrote:
> > >> Dear group.
> > >> How do I do to, within a scrip, check if a directory I 
> > empty or not.
> > >> Cant find a way to do this in a simple way.
> > > 
> > > DIR_CONTENTS=`ls $DIR`
> > > if [ "$DIR_CONTENTS" == "" ]
> > > then
> > >   echo "$DIR is empty"
> > > else
> > >   echo "$DIR contains:"
> > >   echo $DIR_CONTENTS
> > > fi
> > 
> > I'd use "ls -A $DIR" there rather than "ls $DIR" so as to 
> > spot files with names starting with a dot.

Yes - nice improvement Paul.

> > 
> > Paul.
> Thanks all, however for some reason 'ls $DIR' didn't work.
> $(ls $DIR) did the job.

You had the ticks the wrong way. But using the round brackets is
equivalent.

> 
> Now, I want to exlude sub-dirs in the ls command?

Look at using the "find" command with the option -maxdepth 1

Eg.

DIR_CONTENTS=$( find $DIR -maxdepth 1 -type f )

Note that this will _only_ find files, not symlinks, block/char special
files, sockets etc. See the find manpage for details on "-type f".

Cheers, Ben

> 
> With best regards
> 
> Tomas Larsson
> Sweden
> 
> Verus Amicus Est Tamquam Alter Idem
> 


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux