In bash. So that 001 file.ext
becomes file.ext
. How would I 开发者_StackOverflow中文版do that?
You can use two seds expressions. The first one removes everything upto the last space, and the second strips off leading digits.
$ echo "001 fiile.ext" | sed -e 's/^.* //' -e 's/^[0-9]*//g'
fiile.ext
for f in *file.ext
do
newname=$(echo "$f" | sed 's/^[0-9 ]*//')
mv "$f" "$newname"
done
Or in pure Bash:
shopt -s extglob
for f in *file.ext
do
mv "$f" "${f##+([0-9 ])}"
done
If you just want to convert filenames (as vars in a script), you can just do something like:
[[ $FILENAME =~ ^[0-9\s]+(.+) ]] && FILENAME="${BASH_REMATCH[1]}"
If you're trying to actually rename filenames on the filesystem, something like this is more appropriate
for F in *.ext; do
[[ $F =~ ^[0-9\s]+(.+) ]] && mv "$F" "${BASH_REMATCH[1]}"
done
精彩评论