Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何递归地将图像从一个文件夹调整到另一个文件夹?_Bash_Thumbnails - Fatal编程技术网

Bash 如何递归地将图像从一个文件夹调整到另一个文件夹?

Bash 如何递归地将图像从一个文件夹调整到另一个文件夹?,bash,thumbnails,Bash,Thumbnails,我想创建我组织到一组嵌套子目录中的图像缩略图,并将其镜像到文件结构中,以便以下类型的命令: ./imageresize.sh Large Small 10 …将转换“/Large”下嵌套目录中的任何.jpg或.jpg文件: 将10%的缩略图添加到具有不同顶部目录的镜像目标(“小”而不是“大”,例如): 这就是我目前所拥有的,但我似乎无法让它工作。$newfile变量似乎无效,但我不知道为什么,在测试时,它会将“convert”命令的结果输出到屏幕。非常感谢您的帮助/建议 #!/bin/bash

我想创建我组织到一组嵌套子目录中的图像缩略图,并将其镜像到文件结构中,以便以下类型的命令:

./imageresize.sh Large Small 10
…将转换“/Large”下嵌套目录中的任何.jpg或.jpg文件:

将10%的缩略图添加到具有不同顶部目录的镜像目标(“小”而不是“大”,例如):

这就是我目前所拥有的,但我似乎无法让它工作。$newfile变量似乎无效,但我不知道为什么,在测试时,它会将“convert”命令的结果输出到屏幕。非常感谢您的帮助/建议

#!/bin/bash

#manage whitespace and escape characters
OIFS="$IFS"
IFS=$'\n'

#create file list
filelist=$(find ./$1/ -name "*.jpg" -o -name "*.JPG")

for file in $filelist
do

#create destination path for 'convert' command
newfile= ${file/$1/$2}

convert "$file" -define jpeg:extent=10kb -scale $3% "$newfile"
done

不知道这是否只是一个复制/粘贴错误,或者它是否确实存在于脚本中,但这一行:

newfile= ${file/$1/$2}
在Bash中是无效的赋值,因为赋值时不允许使用
=
周围的空格

请尝试以下方法:

newfile=${file/$1/$2}
作为旁注
find
也有一个不区分大小写的搜索,
-iname
,因此您可以执行以下操作:

filelist=$(find ./$1/ -iname "*.jpg")

它还有用于在结果集上执行命令的
-exec
。这里解释得很好:因此您可以在一个
查找
中完成它。(注意:这并不一定更好,在大多数情况下,这只是一个偏好的问题,我只是把它作为一种可能性提出来。)

因此,修改了工作脚本,并进行了madsen和4ae1e1建议的更正(感谢这两项),并使用rsync命令首先创建目录结构(然后从目标清除无关文件):)。我添加了一个额外的参数和参数检查器,以便现在您可以指定源、目标、以kb为单位的近似目标文件大小以及原始文件的百分比。希望它能帮助其他人。:)


我没有看其他内容,但至少在
newfile=
后面有一个空格。谢谢你的回复。。。而且-iname选项是完美的。我已经按照您的建议更正了格式,它修复了一个我实际上没有意识到的问题,我刚刚发现另一个问题,那就是我需要在转换之前创建目录!因此,我添加了一行:rsync-a--include'/'--exclude'./$1//$2/--不是很优雅,但它可以工作--我将把工作脚本作为一个单独的答案发布在下面。再次感谢!以
OIFS=“$IFS”
IFS=$'\n'
开头的脚本很可能是一个坏脚本!这对我来说似乎是可行的,我复制并粘贴了这一部分,试图处理另一个脚本中的空格/转义字符,但如果你能为我指出正确的方向来纠正错误,我将不胜感激!)嗯,当文件名中有全局字符(例如,
*
[
]
)或换行符时,情况会严重恶化。首先,一个好的指针是:谢谢!我去看看。:)
newfile=${file/$1/$2}
filelist=$(find ./$1/ -iname "*.jpg")
#!/bin/bash

#manage whitespace and escape characters
OIFS="$IFS"
IFS=$'\n'

#check parameters
if [ $# -lt 4 ] || [ "$1" = "--help" ]
then # if no parameters or '--help' as $1 - show help
    echo "______________________________________________________________"
    echo ""
    echo "Useage: thumbnailmirror [Source] [Destination] [Filesize] [Percentage]"
    echo "Source - e.g. 'Fullsize' (directory must exist)" 
    echo "Destination - e.g. 'Thumnail' (directory must exist)"
    echo "Filesize - approx filesize in kb  e.g. '10'"
    echo "Percentage - % of reduction (1-100)"
    echo "e.g. thumbnailmirror Fullsize Thumnail 18 7"
    echo "______________________________________________________________"


else # parameters exist
    #syncronise directory structure (directories only)
    rsync -a --include '*/' --exclude  '*'  ./$1/ ./$2/
    # delete any extraneous files and directories at destination
    rsync -a --delete --existing --ignore-existing ./$1/ ./$2/
    #create file list ( -iname means not case sensitive)
    filelist=$(find ./$1/ -iname "*.jpg")

    for file in $filelist
      do
      #define destination filename for 'convert' command
      newfile=${file/$1/$2}

      if [ ! -f "$newfile" ] #if file doesn't exists create it
      then
          convert "$file" -define jpeg:extent=$3kb -quality 100 -scale $4% "$newfile"
          echo "$file resized"
      else #skip it
          echo "Skipping $file - exists already"
      fi
      done
fi