In Bash, you can use the nullglob
option to verify if any files with a specific pattern exist in a directory or not. The nullglob
option causes the shell to expand patterns to an empty list if no matches are found. Here’s how you can use the nullglob
option to verify if *.c
files exist in a directory:
- Open a terminal and navigate to the directory you want to check.
- Set the
nullglob
option using the following command:shopt -s nullglob
- Use the
ls
command to list all the*.c
files in the current directory. If there are no files, the output will be empty.ls *.c
- If there are files, you can use them in your script or further processing.
- After you are done checking, unset the
nullglob
option to avoid unexpected behavior:shopt -u nullglob
For example, if you want to check if there are any *.c
files in the /home/user/code
directory, you can use the following commands:
cd /home/user/code
shopt -s nullglob
if [ -n "$(ls *.c)" ]; then
echo "C files found!"
else
echo "No C files found."
fi
shopt -u nullglob
This will check if any *.c
files exist in the /home/user/code
directory and output a message accordingly.