系统相关
首页 > 系统相关> > linux – 使用BASH中的特殊字符从文件名中删除子字符串

linux – 使用BASH中的特殊字符从文件名中删除子字符串

作者:互联网

我的目录中有一个文件列表:

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”.例如:

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

但所有这些都没有效果,产生错误消息:

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命令中试试这个:

mv "$f" "${f/.so/}"

.so的第一场比赛正在被空字符串取代.

标签:bash,linux,batch-processing,filenames,batch-rename
来源: https://codeday.me/bug/20190824/1709375.html