简体   繁体   English

BASH脚本通过*%*的文件名中的空格来创建*文件名

[英]BASH script to *create* filenames with spaces from filenames with “%20”

First, I know this sounds ass backwards. 首先,我知道这听起来倒是混蛋。 It is. 它是。 But I'm looking to convert (on the BASH command line) a bunch of script-generated thumbnail filenames that do have a "%20" in them to the equivalent without filenames. 但是,我正在寻找(在BASH命令行上)将一堆脚本生成的缩略图文件名(其中确实包含“%20”)转换为没有文件名的等效文件名。 In case you're curious, the reason is because the script I'm using created the thumbnail filenames from their current URLs, and it added the %20 in the process. 如果您感到好奇,那是因为我正在使用的脚本从其当前URL创建了缩略图文件名,并在此过程中添加了%20。 But now WordPress is looking for files like "This%20Filename.jpg" and the browser is, of course, removing the escape character and replacing it with spaces. 但是现在WordPress正在寻找“ This%20Filename.jpg”之类的文件,浏览器当然会删除转义符并将其替换为空格。 Which is why one shouldn't have spaces in filenames. 这就是为什么文件名中不应包含空格的原因。

But since I'm stuck here, I'd love to convert my existing thumbnails over. 但是由于我一直呆在这里,所以我想将现有的缩略图转换过来。 Next, I will post a question for help fixing the problem in the script mentioned above. 接下来,我将在上述脚本中发布一个问题以帮助解决该问题。 What I'm looking for now is a quick script to do the bad thing and create filenames with spaces out of filenames with "%20"s. 我现在正在寻找的是一种快速脚本来完成坏事情,并创建文件名,并在文件名中以“%20”表示空格。

Thanks! 谢谢!

If you only want to replace each literal %20 with one space: 如果你只是想替换每个文字%20有一个空间:

for i in *; do
    mv "$i" "${i//\%20/ }"
done

(for instance this will rename file%with%20two%20spaces to file%with two spaces ). (例如,这会将file%with%20two%20spaces重命名为file%with two spaces )。 You'll probably need to apply %25 -> % too though, and other similar transforms. 您可能也需要应用%25 -> % ,以及其他类似的转换。

convmv can do this, no script needed. convmv可以执行此操作,不需要脚本。

$ ls
a%20b.txt
$ convmv --unescape *.txt --notest
mv "./a%20b.txt"    "./a b.txt"
Ready!
$ ls
a b.txt

personally, I don't like file names with spaces - beware you will have to treat them specially in future scripts. 就个人而言,我不喜欢带有空格的文件名-请注意,在以后的脚本中,您将必须特别对待它们。 Anyway, here is the script that will do what you want to achieve. 无论如何,这是将执行您想要实现的脚本。

#!/bin/sh
for fname in `ls *%20*`
do
  newfname=`echo $fname | sed 's/%20/ /g'`
  mv $fname "$newfname"
done;

Place this to a file, add execute permission and run this from the directory where you have file with %20 in their names. 将此文件放置到文件中,添加执行权限,然后从名称包含%20的文件所在的目录中运行该文件。

Code : 代码:

#!/bin/bash

# This is where your files currently are
DPATH="/home/you/foo/*.txt"

# This is where your new files will be created
BPATH="/home/you/new_foo" 

TFILE="/tmp/out.tmp.$$"
[ ! -d $BPATH ] && mkdir -p $BPATH || :
for f in $DPATH
do
  if [ -f $f -a -r $f ]; then
    /bin/cp -f $f $BPATH
   sed "s/%20/ /g" "$f" > $TFILE && mv $TFILE "$f"
  else
   echo "Error: Cannot read $f"
  fi
done
/bin/rm $TFILE

Not bash, but for the more general case of %hh (encoded hex) in names. 不是bash,而是名称中%hh(编码的十六进制)的更一般的情况。

#!/usr/bin/perl
foreach $c(@ARGV){
    $d=$c;
    $d=~s/%([a-fA-F0-9][a-fA-F0-9])/my $a=pack('C',hex($1));$a="\\$a"/eg;
    print `mv $c $d` if ($c ne $d);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM