How can I iterate over the contents of a directory in unix without using a wildcard? How can I iterate over the contents of a directory in unix without using a wildcard? unix unix

How can I iterate over the contents of a directory in unix without using a wildcard?


*.jpg would yield the literal *.jpg when there are no matching files. Looks like you need nullglob. With Bash, you can do this:

#!/bin/bashshopt -s nullglob                # makes glob expand to nothing in case there are no matching filesfor f in cat*.jpg dog*.jpg; do   # pick only cat & dog files  first3=${f:0:3}                # grab first 3 characters of filename  [[ -d "$first3" ]] || continue # skip if there is no such dir  mv "$f" "$first3/$f"           # movedone