sed -i 's/old_password/new_password/' *.txt This command updates the password in all files with the .txt extension in the current directory. Here's an example use case in a Bash script:
Meet Alex, a system administrator who needs to modify a large number of configuration files on a Linux server. The files contain a specific setting that needs to be updated, but there are hundreds of files to change. Manually editing each file would be a tedious and time-consuming task. That's when Alex discovers the power of the sed command. The Problem Alex has a file called config.txt with the following contents: Setedit Command
sed -i 's/old_password/new_password/' config.txt This command updates the original file config.txt with the new password. What if Alex needs to update the password in multiple files? sed can handle that too: sed -i 's/old_password/new_password/' *
sed 's/old_password/new_password/' config.txt The s command in sed stands for "substitute." It searches for the pattern old_password and replaces it with new_password . Running the sed command produces the following output: Manually editing each file would be a tedious
server_ip=192.168.1.100 username=admin password=new_password As you can see, the password has been updated successfully. By default, sed outputs the modified text to the console. To edit the file in-place, Alex uses the -i option:
# Update password in config files sed -i 's/old_password/new_password/' /path/to/config/*.txt