5

I am trying to rename multiple files with extension xyz[n] to extension xyz

example :

mv *.xyz[1] to *.xyz

but the error is coming as - " *.xyz No such file or directory"

theharshest
  • 7,767
  • 11
  • 41
  • 51
Learner
  • 1,544
  • 8
  • 29
  • 55

4 Answers4

5

Don't know if mv can directly work using * but this would work

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

Let's say we have some files as shown below.Now i want remove the part -(ab...) from those files.

> ls -1 foo*
foo-bar-(ab-4529111094).txt
foo-bar-foo-bar-(ab-189534).txt
foo-bar-foo-bar-bar-(ab-24937932201).txt

So the expected file names would be :

> ls -1 foo*
foo-bar-foo-bar-bar.txt
foo-bar-foo-bar.txt
foo-bar.txt
> 

Below is a simple way to do it.

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

for detailed explanation check here

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • Thanks Sarathi for replying When I fired the command on the terminal I got the error as nawk command not found Exact O/P is "-bash: nawk: command not found" Then I tried with awk command as ls -1 |awk '/110X/{old=$0;gsub(/[/,"",$0);system("mv \""old"\" "$0)}' and I got the error " awk: nonterminated character class [ source line number 1 context is >>> /110X/{old=$0;gsub(/[/,"",$0) " – Learner Jan 10 '13 at 14:01
  • My file names are like 20130110X1YZ.rtf[2] 20130110XYZ.rtf[1] 20130110XYZ1.rtf[2] and I want to remove the [n] values from the name like 20130110X1YZ.rtf for 20130110X1YZ.rtf[2] – Learner Jan 10 '13 at 14:02
  • You can try awk instead of nawk.nawk i use on solaris.So it worked on solaris. – Vijay Aug 26 '13 at 05:41
0

Here is another way using the automated tools of StringSolver. Let us say your first file is named abc.xyz[1] a second named def.xyz[1] and a third named ghi.jpg (not the same extension as the previous two).

First, filter the files you want by giving examples (ok and notok are any words such that the first describes the accepted files):

filter abc.xyz[1] ok def.xyz[1] ok ghi.jpg notok

Then perform the move with the filter it created:

mv abc.xyz[1] abc.xyz
mv --filter --all

The second line generalizes the first transformation on all files ending with .xyz[1].

The last two lines can also be abbreviated in just one, which performs the moves and immediately generalizes it:

mv --filter --all abc.xyz[1] abc.xyz

DISCLAIMER: I am a co-author of this work for academic purposes. Other examples are available on youtube.

Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
0

I think mv can't operate on multiple files directly without loop. Use rename command instead. it uses regular expressions but easy to use once mastered and more powerful.

rename 's/^text-to-replace/new-text-you-want/' text-to-replace*

e.g to rename all .jar files in a directory to .jar_bak

rename 's/^jar/jar_bak/' jar*
Mandrake
  • 411
  • 4
  • 2