Linux commands to add or change the content of a file

Last updated on January 19th, 2023 at 03:28 pm.

Linux commands to add or change the content of a file

When working with your cloud server, you will need to edit files. There are several commands in Linux that can be used to add or change the content of a file. Some of the most commonly used commands include:

  • echo: The echo command is used to add text to a file or display text . For example, echo "Hello, world" > file.txt will create a new file named “file.txt” with the text “Hello, world” in it.
  • cat: This cat command is used to display the contents of a file on the screen. It can also be used to concatenate multiple files together. For example, cat file1.txt file2.txt > file3.txt will create a new file named “file3.txt” that contains the contents of both “file1.txt” and “file2.txt”.
  • nano: Nano is a command-line text editor in Linux operating systems. It is used to create and edit text files. For example, nano file.txt will open the “file.txt” in nano editor, and you can edit it, save it and exit using CTRL + X, then press Y, then enter.
  • vi: Vi/Vim is a cmd text editor. It is used to create and edit text files. For example, vi file.txt will open the “file.txt” in vim editor, and you can edit it, save it and exit it using Shift + ZZ.
  • sed: The sed command is stream editor and can be used to edit a file or stream of text in a variety of ways. It can be used to add, remove, or replace text in a file. For example, sed 's/oldtext/newtext/g' file.txt will replace all occurrences of “oldtext” with “newtext” in the file “file.txt”.

More about echo and sed

echo

echo is a command-line utility on Linux and other Unix-based operating systems that is used to display a line of text on the screen. It can also be used to write text to a file. The basic syntax of the echo command is:

echo [options]

For example, to display the text “Hello, world” on the screen, you would use the command:

echo "Hello, world"

To write text to a file, you can use the > operator to redirect the output of the echo command to a file. For example, the following command will create a new file named “file.txt” and write the text “Hello, world” to it:

echo "Hello, world" > file.txt

Advanced uses of echo – summary:

Creating a new file:

echo "This is the content of the file" > newfile.txt 

This command will create a new file named newfile.txt with the specified content.

Appending to an existing file:

echo "This is additional content" >> existingfile.txt 

This command will add the specified text to the end of the existingfile.txt file.

Using variables:

name="John" ; echo "Hello, $name" 

This command will display the string “Hello, John” by using the variable name

Using special characters:

echo -e "This is a new line\nThis is another line" 

This command uses the -e option to enable the interpretation of backslash escapes. This will print “This is a new line” on one line and “This is another line” on the next line.

Using escape sequences:

echo -e "\033[1;31mThis text is red\033[0m" 

This command uses the -e option to enable the interpretation of backslash escapes, and uses ANSI escape sequences to change the color of the text to red.

sed

sed is a command-line utility in Linux and other Unix-based operating systems that is used to edit a file or stream of text in a variety of ways. It can be used to add, remove, or replace text in a file.

The basic syntax of the sed command is:

sed [options] [script] [input_file]

For example, to replace all occurrences of the word “oldtext” with “newtext” in the file “file.txt”, you would use the command:

sed 's/oldtext/newtext/g' file.txt

The s in the command stands for “substitute”, the first oldtext is the text to be replaced, the second newtext is the replacement text, and the g at the end of the command specifies that all occurrences of the text should be replaced (not just the first one)

You can also use sed to edit the file in place and save the changes with the -i option

sed -i 's/oldtext/newtext/g' file.txt

You can also use sed to perform many other operations like adding, deleting, inserting lines, etc. you can find more examples and use cases by searching for sed tutorials or sed examples.

Advanced uses of sed summary:

Replacing a specific line:

sed '3s/old/new/' file.txt 

This command will replace the 3rd line of the file.txt with the string new wherever old is found.

Replacing a specific word in all lines:

sed 's/old/new/g' file.txt 

This command will replace all occurrences of the word old with new in all lines of the file.txt file.

Replacing a specific word in a range of lines:

sed '2,5 s/old/new/g' file.txt 

This command will replace all occurrences of the word old with new in lines 2 to 5 of the file.txt file.

Inserting text:

sed '3i\ This is the new line.' file.txt 

This command will insert the string “This is the new line.” before the 3rd line of the file.txt file.

Deleting lines:

sed '3d' file.txt 

This command will delete the 3rd line of the file.txt file.

Using regular expressions:

sed -r 's/([0-9]{3})-([0-9]{2})-([0-9]{4})/\3-\2-\1/' file.txt 

This command uses regular expressions to match the pattern of a date in the format of ddd-dd-dddd and switch it to format dddd-dd-ddd

Using hold buffer:

sed 'h;n;G' file.txt 

This command uses hold buffer to copy the first line of the file and append it to the second line, and then prints out the second line.

Using multiple commands:

sed -e 's/old/new/g' -e '3d' file.txt 

This command will replace all occurrences of the word old with new in all lines of the file.txt file, and then delete the 3rd line of the file.

These are some advanced uses of the sed command, but it has many other capabilities as well.

Comment Here

Need WordPress help? Linux Server help? Talk to us.

  • We are your own WordPress customer service.
  • We set up Linux servers and install or migrate WordPress. Learn more here.
  • We support WooCommerce too.
  • Check out our WordPress customer support plans here or contact us below .

If you have any questions regarding WordPress support, Linux server support or any of our services, feel free to reach out or read more on our services page.

Join this free course:

How to host multiple WordPress
websites on a VPS

Close me!