Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm deploying my project to production server using scp command. As part of the deployment process I upload to server the .htaccess file. Locally, the environment is set to DEVELOPMENT:

#Set environment
#[PRODUCTION, STAGING, TESTING, DEVELOPMENT] 

SetEnv KOHANA_ENV 'DEVELOPMENT'

However, when uploaded to server it needs to be changed to PRODUCTION. Is there any way I can accomplish that with ssh?

share|improve this question
    
Why can't you change the file locally and then copy it? –  i08in Jun 20 at 12:03
    
I don't mind changing it locally as long as it is done automatically by script. –  Maximus Jun 20 at 12:05

3 Answers 3

up vote 6 down vote accepted

You can do it with a sed command and the -i flag to edit the file in place:

sed -i "s/SetEnv KOHANA_ENV 'DEVELOPMENT'/SetEnv KOHANA_ENV 'PRODUCTION'/g" /path/to/.htaccess

Via ssh I would look like this:

ssh user@host "sed -i \"s/SetEnv KOHANA_ENV 'DEVELOPMENT'/SetEnv KOHANA_ENV 'PRODUCTION'/g\" /path/to/.htaccess"

Working with sed to replace strings in files is quite simple:

sed -i 's/search_string/replace_string/g' file

Replaces all occurences of search_string with replace_string of the gives file. -i causes sed to edit the file in place, instead of writing to standard output (stdout).

If you only want the first occurence to be replace use this sed command:

sed -i '0,/search_string/s/search_string/replace_string/' file

For more information about sed use man sed.

share|improve this answer
    
Thanks, that works! I assume this command will replace all instances in the file, correct? Although it works for me know, can you please provide example of replacing text on a particular? It seems that it is possible based on the description of the sed command, however, I can't figure out how I can do that. Thanks in advance! –  Maximus Jun 20 at 12:33
1  
@Maximus yes it replaces all instances, see my edit for replacing strings in file in general. –  chaos Jun 20 at 12:42
    
Got you, thanks a lot! Best,M –  Maximus Jun 20 at 12:47

If you want to convert just the 28th line, you can do:

sed '28s/DEVELOPMENT/PRODUCTION/' file

(combinable with -i option, of course)

If you want to avoid modifying a local file, you can do:

< /local/path/to/.htaccess sed '28s/DEVELOPMENT/PRODUCTION/' | ssh user@host "cat > /server/path/to/.htaccess"
share|improve this answer
    
Great, thanks a lot for the example! –  Maximus Jun 20 at 13:59

You can directly write to the file locally as follows:

echo "#Set environment
#[PRODUCTION, STAGING, TESTING, DEVELOPMENT] 

SetEnv KOHANA_ENV 'PRODUCTION'" > /path/to/.htaccess

Remember to replace /path/to/ with the path to .htaccess and then copy the file using scp. This will replace the contents of the file.

share|improve this answer
    
Thanks for you suggestion. The problem is that the .htaccess file contains a lot of text besides the line that I need to change. The solution that you offered will erase everything else, correct? –  Maximus Jun 20 at 12:12
    
Do you know the line number of the file which you want to modify? –  i08in Jun 20 at 12:16
    
Yes, it's 28th, which is SetEnv KOHANA_ENV 'PRODUCTION' –  Maximus Jun 20 at 12:19
    
@Maximus: chaos' answer should work now. –  i08in Jun 20 at 12:21
    
Yep, it did. Thanks! –  Maximus Jun 20 at 12:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.