Hostwinds Tutorials

Search results for:


Table of Contents


Text Files in Linux
Command-Line Text Editors in Linux
Using Nano Text Editor
Common Nano Commands
Using Vi Text Editor to Modify Files
Vi Modes
Key Functions of Command Mode:
Switching Between Modes:
Common Vi Commands
Viewing Files in Linux
File Paths in Linux
Absolute Paths
Relative Paths
Copying Files in Linux
Moving Files in Linux

How to Edit Files from a Linux Shell

Tags: Linux 

Text Files in Linux
Command-Line Text Editors in Linux
Using Nano Text Editor
Common Nano Commands
Using Vi Text Editor to Modify Files
Vi Modes
Key Functions of Command Mode:
Switching Between Modes:
Common Vi Commands
Viewing Files in Linux
File Paths in Linux
Absolute Paths
Relative Paths
Copying Files in Linux
Moving Files in Linux

Editing files in Linux is a core skill for working with configuration files, scripts, and more. Linux offers various tools for creating, modifying, copying, and moving files, with both terminal-based and graphical options available.

This guide will teach you how to work with a file in Linux. Specifically, we'll walk through how to open, edit, copy and move files around using command-line interface (CLI) text editors Nano and Vi.

Text Files in Linux

In Linux, text files are simple files that contain human-readable data, used for configuration settings, shell scripts, and logs, among other functions across Linux systems.

Unlike binary files, which require specific applications to interpret, text files can be opened, edited, and modified using a text editor.

Command-Line Text Editors in Linux

Command-line text editors in Linux allow you to create and edit text files directly from the terminal. These editors are lightweight, making them perfect for tasks like editing configuration files, scripts, and logs—especially when working on remote servers or systems without a graphical interface.

Linux offers several command-line text editors, with nano and vi (or its enhanced version, vim) being the most popular choices:

Nano is a simple, beginner-friendly editor that's easy to navigate, providing basic text editing features ideal for new users.

Vi (and Vim) are more advanced, feature-rich editors. They're widely used by experienced users who need powerful tools for complex editing tasks.

Using Nano Text Editor

Nano is one of the simplest text editors in Linux, making it ideal for beginners.

To edit a file with nano, start by opening the file using the following command:

nano filename.txt

Once the file is open, you can navigate through the file using the arrow keys, making modifications as needed.

When you're done, you can save (write out) the file by pressing CTRL + O and then CTRL + X to exit.

Common Nano Commands

  • CTRL + O: Save the file.

  • CTRL + X: Exit nano.

  • CTRL + W: Search for text within the file.

  • CTRL + K: Cut the current line.

  • CTRL + U: Paste the previously cut line.

Using Vi Text Editor to Modify Files

Vi is a more robust text editor, typically used by more experienced folks.

To open a file with Vi, use the following command:

vi filename.txt

Vi Modes

Vi operates in two modes: command mode and insert mode.

Command mode is the default mode when opening a file in Vi and is specifically used for navigation and issuing instructions (commands) to the editor.

Note that command mode does not allow you to add or edit the actual text within the file.

Key Functions of Command Mode:
  • Move the cursor using arrow keys or keys like h, j, k, l.

  • Delete lines or characters (dd deletes a line, x deletes a character).

  • Save the file (:w) or quit Vi (:q).

  • Search for text (/search_term).

Insert mode is where you can type and edit text to the content of the file, similar to how you would in nano.

Switching Between Modes:

  • To switch from command mode to insert mode, press i, a, or o.
  • To return from insert mode to command mode, press ESC.

Common Vi Commands

  • i - Switch to insert mode for editing.

  • ESC - Return to command mode.

  • :w - Save the file.

  • :q - Quit Vi.

  • :wq - Save and quit.

  • :q! - Quit without saving

  • dd: - Delete the current line.

  • /search_term - Search for text in the file.

Viewing Files in Linux

You can access files in Linux by using the 'cat' (concatenate) command.

The cat command does not allow you to edit a file; it only opens the file and displays its contents.

The primary purpose of cat is to read and display (print) the contents of a file to the terminal.

For example, if you run:

cat filename.txt

It will simply show the contents of filename.txt in the terminal but won't let you make any changes to the file.

If you want to edit a file, you'll need to use a text editor like nano or vi.

File Paths in Linux

A file path is like a map that tells you where to find a specific file or directory. It's a sequence of locations, starting from one folder and going through others until it reaches the file you want.

In Linux there are absolute paths and relative paths. Both types of paths point to files or directories, but they work differently depending on your current location in the directory structure.

Absolute Paths

An absolute path is the complete path to a file or directory, starting from the root directory (/). It specifies the location of a file or folder regardless of the current working directory. No matter where you are in the system, an absolute path will always point to the same file.

Example:

/home/user/Documents/file.txt
  • This path begins from the root directory (/), then moves through home, user, and Documents to reach file.txt. Even if you are in a different directory, using this absolute path will take you to the correct file.

Relative Paths

A relative path refers to a file or directory in relation to the current working directory (where you are in the file system). Instead of starting from the root, it starts from your current directory.

Example:

Documents/file.txt
  • If you're in /home/user/, this relative path leads to file.txt inside the Documents directory. The command will only work if you are already in the /home/user/ directory.

Relative paths can also use shortcuts:

  • '.' represents the current directory.
  • '..' moves up one level to the parent directory.

Example Using '..':

../file.txt
  • This path points to file.txt in the parent directory of your current location.

Copying Files in Linux

To copy files in Linux, use the 'cp' command. This creates a duplicate of a file in the specified location:

cp /path/to/source/file /path/to/destination/

To copy directories, you need to add the '-r' option for recursive copying:

cp -r /path/to/source/directory /path/to/destination/

Moving Files in Linux

Moving files in Linux is done using the 'mv' command, which is both a move and rename function. To move a file from one location to another:

mv /path/to/source/file /path/to/destination/

If you want to rename a file, you can use the same mv command but provide a new filename:

mv old_filename.txt new_filename.txt

Written by Michael Brower  /  March 28, 2017