Use BASH nullglob To Verify *.c Files Exists or Not In a Directory

by on March 1, 2009 · 6 comments· LAST UPDATED March 1, 2009

in , ,

Can you explain me usage of nullglob variable under BASH? How do I check for any *.c files in any directory?

BASH shell has the following two special variables to control pathname expansion. Bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.

a] nullglob : If set, bash allows patterns which match no files to expand to a null string, rather than themselves. This is useful to check for any *.mp3 or *.cpp files in directory.

b] dotglob If set, bash includes filenames beginning with a . in the results of pathname expansion.

How do I set and unset nullglob variable?

Use shopt command to toggle the values of variables. The -s option enable nullglob effects and the -u option disable nullglob option.

shopt -s nullglob # enable 
shopt -u nullglob # disable

Here is sample shell script to see if *.mp3 exists or not in a directory:

#!/bin/bash
old=$(cd)
[ $# -eq 0 ] && exit 1
[ -d $1 ] && cd $1 || exit 2
 
shopt -s nullglob
found=0
for i in *.mp3; do
	echo "File $i found" # or take other action
	found=1
done
shopt -u nullglob
[ $found -eq 0 ] && echo "Directory is empty"
cd $old

Without nullglob i will expand to *.mp3 only if there are no files in given directory. You can also use GNU find command to find out if directory is empty or not i.e. check for any *.c files in a directory called ~/project/editor:

find ~/project/editor -maxdepth 0 -empty -exec echo {} directory is empty. \;

Where,

  • -maxdepth 0: Do not scan for sub directories.
  • -empty : File is empty and is either a regular file or a directory.
  • -exec echo {} directory is empty. \; : Display message if directory is empty.
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 6 comments… read them below or add one }

1 bratwurst March 7, 2009 at 11:33 am

How do you know these things? Either you must have the memory of an elephant, or know how to use google in some magic way :)

One thing I don’t understand is why certain bash variables are stored with shopt and others with environment variables (like IFS). Historic reasons?

Reply

2 nixCraft March 7, 2009 at 11:43 am

How do you know these things? Either you must have the memory of an elephant, or know how to use google in some magic way :)

I’ve been using UNIX and Linux for over decade so I know lots of things. bash man page has all the info.

One thing I don’t understand is why certain bash variables are stored with shopt and others with environment variables (like IFS). Historic reasons?

shopt use to toggle the values of variables controlling optional behavior. Usually it can be on or off only. On other hand, IFS value can be anything as per users requirements.

Reply

3 jaysunn January 10, 2010 at 4:37 pm

Great Lesson,
I was just introduced to this via Vivek. I am planning to use this feature in many ways.

Thanks,

Jaysunn

Reply

4 waltz November 15, 2010 at 6:32 am

great explanation to make complicated things simple!
Nice example too.
Thank you very much

Reply

5 flabdablet November 24, 2010 at 8:51 am

The for name in pattern construction is good for doing stuff to all matching files, but sometimes all that’s needed is a quick test for whether at least one matching file exists. The stat command can help with that:

if stat -t *.mp3 >/dev/null 2>&1
then
echo At least one file matching *.mp3 exists
else
echo No files match *.mp3
fi

This is portable to shells other than bash, many of which don’t support nullglob.

Reply

6 Chelsia July 23, 2011 at 11:19 am

Mighty useful. Make no mistake, I aprpceiate it.

Reply

Leave a Comment

Tagged as: , , , , , , , ,

Previous Faq:

Next Faq: