我怎么能在一个基础文件夹的子文件夹中的每一个运行的bash [英] How can I run a bash in every subfolder of a base folder

查看:107
本文介绍了我怎么能在一个基础文件夹的子文件夹中的每一个运行的bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要优化我所有的*。PNG图像,发现的所以我做的bash作为它说:

I want to 'optimize' all my *.png images and found this so I made the bash as it says with:

#!/bin/bash    
pngnq -vf -s1 *.png    
rename -f 's/-nq8.png$/.png/' *.png    
optipng -o7 *.png

在这里,我有2个问题:

And here I have 2 problems:

第一个是第二线(重命名)什么也不做......它应该改变所有命名图像 [名] -nq8.png 这是在bash的第一行原来的名称的结果。

The first one is that the second line (rename) does nothing... it is supposed to change all the images named [name]-nq8.png which is the result of the first line of the bash to the original name

第二,主要的问题是:我有一个文件夹命名为ImagesFolder和里面我有其他的文件夹更* .pngs,我要运行这个庆典在ImagesFolder的每个子文件夹,但我不知道如何做到这一点(我在UNIX和shellscripts等是新)和我试过没有工作或者是一个烂摊子...

The second and main problem is: I have a folder named ImagesFolder and inside I have other folders with more *.pngs and I want to run this bash in every subfolder of ImagesFolder but I don't know how to do it (I'm new in unix and shellscripts and so on) and what I tried didn't work or was a mess...

要更好地理解它我有什么是一样的东西:

To understand it better what I have is something like:

ImagesFolder
  |-Folder1
  |     |- Folder12
  |     |      |-20 images here
  |     |- Folder13
  |     |      |- 10 images more here
  |     |- 5 images here 
  |-more images

和我想的脚本从ImagesFolder运行

And I want the script to run from ImagesFolder

感谢你了!

推荐答案

我可能会做的是:

1)固定脚本,以便它在当前目录需要ARGS而不是通配的:

1) Fix the script so it takes args instead of the globbing in the current directory:

#!/bin/bash    
pngnq -vf -s1 "$1"    
mv "${1%.png}-nq8.png" "$1"
optipng -o7 "$1"

2),测试它,它的作品如预期

2) Test it that it works as intended

3)找到的xargs 并行(我敢打赌,你有一个多核处理器)

3) Run it with find and xargs in parallel (I bet you have a multicore processor)

find . -name '*.png' -print0 | xargs -0 -P6 -n1 the_script

该脚本可以内嵌了。但我的主要观点的,使用并行如果你在多核。这将加速了很多东西。

The script could be inline too. But my main point its, use parallelism if you're on multicore. It will speed things up a lot.

这篇关于我怎么能在一个基础文件夹的子文件夹中的每一个运行的bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆