Replace all occurances of a string in all file-/foldernames, recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace all occurances of a string in all file-/foldernames, recursively
# 1  
Old 04-11-2006
Lightbulb Replace all occurances of a string in all file-/foldernames, recursively - SOLVED

I need a script that will replace all occurances of a string in all filenames and foldernames, recursively.

Right now I have this script:
Code:
for f in `find -name *eye*`; do
  echo processing $f
  g=`expr "xxx$f" : 'xxx\(.*\)' | tr 'eye' 'm'`
  mv "$f" "$g"
done

The problem is that tr replaces the characters e and y with m instead of the string "eye".

I adapted this from http://webxadmin.free.fr/article/she...o-lowe-135.php
so I don't really know what the regular expressions in expr are doing.

It is aggravating because I am so close, but I can't get it to work.

Last edited by TheMJ; 04-12-2006 at 02:40 AM.. Reason: added SOLVED to title
# 2  
Old 04-11-2006
I had problems getting your script to work but here is one that does:
Code:
for f in `find . -name \*eye\* -print`; do
  echo processing $f
  g=`print "$f" | nawk 'gsub(/eye/,"m")'`
  mv "$f" "$g"
done

"tr" in the script that your link reference is simply changing upper case names to lower case.
# 3  
Old 04-12-2006
Great, that script worked perfectly.
Thanks for the quick reply tmarikle.
-TheMJ
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

2. Shell Programming and Scripting

2 files replace multiple occurances based on a match

Hi All, I need some help trying to achieve the below but everything I've tried has failed, I have 2 files which i'm trying to carry out a match based on the first column from file 1, take that value find it in file 2 if found replace it with the second column from File 1 Lookup File: File 1... (3 Replies)
Discussion started by: mutley2202
3 Replies

3. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

4. Shell Programming and Scripting

How to find and replace a string with spaces and / recursively?

Hi all, I wanted to find and replace an email id from entire directory structure on a Linux server. I found that find . -type f -print0 | xargs -0 sed -i 's/abc@yahoo.com/xyz@gmail.com/g' would do it perfectly. But my search criteria has extended and now I want to search for a string1 like... (2 Replies)
Discussion started by: pat_pramod
2 Replies

5. Shell Programming and Scripting

Replace first 3 occurances of a character in a file for each line

Hi all, I am very new to unix - ksh. I want to replace some characters in a file with some other, but only for first three occurances for each line. For eg: the first 3 occurances character ']' has to be replaced with ',' in each line. Please help. thanks Sreejith (1 Reply)
Discussion started by: rsreejithmenon
1 Replies

6. Shell Programming and Scripting

How to count number of occurances of string in a file?

Gurus, Need little guidance. I have A.txt and B.txt file. B.txt file contains Unique strings. Sample content of B.txt file for which i cut the fourth column uniquely and output directed to B.txt file And A.txt file contains the above string as a fourth column which is last column. So A.txt... (7 Replies)
Discussion started by: Shirisha
7 Replies

7. Shell Programming and Scripting

Replace 2 Occurances of Space (sed)

I have a large file that looks like the below output: system SUNWxwmod X Window System kernel modules system SUNWxwoft X Window System optional fonts system SUNWxwopt X Window System Optional Clients system ... (1 Reply)
Discussion started by: hxman
1 Replies

8. UNIX for Dummies Questions & Answers

Need to cut a string from a file recursively.

I have a file exep_file.txt with contents similar to, xyx request 1329207. sdf sedf sg gds request 1329207. as as fa df sdf request 1329207. xyx request 1329209. sdf sedf sg gds request 1329209. as as fa df sdf request 1329209. I need an output like 1329207 1329209 ... (2 Replies)
Discussion started by: happyrain
2 Replies

9. Shell Programming and Scripting

find and replace a search string recursively in files

Hi , I have a directory structure as dir and subdirectories and files under it and so on.now I need to find the files which contain the search string under every dir and subdir and replace . my search string is like searchstring=/a/b string to be replaced=/a/c/b please help. ... (7 Replies)
Discussion started by: mohanpadamata
7 Replies

10. Shell Programming and Scripting

replace a word in a file only first 10 occurances

file.txt contains abc :-X 1234 :-X fjhfkf :-X ffwerw :-X fdfgetw :-X hwfhe89 :-X in the this file there are 6 occurance of -X i want to replace only first 3 occurances of '-X' with 'XY' and write back to same file. (4 Replies)
Discussion started by: nawazkhan77
4 Replies
Login or Register to Ask a Question