Interacting with files from multiple directories via bash script

前端 未结 2 724
时光说笑
时光说笑 2021-01-22 15:37

I generated a script that iterates through several .csv files, converting relevant files to UTF-8:

#!/bin/bash

cd /home/user/prod/
charset=\"text/plain; charset         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-22 15:50

    You already accepted the answer of @Charles Duffy but (if I understood well) your question is about having files in different directories so if you need to work with multiple csv files on multiple directories you can use the following snippet:

    # array containing the different directories to work with
    pathDir=("/foo/bar/dir1" "/buzz/fizz/dir2")
    
    for dir in "${pathDir[@]}" # For each directory
    do
        for file in "$dir/"*.csv; do # For each csv file of the directory
    
            if [[ $(file -i "$file") == "$file: $charset" ]]; then
                iconv -f ISO-8859-1 -t UTF-8 "$file" > "$file.new";
                mv -f "$file.new" "$file";
            fi
    
        done
    done
    

    The pathDir variable is an array which contains the path of different directories.

    The first for loop iterate through this array to get all the paths to check.

    The second for loop as in the previous answer iterate through the files of the current tested directory.

提交回复
热议问题