Search code examples
bashglobmv

Move files matching glob in for...done loop returns 'no such file or directory'


I'm moving files based on their extension.

The following works as expected when there are files matching the glob; but when there are no matching files it returns

'mv: rename /Users/xxx/Downloads/*.txt to /Volumes/Internal_HD/*.txt: no such file of directory'

in_path=/Users/xxx/Downloads
out_path=/Volumes/Internal_HD

for f in "$in_path"/*.txt; do
    mv -v "$f" "$out_path"
done;

['xxx' is the username.]


Solution

  • Set the nullglob option, otherwise the pattern expands to itself if no files match it:

    shopt -s nullglob