mv: cannot stat '*.dds': No such file or directory

I’m using two short bash scripts for renaming all *.dds files in all directories.

The first one:

for i in *.dds;
do let j+=1;
mv $i 0150$;

The second one:

for d in $(find . -type d);
    cd $CURDIR/"$d"

I’m running the second one and obtain:

mv: cannot stat '*.dds': No such file or directory

But *.dds files are renamed correctly. I would like to understand my mistake which leads to the error.

Asked By: misha.physics


In your rename script, the *.dds glob expands to the file names in the current directory that end with a .dds extension:, and so on.

The problem here is a directory that does not have any files ending with a .dds extension, so there is nothing for the glob to match. In this scenario, the default behaviour of bash is to return the literal string ‘*.dds’. This gets passed on to the mv command, which prints an error as no such file exists.

The shell option ‘nullglob’ can adjust this behaviour. With ‘nullglob’ turned on, non-matching globs will disappear instead of returning the literal string. With zero arguments, the for loop will skip the empty directory.

You can turn on ‘nullglob’ just before your loop:

shopt -s nullglob
for i in *.dds;

More details and examples are available here: . Do note the warning and portability sub-sections, as they highlight some of the concerns in using ‘nullglob’.

Answered By: Haxiel
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.