6

I'm trying to find certain files with the name "stringx" and replace the name (but not the extension) with "stringy". So basically for stringx.txt and stingx.cs, I'd want stringy.txt and stringy.cs. I've attempted to test my rename command regex using following, but it returns no results:

rename -n 'stringx./s/\*$stringy./\/' stringx.*

I'm running csh under Centos. I can see that my regex is probably not correct, but I can't see any results to verify this. I can see that there are files that are named stringx. with the following:

find ./ -name 'stringx.*'
Aaron Newton
  • 475
  • 1
  • 5
  • 8

1 Answers1

11
rename stringx stringy stringx.*

There are (at least) two utilities called rename on typical Linux systems. Debian and derived distributions (including Ubuntu) ship a Perl script whose syntax would be

rename 's/stringx/stringy/' stringx.*

On other distributions, including CentOS, rename is from util-linux, and its syntax is what I wrote above. This utility is available under the name rename.ul on Debian and derivatives.

If you want to recurse in subdirectories, assuming that by csh you mean tcsh (but really, think of entering the mid-1990s and switching to zsh):

set globstar
rename stringx stringy stringx.* **/stringx.*
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • "There are numerous inconsistencies in your example names. I assume you means you have files called stringx dot something and want to rename them to stringy dot something." - correct - I have updated the question. As for shells, the decision to use c-shell is non-trivial. However, the last option you suggested seems to work nicely - thank you. – Aaron Newton Nov 16 '11 at 23:45
  • Gilles, I thought about your comment today - "but really, think of entering the mid-1990s and switching to zsh" - and you have a point here. For my non- *csh* dependent stuff, I installed *zsh*. I also have *screen* installed now, so I can create a new screen (which will be in the same working directory I was just in), type in *zsh* and voila - I'm in the mid-1990s. I'm not sure that everything works identically as if I'd set *zsh* to the default shell for that user, but it does seem to simplify things (e.g. I can get a directory listing when I use *ls* and ). – Aaron Newton Dec 02 '11 at 12:52