LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2022, 09:59 PM   #1
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,434

Rep: Reputation: 110Reputation: 110
How do I open a plain text file in any Linux?


I am not sure, but I believe in Windows you can run

Code:
> start SomeTextFile.txt
on a terminal and Windows will open the text file with whatever program is configured as the default one for opening .txt files.

Whether that is true or not, is there such a thing on Linux?

I remember there used to be such a command for KDE a very long time ago (like 15 years ago), but I don't know if it is still used and, anyway, I need some command that does not rely on KDE. It just has to assume the user has a graphical environment. Is that possible?

TIA
 
Old 09-16-2022, 10:40 PM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,762

Rep: Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763
Wow! there are SO many ways to open a text file!
Do you mean to read, or to edit?
If the file is names "file.txt" one could display it with
Code:
cat file.txt
or
Code:
less file.txt
or
Code:
more file.txt
or
Code:
view file.txt
or possibly a dozen or more additional options.

One might open it for editing with
Code:
vi file.txt
or
Code:
vim file.txt
or
Code:
nano file.txt
or
Code:
kate file.txt
or
Code:
joe file.txt
or
Code:
pico file.txt
or any of many other options depending upon what text editors you might have installed.

If you have a file manager installed, you can open that (generally it is an Xwindows program dso you open it in your gui desktop interface from the menu system) and click on the txt file and it will might in your default text editor - however you have that set at the time.


What is it you really want to do?

Last edited by wpeckham; 09-16-2022 at 10:43 PM.
 
2 members found this post helpful.
Old 09-16-2022, 10:43 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,783

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
Should be possible. I don't know if any such command that exists but fairly easy to create a script i.e. find the mime type via the find command and then find what application is associated with that type by searching the mimeapps.list file.

https://wiki.archlinux.org/title/XDG_MIME_Applications

Last edited by michaelk; 09-16-2022 at 10:45 PM.
 
2 members found this post helpful.
Old 09-16-2022, 11:16 PM   #4
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,665

Rep: Reputation: Disabled
You can set the $EDITOR variable in Linux, technically you can name this variable whatever you want, just make sure you are not interfering with existing variables.
 
1 members found this post helpful.
Old 09-16-2022, 11:23 PM   #5
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,151

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Can also do alias's. I don't use them but many do. Then you can type in whatever you want.

https://vitux.com/how-to-create-alia...nds-in-debian/

example

Code:
start='nano'
or any other command you can pass.

Code:
runthisnow='apt update && apt full-upgrade -y'
 
1 members found this post helpful.
Old 09-16-2022, 11:29 PM   #6
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,623
Blog Entries: 40

Rep: Reputation: Disabled
GUI, Folks!

If there is a taskbar with a kind of “start”-button, the “utilities” should comprise some text-editor.
Alternatively there should be a “run” function, although there is no obligation, as far as I know: Usually you hit Alt+F2 and name the program or command-line that you wish to start.
Examples are listed above in this thread, but the command-line editors need to be preceded by the call to a terminal application, like:
Code:
/usr/bin/lxterminal -e "vim file.txt"
Compared to your Windows-command that you state to use “in a terminal application”, the procedure is identical on Linux. Open a terminal and use one of the calls in post #2, above.
 
1 members found this post helpful.
Old 09-17-2022, 04:57 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,357
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by lucmove View Post
I remember there used to be such a command for KDE a very long time ago (like 15 years ago), but I don't know if it is still used and, anyway, I need some command that does not rely on KDE. It just has to assume the user has a graphical environment. Is that possible?
Many distros will check a file and then launch the appropriate graphical program if you run xdg-open with the file name as the first option.

Code:
xdg-open somefile
Otherwise, you'll have to know or guess whether to use geany, kate, gedit, mousepad, leafpad, etc. and use one of them by name. I have a script which is on many different desktops, each with a different distro, and it does just that.
 
5 members found this post helpful.
Old 09-17-2022, 05:19 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,783

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
Completely forgot about xdg-open. It uses the mime type to determine what application to use.
 
1 members found this post helpful.
Old 09-17-2022, 05:49 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
As a senior member this seems a bit odd, however:

https://googlethatforyou.com?q=How%2...any%20Linux%3F
 
1 members found this post helpful.
Old 09-17-2022, 08:02 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by lucmove View Post
I am not sure, but I believe in Windows you can run

Code:
> start SomeTextFile.txt
on a terminal and Windows will open the text file with whatever program is configured as the default one for opening .txt files.

Whether that is true or not, is there such a thing on Linux?

I remember there used to be such a command for KDE a very long time ago (like 15 years ago), but I don't know if it is still used and, anyway, I need some command that does not rely on KDE. It just has to assume the user has a graphical environment. Is that possible?

TIA
I do not believe this is specifically true. Using start in the Windows command prompt should attempt to run a program and unless the file is a script or executable, this will not work.

Linux:
Well many have answered. Do we misunderstand your question? Because you obviously know Linux. wpeckham very much covered it.

And you do not require a graphical environment.
 
Old 09-17-2022, 09:03 AM   #11
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556
Quote:
Originally Posted by rtmistler View Post
I do not believe this is specifically true. Using start in the Windows command prompt should attempt to run a program and unless the file is a script or executable, this will not work.
Nope, it will work:
Quote:
Originally Posted by https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc770297(v=ws.10)
You can run nonexecutable files through their file association by typing the name of the file as a command.

Quote:
Originally Posted by rtmistler View Post
Well many have answered. Do we misunderstand your question? Because you obviously know Linux. wpeckham very much covered it.
Probably because of the poorly worded title, many misunderstood what was being asked.

I'm pretty sure Turbocapitalist answered the actual question - i.e: xdg-open is the equivalent to the above behaviour.

(A search for "linux open file in default application" contains many results saying the same thing...)

 
2 members found this post helpful.
Old 09-17-2022, 09:28 AM   #12
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,434

Original Poster
Rep: Reputation: 110Reputation: 110
xdg-open is just what the doctor ordered. Thank you.

The only question now is whether it is available in a large enough number of installations.

I think I will embed a basic text editor in my application. I guess it's the only foolproof way.
 
Old 09-17-2022, 10:38 AM   #13
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,762

Rep: Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763Reputation: 2763
If those are solutions, then mimetype and mimeopen might also be useful!

As mentioned, once you find the command you want to use one could alias "start" to be an alias for that command.

Last edited by wpeckham; 09-17-2022 at 10:41 AM.
 
1 members found this post helpful.
Old 09-17-2022, 01:01 PM   #14
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,003

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
Quote:
Originally Posted by lucmove View Post
I think I will embed a basic text editor in my application. I guess it's the only foolproof way.
If this is a GTK application, you can use the built-in editor, GtkTextView.
Ed
 
1 members found this post helpful.
Old 09-17-2022, 02:49 PM   #15
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Just going to add that the OSX equivalent to "start" and "xdg-open" is "open".
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to export just source/destination field of pcap file into a plain text file kikilinux Linux - Networking 5 11-27-2014 07:13 AM
NS2: Need of plain plain aodv.cc and aodv.h files chenil Linux - Software 1 07-10-2013 06:17 AM
The best way to convert from text/x-c++ iso-8859-1 to text/plain utf8. rm_-rf_windows Linux - General 1 10-03-2012 04:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration