11

I was trying to rename some files to another extension:

# mv  *.sqlite3_done *.sqlite3

but got an error:

mv: target '*.sqlite3' is not a directory

Why?

Ry-
  • 218,210
  • 55
  • 464
  • 476
AngeloC
  • 3,213
  • 10
  • 31
  • 51
  • 1
    I think this has been answered here: https://stackoverflow.com/questions/26519301/bash-error-renaming-files-with-spaces-mv-target-is-not-a-directory Does that sort it for you? – Grey Aug 16 '17 at 01:34

3 Answers3

9

mv can only move multiple files into a single directory; it can’t move each one to a different name. You can loop in bash instead:

for x in *.sqlite3_done; do
    mv -- "$x" "${x%_done}"
done

${x%_done} removes _done from the end of $x.

Ry-
  • 218,210
  • 55
  • 464
  • 476
9

the easy way is use find

find . -type f -name '*.sqlite3_done' -exec sh -c 'x="{}"; mv "$x" "${x%_done}"' \;
Mattia Moscheni
  • 144
  • 1
  • 2
8

The wildcard expansion results in multiple names being passed to the command. The shell thinks you are trying to move multiple files to the *.sqlite3 directory.

You need to use a loop:

for nam in *sqlite3_done
do
    newname=${nam%_done}
    mv $nam $newname
done

The %_done says to remove the last occurrence of _done from the string.

If you may have spaces in your filenames you will want to quote the filenames.

Ry-
  • 218,210
  • 55
  • 464
  • 476
user1683793
  • 1,213
  • 13
  • 18