Simple as this.
(bash)
for i in * ; do mv -- "$i" "${i:0:5}" ; done
Voila.
And an explanation from Advanced Bash-Scripting Guide (Chapter 10. Manipulating Variables), (with extra NOTEs inline to highlight the errors in that manual):
Substring Extraction
${string:position}
Extracts substring from $string
at $position
.
If the $string
parameter is "*" or "@", then this extracts the positional parameters, starting at $position
.
${string:position:length}
Extracts $length
characters of substring from $string
at $position
.
NOTE missing quotes around parameter expansions! echo
should not be used for arbitrary data.
stringZ=abcABC123ABCabc
# 0123456789.....
# 0-based indexing.
echo ${stringZ:0} # abcABC123ABCabc
echo ${stringZ:1} # bcABC123ABCabc
echo ${stringZ:7} # 23ABCabc
echo ${stringZ:7:3} # 23A
# Three characters of substring.
# Is it possible to index from the right end of the string?
echo ${stringZ:-4} # abcABC123ABCabc
# Defaults to full string, as in ${parameter:-default}.
# However . . .
echo ${stringZ:(-4)} # Cabc
echo ${stringZ: -4} # Cabc
# Now, it works.
# Parentheses or added space "escape" the position parameter.
The position and length arguments can be "parameterized," that is,
represented as a variable, rather than as a numerical constant.
If the $string
parameter is "*" or "@", then this extracts a maximum
of $length
positional parameters, starting at $position
.
echo ${*:2} # Echoes second and following positional parameters.
echo ${@:2} # Same as above.
echo ${*:2:3} # Echoes three positional parameters, starting at second.
NOTE: expr substr
is a GNU extension.
expr substr $string $position $length
Extracts $length
characters from $string
starting at $position
.
stringZ=abcABC123ABCabc
# 123456789......
# 1-based indexing.
echo `expr substr $stringZ 1 2` # ab
echo `expr substr $stringZ 4 3` # ABC
NOTE: That echo
is redundant and makes it even less reliable. Use expr substr + "$string1" 1 2
.
NOTE: expr
will return with a non-zero exit status if the output is 0 (or -0, 00...).
BTW. The book is present in the official Ubuntu repository as abs-guide
.
bash
tag if you're asking for ash
solution?