0

I have a directory like: /data/work/files/

calldata_phonecalls_2131201401_01.zip calldata_phonecalls_7373201401_02.zip

In this directory I want to create new files corresponding to the zipfiles like , but modifying a small part of the name in BASH:

calldata_calllog_2131201401_01.tsv calldata_calllog_7373201401_02.tsv

pl note "phonecalls" changed to "calllog"

Pl help.

user1189851
  • 4,861
  • 15
  • 47
  • 69

2 Answers2

4

Using Shell Parameter Expansion and basename:

for f in /data/work/files/*.zip; do
  mv "$f" "$(basename "${f/phonecalls/calllog}" .zip).tsv"
done

Using rename (part of perl distribution):

rename 's/phonecalls/calllog/;s/\.zip$/.tsv/' /data/work/files/*.zip
devnull
  • 118,548
  • 33
  • 236
  • 227
1

Using rename :

Using one version of rename:

rename 's/^fgh/jkl/' fgh*

Using another version of rename :

rename fgh jkl fgh*

You should check your platform's man page to see which of the above applies.

Using mv:

find ./ -name "*.xyz\[*\]" | while read line
do 
mv "$line" ${line%.*}.xyz
done

Another way

ls -1 | nawk '/foo-bar-/{old=$0;gsub(/-\(.*\)/,"",$0);system("mv \""old"\" "$0)}'

Yet another:

for f in fgh*; do mv $f $(echo $f | sed 's/^fgh/jkl/g'); done

There are several other answers under stackoverflow and superuser pages:

Copied/summarized from the following links:

Rename multiple files in Unix

How to use mv command to rename multiple files in unix?

Community
  • 1
  • 1
Satyajit R
  • 72
  • 4