Renaming multiples files with a bash loop
Several mistakes here:
NEWNAME =
should be without space. Here bash is looking for a command namedNEWNAME
and that fails.- you parse the output of ls. this is bad if you had files with spaces. Bash can build itself a list of files with the glob operator
*
. - You don't escape
"$i"
and"$NEWNAME"
. If any of them contains a space it makes two arguments for mv. - If a file name begins with a dash
mv
will believe it is a switch. Use--
to stop argument processing.
Try:
for i in chr*do mv -- "$i" "${i/%.fasta/.fa}"done
or
for i in chr*do NEWNAME="${i/%.fasta/.fa}" mv -- "$i" "$NEWNAME"done
The "%{var/%pat/replacement}
" looks for pat
only at the end of the variable and replaces it with replacement
.