LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-06-2013, 03:43 AM   #1
prasad.bh
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Rep: Reputation: Disabled
Smile Need help to rename file through shell script


Hello,
I need some help in shell script..

I have some particular folders in linux server and it has file something like it.

MVSTP_FID_FID_POS_20130205033112.txt

Now, I want to rename it to : MVSTQ_FID_FID_POS_20130205033112.txt

Note, the only difference is Q in the original name..

I just want to this one character change..

COuld anyone enlighten me how I can achieve this ?

Thanks in advance
 
Old 02-06-2013, 04:58 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Use mv cmd
Code:
mv MVSTP_FID_FID_POS_20130205033112.txt MVSTQ_FID_FID_POS_20130205033112.txt
http://linux.die.net/man/1/mv
 
Old 02-06-2013, 05:29 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If the first part of the file names is always the same you can easily change them by means of rename. Beware that rename comes in two different flavours, mostly depending on your Linux distribution:
Code:
rename MVSTP MVSTQ MVSTP*
or
Code:
rename 's/MVSTP/MVSTQ/' MVSTP*
To avoid confusion or for portability, use the mv command - as suggested by Chris - and compute the new name using shell parameter substitution, e.g.
Code:
for file in MVSTP*
do
  mv $file ${file/MVSTP/MVSTQ}
done
Please see http://www.gnu.org/software/bash/man...eter-Expansion for details. On the other hand, if the pattern is not the same please clarify and show us different file names in order to see the common features.
 
Old 02-06-2013, 06:15 AM   #4
prasad.bh
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
Use mv cmd
Code:
mv MVSTP_FID_FID_POS_20130205033112.txt MVSTQ_FID_FID_POS_20130205033112.txt
http://linux.die.net/man/1/mv
I know the command, but wanted to change it through script.. thanks for your answer anyway
 
Old 02-06-2013, 06:16 AM   #5
prasad.bh
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
If the first part of the file names is always the same you can easily change them by means of rename. Beware that rename comes in two different flavours, mostly depending on your Linux distribution:
Code:
rename MVSTP MVSTQ MVSTP*
or
Code:
rename 's/MVSTP/MVSTQ/' MVSTP*
To avoid confusion or for portability, use the mv command - as suggested by Chris - and compute the new name using shell parameter substitution, e.g.
Code:
for file in MVSTP*
do
  mv $file ${file/MVSTP/MVSTQ}
done
Please see http://www.gnu.org/software/bash/man...eter-Expansion for details. On the other hand, if the pattern is not the same please clarify and show us different file names in order to see the common features.
Hi colucix, your last code worked for me.. Thank you very much...

This issue is resolved
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to get name of file, delete original file, rename blank file chrisgti Linux - General 11 09-15-2012 02:49 AM
[SOLVED] Shell Script to rename and move files to a different folder in Windows Simon1984 Linux - Newbie 10 05-18-2012 10:50 AM
Shell script, recursive rename/string replace? lrall Linux - General 1 07-27-2010 10:12 PM
how to rename a file and copy a file in a shell zach014 Linux - Newbie 6 11-23-2006 09:23 AM
File rename script jjwl89 Linux - General 1 03-14-2005 06:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration