You are currently viewing How to Edit Files in Linux

How to Edit Files in Linux

Linux is a powerful kernel with a powerful terminal and command line interface. This interfaces provides users with a wide variety of tools and commands to edit a file or manipulate files or view files. There are typically only few common ways to view a file, lets skip all the GUI editors and focus on the command line editors in this article. 

Nano: The Beginner’s Text Editor:

Nano is a simple, yet a user-friendly text editor that is perfect for the beginners. To edit a file using Nano, open a terminal and type:

nano filename

Navigate in the file using arrow keys, make edits, and use Ctrl + O to save and Ctrl + X to exit.

Vi and Vim: The Powerhouse Editors:

Vi and Vim are powerful text editors that come pre-installed on most Linux Distros. To open a file with Vim, use:

vim filename

Vi and Vim have various modes, such as command mode, insert mode, and visual mode. Press 'i' to enter insert mode, make changes, and then press Esc to return to command mode. Save changes with :w and exit with :q .

Emacs: The Extensible Editor:

Emacs is another feature-rich text editor with a steep learning curve but immense power. To open a file in Emacs, use:

emacs filename

Emacs has multiple modes and a vast variety of commands. Use Ctrl + X followed by Ctrl + S to save and Ctrl + X followed by Ctrl + C to exit.

Sed: Stream Editor for Text Transformation:

Sed is a command-line stream editor that shines in performing text transformations. To edit a file using sed, use:

sed -i 's/old-text/new-text/g' filename

This command replaces all occurrences of ‘old-text’ with ‘new-text’ in the specified file. The -i option edits the file in place.

Awk: Pattern Scanning and Text Processing:

Awk is a really good program for pattern scanning and text processing. To edit a file using awk, use:

awk '{print $1, $3}' filename > newfile

This command prints the first and third columns of the file and redirects the output to a new file. Adjust the command based on your specific requirements.

Cat and Echo: Simple Text Output:

The cat and echo commands can be used to create or append text to a file. For example:

echo "New content" >> filename
cat filename

This appends "New content" to the end of the file. Be cautious with these commands, as they overwrite existing content without any editing features.

Gedit: Graphical Text Editor:

Gedit is a graphical text editor that provides a user-friendly interface for editing files. To open a 

file with Gedit, use:

gedit filename

Gedit is particularly a useful tool for those who prefer a graphical interface over the command line.

Using Redirects and Pipes:

Linux offers powerful I/O redirection and piping capabilities. For instance:

cat filename | grep "search-term" inputfile > outputfile

This command searches for “search-term” in inputfile and writes the results to outputfile. Combining commands with pipes (|) allows for more complex file editing workflows.


File editing is a art in Linux since some fanatic linux users prefer to perform all sorts of file manipulation in command line instead of using a interface. This also comes in hand for others when interacting with servers and containers since they most of the time lack a GUI interface. But on the whole edit your file, ruin your configs, bring down the prod on a friday eve and have a great enjoyable weekend with you senior engineers. 

Leave a Reply