別のファイルがあるかどうかに応じてフォルダのファイル名を変更する[閉じる]

別のファイルがあるかどうかに応じてフォルダのファイル名を変更する[閉じる]

現在の状況は、トラフィックの種類(ftp.csv、http.csvなど)とメトリック(cpu.csvとmemory.csv)を含む複数のフォルダがあることです。

フォルダ1> cpu.csv http.csv

フォルダ2> cpu.csv ftp.csv

すべてのフォルダのインジケータファイルには同じ名前(cpu.csvなど)があるので、ftp.csvを含むフォルダのcpu.csvの名前をcpu_ftp.csvに変更し、http.csvフォルダのcpu.csvの名前を変更したいと思います。 、CPU .csvをcpu_http.csvに移動したいと思います。

以下のようにフォルダを移動したいと思います。 1>cpu_http.csv http.csv

bashスクリプトで実装するのに役立ちます。

ベストアンサー1

そして強く打つ:

#!/bin/bash

for d in /folder[0-9]*
do
    type=""   # traffic type (either `http` or `ftp`)
    if [ -f "$d/ftp.csv" ]; then     # check if file `ftp.csv` exists within a folder
        type="ftp"
    elif [ -f "$d/http.csv" ]; then  # check if file `http.csv` exists within a folder
        type="http"
    fi
    # if `traffic type` was set and file `cpu.csv` exists - rename the file
    if [ ! -z "$type" ] && [ -f "$d/cpu.csv" ]; then
        mv "$d/cpu.csv" "$d/cpu_$type.csv"
    fi        
done

おすすめ記事