批量删除文件名中带有特殊字符的子字符串
问题描述:
我的目录中有文件列表:
I have a list of files in my directory:
opencv_calib3d.so2410.so
opencv_contrib.so2410.so
opencv_core.so2410.so
opencv_features2d.so2410.so
opencv_flann.so2410.so
opencv_highgui.so2410.so
opencv_imgproc.so2410.so
opencv_legacy.so2410.so
opencv_ml.so2410.so
opencv_objdetect.so2410.so
opencv_ocl.so2410.so
opencv_photo.so2410.so
它们是批处理重命名所产生的一系列错误的产物,现在我不知道如何从每个重命名中删除中间的".so".例如:
They're the product of a series of mistakes made with batch renames, and now I can't figure out how to remove the middle ".so" from each of them. For example:
opencv_ocl.so2410.so
应该是opencv_ocl2410.so
这是我尝试过的:
# attempt 1, replace the (first) occurrence of `.so` from the filename
for f in opencv_*; do mv "$f" "${f#.so}"; done
# attempt 2, escape the dot
for f in opencv_*; do mv "$f" "${f#\.so}"; done
# attempt 3, try to make the substring a string
for f in opencv_*; do mv "$f" "${f#'.so'}"; done
# attempt 4, combine 2 and 3
for f in opencv_*; do mv "$f" "${f#'\.so'}"; done
但是所有这些 均无效,并产生错误消息:
But all of those have no effect, producing the error messages:
mv: ‘opencv_calib3d.so2410.so’ and ‘opencv_calib3d.so2410.so’ are the same file
mv: ‘opencv_contrib.so2410.so’ and ‘opencv_contrib.so2410.so’ are the same file
mv: ‘opencv_core.so2410.so’ and ‘opencv_core.so2410.so’ are the same file
...
答
在您的mv
命令中尝试以下操作:
Try this in your mv
command:
mv "$f" "${f/.so/}"
.so
的第一个匹配项已替换为空字符串.
First match of .so
is being replaced by empty string.