Hostwinds Tutorials
Search results for:
Table of Contents
Tags: 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.
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 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.
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.
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.
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 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.
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.
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.
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.
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.
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
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
Relative paths can also use shortcuts:
Example Using '..':
../file.txt
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 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