Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

migrated from serverfault.com May 20 '10 at 0:21

This question came from our site for professional system and network administrators.

    
woops...I just realized this better goes to SUPERUSER. Can someone move this question please. Sorry –  user4747 May 19 '10 at 21:39

2 Answers 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.

share|improve this answer
    
To include the backticks, escape them with \` –  drewk May 20 '10 at 1:32
    
This is closer to correct if not correct btw. +1 –  drewk May 20 '10 at 1:33
    
@drewk: tried that (and repeated after your comment) -- works for inserting backtick in normal text, but loses the <code> insert –  mpez0 May 20 '10 at 11:26
    
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 May 20 '10 at 20:35

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
    
share|improve this answer
2  
This does not rename the content within the files. Also, if there are any spaces in the file names, the `cat filelist` will break. –  drewk May 20 '10 at 0:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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