Edit Files in Linux

Jeremiah Muchiri
Published: April 22, 2024

The short answer

In Linux, the most popular way to edit the content of a file is to open it with an in-terminal text editor such as Vim using the following command:

Bash
$ vim <file>

Where:

  • file is the relative or absolute path to the file you want to edit.

Then:

  1. Press the i key to enter the Insert mode.
  2. Make your edits.
  3. Press the ESC key to exit the Insert mode.
  4. Type :wq and press ENTER to save your changes and quit Vim.

You can learn more about Vim by reading our other article on how to switch between Vim's 7 modes.

Editing read-only files with Vim

To edit a read-only file, you can open it with Vim using the following command:

Bash
$ vim <file>

Once you've made your changes, you can type the following command and press ENTER to save the file and quit Vim:

Bash
:wq !sudo tee %

Where:

  • :wq is used to save (i.e., write) and exit (i.e., quit) Vim.
  • !sudo is used to run the following command with root privileges in Vim.
  • tee reads from standard input and writes to standard output and files simultaneously.
  • % represents the name of the current file.

You can learn more about read-only files by reading our other article on Linux file permissions and how to change file permissions in Linux.

Editing zipped files with Vim

Vim provides out of the box support for editing the files contained in an archive without having to extract them.

To edit a file within an archive, you can open it using the following command:

Bash
$ vim <file>

Then:

  1. Navigate to the file you want to edit using the UP and DOWN arrow keys and hit ENTER.
  2. Press the i key to enter the Insert mode.
  3. Make your edits.
  4. Press the ESC key to exit the Insert mode.
  5. Type the :wq command and press the ENTER key to save the changes and close the file.
  6. Type the :q command and press the ENTER key to exit Vim.

Note that zip files aren't the only compressed files that you can open/edit with Vim. You can also edit gzipped files, tarballs, JPEGs, and .bzip2 amongst others.

Editing binary files with Vim

To open and edit a file in Binary mode with Vim, you can use the -b flag as follows:

Bash
$ vim -b <file>

Type the :%!xxd command within the editor to replace the buffer with an editable hexadecimal dump where:

  • The left column shows the offset of the first byte on each line.
  • The middle column shows the bytes in hexadecimal.
  • The right column shows the printable characters.

Then:

  1. Press the i key to enter the Insert mode.
  2. Make your edits to the hexadecimal part.
  3. Press the ESC key to exit the Insert mode.
  4. Type the :%!xdd -r command and press the ENTER key to reverse the buffer to its binary form.
  5. Type the :wq command and press the ENTER key to save the changes and close the file.

Editing files with Nano

Nano is a built-in Linux editor that provides a straightforward and simpler editing experience.

To edit a file with Nano, you can use the nano command as follows:

Bash
$ nano <file>

Where:

  • file is the relative or absolute path to the file you want to edit.

Once you’ve made your modifications, you can press CTRL+O to save your file and CTRL+X to exit the editor.

Note that unlike Vim, Nano doesn’t have several edition modes and is automatically in the equivalent of Vim’s Insert mode.

Editing files with Emacs

Emacs is yet another file editor available on Linux with similar features to Vim.

To edit a file with Emacs, you can use the emacs command as follows:

Bash
$ emacs <file>

Once you’ve made your modifications, you can press CTRL+X then CTRL+S to save your file and CTRL+X then CTRL+C to exit the editor.

Overwriting the content of a file

To overwrite the content of a file, you can use the single output redirection operator > as follows:

Bash
$ command > file

Where:

  • command is a shell command like echo or cat.
  • file is the relative or absolute path to the file you want to write into.

For example, the following echo command will write the string ”Hello, World!” into the file named hello.txt:

Bash
$ echo “Hello, World!” > hello.txt

And the following command will read from the standard input until it encounters an end-of-file (i.e., CTRL+D) and write the content typed in the terminal window in the file named hello.txt:

Bash
$ cat > lines.txt
Hello, World!
Bonjour, Monde!
^D

Note that the output redirection operator will automatically create the file if it doesn’t exist.

Appending content to a file

To append content to an existing file without overwriting it, you can use the double output redirection operator >> as follows:

Bash
$ command >> file

For example, the following command will append the string “Hallo, Welt!” at the end of the file named hello.txt:

Bash
$ echo “Hallo, Welt!” >> hello.txt

Editing a file based on patterns with sed

The sed command is a powerful file editing command that allows you to edit files on the fly without opening them.

Find and replace text occurrences within a file

To replace all the occurrences of a pattern in a file, you can use the sed command with the -i flag as follows:

Bash
$ sed -i 's/<old_string>/<new_string>/g' <file>

Where:

  • old\_string is the string you want to replace.
  • new\_string is the string that will replace the occurrences of old\_string.
  • file is the relative or absolute path to the file you want to make changes to.
  • s is the substitute command of sed for “find and replace”.
  • / serves as a delimiter.
  • g is used to replace all occurrences of old\_string.

For example, the following command will replace all the occurrences of the word "Adventure" by the word "Exploits" in the file named Sherlock.txt:

Bash
$ sed -i 's/Adventure/Exploits/g' Sherlock.txt

Easily retrieve this command using Warp AI Command Suggestions

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering Replace string in file in the AI Command Suggestions will prompt a list of sed commands that can then quickly be inserted into your shell by doing CMD+ENTER.

Deleting lines containing a specified string

To remove all the lines of a file containing a specific word, you can use the following sed command:

Bash
$ sed -i '/<string>/d' <file>

For example, the following command will remove all the lines containing the word "The" in the file named Sherlock.txt:

Bash
$ sed -i '/The/d' Sherlock.txt

Deleting specific lines

To remove a specific line number of a file, you can use the following sed command:

Bash
$ sed '<n>d' <file>

Alternatively, to remove multiple lines at once, you can use the following syntax instead:

Bash
<;code class="language-shell">$ sed '<n>,<m>d' <file>

For example, the following command will remove the first line of the file named sherlock.txt:

Bash
$ sed '1d' sherlock.txt

And this command will remove the lines 2 through 6 of the same file:

Bash
$ sed '2,6d' Sherlock.txt

Editing the crontab file

To edit the crontab file in Linux, you can use the crontab command with the -e flag as follows:

Bash
$ crontab -e

Note that this command will open the crontab file using the text editor defined in the EDITOR environment variable or Vi/Vim by default.

If you want to use another text editor, you can use the following command:

Bash
$ EDITOR=<path> crontab -e

Where:

  • path is the path to the text editor's binary.

For example, the following command will open the crontab file using Nano:

Bash
$ EDITOR=/bin/nano crontab -e
Written by
Jeremiah Muchiri
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.