简体   繁体   English

bash脚本正则表达式

[英]bash script regular expressions

I am not very familiar with Linux scripts using reg expressions, 我对使用reg表达式的Linux脚本不是很熟悉,

but I need a script that will do this: 但我需要一个脚本来做到这一点:

copy A/first_image.jpg  to B/first_image/thumb.jpg

copy A/second_image.jpg to B/second_image/thumb.jpg

etc. 等等

every image in a folder A is copied in a directory with his name (in a directory B), and renamed to thumb.jpg 文件夹A中的每个图像都会复制到具有其名称的目录中(在目录B中), thumb.jpg命名为thumb.jpg

How can I do this? 我怎样才能做到这一点?

You use filename "globbing", which is comparable to, but different from, regular expressions as such. 您使用的文件名“ globbing”与正则表达式类似,但与之不同。 You also use a loop and the command basename . 您还可以使用循环和命令basename Like this: 像这样:

for file in A/*.jpg
do
    base=`basename $file .jpg`
    mkdir -p B/${base}
    cp A/${base}.jpg B/${base}/thumb.jpg
done

Run this script from parent directory of A, B, C etc: A, B, C等的父目录运行此脚本:

for s in */*.jpg; do
    f="${s##*/}"
    d="${s%%/*}/${f%%.*}"
    mkdir -p "$d" 2>/dev/null
    cp "$s" "$d/thumb.jpg"
done

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

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