Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

421
Views
Exclude filenames with certain prefix in for loop (globbing)

This is probably quite easy, but I can't figure it out. In a for loop I want to exclude certain files with the prefix zz (e.g. zz131232.JPG) but I don't know how to exclude these files.

for i in *.JPG; do
    # do something
done

How do I modify the 'for rule' to exclude files with the prefix zz?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Something like

for i in *.JPG; do
  [[ $i != "zz"* ]] && echo "$i"
done

or skip them:

for i in *.JPG; do
  [[ $i == "zz"* ]] && continue
  # process the other files here
done
over 4 years ago · Santiago Trujillo Report

0

If you are dealing with many files you can also use GLOBIGNORE or extended globbing to avoid expanding the files you wish to skip in the first place (which might be faster):

GLOBIGNORE='zz*'
for file in *.JPG; do
    do_something_with "${file}"
done
# save and restore GLOBIGNORE if necessary

or

shopt -s extglob # enable extended globbing
for file in !(zz*).JPG; do
    do_something_with "${file}"
done
shopt -u extglob # disable extended globbing, if necessary

Note that if there are no .JPG files in the current directory the loop will still be entered and $i be set to the literal *.JPG (in your example), so you either need to check if the file exists inside the loop or use the nullglob option.

for file in *.JPG; do
    [ -e "${file}" ] || continue
    do_something_with "${file}"
done

or

shopt -s nullglob
for file *.JPG; do
    do_something_with "${file}"
done
shopt -u nullglob # if necessary

Try the following in your shell to understand what happens without nullglob:

$ for f in *.doesnotexist; do echo "$f"; done
*.doesnotexist
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!