R4F6
R4F6

Reputation: 792

How do I mass rename only part of file names?

Ive got hundreds of files I need to rename. Currently they look something like this:

localhost.programA.programA1.programA1stuff

and I want to change it to

unixServer.programA.programA1.programA1stuff

As you can see, I just want to change the first portion of the file name to be the new host name. I am new-ish to unix/linux so Im not sure if there is an easy way to do this that isnt by hand. I imagine that there is some kind of crazy awk/sed one liner that could do this, but I am not remotely familiar with either one of those tools to even know where to begin.

Upvotes: 1

Views: 107

Answers (3)

slayedbylucifer
slayedbylucifer

Reputation: 23502

In Bash:

$ find . -iname "localhost*"
./localhost.programA.programA1.programA1stuff

$ find . -iname "localhost*" | while read file; do mv $file ${file/localhost/unixserver}; done

$ find . -iname "unixserver*"
./unixserver.programA.programA1.programA1stuff

Upvotes: 1

Leo Prince
Leo Prince

Reputation: 2050

Well, rename command just worked for me.

root@H61H2-MV:~/3# for i in {a..e} ; do touch localhost.program$i.program$i1.program$i1stuff ; done
root@:~/# ls
localhost.programa.program.program  localhost.programd.program.program
localhost.programb.program.program  localhost.programe.program.program
localhost.programc.program.program
root@technomics-H61H2-MV:~/# rename 's/localhost/unixServer/' localhost.*
root@H61H2-MV:~/# ls
unixServer.programa.program.program  unixServer.programd.program.program
unixServer.programb.program.program  unixServer.programe.program.program
unixServer.programc.program.program
root@H61H2-MV:~/# 

Rename command syntax as I used, will work like charm for your requirement.. give it a try..

Upvotes: 1

Antoine Foucault
Antoine Foucault

Reputation: 70

You can use the "rename" command.

In your case :

rename localhost unixServer ./localhost*

You can be more specific if you want.

http://man7.org/linux/man-pages/man1/rename.1.html

Upvotes: 3

Related Questions