批量重命名URL编码的文件名和文件夹路径

时间:2019-04-22 19:49:48

标签: linux bash macos cygwin

我已经下载了一些文件,不幸的是该工具在文件路径中使用URL编码。我该如何解决?

一些路径:

/tmp/site/test/m%3dplusone
/tmp/site/test/m%3dplusone/rt%3dj
/tmp/site/test/m%3dplusone/rt%3dj/sv%3d1/35.jpg%3f_%3d15099203492
/tmp/site/test/m%3dplusone/rt%3dj/sv%3d1/d%3d1
/tmp/site/test/m%3dplusone/rt%3dj/sv%3d1/d%3d1/ed%3d1
/tmp/site/test/m%3dplusone/rt%3dj/sv%3d1/d%3d1/ed%3d1/am%3dwQ
/tmp/site/test/m%3dplusone/rt%3dj/sv%3d1/d%3d1/ed%3d1/am%3dwQ/rs=AGLTcCOhuNhUv0siK0B4I8EcahYmZPyCaA/cb%3dgapi.loaded_0
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.e1PemSdvnM4.O/m%3dplusone
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.e1PemSdvnM4.O/m%3dplusone/rt%3dj
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.e1PemSdvnM4.O/m%3dplusone/rt%3dj/sv%3d1
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.e1PemSdvnM4.O/m%3dplusone/rt%3dj/sv%3d1/d%3d1
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.e1PemSdvnM4.O/m%3dplusone/rt%3dj/sv%3d1/d%3d1/ed%3d1
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.e1PemSdvnM4.O/m%3dplusone/rt%3dj/sv%3d1/d%3d1/ed%3d1/am%3dwQ
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.e1PemSdvnM4.O/m%3dplusone/rt%3dj/sv%3d1/d%3d1/ed%3d1/am%3dwQ/rs=AGLTcCOhuNhUv0siK0B4I8EcahYmZPyCaA/cb%3dgapi.loaded
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.gzO-5OVAi4o.O/m%3dplusone
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.gzO-5OVAi4o.O/m%3dplusone/rt%3dj
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.gzO-5OVAi4o.O/m%3dplusone/rt%3dj/sv%3d1
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.gzO-5OVAi4o.O/m%3dplusone/rt%3dj/sv%3d1/d%3d1
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.gzO-5OVAi4o.O/m%3dplusone/rt%3dj/sv%3d1/d%3d1/ed%3d1
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.gzO-5OVAi4o.O/m%3dplusone/rt%3dj/sv%3d1/d%3d1/ed%3d1/am%3dwQ
/tmp/site/_/scs/apps-mozaic/_/js/k%3doz.gapi.en_US.gzO-5OVAi4o.O/m%3dplusone/rt%3dj/sv%3d1/d%3d1/ed%3d1/am%3dwQ/rs=AGLTcCN8zpbBBFew5eMDpwxBKYKKSK8U4Q/cb%3dgapi.loaded
/tmp/site/label%3fnode%3d0%26group%3d1%26allgroups%3d1%26offset%3d0%26nbins%3d1%26attrs%3dCategory%2b102275%26irversion%3d620
/tmp/site/id%3fd_visid_ver%3d1.9.0%26d_field%20group%3dA%26mcorgid%3d700CFDC5570CBFE67F000101%2540AdobeOrg%26mid%3d49349141751585207132014752155379255530%26callba
/tmp/site/id%3fd_visid_ver%3d1.9.0%26d_fieldgroup%3dA%26mcorgid%3d700CFDC5570CBFE67F000101%2540AdobeOrg%26mid%3d49349141751585207132014752155379255530%26ts%3d1
/tmp/site/b/ss/global/search%3Fq%3Da1%2Cb2%2Bc3%2Cd%26param%3Dvalue
/tmp/static_assets/media/jammer/megamenu/content_spots/custom/2674305-createyourown%7e1.jpg
/tmp/personal/vt/data%3dNwqMUcvO0zp-zhtVWUbRaUuK8hKaosTsTcCU58fnwoZT5MKfrwefqzeUX84jI1IUbrkBAA7G0LvDFgsIDzCc69CTnzc8Aq4S2HX1r6Mx5VgYQuGShWuY2O0516KXD9a431pw9S06KZJs

这些需要保存到其URL解码路径,例如

  • %3d-> =
  • %3f->无法保存为?,因为没有文件系统支持,因此可以将其替换为-
  • 路径中也有空格,例如%2b
  • 等等。

到目前为止,我可以解决以下问题:

  • 在文件扩展名后删除查询字符串: find . -name '*%3f*' -exec bash -c 'mv $0 ${0/\%3f*}' {} \;

  • 用以下命令替换“ @”字符。 $ find . -name '*%40*' -exec bash -c 'echo $0 ${0/\%40/@}' {} \;

我尝试了类似的方法来重命名/移动目录,但是由于只有第一个出现的位置被替换,所以它不起作用。

$ find . -type d -name '*%3d*' -exec bash -c 'echo $0 ${0/\%3d/=/g}' {} \;/g或全局开关不起作用)

环境是Windows中的macOS / Cygwin。众所周知,macOS默认不支持rename命令。一线会有所帮助。

请帮助并提前谢谢您。

1 个答案:

答案 0 :(得分:1)

尝试使用deurlname包中的renameutils实用程序... cygwinhomebrew都可用...

然后:find /tmp/site | tac | while read line;do deurlname "${line}";done

此外,您的find仅重命名第一个匹配项,因为重命名父文件夹后,其所有内容都将以file not found失败。因此,如果您没有并且不愿意安装homebrew / renameutils,请尝试find /tmp/site | tac | while read line;do mv "${line}" "${line//%3d/=}";done

此外,在执行-exec bash -c '$0 ${0/...'时,您只引用了整个命令,因此,如果尝试将%20重命名为空格,它将失败。如果要使用它,请将文件名双引号括为-exec bash -c 'mv "$0" "${0/...}" {} \;。另外,bash的变量替换${0/}(带有一个斜杠)仅替换第一个匹配项,因此请使用${0//}替换每个匹配项。

或者,如果您想使用find + sedfind /tmp/site | tac | while read line;do mv "${line}" "$(sed -e 's|%3d|=|g' -e 's|%26|\&|g' -e 's|%3f|-|g' <<<$line)";done

一次重命名所有url标签,

我正在使用tac来反转find的输出,避免了在子项之前重命名父文件夹的问题。