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 8517321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:39:57+00:00 2026-06-11T05:39:57+00:00

Possible Duplicate: Checking if a directory contains files I want to see if a

  • 0

Possible Duplicate:
Checking if a directory contains files

I want to see if a directory is empty, so I am using [ as follows:

[ -f "./ini/*"]

And the return value is always 1, even though the directory is not empty.
Any ideas what’s wrong?

  • 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-06-11T05:39:58+00:00Added an answer on June 11, 2026 at 5:39 am

    Try this:

    $ echo "*"   #With quotes
    

    vs.

    $ echo *    #No quotes
    

    See the difference? When you put quotes around a string, the shell can’t glob to replace the asterisk with the matching files.

    There are multitude of ways to get what you want. Simplest is to use the ls command without the test command. The if statement takes the output results of the command you give it. The [...] simply are an alias to the test command. These two statements are equivalent:

    $ if test -f foo.txt
    
    $ if [ -f foo.txt ]
    

    Basically, all the test command does is return a 0 if the test is true or a non-zero if the test is false. All the if command does is execute the if statement if the command it’s given returns a 0 exit code. The reason I’m going all through this is to say let’s forget the test and simply use the ls command.

    Assume that you have a file foo.txt, but no file bar.txt. Try the following:

    $ ls foo.txt > /dev/null 2>&1
    $ echo $?
    0
    $ ls bar.txt > /dev/null 2>&1
    $ echo $?
    2
    

    The > /dev/null 2>&1 is used to inhibit all output. Notice that the exit code of the ls command when a file exists is 0 while the exit code of the ls command when the file doesn’t exist isn’t 0. (It’s 2 in this case). Now, let’s use that instead of the test command:

    if ls ./ini/* > /dev/null 2>&1
    then
        echo "There are files in that directory!"
    else
        echo "There are no files in that directory"
    fi
    

    Notice I don’t even bother with the test command (which can be the word test or the [..]).

    Hope this makes sense to you. It’s sometimes difficult for people to realize that in BASH shell, the if command and the [...] are two separate commands, and all if is doing is running the command it’s given and then acting depending upon the exit code of the command it runs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I get a directory size (files in the directory) in
Possible Duplicate: Checking for string contents? string Length Vs Empty String In .NET, which
Possible Duplicate: More concise way to check to see if an array contains only
Possible Duplicate: What is the valid range for program return value in Linux/bash? Here
Possible Duplicate: checking if pointer points within an array If I have an array
Possible Duplicate: PHP - reversed order in if statement Checking for null - what
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to do the vector of sets in C++? I want to
Possible Duplicate: check whether internet connection is available with C# I just want to

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.