How to Create and Edit Text Files in Linux Using Terminal

Table of contents:

How to Create and Edit Text Files in Linux Using Terminal
How to Create and Edit Text Files in Linux Using Terminal

Video: How to Create and Edit Text Files in Linux Using Terminal

Video: How to Create and Edit Text Files in Linux Using Terminal
Video: How to Extract RAR Files on Ubuntu 18.04 LTS? 2024, November
Anonim

This wikiHow teaches you how to use the Terminal application on Linux to create text files. After that, you can use one of Linux's built-in text editing programs to modify the file.

Step

Part 1 of 4: Opening Terminal

Create and Edit Text File in Linux by Using Terminal Step 1
Create and Edit Text File in Linux by Using Terminal Step 1

Step 1. Open Terminal

How, click Menu, and look for the Terminal app icon, which is a black box with a white ">_" symbol inside. Click the icon when you find it. Usually, Terminal is in the bar on the left side of the Menu window.

You can also click the search bar at the top of the Menu window and type in a terminal to start a search

Create and Edit Text File in Linux by Using Terminal Step 2
Create and Edit Text File in Linux by Using Terminal Step 2

Step 2. Type ls into Terminal, then press Enter

The terminal will open to your home directory, but the ls command displays all the folders in your current directory. To create a text file in one of these directories, you will need to change the current directory.

Create and Edit Text File in Linux by Using Terminal Step 3
Create and Edit Text File in Linux by Using Terminal Step 3

Step 3. Locate the directory where you want to load the text file

All directory names listed under the ls command (eg "Desktop") are places you can go.

Create and Edit Text File in Linux by Using Terminal Step 4
Create and Edit Text File in Linux by Using Terminal Step 4

Step 4. Type in cd directory

You will replace the name " directory " with the desired directory name. This command changes Terminal from the current directory to one of the names you created.

  • For example, type cd Desktop to change the location of the Terminal command to the Desktop directory.
  • If you want to create a text file in a special folder within the selected directory, include a "/" symbol after the directory and type in the file name. For example, type cd Documents/Others to create an “Others” directory within the Documents directory.
Create and Edit Text File in Linux by Using Terminal Step 5
Create and Edit Text File in Linux by Using Terminal Step 5

Step 5. Press Enter

This will execute your command, and the Terminal's target directory will change from the home directory to the directory you specified.

Create and Edit Text File in Linux by Using Terminal Step 6
Create and Edit Text File in Linux by Using Terminal Step 6

Step 6. Choose a text editing program

You can create simple text files quickly, or you can use Vim or Emacs to create and edit text files in more depth. Once you're in the directory where you want your text files to be, it's time to create your text files.

Part 2 of 4: Creating Text Files Quickly

Create and Edit Text File in Linux by Using Terminal Step 7
Create and Edit Text File in Linux by Using Terminal Step 7

Step 1. Type

cat > filename.txt

to Terminals.

You will replace “filename” with the desired text file name (eg “sample”).

For example: when creating a file called “cat”, type cat > cat.txt

Create and Edit Text File in Linux by Using Terminal Step 8
Create and Edit Text File in Linux by Using Terminal Step 8

Step 2. Press Enter

Thus, a new text file with the specified name will appear in the current directory. Your cursor will appear on a blank line in Terminal.

Create and Edit Text File in Linux by Using Terminal Step 9
Create and Edit Text File in Linux by Using Terminal Step 9

Step 3. Enter the document text

You can type normally like any other text document. To save the current line of text and move to the next line, press Enter.

If your text file directory opens, please double click the text file when it appears to open it

Create and Edit Text File in Linux by Using Terminal Step 10
Create and Edit Text File in Linux by Using Terminal Step 10

Step 4. Press Ctrl+Z

This shortcut saves your work and returns you to the Terminal command line so you can continue entering commands.

Create and Edit Text File in Linux by Using Terminal Step 11
Create and Edit Text File in Linux by Using Terminal Step 11

Step 5. Type

ls -l filename.txt

to Terminals.

You will replace "filename" with the name of your text file. This command will search for your files, and ensure they are safely stored in your directory.

  • For example: to open a file named "textfile", please type ls -ltextfile.txt.
  • The letters in this code are lowercase “L”, not uppercase “i”.
Create and Edit Text File in Linux by Using Terminal Step 12
Create and Edit Text File in Linux by Using Terminal Step 12

Step 6. Press Enter

This will show the time, date, and name of your file to the next line indicating that you created and saved the file in your directory.

Part 3 of 4: Using Vim

Create and Edit Text File in Linux by Using Terminal Step 13
Create and Edit Text File in Linux by Using Terminal Step 13

Step 1. Type

vi filename.txt

to Terminals.

The "vi" part of this command selects Vim as the text editing program. You will replace "filename" with the name of the new file you want.

  • For example, for a file named " tamin ", please type vi tamin.txt.
  • If your current directory has a file with the same name, this command will open the file.
Create and Edit Text File in Linux by Using Terminal Step 14
Create and Edit Text File in Linux by Using Terminal Step 14

Step 2. Press Enter

A new file will be created and opened in the Vim editor. You'll see a blank Terminal window with a (~) symbol on each line, as well as a text file name at the bottom of the window.

Create and Edit Text File in Linux by Using Terminal Step 15
Create and Edit Text File in Linux by Using Terminal Step 15

Step 3. Press the i key on the keyboard

This will put the document into " Insert " mode, and you can enter text as needed.

you will see -- INSERT -- appears at the bottom of the window when pressing the I key.

Create and Edit Text File in Linux by Using Terminal Step 16
Create and Edit Text File in Linux by Using Terminal Step 16

Step 4. Enter the text of your document

You can type normally like any other text document. To save the current line of text and move to the next line, press Enter.

Create and Edit Text File in Linux by Using Terminal Step 17
Create and Edit Text File in Linux by Using Terminal Step 17

Step 5. Press the Esc key

This key is usually located in the upper-left corner of the computer keyboard. This button will put Vim into " Command " mode.

You will see a cursor appear at the bottom of the window

Create and Edit Text File in Linux by Using Terminal Step 18
Create and Edit Text File in Linux by Using Terminal Step 18

Step 6. Type

:w

go to Terminal and press Enter.

This command will save the document as it is.

Create and Edit Text File in Linux by Using Terminal Step 19
Create and Edit Text File in Linux by Using Terminal Step 19

Step 7. Type

:q

go to Terminal and press Enter.

This will let you exit Vim and return to the main Terminal interface. Your text file is now in the specified directory.

  • You can check text files by typing ls into Terminal and pressing Enter and then searching for the text file name.
  • You can also type:wq to save the file and exit Vim in one command.
Create and Edit Text File in Linux by Using Terminal Step 20
Create and Edit Text File in Linux by Using Terminal Step 20

Step 8. Reopen the file from a Terminal window

As when creating a file, type vi filename.txt to open the file. This time, when you open the file, you will see all the changes that were previously made.

Part 4 of 4: Using Emacs

Create and Edit Text File in Linux by Using Terminal Step 21
Create and Edit Text File in Linux by Using Terminal Step 21

Step 1. Type

emacs filename.txt

to Terminals.

You will replace “filename” with the desired text file name.

Create and Edit Text File in Linux by Using Terminal Step 22
Create and Edit Text File in Linux by Using Terminal Step 22

Step 2. Press Enter

Provided that the name of your text file does not match an existing text file in the current directory, a new text file will open in the Emacs editor.

  • For example, a file named "newfile" can be opened by typing emacs newfile.txt.
  • If you enter the name of a file that is already in the directory, this command will open the file.
Create and Edit Text File in Linux by Using Terminal Step 23
Create and Edit Text File in Linux by Using Terminal Step 23

Step 3. Get to know Emacs commands

Emacs comes with many useful commands that let you view documents, look for related information or help, manipulate text, and understand your code. These commands fall into two categories: Control commands and Meta commands.

  • Control Command written like this: C-. To run the Control command, hold down the Ctrl key and press the desired letter key at the same time. (eg Ctrl and A keys).
  • Meta Command (or Escape) is written as follows: M- or ESC. The letter "M" refers to the alt=""Image" or Esc key on the keyboard because not all computers have the Alt key.</li" />
  • The Meta command is written like this: Ca b (or Ma b) and you have to hold down Ctrl (or alt=""Image" or Esc) while pressing the first key (for example a), then release both keys and immediately press the second key ((b).</li" />
Create and Edit Text File in Linux by Using Terminal Step 24
Create and Edit Text File in Linux by Using Terminal Step 24

Step 4. Enter the document text

You can type normally like any other text document. To save the current line and move to the next line, press Enter.

Create and Edit Text File in Linux by Using Terminal Step 25
Create and Edit Text File in Linux by Using Terminal Step 25

Step 5. Press Ctrl+X, then press S to save your file.

Create and Edit Text File in Linux by Using Terminal Step 26
Create and Edit Text File in Linux by Using Terminal Step 26

Step 6. Press Ctrl+X, then press Ctrl+C.

This will let you exit the Emacs text editor and return to the directory in Terminal. Your text file should be saved under the name you chose and be in this directory.

Create and Edit Text File in Linux by Using Terminal Step 27
Create and Edit Text File in Linux by Using Terminal Step 27

Step 7. Reopen the text file

To do this, just type emacs filename.txt into Terminal. As long as you're in the same directory as the file, the text file will open in Emacs. You can continue editing, if you want.

Tips

  • Vim is a program that is generally accessible on all versions of Linux, while Emacs is a more user-friendly editor making it easier for beginners to use.
  • You can open the “Help” window in Emacs by pressing Ctrl+H, then release both keys and press T immediately. The Help menu displays additional command keys and other functions in the Emacs editor that can help as you write.

Recommended: