mv: cannot stat error : No such file or directory error

charlie picture charlie · Oct 4, 2012 · Viewed 54.9k times · Source

I need to move the files of a directory to another directory.I get stat error when I used the following program.

for i in dir1/*.txt_dir; do
mv $i/*.txt  dir2/`basename $i`.txt
done

error message

mv: cannot stat `dir1/aa7.txt_dir/*.txt': No such file or directory

Answer

sushant-hiray picture sushant-hiray · Feb 13, 2013

Normally, when a glob which does not match any filenames is expanded, it remains unchanged. Thus, you get results like this:

$ rm .bak rm: cannot remove `.bak': No such file or directory

To avoid this we need to change the default value of nullglob variable.

    #BASH

    shopt -s nullglob

    for i in dir1/*.txt_dir; do
       mv $i/*.txt  dir2/'basename $i'.txt
    done

Read more about it here: http://mywiki.wooledge.org/NullGlob

Hope this helps!