How to Open a File in Terminal or Command Prompt (Windows, MacOS, & Unix)


In the realm of computing, the command-line interface (commonly referred to as the terminal) provides a versatile and powerful means of file manipulation. Mastering the use of your terminal is an invaluable skill. In this tutorial, I will show you how to open various types of files using the terminal across a variety of operating systems.


Why Use Terminal

While your GUI is intuitive and user-friendly, the terminal is indispensable for a range of tasks that require precision, automation, and batch processing (my favorite.) Utilizing the terminal enables you to write scripts to automate repetitive tasks or execute operations on multiple files simultaneously.


Before we delve into specific commands, you'll need to identify where the terminal application can be accessed on your operating system.
  • Windows: Use PowerShell or Command Prompt.
  • MacOS: Your Terminal can be found in the Utilities folder within the Applications directory.
  • Linux: Your terminal can usually be opened using the Ctrl+Alt+T keyboard shortcut.

Opening Text Files

Text files are the easiest file types to open via the terminal, so we'll begin here. Included below are procedures for opening a text file in MacOS, Linux, and Windows.

cd /path/to/directory

Opening Files With MacOS and Linux Terminal

MacOS is based on Unix, so you'll use the same commands. You'll want to start by opening terminal.

In MacOS:
Open the Applications directory, then the Utilities folder. You'll find terminal here.

In Linux:
Hold Ctrl + Alt + T (this is a shortcut for opening terminal.) Once you've opened terminal, you'll want to navigate to the location of your text file. Once there, you can open your text file by typing the following, where filename.txt is the name of your file:

open filename.txt

VoilĂ ! Now you've opened a text file using terminal! If you would like to use a specific terminal-based text editor like emacs, vim, or nano, you can do this by typing (change vi to emacs or nano if you wish to use those instead of vim):

vi filename.txt

You can also open other file types as a text file to see what's inside. For example, I often open .html files as text files so I can see the code behind a webpage instead of viewing the webpage itself. This can be achieved as follows:

vi coolsite.html

Opening a File in Its Default Software

To get a file to open in the software that is defaulted to handle a particular filetype, type in:

xdg-open filename

Opening Other File Types in Terminal

To open things other than text files, you'll still be using the open command.

PDF Files
You can open PDFs with the open command just as you did with text files.

open filename.pdf

Images

open filename.jpg

Webpages

open http://www.example.com

By now, you should notice a pattern in the code you're typing:
open the_thing_you_want_to_open.filetype

Now you've mastered opening various files using Terminal.

How to Open a File With Windows Command Prompt

Now let's open a text file using command prompt in Windows! First, you'll need to open command prompt. One way to do this is by doing the following:
  1. Press Win + R to open the Run dialog box.
  2. Type "cmd" and press Enter or click OK.
  3. Navigate to the directory containing your text file: cd C:\path\to\directory
In Windows, you'll be opening your text file using Windows' native editor, Notepad. To do this, type the following code into your command prompt where filename.txt is the name of your file:

notepad filename.txt

Great, now you've opened a text file using the command prompt in Windows! If you do want to use specific software to open your file in Windows, just edit the above code using the name of the software instead of Notepad. Pretty cool, yeah?

Opening Other File Types in Windows Command Prompt

This is where things get a little tricky. In Windows, you must specify not only the file you want to open but also what software you want to open it with. For example, if you want to open a pdf, you must specify that you want to open it with Adobe Reader (or whatever other pdf reader you have installed), as follows:

AcroRd32.exe /A "filename.pdf"

For Images
mspaint filename.jpg

For Webpages
Fortunately, webpages are easy. They'll open in your default browser:

start http://www.example.com

Woot! You've just mastered how to open various files in terminal, which is actually a pretty awesome skill to have! Terminal offers enormous flexibility and power when it comes to opening and manipulating files. By investing your time in working in terminal, you've opened the door to more efficient use of your machine, and it may even be a gateway to learning more computing skills
.

Comments