-2

Hi I have file in my directory as below:

ewPrd030MmCommonEaiClcBroker034_RSS.ActiveStores_cache.ver
ewPrd030MmCommonEaiClcBroker034_RSS.ActiveStores.Count_cache.dat

I need to rename as below:

ewPpt030MmCommonEaiClcBroker034_RSS.ActiveStores_cache.ver
ewPpt030MmCommonEaiClcBroker034_RSS.ActiveStores.Count_cache.dat

Can you please let me the know a one liner that can do this?

3
  • 1
    Is this Unix or Linux? Which Unix? Which Linux? What shell do you use? Do you want to do this for one file or for many similar files?
    – terdon
    Sep 27, 2013 at 15:08
  • You're just changing occurrences of rd to pt? Sep 27, 2013 at 15:29
  • Its Unix, Shell: SHELL=/bin/ksh Yes i want to change occurrence of rd to pt.
    – Yugesh
    Sep 27, 2013 at 16:03

2 Answers 2

1

rename can be used for batch renaming files

With your example:

$ rename -v 's/^ewPrd/ewPpt/' *ActiveStores*
ewPrd030MmCommonEaiClcBroker034_RSS.ActiveStores_cache.ver renamed as ewPpt030MmCommonEaiClcBroker034_RSS.ActiveStores_cache.ver
ewPrd030MmCommonEaiClcBroker034_RSS.ActiveStores.Count_cache.dat renamed as ewPpt030MmCommonEaiClcBroker034_RSS.ActiveStores.Count_cache.dat
3
  • Careful, there are various implementations of rename and they have different formats. Since the OP has not mentioned his OS, this may well not work for him. +1 one though cause that's certainly not your fault.
    – terdon
    Sep 27, 2013 at 15:47
  • Hi, the rename command is not working. it is throwing below error:ksh: rename: not found
    – Yugesh
    Sep 27, 2013 at 16:01
  • On my system (debian 7) rename is a perl script (provided by the perl package). rename.ul is also installed and works for me (provided by util-linux).
    – pat
    Sep 27, 2013 at 16:15
1

If you want to rename many files, do something like this:

for f in *Prd*;do mv "$f" "${f/Prd/Ppt}"; done

This will go through all files whose name contains Prd in the current directory and rename them. It takes advantage of bash's (I am assuming you are using bash) string substitution capabilities.

2
  • Hi my question was to replace Prd to Ppt.
    – Yugesh
    Sep 27, 2013 at 16:06
  • @Yugesh sorry, answer updated. What operating system are you using? Which Unix? rename might be better if it is available to you. Also, this assumes you are using bash.
    – terdon
    Sep 27, 2013 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.