Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a few folders named folder1 folder2 etc.. under a root folder. In each folder there are many xml files. In each xml file there are number of occurence of text patern (in case of under folder1) folder1.yyy . I want to replace all the with just xxx and folder2.yyy with yyy etc. I know first I need to get all the folder list and process each files under each folder and replace the texts accordingly. I using Ubuntu linux distro I tried sed but didnt succeed. Could you show me a qucik way to do this no matter whether it perl or bash or any other script. Thanks

share|improve this question
up vote 3 down vote accepted

(Edited as per your comment):

for dir in folder1 folder2; do
  sed -i "s/$dir\.//" $dir/*

What this will do is for each directory you list (the folder1 folder2 bit) it will do a simple "stream edit" (sed) on every file in that directory and search for the directory name followed by a dot and replace it with nothing.

share|improve this answer
Womble xxx is any string (I want to replace "folder1." with "") and folders names are changing script must get all the folder names. Thanks – Gok Demir Aug 19 '09 at 7:52
especially sed "-i" option what I looked for thanks. You saved my day. – Gok Demir Aug 19 '09 at 9:17

If you have mmv installed, you might try something like

for dir in *; do
    mmv "$dir/$dir*" "$1";

mmv should be part of most distributions, but is not installed by default some cases. I'm not sure if it is in Ubuntu.

share|improve this answer

Your Answer


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.