Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1050651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:48:10+00:00 2026-05-16T16:48:10+00:00

I’ve mastered the basics of Bash compound conditionals and have read a few different

  • 0

I’ve mastered the basics of Bash compound conditionals and have read a few different ways to check for file existence of a wildcard file, but this one is eluding me, so I figured I’d ask for help…

I need to:
1.) Check if some file matching a pattern exists
AND
2.) Check that text in a different file exists.

I know there’s lots of ways to do this, but I don’t really have the knowledge to prioritize them (if you have that knowledge I’d be interested in reading about that as well).

First things that came to mind is to use find for #1 and grep for #2

So something like

if [ `grep -q "OUTPUT FILE AT STEP 1000" ../log/minimize.log` ] \
      && [ `find -name "jobscript_minim\*cmd\*o\*"` ]; then
   echo "Both passed! (1)"
fi

That fails, though curiously:

if `grep -q "OUTPUT FILE AT STEP 1000" ../log/minimize.log` ;then
   echo "Text passed!"
fi
if `find -name "jobscript_minim\*cmd\*o\*"` ;then
   echo "File passed!"
fi

both pass…

I’ve done a bit of reading and have seen people talking about the problem of multiple filenames matching wildcards within an if statement. What’s the best solution to this? (in answer my question, I’d assumed you take a crack at that question, as well, in the process)

Any ideas/solutions/suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:48:10+00:00Added an answer on May 16, 2026 at 4:48 pm

    Let’s tackle why your attempt failed first:

    if [ `grep -q …` ]; 
    

    This runs the grep command between backticks, and interpolates the output inside the conditional command. Since grep -q doesn’t produce any output, it’s as if you wrote if [ ];

    The conditional is supposed to test the return code of grep, not anything about its output. Therefore it should be simply written as

    if grep -q …;
    

    The find command returns 0 (i.e. true) even if it finds nothing, so this technique won’t work. What will work is testing whether its output is empty, by collecting its output any comparing it to the empty string:

    if [ "$(find …)" != "" ];
    

    (An equivalent test is if [ -n "$(find …)" ].)

    Notice two things here:

    • I used $(…) rather than backticks. They’re equivalent, except that backticks require strange quoting inside them (especially if you try to nest them), whereas $(…) is simple and reliable. Just use $(…) and forget about backticks (except that you need to write \` inside double quotes).

    • There are double quotes around $(…). This is really important. Without the quotes, the shell would break the output of the find command into words. If find prints, say, two lines dir/file and dir/otherfile, we want if [ "dir/file dir/otherfile" = "" ]; to be executed, not if [ dir/file dir/otherfile = "" ]; which is a syntax error. This is a general rule of shell programming: always put double quotes around a variable or command substitution. (A variable substitution is $foo or ${foo}; a command substitution is $(command).)


    Now let’s see your requirements.

    1. Check if some file matching a pattern exists

      If you’re looking for files in the current directory or in any directory below it recursively, then find -name "PATTERN" is right. However, if the directory tree can get large, it’s inefficient, because it can spend a lot of time printing all the matches when we only care about one. An easy optimization is to only retain the first line by piping into head -n 1; find will stop searching once it realizes that head is no longer interested in what it has to say.

      if [ “$(find -name “jobscript_minimcmdo” | head -n 1)” != “” ];

      (Note that the double quotes already protect the wildcards from expansion.)

      If you’re only looking for files in the current directory, assuming you have GNU find (which is the case on Linux, Cygwin and Gnuwin32), a simple solution is to tell it not to recurse deeper than the current directory.

      if [ “$(find -maxdepth 1 -name “jobscript_minim*cmd*o*”)” != “” ];

      There are other solutions that are more portable, but they’re more complicated to write.

    2. Check that text in a different file exists.

      You’ve already got a correct grep command. Note that if you want to search for a literal string, you should use grep -F; if you’re looking for a regexp, grep -E has a saner syntax than plain grep.

    Putting it all together:

    if grep -q -F "OUTPUT FILE AT STEP 1000" ../log/minimize.log &&
       [ "$(find -name "jobscript_minim*cmd*o*")" != "" ]; then
      echo "Both passed! (1)"
    fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.