Kiến Thức Linux

How to Open a File in Bash

Upgrade Linux Kernel in Ubuntu

Introduction

Bash is a command-line shell that allows users to interact with Linux. It is a powerful tool users run from the terminal or other programs. The two main ways to open a file in Bash are from the terminal or using a text editor.

The following text explains how to open a file in Bash using different methods.

Prerequisites

How to Open a File in Bash Using Terminal

This method allows users to view files in the Terminal but not edit them. The following sections provide helpful commands that perform this task.

Method 1: cat

The simplest way to open a file in Bash is to use the cat command. The cat command prints the contents of files without having to open them for editing.

To open the file in Bash with cat, use the following syntax:

cat [filename]

For example, a MyFile text file in the Home directory looks like this:

MyFile example text

Display the MyFile contents on the terminal with:

cat MyFile
Terminal output for cat

The output shows the MyFile content. For large files, scroll using the arrow keys or Page Up and Page Down.

Method 2: more

Another way to open a file in Bash is to use more. The more command displays the file contents one screen at a time. To open a file using more, use the following syntax:

more [filename]

For example, a file called MyFile2 contains more text than MyFile:

To open a file in Bash, run:

more MyFile2

The output prints the first screen. If the file content takes up more than one screen, a message at the bottom prompts users to scroll through the file. The message also shows the percentage already printed. In this case, the -50% indicates that 50% of file content has been printed

To move one line down, use Space. To scroll to the next screen, press Enter until reaching the end.

Exit the command with the q key.

Method 3: less

The less command is similar to more, but it offers a few additional features.

Use the following syntax to open a file with less:

less [filename]

For example, open MyFile2 with:

less MyFile2

The output prints the file’s first few lines. The bottom-left corner includes the file name. Additional less features are:

  • Scrolling through the file using the same keys as more, but without the prompt at the bottom.
  • Searching for text in the file by piping the less command output to the grep command.
  • Scrolling through the file backward with the b key.

Open a file and find a specific word with the following syntax:

less [filename] | grep 'word'

For example, find the word shebang in MyFile2 with:

less MyFile2 | grep shebang
Terminal output for less | grep

The output shows the file contents and highlights all the occurrences of the word shebang.

How to Open a File in Bash with a Text Editor

In addition to using the terminal, users can open a file in Bash with a text editor. Some extra functions of using this method are:

  • Editing the file contents.
  • Saving the file.
  • Searching for text in the file.
  • Highlighting syntax to make the code easier to read.

The most popular Linux text editors are Nano, Vi/Vim, and Gedit. The following text explains how to open a bash file using a text editor.

Method 1: Nano

Nano is an easy-to-use text editor included in most Linux distributions. It is beginner-friendly since users don’t have to learn a lot of complex commands.

To open a file in Nano, use the following command:

nano [filename]

For example, open MyFile in Nano with:

nano MyFile
Text editor: Nano

The text editor opens the file and allows editing.

To save the changes, press the Ctrl+O key. This will open a prompt where users enter the file name to save the changes. To exit the file, press the Ctrl+S key. This combination of keys saves the changes to the file and closes the text editor.

Method 2: Vi/Vim

Both Vi and Vim are modal editors, meaning there are different modes for editing, navigating, and searching.

However, Vi is an older model initially developed for Unix systems. Vim, on the other hand, is a newer, more versatile, and customizable version. It includes extra features, such as syntax highlighting, code folding, a built-in debugger, and windows splitting and tabs. Still, Vi is a popular text editor, especially among experienced Unix users.

Vi and Vim work similarly and use the same commands to save and exit a file, search for specific text, or find and replace words.

The syntaxes to open a file in Bash using Vi and Vim are similar:

vi [filename]

And:

vim [filename]

For example, use Vi to open MyFile:

vi MyFile
Vi text editor MyFileVi text editor MyFile

Vi opens the file and allows edit access. To open a file in Bash using Vim, run:

vim MyFile2

Method 3: Gedit

Gedit is a simple and easy-to-use GUI-based text editor. However, it brings robust features, such as syntax highlighting, code folding, a plugin system, and a built-in debugger.

To open a file in Gedit from the terminal, use the following syntax:

gedit [filename]

For example, open MyFile with:

gedit MyFile

The output does not print the file in the terminal. However, the command opens the file in a Gedit text. Gedit also allows opening any file from the Gedit text editor.

Conclusion

After reading this article, you know various methods to open a file in Bash using the terminal and text editors.

Next, learn what the Bash function is and how to use it.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !