tyhunter
V2EX  ›  问与答

请教下 bash 脚本如何满足这种场景下的重命名

  •  
  •   tyhunter · Feb 28, 2022 · 1037 views
    This topic created in 1533 days ago, the information mentioned may be changed or developed.
    场景如下:当前存在一些文件需要用多个指定值替换掉多个匹配值
    (如 xxx_A 重命名为 xxx-e01 ,xxx_B 重命名为 xxx_e02 ,分别对应的关系),想到了 sed 命令,但是发现无法执行

    from_folder=/root/test ###原文件夹路径
    new_folder=/root/new ###新文件夹路径

    for file in $(find $from_folder) ###列出所有文件包含路径
    do
    [[ -d $file ]] && continue ###判断是否为文件夹
    if [[ -e ${file##*/} ]];then ###判断是否存在重名文件
    mv $file $(echo $file | sed 's/\//_/g') ####如果是重名,在名字前加入路径
    else
    newfile='echo $file | sed 's/_A/-e01/;s/_B/-e02/;s/_C/-e03/;s/_D/-e04/;'
    mv $file $newfile ###重命名文件
    mv $newfile $new_folder
    fi
    done

    但执行的时候报错了,提示 mv: target 's/_A/-e01/' is not a directory ,小白搞不清楚是哪里赋值传递出错了,还请 bash 大佬指点一下
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5644 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 07:21 · PVG 15:21 · LAX 00:21 · JFK 03:21
    ♥ Do have faith in what you're doing.