0

I am writing a shell script to bulk renamed file.

I have thousand thousand csv file like tis

db_dd.ok.csv
kk_dl.rk.csv
mk_kal.csv

and i am trying to rename all these files and the final file name will be these:

dd.ok.csv
dl.rk.csv
kal.csv

and this is my shell script:

#!/bin/bash
for x in *.csv
do
    echo $x | grep '([a-zA-Z]+\.csv)'
done

My regex working good but i can't run here mv command with the value of grep, so i tried to put grep value in a variable then i will run mv command, i failed.

can anyone help me how can I do this?

  • Any reason you're not using the `rename` command? – Barmar Mar 24 '22 at 18:28
  • Why can't you run `mv` with the value of `grep`? Use `$(command)` to substitute the output of a command into the command line. This is one of the most basic shell scripting operations. – Barmar Mar 24 '22 at 18:30
  • how can i do this? –  Mar 24 '22 at 18:34
  • See the linked question. Set a variable `dest` to the command output, then use `mv "$x" "$dest"` – Barmar Mar 24 '22 at 18:37
  • I dont see any option to asign variable with grep value in foor loop –  Mar 24 '22 at 18:42
  • What do you mean? Doesn't the other question show how to do: `dest=$(echo "$x" | grep ...)` – Barmar Mar 24 '22 at 18:43

0 Answers0