Naaz
Naaz

Reputation: 57

script to Rename files in a Directory

I am creating a script to rename 3 files in a directotry, if the files exist

for Ex. I have the files as

customer.txt
account.txt
reference.txt

Need to rename them as

customer_arrivaldate.txt
account_arrivaldate.txt
reference_arrivaldate.txt

I am taking the file names in a text file and then renaming them

Iam doing something like

for var1 in `cat ${directory}file1.txt`
do
   file_name=${var1}_arrivaldate.txt

I need to modify them directly, kindly suggest some modifications

Upvotes: 0

Views: 61

Answers (1)

mattias
mattias

Reputation: 2108

This can be achieved with the following:

#!/bin/bash
dateVar="2014-11-16"
for file in *.txt ; do mv $file ${file//.txt/_$dateVar.txt} ; done

Explanation: All files in current directory ending with .txt will be renamed to $filename -txt +dateVar+.txt

Upvotes: 1

Related Questions