In bash, I have an unzipped directory where I have many file names that start with _
.
I'm pretty sure I can use rename
or mv
to simply remove the _
prefix of each file that has it.
I try:
for file; do
mv "$file" "${file//_/}"
done
But that doesn't have the effect I'd like. What command can I use to remove the _
prefix from all files?
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments