1

I have a WordPress site and want to change first line of all php files recursively using bash. How should I do? I am not very familiar with bash.

Thanks!

1

3 Answers 3

5

Change first line to some other line:

sed -i '1s/.*/changed line/' *.php

Adding a line before first line:

sed -i '1s/^/changed line\n/' *.php
Sign up to request clarification or add additional context in comments.

1 Comment

While it worked, it also modified all the line endings in my files. In vim I see ^M at the end.
3

In order to modify the first line of the file, you can use this :

awk 'NR==1 {$0="what you want"} 1' *.php

You'll find more info here : bash: replace an entire line in a text file

Then to do it recursively, you can use a find first and then execute the awk command on every found file.

find . -name "*.php" -exec awk 'NR==1 {$0="what you want"} 1'

More info about the find command here : https://www.gnu.org/software/findutils/manual/html_mono/find.html#Scope

2 Comments

There is a slight catch here. awk writes the o/p to stdout, not back to file.
I used both of the responds and executed the following: find . -name "*.php" -exec sed -i '1 s/.*/<?php/' '{}' \;
2

I used both above answers and executed the following:

find . -name "*.php" -exec sed -i '1 s/.*/<?php/' '{}' \;

So thanks both of you Theox and anishsane for your help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.