Find all .sh files in file system and need to replace the string inside .sh files


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Find all .sh files in file system and need to replace the string inside .sh files
# 1  
Old 08-21-2019
Find all .sh files in file system and need to replace the string inside .sh files

Hi All,
I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this.
Code:
find / -name "*.sh" -print 2>/dev/null

# 2  
Old 08-21-2019
Try

Code:
awk '{gsub ("vijay@gmail.com", "vijay.bhaskar@gmail.com")} 1' $(find /home -name "*.sh")


Last edited by RudiC; 08-21-2019 at 05:49 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-21-2019
Running find against / (root) is not great idea.

Run your code as a non-privleged user.

Try
Code:
find /  -type f -name '*.sh' -exec grep -l 'vijay@gmail.com'  {} \; > ~/mylist

then check the list. If you like it:
Code:
while read filename 
do
    sed  -i 's/vijay@/vijay.bhaskar@/g' $filename
done  <  ~/mylist

If you hit permission errors then you need to rethink what you are doing because you may have selected system files you should not have changed earlier.
You can run the file loop statement over and over until you are error free. Why? Because will not change anything in files it already fixed.

Check back here if you need help.
Edit:
Rudi beat me to an answer. I still think a little caution is good idea as well.
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 08-22-2019
Hi Jim,

Thanks so much for your suggestions. I am very happy with your solution.

Last edited by bhas85; 08-22-2019 at 09:23 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

2. Ubuntu

Trying to find files on a system that contain a certain string

I have tried just about every find and grep command possible and I cannot find these damn files!! This is the problem: On the node you just swapped in, there are 5 JPEG files whose names contain the word "intro" in some form. Find all five files from on the entire disk (i.e. from root /). ... (2 Replies)
Discussion started by: dukeu69
2 Replies

3. UNIX for Dummies Questions & Answers

Finding files with a certain name string inside of another file.

Hi, I have a very large file that contains a listing of all files on the system. I need to create a listing from that file of all files that start with the following format: s???_*, whereas the '?' represents characters, so the file name begins with an 's' followed by three other characters and... (4 Replies)
Discussion started by: tes218
4 Replies

4. Shell Programming and Scripting

Find text containing paths and replace with a string in all the python files

I have 100+ python files in a single directory. I need to replace a specific path occurrence with a variable name. Following are the find and the replace strings: Findstring--"projects\\Debugger\\debugger_dp8051_01\\debugger_dp8051_01.cywrk" Replacestring--self.projpath I tried... (5 Replies)
Discussion started by: noorsam
5 Replies

5. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

6. 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

7. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

8. Shell Programming and Scripting

find and replace string in a directory files

Hi, I have a directory has DIR1 and the D1 directory has 200+ files. I want change the string from "Bangalore" to "Bangaluru" in all files in the D1 directory. Thanks (2 Replies)
Discussion started by: koti_rama
2 Replies

9. Shell Programming and Scripting

How to find a string inside files

Hi, I would like to know how to get a list of files that contain a specific string inside them. Thanks (12 Replies)
Discussion started by: yoavbe
12 Replies

10. UNIX for Dummies Questions & Answers

Find and replace a string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (2 Replies)
Discussion started by: pharos467
2 Replies
Login or Register to Ask a Question