How do I do a recursive, in-file replace? - linux

Let's say I want to scan through every file in this directory (recursive, too).
And I want to replace happy.magic with sad.apps.magic
How can I do that?

find . -type f -exec sed -i 's,happy.magic,sad.apps.magic,g' {} \;

Eric is right, but just to be clear:
find ./ -type f -exec sed -i "s/happy\.magic/sad\.apps\.magic/g" {} \;

Related

sed ack search / replace line break with string

I have looked into a few SO threads, non of which have helped my specific situation.
I am trying to update a PHP app that I took over from php 5.6 to php 8.0
With that said there are MANY instances that look like:
<?
echo ...
function
I need to find all cases where <? is followed directly by a newline and replace it with <?php(newline)
Per the SO posts I've read .. I think I am coming close with the following:
find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/\<\?\n/\<\?php\n/g" {} \;
I think I am close .. But I can't figure out why it won't replace <?\n with <?php\n as the sed statement works without the newline. But per THIS POST it looks like I am doing it correctly.
What am I doing wrong?
Iterations I've tried:
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/\<\?\n/\<\?php\n/g" {} \;
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<\?\n/<\?php\n/g" {} \;
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<?\n/<?php\n/g" {} \;
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<?\n\r/<?php\n/g" {} \;
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<?\r\n/<?php\n/g" {} \;
The sed command itself could be something as simple as:
sed -i 's/<?$/<?php/'
Glue that together with find and it might work for you.
$ is an anchor matching the end of a line, you might consider using ^ to anchor the match to the beginning as well:
s/^<?$/<?php/

Formatting md5sum differently?

I need to find the md5sum of files recursively and list these files alphabetically. However, in my final output I don't want the sum to actually show up. For example if I issue:
find -not -empty -type f -exec md5sum "{}" \;
I get this:
0df8724ef24b15e54cc9a26e7679bb90 ./doc1.txt
d453430ce039863e242365eecaad7888 ./doc2.txt
53b2e8ae1dfaeb64ce894f75dd6b957c ./test.sh~
1ba03849883277c3c315d5132d10d6f0 ./md5file.txt
6971b4dbbd6b5b8d1eefbadc0ecd1382 ./test.sh
is there a simple way make this command to show only the files like:
./doc1.txt
./doc2.txt
./test.sh~
./md5file.txt
./test.sh
thx!
As Cyrus and Sriharsha say, simply using:
find -not -empty -type f
will give you the result you need.
Pass the output of find command to awk or cut.
find -not -empty -type f -exec md5sum "{}" \; | awk '{print $2}'
OR
Use sed if the filename contains spaces.
find -not -empty -type f -exec md5sum "{}" \; | sed 's/^[^ ]\+ \+//'

Find and exec command in linux. Is it possible to pipe 2 find and exec commands

I'm trying to accomplish this task
1) Find directory A (DIR_A) and copy all files in the directory(including its sub-directory, if any) into a new directory called DIR_B
2) In directory (DIR_B),replace the word apple with orange
I executed the following code and for some reason, it copies all the files but it fails on the second task (replace apple with orange). I would appreciate help on this. Below is my code
find DIR_A -iname FILEA -type f -exec cp {} DIR_B \;|find DIR_B/ -iname \*.* -type f -exec sed -i "s|apple|orange|g" {} \;
Rather than trying to piping the output from one find into the other, why not just run them sequentially? I'm not sure that find reads from its stdin.
find DIR_A -iname FILEA -type f -exec cp {} DIR_B \; ; find DIR_B/ -iname \*.* -type f -exec sed -i "s|apple|orange|g" {} \;
I've replaced your pipe with a semi-colon.
Try this :
Sed Syntax :
sed 's/old/new/g'
find DIR_A -iname FILEA -type f -exec cp {} DIR_B \;|find DIR_B/ -iname \*.* -type f -exec sed -i "s/apple/orange/g" {} \;

Linux find and replace

How can I replace "abc" with "abcd" on all files of a folder using shell?
Is it possible using sed command?
Try the following command for the file file.txt:
sed -i 's/abc/abcd/g' file.txt
Try the following command for all files in the current folder:
find . -maxdepth 1 -type f -exec sed -i 's/abc/abcd/g' {} \;
For the files in the current directory and all subdirectories:
find . -type f -exec sed -i 's/abc/abcd/g' {} \;
Or if you are fan of xargs:
find . -type f | xargs -I {} sed -i 's/abc/abcd/g' {}
sed -i 's/abc/&d/g' *
should work.
Yes:
find /the/folder -type f -exec sed -i 's,\<abc\>,&d,g' {} \;

In Unix,cmd to search a file recursively and retrieve the file instead of just the path of the file

In Unix, what is the single cmd that lets me search and locate a file recursively and then retrieve the file instead of just the path of the file?
What do you mean by retrieve?
You can simply use -exec argument to find.
$ find /path/to/search -type f -name '*.txt' -exec cat {} \;
$ find /path/to/search -type f -name 'pattern' -exec cp {} /path/to/new \;
The second one should work.
cat `find /wherever/you/want/to/start/from -name name_of_file`
Note those quotes are backquotes (`).

Resources