1) for file; do
loops over the positional parameters, i.e. command line arguments to the script. Use for file in *; do
to loop over the files in the directory (or _*
to just take the ones with an underscore prefix.)
2) You can use ${file#_}
instead of ${file//_/}
to remove the underscore from the beginning of the filename. ${par#word}
specifically removes a part from the beginning, and it's a standard feature, unlike ${par/pat/repl}
. And of course ${file//_}
would remove all slashes (since you used a double-slash), not just the first one.
3) at least on Linux (GNU userland) and FreeBSD, you can use mv -n
to ask it to not overwrite any files. Just in case.
So,
for file in _*; do
mv -n -- "$file" "${file#_}"
done
for file; do <stuff>; done
is a syntax error._
prefixfor x; do
is a shortcut forfor x in "$@"; do
, so, assuming that the code is in its own script file, this may work (if called as e.g.script.sh _*
). I understand that's a lot off assumptions :).