iniファイルに基づいてターゲットにファイルをコピーする

iniファイルに基づいてターゲットにファイルをコピーする

config.ini私はそれぞれ、ファイルとJPEG画像を含む数千のサブディレクトリを持つディレクトリを持っています。 iniファイルには、画像が撮影された時間をエンコードするセクションが含まれていますが、これに限定されません。

[Acquisition]
Name=coating_filtered_001
Comment=Image acquisition
Year=2017
Month=3
Day=21
Hour=13
Minute=2
Second=34
Milliseconds=567

この質問の場合、画像ファイルの名前は常に同じですimage.jpg

すべての画像ファイルを別の(単一)ディレクトリにコピーし、名前を類似または類似したものに変更したいと思いますyyyy-mm-ddThh:mm:ss:NNN.jpg。つまり、iniファイルのタイムスタンプで構成されるファイル名です。

コマンドラインでこれを達成できますか?

ベストアンサー1

それできるコマンドラインでこれを行うことは可能ですが、コマンドラインからスクリプトを実行する方が簡単な解決策になります。

基本ステップ:

  • 繰り返すディレクトリのリストを取得します。
    find ${directory} -mindepth 1 -type d

  • 各ディレクトリの存在を確認してconfig.iniくださいimage.jpg
    if [ -f ${subdir}/config.ini -a -f ${subdir}/image.jpg ]; then ...

  • config.iniでタイムスタンプの正しい部分をすべて確認してください。
    さまざまなものgrep ^Year= ${subdir}/config.iniなど^Month
  • タイムスタンプを含むimage.jpgファイルをコピーします。
    cp ${subdir}/image.jpg ${copydir}/${timestamp}.jpg

私はこれらのシーケンスをスクリプトに入れる方が簡単で安全であると思います。スクリプトでは、読みやすい出力、エラー処理などを簡単に配置できます。

以下は、これらの手順を実行するサンプルスクリプトです。

#!/bin/bash

imagepath="/path/to/images"
copydir="/path/to/copies"

# step 1: find all the directories
for dir in $(find ${imagepath} -mindepth 1 -type d); do
    echo "Procesing directory $dir:"
    ci=${dir}/config.ini
    jp=${dir}/image.jpg

    # step 2: check for config.ini and image.jpg
    if [ -f ${ci} -a -f ${jp} ]; then
        # step 3: get the parts of the timestamp
        year=$(grep ^Year= ${ci}   | cut -d= -f2)
        month=$(grep ^Month= ${ci} | cut -d= -f2)
        day=$(grep ^Day= ${ci}     | cut -d= -f2)
        hour=$(grep ^Hour= ${ci}   | cut -d= -f2)
        min=$(grep ^Minute= ${ci}  | cut -d= -f2)
        sec=$(grep ^Second= ${ci}  | cut -d= -f2)
        ms=$(grep ^Milliseconds= ${ci} | cut -d= -f2)

        # if any timestamp part is empty, don't copy the file
        # instead, write a note, and we can check it manually
        if [[ -z ${year} || -z ${month} || -z ${day} || -z ${hour} || -z ${min} || -z ${sec} || -z ${ms} ]]; then
            echo "Date variables not as expected in ${ci}!"
        else
            # step 4: copy file
            # if we got here, all the files are there, and the config.ini
            # had all the timestamp parts.
            tsfile="${year}-${month}-${day}T${hour}:${min}:${sec}:${ms}.jpg"
            target="${copydir}/${tsfile}"
            echo -n "Archiving ${jp} to ${target}: "
            st=$(cp ${jp} ${target} 2>&1)
            # capture the status and alert if there's an error
            if (( $? == 0 )); then
                echo "[ ok ]"
            else
                echo "[ err ]"
            fi
            [ ! -z $st ] && echo $st
        fi
    else
        # other side of step2... some file is missing... 
        # manual check recommended, no action taken
        echo "No config.ini or image.jpeg in ${dir}!"
    fi
    echo "---------------------"
done

誤ってファイルを削除しないように、これらのスクリプトを使用するときは注意してください。このスクリプトはコピー操作を1回だけ実行するため、非常に保守的であり、ソースファイルを破損しません。ただし、必要に応じて特定のジョブを変更したり、メッセージを出力したりする必要があります。

おすすめ記事