I need to replace a string in a lot of files in a folder, with only ssh
access to the server. How can I do this?
相关问题
- how to split a list into a given number of sub-lis
- Is shmid returned by shmget() unique across proces
- Generate string from integer with arbitrary base i
- how to get running process information in java?
- Converting a string array to a byte array
This worked for me:
Howerver, this did not:
sed -i 's/string1/string2/g' *
. Maybe "foo" was not meant to be string1 and "bar" not string2.I am giving an example for fixing a common shebang error in python sources.
You can try the grep/sed approach. Here is one that works with GNU sed and won't break a git repo:
Or you can use greptile :)
I just tested the first script, and the second should work as well. Be careful with escape characters, I think it should be easier to use greptile in most cases. Of course, you can do many interesting things with sed, and for that it may be preferable to master using it with xargs.
The stream editor does modify multiple files “inplace” when invoked with the
-i
switch, which takes a backup file ending as argument. Soreplaces
foo
withbar
in all files in this folder, but does not descend into subfolders. This will however generate a new.bak
file for every file in your directory. To do this recursively for all files in this directory and all its subdirectories, you need a helper, likefind
, to traverse the directory tree.find
allows you further restrictions on what files to modify, by specifying further arguments likefind ./ -name '*.php' -or -name '*.html' -print0
, if necessary.Note: GNU
sed
does not require a file ending,sed -i 's/foo/bar/g' *
will work, as well; FreeBSDsed
demands an extension, but allows a space in between, sosed -i .bak s/foo/bar/g *
works.script for multiedit command
From Kaspar's answer I made a bash script to accept command line arguments and optionally limit the filenames matching a pattern. Save in your $PATH and make executable, then just use the command above.
Here's the script:
Occurrences of "foo" will be replaced with "bar".