-1

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

xdg-open <location>

./filename

and none of them seems to be working, maybe from syntax or not? I receive errors like

# 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

17
  • 1
    What have you searched online before asking this question here ? Commented Jan 28, 2020 at 16:13
  • 1
    Welcome to unix stack exchange. I suggest you look into the following commands: cat, nano, vi, and vim Commented Jan 28, 2020 at 16:36
  • 1
    @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 Commented Jan 28, 2020 at 16:43
  • 1
    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. Commented Jan 28, 2020 at 16:49
  • 1
    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 Commented Jan 28, 2020 at 17:15

4 Answers 4

5

There are a few solutions:

vi <filename>
vim <filename>
nano <filename>
cat <filename>

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.

2
  • 1
    Generically, $VISUAL or sensible-editor, of course. jdebp.uk./FGA/unix-editors-and-pagers.html Commented Jan 28, 2020 at 18:20
  • You should add less to that list. Commented Jan 28, 2020 at 18:57
1

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
0

Use vi Random_File.sh

or nano Random_File.sh

0

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.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.