How to open a file in a terminal using Linux commands

  

In a Linux system, opening a file is as simple as a startup icon, but how to open a file in the terminal without the startup icon? Using the command can solve this problem very well. The following small series teaches you how to use the command to open files in the Linux terminal. Let's learn together.

work in a Linux environment, most of the time in dealing with the terminal, often encounter To view a file, such as a picture or a word document (can not be viewed directly in the terminal ). At this time, the usual way is to switch to the gui interface, go to the directory where the file is located, and double-click the file you want to open. Another way is to open the file by entering the specified program directly in the shell, for example: evince xx.pdf View a pdf file. Of course, the premise of this is that you have to remember the names of those programs, such as you can remember to use LibreOffice to open a word file command? Anyway, I can't remember.

A lot of default file opening methods have been defined in the graphical interface. For example, if you double-click a web page file, it will automatically open with the default browser. Double-clicking on a picture will open with the default image browser, so the terminal There should also be a corresponding mechanism. Follow this idea to go to google, and found the xdg-open command (compatible with gnome and kde), this command will use the default settings of the file in the graphical interface to open the same way (and directly double-click to open the file should be the same behavior ).

With this command, it is much more convenient to open a file under the terminal, such as opening a pdf file with the default pdf reader:

xdg-open xxx.pdf

You can also open the directory with the file manager under the graphic, such as opening the user's home directory:

xdg-open ~

A very useful tool!

The specific operation is as follows:

xdg-open { file |  URL }

xdg-open { --help |  --manual |  --version }

OK, is it more convenient now, you can open the shell with the shortcut key Ctrl+Alt+T, F11 can be displayed in full screen (Alt+F9 minimizes the window, Alt+F10 maximizes/restores The window), supplemented by the xdg-open command, greatly reduces the mouse operation and greatly improves the efficiency.

Attached to the xdg-open manual section information:

xdg-open opens a file or URL in the user‘s preferred application. If a URL is provided the URL will be opened in the user’ s preferred web browser. If a file is provided the file will be opened in the preferred application for files of that type. xdg-open supports file, ftp, http and https URLs.

xdg-open is for use Inside is a desktop session only. It is not recommended to use xdg-open as root.

The above is the method of opening a file in a Linux terminal. Using this method can reduce the mouse operation, and the file is easy to open in the terminal. A lot, is it very simple? Give it a try.

Copyright © Windows knowledge All Rights Reserved