search multiple php files, and replace text? - linux

i have several php files, and there's references to .html files in those.
I need to replace ".html" to ".php"
how can i do this in bash ?

for file in $(find . -name "*.php"); do
sed "s/\.html/.php/g" $file > $$ && mv $$ $file
done

find -name '*.php' -exec sed -ie 's:.html:.php:g' {} \;

Try sed:
find -name "filenamepattern.php" -print0 | xargs -0 sed 's/\.html/\.php/g'

GNU find
find /path -type f -iname '*.php' -exec sed -i.bak 's/\.html/\.php/g' {} +;

Related

How to convert some files from dos format to unix

I know how to change file format from dos to unix by use dos2unix, but how can I change ALL the files will under a directory tree. Can dos2unix change files recursively?
for example, I have some files like following:
TOPDIR
|
+-----dir1
| |
| +---file1,file2, file3
|
+-----dir2
|
+---file4,file5
How can I change them in one time, or use some shell scripts?
better to do find /path -type -f -exec dos2unix '{}' \;
find /path -name '*' -type f -exec dos2unix {} \;
dos2unix -k `find . -type f`
find . -type f -exec dos2unix -k '{}' \;
find . -type f -print | xargs dos2unix -k
Any of above command can be used from TOPDIR

find & sed: remove lines

I am trying to delete some line in PHP files. I tried to use an find, exec combination:
find . -name '*.php' -exec sed '/#category/d' {} \;
but it only prints out the files contents. Is there anythin wrong in the syntax? Or what is the problem?
Could you try this command:
find . -name '*.php' -exec sed -i '/#category/d' {} \;
I think you've missed -i option
It works, but probably not how you expect.
find . -name '*.php' -exec sed -i '/#category/d' {} \;
Will kill the lines in question.
This should be the command for sed so try to add -i :
sed -i ".bak" '/culpa/d' test.txt
find . -name '*.php' -exec sed -i '/#category/d' {} \;
Source of the answer:
Bash - find a keyword in a file and delete its line

dos2unix command

I have this script
#!/bin/sh
for i in `ls -R`
do
echo "Changing $i"
fromdos $i
done
I want to remove "^M" charcaters from many files which are in more subdirectories. I got this:
fromdos: Unable to access file
Is there somethig i'm missing?
Thanks in advance.
ls -R lists everything, including directories. So you're telling fromdos to act on actual directories is some cases.
Try something like this:
find . -type f -exec fromdos {} \;
I guess you don't need a for loop.
Here is a quick panorama of solutions for files with extension ".ext" (such commands shall be somehow restrictive)
note : ^M is obtained with CTRL-V" + "CTRL-M"
# PORTABLE SOLUTION
find /home -type f -name "*.ext" -exec sed -i -e 's/^M$//' {} \;
# GNU-sed
find /home -type f -name "*.ext" -exec sed -i -e "s/\x0D$//g" {} \;
# SED with more recent nux
find /home -type f -name "*.ext" -exec sed -i -e "s/\r$//g" {} \;
# DOS2UNIX
find /home -type f -name "*.ext" -print0 | while read -r -d "$(printf "\000")" -r path; do dos2unix $path $path"_new"; done
# AWK
find /home -type f -name "*.ext" -print0 | while read -r -d "$(printf "\000")" -r path; do awk '{ sub("\r$", ""); print }' $path > $path"_new"; done
# TR
find /home -type f -name "*.ext" -print0 | while read -r -d "$(printf "\000")" -r path; do cat $path | tr -d '\r' > $path"_new"; done
# PERL
find /home -type f -name "*.ext" -exec perl -pi -e 's/\r//g' {} \;

how to find files containing a string using egrep

I would like to find the files containing specific string under linux.
I tried something like but could not succeed:
find . -name *.txt | egrep mystring
Here you are sending the file names (output of the find command) as input to egrep; you actually want to run egrep on the contents of the files.
Here are a couple of alternatives:
find . -name "*.txt" -exec egrep mystring {} \;
or even better
find . -name "*.txt" -print0 | xargs -0 egrep mystring
Check the find command help to check what the single arguments do.
The first approach will spawn a new process for every file, while the second will pass more than one file as argument to egrep; the -print0 and -0 flags are needed to deal with potentially nasty file names (allowing to separate file names correctly even if a file name contains a space, for example).
try:
find . -name '*.txt' | xargs egrep mystring
There are two problems with your version:
Firstly, *.txt will first be expanded by the shell, giving you a listing of files in the current directory which end in .txt, so for instance, if you have the following:
[dsm#localhost:~]$ ls *.txt
test.txt
[dsm#localhost:~]$
your find command will turn into find . -name test.txt. Just try the following to illustrate:
[dsm#localhost:~]$ echo find . -name *.txt
find . -name test.txt
[dsm#localhost:~]$
Secondly, egrep does not take filenames from STDIN. To convert them to arguments you need to use xargs
find . -name *.txt | egrep mystring
That will not work as egrep will be searching for mystring within the output generated by find . -name *.txt which are just the path to *.txt files.
Instead, you can use xargs:
find . -name *.txt | xargs egrep mystring
You could use
find . -iname *.txt -exec egrep mystring \{\} \;
Here's an example that will return the file paths of a all *.log files that have a line that begins with ERROR:
find . -name "*.log" -exec egrep -l '^ERROR' {} \;
there's a recursive option from egrep you can use
egrep -R "pattern" *.log
If you only want the filenames:
find . -type f -name '*.txt' -exec egrep -l pattern {} \;
If you want filenames and matches:
find . -type f -name '*.txt' -exec egrep pattern {} /dev/null \;

moving files recurisively on linux

find ./dir -type f -iname "*.t[argz]*[bz2]" -print | xargs mv --target-directory=dir
seems to fail on file that has spaces in the name.
how to improve it? or alternative?
thanks for answer below: my mv doesn't support --null or -0, I'm using cygwin:
$ mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Mandatory arguments t
.
Use -print0 instead of -print on the find command, and the xargs -0 (or --null) option - then NULs will be used as separators rather than newlines and spaces.
find ./dir -type f -iname "*.t[argz]*[bz2]" -print0 | xargs --null mv --target-directory=dir
Have you looked at the -exec option for find?
find ./dir -type f -iname "*.t[argz][bz2]" -exec mv {} --target-directory=dir ';'
The -exec option will execute any options following it as a command until it sees the semi-colon wrapped in quotes. This way you won't have to deal with the spacing issue.
GNU find
find ./dir -type f -iname "*.t[argz]*[bz2]" -exec mv "{}" /destination +;

Resources