4

Objective: Open a text file and other types using the terminal.

Let's suppose I have a file named myFile.txt and I want to launch this file (open it, not sure how I'm supposed to say it), how can I do this?

As I searched all I could find was articles about create and edit files or display the content in the terminal.

Some of the failed attempts:

./myFile.txt

/myFile.txt

cd myFile.txt

cd ./myFile.txt

cd /myFile.txt

eog myFile.txt

And, yes, I used the commands in the folder containing the file in question.

3
  • 3
    Hm, it's not immediately obvious to me what you want to do with the file. If you don't want to view or edit it, what exactly do you want to do with it? I'm only asking because your answer will help people recommend a particular program to you that will do what you want. (Typically, when one asks how to launch or open a file, they mean something like view or edit the contents.)
    – nivk
    Nov 30, 2019 at 21:49
  • 3
    Alternatively, if the file is a program that you are trying to run or execute, you may be doing it correctly with ./myFile.txt, but you only need to set the file to be executable: chmod +x myFile.txt. Hope this helps.
    – nivk
    Nov 30, 2019 at 21:51
  • 2
    Did you create myfile.txt in the first place or did something else create it? If you created it what program did you use to create it? This question might be an X-Y problem Nov 30, 2019 at 22:09

2 Answers 2

5

xdg-open will try to guess your desktop environment and the file's mime type and try to open it in the appropriate application:

xdg-open myFile.txt
0

Select the tool manually

Typically, when using the terminal window and command lines, you are expected to know which tool you want to use. I know that this can be difficult in the beginning, but there are several things you might want to do.

If you use a graphical file manager, there is

  • a main tool (for the main task), that you start by left-double-clicking on the file's icon.
  • and often a few other tools (for other tasks), that you can select after right-clicking on the file's icon.

The main tasks for a text file might be editing and viewing.

  • In standard Ubuntu desktop there is gedit for these tasks.

  • If you want only viewing in text mode, you can use less

  • If you want to edit in text mode, you can use nano

So I suggest that you try

gedit file.txt
less file.txt
nano file.txt

You mentioned some alternatives that do not work (because ...)

./myFile.txt

This is typically used to run an executable file

/myFile.txt

This points to a file in the root directory /

cd myFile.txt
cd ./myFile.txt
cd /myFile.txt

This changes the terminal window to the specified directory (cd 'change directory' makes the terminal window look at the specified directory. You should specify a directory (not a file) after the cd command.)

eog myFile.txt

eog "Eye of Gnome" is a viewer for picture files.

xdg-open

Until you know which tool to use, you can try with xdg-open. It will select a tool for you depending of the kind of file you are using. I would expect that it will select gedit for text files and eog for picture files (in standard Ubuntu desktop).

xdg-open file.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .