It is currently Thu Apr 17, 2014 1:38 am

determine exist/not files matching type/name criteria in dir

All times are UTC - 6 hours

Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Sat Apr 20, 2013 12:06 pm   

Joined: Sat Apr 20, 2013 11:39 am
Posts: 2
I need to determine whether any files matching certain criteria are present in a specified directory.

If it matters the criteria are:

directories, the name of which ends in "_files"
"regular" files, the name of which ends in ".html"
"regular" files, the name of which ends in ".htm"

I don't need to know how many there are or which types. I just need a simple binary logic response:

yes, one or more files of one or more of these types of files is present in the directory specified.
no, no such files are present in the directory specified.

The path/filename of the directory will be passed to the script in a variable and stuff I've already done will have established that the variable contains a valid directory path/filename.

I am trying doing this with ls commands and putting the output in a variable, somehow extracting the first 50 characters or so of the variable to establish a new variable (I know, I am the spiritual descendant of Rube Goldberg) and checking if this matches the expected output in the event no such files are found but so far I've had no sucess. Right now I'm bogging down at the point of establishing a variable containing the output of an ls command. I've tried a lot of different nomenclatures with `s, (s, $s, "s, and 's in a remarkably large number of permutations, but so far it hasn't worked. I wonder if maybe the size of the output exceeds the permissable size of variables. I'm wondering if there is a better way to do this anyway. Any suggestions?

 PostPosted: Tue Apr 23, 2013 9:28 am   
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 223
Location: London - UK
Hope this helps...

if find /path/to/directory -maxdepth 1 -type d -iname \*_files -o -type f -iname \*.htm -o -type f -iname \*.html >/dev/null
  echo yes
  echo no

 PostPosted: Wed Apr 24, 2013 7:55 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 511

the 100% bash way
shopt -s extglob nullglob globstar
filelist=( /startingDirectory/**/*_files/*.htm{,l} )
(( ${#fileList[@]} )) && echo "there are files" || echo "no files"

 PostPosted: Sat Apr 27, 2013 2:39 pm   

Joined: Sat Apr 20, 2013 11:39 am
Posts: 2
Darthwavy; Watael:


Is it the custom here to mark threads "solved"? I don't see a thread tools dropdown or solved button.

 PostPosted: Wed Nov 06, 2013 1:14 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
POSIX find(1)'s desc. # wrote:
The following exit values shall be returned:

All path operands were traversed successfully.

An error occurred.

You can't only check whether a file exists with find(1).

Your piece of code will always be printing « yes » unless you
provide a non-existing directory or pass incorrect arguments.


You're not using any extglob pattern, enabling it is useless.
You'll make bash(1) happy by telling it not to seek for them.

Also, I would just check whether there is one element in fileList to avoid
getting the number of elements which isn't needed to be known; as in:
[[ $fileList ]] && ...

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 8 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group