Linux administrators who want to modify files without overwriting the original have many options, but one of the...
most efficient tools is the stream editor -- or sed.
The stream editor is a default part of most Linux distributions. It enables you to perform text file manipulations in the operating system with Linux sed commands.
Like most Linux applications, sed can process piped input, which makes it an effective scripting tool. You can use it as a basic find-and-replace tool, as in the example command below, which looks for the occurrences of one and replaces it with two. The command is closed with a /g.
sed 's/one/two/g' inputfile outputfile
This Linux sed command can help you locate and create a new version of a configuration file, for example. When these functions run as part of a script, they are repeatable and consistent, and you can implement the changes quickly.
But sed's main purpose is changing the contents of text files. It uses a few important command-line switches. The /s means search, and that command is delimited by /g. The -i switch runs the command on the file in place -- it directly modifies the file.
sed -i 's/Port 22/Port 10000/g' /etc/ssh/sshd_config
In this example, the port number used in the Secure Shell server in the /etc/ssh/sshd_config file is changed from the default port 22 to port 10000.
Making file changes with Linux sed commands
It is possible to edit the file in place with sed, but it is somewhat frowned upon. Editing ad hoc can result in issues because sed doesn't have access to the full source code and it can't identify missteps or typos. Additionally, doing so puts the original file in jeopardy because there is no way to revert to the original code once you've changed it.
You can use the -e switch to specify multiple changes in one go. Again, the sshd_config.conf file makes it simple to change multiple lines. The script below might look complex, but the OS simply passes multiple packets of sed changes, each prefixed by -e.
Using the sshd_config file, you can change the port number, disable password authentication and enable public key authentication in one step.
sed -i -e 's/Port 22/Port 10000/g' -e '
s/PermitRootLogin yes/PermitRootLogin no/g' -e '
s/PasswordAuthentication yes/PasswordAuthentication no/g' -e '
s/#PasswordAuthentication no/#PasswordAuthentication no/g' /etc/ssh/sshd_config
The search and replace functions are on a new line; breaking up a command with \ doesn't work because sed treats it as a special character.
Combining prompts in sed
You can also chain together several Linux sed commands to change the location of an application. Manually modifying the file paths has a lot of room for error, but automating it can make life easier.
The backslashes that function as the delimiters might not work for some scripts, but sed allows you to change the delimiter. For example, let's say you have a log file named example.conf with the following contents:
logpath = /var/log/mylogfile.log
To change this path to /my/alternate/path/newlog.log, you can use | as a delimiter because / doesn't work in file system paths.
sed -i 's|/var/log/mylogfile.log|/my/alternate/path/newlog.log|g' example.conf
Other ways to use Linux sed commands include prefixing the search pattern with ^#MyComment; this searches for lines starting with #MyComment. You can use this on the output side so it creates a new line to replace the existing one. You can also look for content at the end of a line using the $ character.
To see some more advanced examples, use the man sed command. It offers a more detailed breakdown of the commands and syntax.