【问题标题】:Script to go to every subdirectory of a folder and run command脚本转到文件夹的每个子目录并运行命令
【发布时间】:2018-05-17 17:19:26
【问题描述】:

我正在尝试编写一个执行以下步骤的脚本: 1. 转到运行脚本时作为参数给出的目录中的每个文件夹。 2.在每个子目录中创建一个名为“Histogram”的文件夹 3. 读出每个以 *.jpg 结尾的文件 4、通过命令“Convert XY -format %c histogram:info:XY.txt”将图片转换为txt文件 5.将txt文件移动到文件夹“Histogram” 6.回到开始目录并继续下一个文件夹

这是我目前得到的:

pfad="${1:-.}"
cd "$pfad/"
mkdir Histogram
for i in 'ls -1 *.jpg'
do
cat $1 |
convert $i -format %c histogram:info:"Histogram_$1'.txt"|
mv "Histogram_$i'.txt" Histogram/

仍然缺少脚本对每个子目录执行命令,文件夹的制作和文件夹中txt文件的移动也不起作用。

【问题讨论】:

  • 这更像是我的家庭作业问题。但无论如何,你可以做一个dirs=`find . -type d`,然后用for dir in $dirs 遍历目录,就像你已经用*.jpg 做的那样。在循环中,您可以创建目录并移动文件
  • 这样的结果是脚本在开始目录中创建了一个文件夹并且不访问子目录。因此,第二个循环也不起作用,因为我收到反馈“无法访问 *jpg:没有这样的文件或目录”,因为开始目录中没有 jpg 文件

标签: linux bash shell loops subdirectory


【解决方案1】:

创建一个脚本,该脚本采用绝对文件名列表,如果不存在则创建直方图文件夹并创建 txt 文件

#!/usr/bin/bash
for absfile in "$@"; do
    dir=${absfile%/*}/Histogram
    file=${absfile##*/}
    mkdir -p "$dir" || exit 1
    convert "$absfile" -format %c histogram:info:"$dir/Histogram_$file.txt" || exit 1
done

在 find 命令中调用此脚本

find root_path -name '*.jpg' -exec ./script.sh {} +

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多