-1

Ciao,

i need rename the file created in one path with this syntax

20230103143408_device_activation.csv

in

20230103143408_attivazioniDevice.csv

This part will be replace _device_activation.csv into _attivazioniDevice.csv

How can i proceed?

mmv '*device_activation.csv' 'attivazioniDevice.csv'

  • do you want to keep the number before _device_activation.csv? – Aven Desta Jan 03 '23 at 15:52
  • Does this answer your question? [Rename multiple files based on pattern in Unix](https://stackoverflow.com/questions/1086502/rename-multiple-files-based-on-pattern-in-unix) – Aven Desta Jan 03 '23 at 16:02

2 Answers2

0

There are many ways to achieve this, but a simple one that springs to mind is using find, cut and xargs like this...

find -type f -name '*device_activation.csv' | cut -d'_' -f1 | xargs -I % sh -c "mv %_device_activation.csv %_attivazioniDevice.csv"

Example usage...

ls -l
-rw-r--r--. 1 kcc users 0 Jan  3 15:39 20230103143407_device_activation.csv
-rw-r--r--. 1 kcc users 0 Jan  3 15:34 20230103143408_device_activation.csv
-rw-r--r--. 1 kcc users 0 Jan  3 15:39 20230103143409_device_activation.csv

find -type f -name '*device_activation.csv' | cut -d'_' -f1 | xargs -I % sh -c "mv %_device_activation.csv %_attivazioniDevice.csv"

ls -l
-rw-r--r--. 1 kcc users 0 Jan  3 15:39 20230103143407_attivazioniDevice.csv
-rw-r--r--. 1 kcc users 0 Jan  3 15:34 20230103143408_attivazioniDevice.csv
-rw-r--r--. 1 kcc users 0 Jan  3 15:39 20230103143409_attivazioniDevice.csv

Lee
  • 149
  • 1
  • 8
0

If you have only one file, then hardcoding is the best solution. But if you have multiple files that end with CSV and want to rename all, Use this.

for file in *.csv;
 do mv $file ${file//_device_activation/_attivazioniDevice}; 
done

terminal output before and after command run

Aven Desta
  • 2,114
  • 12
  • 27