Linux type text file

How to open a text file from terminal? [closed]

Questions describing a problem that can’t be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers.

I am new to linux and I have a quick question for opening text files using my terminal. I tried many times to open a text file using commands such as

# Option “-x” is deprecated and might be removed in a later version of gnome-terminal.# # Use “-- ” to terminate the options and put the command line to execute after it.# -- xdg-open Random_File.sh --: command not found 

I thought that I might have permission issue, but all read, write and execute permissions are available for my text document

Welcome to unix stack exchange. I suggest you look into the following commands: cat , nano , vi , and vim

@ArmenMkrtumyan please refer to my above comment as any one of those commands is capable of opening a .sh file, if you want to edit however then cat won’t help you

Random.sh is not a shell script file. Linux does not use extensions to determine file types. It is a text file, possibly containing assorted shell commands. Might be executable, might be empty, but xdg-open should open it as a text file. Unless, of course, you have managed to create a association to execute it.

since you have worked out the solution with my comment I have turned it into a more flushed out answer for other users benefit. If you are satisfied with it please consider accepting it

4 Answers 4

There are a few solutions:

vi and vim are text editors, anything you can do in vi can be done in vim but both have a big learning curve for beginners. Nano is also a text editor but is much more user friendly than the former (disclaimer: personal opinion), this being said it may not be installed on your system by default. Lastly cat just displays the contents of your file to the command line, so you may not edit with this command.

If the goal is to read a text file from the command prompt, and be able to scroll the text, then most *NIX systems have the utilities less or more that can be used

robert@pip2:/tmp$ less exampleText.txt 

If you just want to spew the text to the command line, then try cat

robert@pip2:/tmp$ cat exampleText.txt 

If you want to edit a file, then almost all *NIX systems will have vi available

robert@pip2:/tmp$ vi exampleText.txt 

You might have some misconfiguration. My comment above about extensions is incomplete. Linux does have a system based on analysing the first few bytes of a file («magic» numbers) because many well-defined formats (executable binary, compressed files, database tables) conform to standards.

However, some tools (launch menus, and xdg-open included) use additional hints to identify specific file types.

The «file» command says this about the files in my home directory:

Paul--) file * > file.txt Box: Bourne-Again shell script, ASCII text executable D_Recovery: directory Executor_1.txt: UTF-8 Unicode text foo.txt: ASCII text, with escape sequences mbox: ASCII text myEnv: ASCII text One: ASCII text One Two Three: ASCII text Pictures: directory Primes: Bourne-Again shell script, ASCII text executable SqlAwk_ENWL.log: ASCII text SqlAwk_NG.log: ASCII text Templates: directory Three: ASCII text Two: ASCII text UL_hSort.txt: ASCII text wdog: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=9a15a7ca3bb94aed54a7a14fb9a11a2dd87d8baa, not stripped wdog.c: C source, ASCII text Xfers: broken symbolic link to /media/paul/0C6E70246E7008AA/Users/Paul/Downloads 

When I run xdg-open UL_hSort.txt , the command prompt comes straight back, but it launches an independent GUI for an editor called Xed. It has a tab for the file, and if I hover over that it says it has a Mime type of plain text document with UTF-8 encoding.

Читайте также:  X org server linux

I can run xdg-open with other files from the list, and it opens them as additional tabs in the same GUI. It even changes their Mime type, and does syntax colouring, if I save or reload the file. If I open a Jpeg, it launches a GUI for Xviewer for that file instead. If I open a .docx file (MS Word), it opens LibreOffice Writer for it. And so on.

Источник

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 find path to directory

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.

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.

Читайте также:  Linux stop docker daemon

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.

Источник

Which extension to use for text files? (Unix/Linux)

I noticed that I can read text files without an extension .txt just fine. How come? Should I save these files with or without the .txt extension? Also, what about .ini files? I usually use them like this: config.ini , should I remove the extension here to? Any general resources on how Linux handles file extensions would be useful.

6 Answers 6

UNIX/Linux does not have the same early DOS / CP/M heritage that Windows does. So extensions are generally less significant to most UNIX utilities and tools.

I usually use a command-line only environment. Extensions in such an environment under Linux aren’t really significant except as a convenience to the operator or user. (I don’t have enough experience with KDE or GNOME to know how their filemanagers deal with extensions.)

But such convenience is usually important. If config.ini is really in Microsoft-standard «.ini» format, I’d let the extension stand. Plain old text files usually carry no extension in Linux, but this isn’t universal for all programs configuration files. The programmer usually gets to decide that.

I think «.txt» is useful under Linux if you want to emphasize that it’s NOT a configuration file or other machine-readable document. However, in source distributions, the convention is to name such files all caps without an extension (i.e. README, INSTALL, COPYING, etc.)

There are some standards and conventions but nothing stopping you from naming anything whatever you want, unless you are sharing things with others.

In Windows, naming a file .exe indicates to the shell (usually explorer.exe ) that it’s an executable file. UNIX builds this knowledge into the file system’s permissions. If the proper x bits (see man chmod ) are set, it is recognized as executable by shells and kernel functions (I believe). Beyond this, Linux doesn’t care, most shells won’t care, and most programs look in the file to find it’s «type.»

Of course, there’s the nice command file which can analyze the file and tell you what it is with a degree of certainty. I believe if it can’t match the data in the file with any known type, and if it contains only printable ASCII/Unicode characters, then it assumes its a text file.

@Bruce Ediger below is absolutely correct. There is nothing in the kernel or filesystem level, i.e. Linux itself, enforcing or caring that the contents of a file needs to match up with its name, or the program that is supposed to understand it. This doesn’t mean it’s not possible to create a shell or launcher utility to do things based on filename.

Источник

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