How to rename everything matching a certain string in a folder

2

1

I am running Linux and I have some basic console knowledge but my current problem is quite difficult and I dont know how to achieve this.

I want/need to rename everything within a folder that matches a given string. By everything I mean

  • folders/files
  • content within a file
  • content in hidden files

Basically I want to refactor a Java-project. Sure, I could use Eclipse to handle the replacing, but this leaves out the folders or resources outside of my workspace.

I was thinking of a script that could do the job for me but this seems rather tricky. For instance when it comes to folder-/file-rename I want to replace only the part of the name that matches my string, the rest should remain untouched.

Maybe someone already has something like this in his/her script-collection :-)

Thanks in advance

Marc

user4747

Posted 2010-05-19T21:38:07.410

Reputation: 141

woops...I just realized this better goes to SUPERUSER. Can someone move this question please. Sorry – user4747 – 2010-05-19T21:39:27.347

Answers

2

A quick and dirty way of doing this could be:

  1. Get a list of all files that match your pattern

    # find /path -name \*pattern\* > filelist
    
  2. Iterate through filelist with a shell script, doing whatever you want to each line:

    #!/bin/sh
    
    for I in `cat filelist`
    do
         # for renaming, use mv
         mv $I new.file.name
    done
    

Tony

Posted 2010-05-19T21:38:07.410

Reputation: 156

2This does not rename the content within the files. Also, if there are any spaces in the file names, the \cat filelist`` will break. – drewk – 2010-05-20T00:58:59.517

2

You'll need to do this in several steps

  1. Generate list of files containing the string you want to change (e.g., find mydir -type f -print | xargs egrep <searchstring>)
  2. For each of those files, do the substitutions (e.g., | while read fn; do sed 's/searchstring/replacement/g' $fn >/tmp/foo && mv /tmp/foo $fn; done)
  3. Now find directory names to change (e.g., find mydir -type d -print | egrep <searchstring>)
  4. and change them (e.g., | while read olddir; do newdir=echo $olddir | sed 's/searchstring/replacement/g'; mv $olddir $newdir; done) (there should be backtics around the echo | sed newdir assignment; I'd appreciate a comment on how to include backtics in an inline code segment)
  5. And now filenames, since the directory names won't change. Very similar to the directory change, only the initial find should be -type f

That should be enough to get you going.

mpez0

Posted 2010-05-19T21:38:07.410

Reputation: 2 578

To include the backticks, escape them with `\`` – drewk – 2010-05-20T01:32:47.747

This is closer to correct if not correct btw. +1 – drewk – 2010-05-20T01:33:57.247

@drewk: tried that (and repeated after your comment) -- works for inserting backtick in normal text, but loses the <code> insert – mpez0 – 2010-05-20T11:26:13.877

I couldnt get your example with the filenames to work. It looks like he is complaining about whitespaces, so I tried the -print0 option but it didnt help.

But it looks like the content of the files has been replaced in some cases – user4747 – 2010-05-20T20:35:21.063