Bash

查找 + 如何重新編輯文件名

  • July 13, 2020

在文件夾下,/home/files我們有以下文件

VER_3all_servers_home\server383_232.35.35.341
VER_3all_servers_home\server323_232.35.35.342
VER_3all_servers_home\server313_232.35.35.343
VER_3all_servers_home\server303_232.35.35.344
VER_3all_servers_home\server381_232.35.35.345
VER_3all_servers_home\server380_232.35.35.346

我們想重新編輯文件,3all_servers_home\從下面的所有文件中刪除單詞-“”/home/files

所以文件看起來像這樣

VER_server383_232.35.35.341
VER_server323_232.35.35.342
VER_server313_232.35.35.343
VER_server303_232.35.35.344
VER_server381_232.35.35.345
VER_server380_232.35.35.346

有什麼建議如何做這種方法嗎?

你可以做:

for f in /home/files/*3all_servers_home*; do 
   mv -- "$f" "${f//3all_servers_home\\}"
done

該格式將從變數${var//glob}中刪除所有匹配的 glob 。glob``var

或者,如果你有(我不記得它在 Red Hat 上叫什麼,perl-rename它將是或之一):rename``prename``perl-rename

rename 's/3all_servers_home\\//' /home/files/*3all_servers_home*

引用自:https://unix.stackexchange.com/questions/598212