Search code examples
linuxbashshellcsvexport-to-csv

renaming *.txt.csv files to .csv in bash script


I have .txt files, which I am taking as $item and then I am changing the encoding with

iconv -f $currentEncoding -t $targetEncoding "$item" -o "$item.tmp"

then I am saving it again to txt file using

 mv "$item.tmp" "$item.txt";

next I am trimming a few things in txt file and saving it as a csv file with

 tr -d '"' < "$item.txt" > "$item.csv";

but eventually my files are getting stored with extension "*.txt.csv" - I want them to be just .csv - can anyone help me please what I am doing wrong or what could I change. Thanks


Solution

  • Run:

    for f in *.txt.csv; do mv $f ${f/.txt./.}; done
    

    If the variable $f contains the string item.txt.csv, the expression ${f/.txt./.} removes .txt from the file name and gives only the string item.csv.

    Caution: if one of the filenames contain spaces, the for statement will not work as expected.