File opening command in linux

How to open the file in bash

The file is used to store the data permanently and use the data in any script when requires. A file can be opened for reading, writing, or appending. Many bash commands exist to open a file for reading or writing, such as `cat`, `less`, `more` etc. Any text editor can be used to open a file in bash. nano, vim, vi, etc., an editor is used to open a file from the terminal. Many GUI editors also exist in Linux to open a file, such as Gedit, Geany, etc. The file can be opened for reading or writing by using bash script also. The ways to open a file for various purposes have been shown in this tutorial.

Open file using Bash commands:

The uses of shell commands to open a file for creating or reading are shown in this tutorial. The uses of `cat`, `less`, and `more` commands have shown here.

Use of `cat` command:

The `cat` is a very useful command of bash to create or display the file’s content. Any file type can be created easily and quickly by opening the file using the `cat` command with the ‘>’ symbol. Run the following `cat` command to open a file named file1.txt for writing. If the filename already exists, then the previous content of the file will be overwritten by the new content; otherwise, a new file will be created.

Add the following content to the file.

A bash script is a command-line interpreted language.
Many automated tasks can be done easily using a bash script.

Press Ctrl+D to finish the writing task. The following output will appear after creating the file.

Now, run the following `cat` command to open the file.txt file for reading.

The following output will appear after executing the above command.

Use of `less` command:

The `less` command is used to open a file for reading only. It is mainly used to read the content of the large file. The user can move backward or forward through the file by using this command. It works faster than other text editors.

Run the following command to open the file1.txt file for reading. Here, the content of the file is very small. So when the user presses the enter key, then the content will go upward. Press the character ‘q’ to return to the command prompt.

Читайте также:  Linux fdisk and fdisk

The following output will appear after opening the file using the `less` command and pressing the enter key.

Use of `more` command:

Like the `less` command, the `more` command is used to open a large file for reading only. This command is mainly used to read a file’s large content in multiple pages to help the readers read long files.

Run the following command to open the file1.txt file for reading by using the `more` command. It is a small file. So all content of the file has displayed on one page.

The following output will appear after opening the file using the `more` command.

Open file using command-line editors:

The uses of vi and nano command-line editors for opening the file to create and read have been shown in this part of this tutorial.

Use of vi editors:

One of the popular text editors of Linux is vi editors. It is installed on Ubuntu by default. The user can easily create, edit, and view any file by using this text editor. The advanced version of vi editors is called vim editor, which is not installed by default. This part of the tutorial shows how to use vi editor to open a file for creating and reading. Run the following command to open the file2.txt file for writing.

You have to press the character ‘i’ to start writing into the vi editor. Add the following content to the file.

Writing a file using vi editors.

You can do any of the following tasks after writing the content of the file.

  1. Type :wq to quit the editor after saving the file.
  2. Type :w to keep the file open in the editor after saving.
  3. Type :q to quit the editor without saving the file.

The following output shows that ‘:wq’ has been typed to quit the editor after saving the file.

Run the following command to open the file2.txt file and check the content exists or not that was added in the file.

The following output shows that the file contains the data that was added before. Here,’:’ has typed to quit the editor.

Use of nano editor:

Another useful and popular editor of Linux is the nano editor that is used to open a file for writing and reading. It is easier to use than the vi editor and more user-friendly than other command-line editors. Run the following command to open the file3.txt file for writing using nano editor.

Add the following content to the file.

Writing a file using nano editor.

If you type Ctrl+X after adding the content to the file, it will ask you to save the file. The following output will appear if you press the character, ‘y’. Now, press the enter to quit the editor after saving the file.

Читайте также:  Удалить пакет линукс rpm

Open file using GUI text editor:

The ways to use gedit and geany GUI-based text editor have shown in the part of this tutorial.

Use of gedit editor:

The gedit is mostly used GUI-based text editor that is installed by default on maximum Linux distributions. Multiple files can be opened by using this editor. Run the following command the open the existing file1.txt file using gedit editor.

The following output will appear after executing the command.

Use of geany editor:

Geany is a more powerful GUI-based editor than the gedit editor, and you have to install it to use it. It can be used to write code for many types of programming languages. Run the following command to install the geany editor.

After installing the editor, run the following command to open the file1.txt file.

The following output will appear after executing the command.

Conclusion:

Many ways to open a file for reading or writing have been shown in this tutorial by using bash command, command-line editors, and GUI-based editors. The Linux users can select any of the ways mention here to open a file in bash.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

How to Open a File in Linux?

One of the main reasons to open a file in Linux is to read its content which can be useful when you want to see what is inside a file without making any changes. Another reason could be to modify the file’s content, which is useful when you need to make changes to a configuration file, script, or other text file types.

This article will show you different ways to open a file on Linux with the following outcomes:

How to Open a File in Terminal?

In Linux, you can open a file by utilizing different commands in the terminal, which are explained below.

Using a cat Command

This is one of the simplest methods to open a file in Linux distribution by following the syntax mentioned below:

You can also use to create a file using a cat command and later save its content as well by pressing “CTRL +D” using a following syntax shown:

For Large files it tries to fit its data on the terminal, but it is not possible then you can access that data by scrolling down as shown below:

Note: You can also use the cat command to open multiple files as well by following the below syntax:

You can get more details about the cat command by reading this article.

Using less Command

The less command will open the file’s data separately, where you need to scroll down just like the above method to see the file’s content. To exit that file, you must press the ‘q’ key from the keyboard.

Читайте также:  Удалить виртуал бокс линукс

Note: You can also exit the screen when you reach the end of the file and go back to the terminal using the ‘-E’ flag as shown below:

Using more Command

The more command works in the same way as others, but it will show you the remaining document that is left in the form of percentage as shown below:

The above image is showing that only the 63% of the text is being displayed on the screen and the remain 37% can be seen by scrolling down. Another advantage is that it displays all the text in the same terminal. You don’t need to press any key as well to exit it as displayed below:

Using the nl Command

The number line command or nl is used to put a number at the start of each new paragraph as shown in the image below:

Using nano Command

Linux comes with the built-in editor with the name of ‘nano’ that you can use not only to open files but also to modify them using the below command:

You can also modify the content if you want to and later save the changes by pressing “CTRL +S” and close the file using “CTRL +X”.

Note: There is another great text editor that you can use with the name of “vim” that you can also use as an alternative to nano.

How to Open a File on a GUI?

There are multiple ways to open a file via GUI, which can be invoked through the terminal.

Using gedit Command

In Linux, the “gedit” is a graphical text editor in Linux that enables you to create and edit text files you can use it by typing the below command:

Although you need to run the command through the terminal, the result can be seen in the GUI form as shown below:

Using xdg-open Command

The “xdg-open” command is used to open a file with the default application for its file type, so if you use it to open a text file, it will be opened with the default text viewer, as shown below.

Directly Accessing the Directory

One of the easiest ways of opening any file in Linux is by “Double Click” on it or by right-clicking on the desired file selecting “Open with Text Editor”.

Conclusion

To open a file using a terminal, you can use a command such as “cat,” “less,” “more,” “nl,” and “nano”. You can use the “gedit” and “xdg-open” commands to open a file using a GUI. Also, one additional method has been discussed, allowing you to open the file directly by accessing the directory. All these methods are practically demonstrated on Linux.

Источник

Оцените статью
Adblock
detector