sed through all subdirectories?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed through all subdirectories?
# 1  
Old 04-11-2012
Java sed through all subdirectories?

I found this awesome sed script here:

https://www.unix.com/shell-programmin...lace-text.html

Code:
sed -i '/MatchText/ s/ReplaceMe/REPLACED/' filename

Question though to save me manually doing this.

How do I do this from a root directory and then go through every subdirectory and run this?

Thanks


Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 04-11-2012 at 11:13 AM.. Reason: code tags, see PM
# 2  
Old 04-11-2012
You will have to feed sed with filenames. You might want to use a preceding find doing that, piping it to sed. Test it on a subdirectory first to not break your system if something goes wrong.

Code:
find / ...your switches etc... | sed -i ...

# 3  
Old 04-11-2012
Code:
sed -i '/MatchText/ s/ReplaceMe/REPLACED/' filename

This code just search and replace the contents inside the file. Exactly what you want to do ?

Last edited by Franklin52; 04-11-2012 at 11:18 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 04-11-2012
I hope you mean from a root directory not from the root directory as some posters have assumed.
# 5  
Old 04-11-2012
Quote:
Originally Posted by kalpeer
Code:
sed -i '/MatchText/ s/ReplaceMe/REPLACED/' filename

This code just search and replace the contents inside the file. Exactly what you want to do ?

Every file ending with php (*php) in every directory below a given location

so I execute it in /root then it will execute in
/root/home,
/root/home/astrocloud,
/root/home/astrocloud/unixNotes and so on

I have a single directory as
Code:
sed -i '/MatchText/ s/ReplaceMe/REPLACED/' *php

---------- Post updated at 01:28 PM ---------- Previous update was at 01:21 PM ----------

Quote:
Originally Posted by methyl
I hope you mean from a root directory not from the root directory as some posters have assumed.
My 'the root' directory is also an 'a root' directory
as in before;

/root/home,
/root/home/astrocloud,
/root/home/astrocloud/unixNotes
# 6  
Old 04-11-2012
Code:
find ./ -iname '*.php' -exec echo sed -i '/MatchText/ s/ReplaceMe/REPLACED/' '{}' ';'

Remove the 'echo' once you've tested and are sure it does what you want.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-11-2012
Or, totally paranoid code:
Code:
cd /root/home
find . -xdev -type f -name '*.php' -print | while read filename
do
       # Remove echo when tested thoroughly
       echo sed -i '/MatchText/ s/ReplaceMe/REPLACED/' "${filename}"
done

This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Linux

Stats on subdirectories

Please help me with a shell script to get the stats on many subdirectories (sub1), (sub2) etc under a mother directory (big) /big | |_______sub1 |_______sub2 |_______sub3 --------- I want to know 1. What is the last file accessed in each subdirectory with date and by whom 2.... (2 Replies)
Discussion started by: digipak
2 Replies

2. Shell Programming and Scripting

diff different subdirectories

I have 2 directories a/ and b/, they have different subdirectories, how to diff with missing file, or missing subdirectory and if i have in a/ directory "a/ACD/DEF" DEF is a file, but in b/ directory "b/ACD/DEF is a SUBDIRECTORY, how to diff it, thanks my solution for directories, ... (7 Replies)
Discussion started by: knajta
7 Replies

3. UNIX for Dummies Questions & Answers

Testing for subdirectories

Hello, Can anyone help me figure out how to test if the item in the directory is a subdirectory? I'm writing a code to copy all the contents of directory1 to directory2, but I want to skip all the subdirectories. Thanks! (4 Replies)
Discussion started by: l flipboi l
4 Replies

4. Shell Programming and Scripting

Please help me on how to loop subdirectories

Here is my question in bash for f in f1 f2 do cd $f cd ??? # i need to enter the two layers of sub folders then find the folder named "abcde" ? cd .. # how to get out two layers subdirectories? cd .. done (3 Replies)
Discussion started by: ksgreen
3 Replies

5. UNIX for Dummies Questions & Answers

Please help me on how to loop subdirectories

Here is my question in bash for f in f1 f2 do cd $f cd ??? # i need to enter the two layers of sub folders then find the folder named "abcde" ? cd .. # how to get out two layers subdirectories? cd .. done (2 Replies)
Discussion started by: ksgreen
2 Replies

6. UNIX for Dummies Questions & Answers

looping through subdirectories

Hi, How to loop through all the subdirectories in a directory, merge the files present in it to a single file in that subdirectory itself and remove the original files? Please advise. (5 Replies)
Discussion started by: er_ashu
5 Replies

7. UNIX for Dummies Questions & Answers

how to see all the subdirectories easily?

Suppose I have two directories a and b. Each directory has a few subdirectories, a1 a2 a3 and b1, b2, b3 respectively. Using ls, I can see a and b. Then I need cd a, ls, cd ../b, ls to see all the subdirectories. How to see all the directories and subdirectories easily, say using just one... (2 Replies)
Discussion started by: fld2007
2 Replies

8. Solaris

/home Subdirectories

Hello: Could someone please explain to me how to create a subdirectory in the /home directory. I have tried creating a new user but the default path for a new user is /export/home. I am running Unix 5.8 on a Sun Blade 100. Thanks. (8 Replies)
Discussion started by: mawalton
8 Replies

9. UNIX for Dummies Questions & Answers

How to search all subdirectories?

Dear All, I want to write the Unix command that searches through all subdirectories, finds the files named ''core'' and deletes them. I will very much appreciate your help. David (4 Replies)
Discussion started by: david_wang
4 Replies
Login or Register to Ask a Question