How to create and edit files in Linux

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM


$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM


$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Linux allows users to create and edit files efficiently. In this guide, we’ll explore the fundamental commands for creating and editing files in Linux.

Using the Touch Command

The touch command in Linux serves a dual purpose: it can be used to create empty files and to update the access and modification timestamps of existing files.

Creating Empty Files

Creating an empty file is a straightforward task with the touch command. Open a terminal and execute the following command:

touch filename.txt

This command will create a new file named “filename.txt” in the current working directory. If the file already exists, touch will update the timestamps without altering the file’s content.

Updating Access and Modification Timestamps

As mentioned earlier, touch can also be used to update the access and modification timestamps of existing files. For instance:

touch -c existing_file.txt

The -c option prevents the creation of a new file if it doesn’t already exist. This is particularly useful when you want to update the timestamps of an existing file without inadvertently creating a new one.

Editing Files with Touch

While the touch command is primarily used for file creation and timestamp updates, you can use a combination of commands to add content. For example:

echo "This is a sample text" > filename.txt
touch filename.txt

Here, the echo command is used to write text into “filename.txt,” and touch is then employed to update the timestamps. This can be handy when you want to create a file with content and simultaneously manage its timestamps.

Using the Echo Command

The echo command in Linux is used for printing text to the terminal. However, it can also be harnessed to create and edit files directly from the command line.

Creating Files

To create a new file with the echo command, you can redirect the output to a file. For example:

echo "Hello, Linux!" > greeting.txt

In this command, the text “Hello, Linux!” is echoed into a file named “greeting.txt.” If the file already exists, it will be overwritten; otherwise, a new file will be created.

Appending Text to Files

The echo command, when used with the append (>>) operator, can add text to an existing file without overwriting its content. For instance:

echo "Additional text" >> greeting.txt

This command appends “Additional text” to the end of the existing “greeting.txt” file.

Editing Files

For more interactive editing, you can use a combination of echo and command substitution. Consider the following example:

echo "Enter your text: "
read user_text
echo "$user_text" > userfile.txt

Here, the user is prompted to enter text, and that text is then echoed into a file named “userfile.txt.”

Using the Cat Command

The cat command can also be employed to create a file and add content simultaneously:

cat > introduction.txt

After executing this command, you can type the content directly. Press Ctrl + D to save and exit.

Editing Files in Linux

If you already have a created file with content and you want to edit it, there are a couple of Linux text editor tools you can use:

Using the Nano Text Editor

Nano is a beginner-friendly text editor. To create or edit a file, type:

nano filename.txt

Here’s a table listing some common keyboard keys used in the Nano text editor along with their descriptions:

Ctrl + GDisplay help (Get Help)
Ctrl + XExit Nano (Close current file if changes are made)
Ctrl + OWrite the current file to disk (Save)
Ctrl + RInsert a file into the current one (Read)
Ctrl + WSearch for a string or a regular expression
Ctrl + KCut (delete) the current line or marked text
Ctrl + UUncut (paste) the last cut text
Ctrl + CShow current line and column number
Ctrl + JJustify the current paragraph (wrap text)
Ctrl + YMove to the previous screenful of text
Ctrl + VMove to the next screenful of text
Ctrl + AMove to the beginning of the current line
Ctrl + EMove to the end of the current line
Ctrl + SpaceSet a mark (for marking text to cut)
Ctrl + Shift + 6Copy the marked text (while marking text)
Alt + UUndo the last operation
Alt + ERedo the last undone operation
Alt + AToggle line numbers
Alt + Shift + 3Comment/uncomment the current line or marked text

It’s important to note that, In Nano, some keys are represented with Ctrl, Alt, or a combination of both. To execute a command, press and hold the specified modifier key(s) and then press the corresponding letter or key. For example, Ctrl + X means pressing and holding the Ctrl key while pressing the X key.

Vi/Vim Text Editor

Vi and Vim are powerful terminal-based editors. To create or edit a file with Vim, enter:

vim filename.txt

Here’s a table listing some common keyboard keys used in the Vim text editor along with their descriptions:

iSwitch to insert mode (start inserting before the cursor)
EscSwitch to normal mode (exit insert mode, navigate the document)
:wSave changes (write)
:qQuit Vim
:q!Quit Vim without saving changes (force quit)
:wqSave changes and quit
yyYank (copy) the current line into the clipboard
pPaste the content from the clipboard after the cursor
ddDelete (cut) the current line
uUndo the last change
Ctrl + rRedo the last undone change
/search_termSearch for a specific term
nMove to the next occurrence of the search term
NMove to the previous occurrence of the search term
:s/old/new/gSubstitute (replace) all occurrences of ‘old’ with ‘new’
:set numberShow line numbers
:set nonumberHide line numbers
GMove to the end of the file
ggMove to the beginning of the file
:line_numberMove to a specific line number
Ctrl + fMove forward one page
Ctrl + bMove backward one page
:w filenameSave the file with a new name
:e filenameOpen a new file
vEnter visual mode (for selecting text)
VEnter visual line mode (select entire lines)
Ctrl + vEnter visual block mode (select a block of text)
yYank (copy) the selected text in visual mode
dDelete (cut) the selected text in visual mode

In Vim, commands are often executed in normal mode, and many commands can be prefixed with a colon : to perform operations. Visual mode is used for selecting text, and insert mode is for inserting or editing text. To execute a command, press the specified key(s) in sequence.

Gedit Text Editor

For a graphical interface, Gedit is a popular choice. To open or create a file, use:

gedit filename.txt

Gedit provides a user-friendly environment for editing text. Close the window to save changes.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.