loading...

Getting Started with the Command Line

dienakakim profile image Dien Tran ・3 min read

If you're an avid computer user who wants more granular control over what your operating system offers, via windows you can open or buttons you can click, chances are you ned to work with the command line. Sometimes called the shell, it comes in many forms: cmd.exe and powershell.exe on Windows or bash on Linux and macOS. Nevertheless, what we are doing here applies to both shells.

Directory navigation

Suppose we have the following directory structure:

<user directory>
...
+-- Desktop/ (we are here)
    +-- program.exe
...

On Windows, you would likely see the following prompt:

C:\Users\<username>\Desktop> _

and on Linux and macOS (probably):

~/Desktop $ _

What you type will automatically appear at the underscore.

Now, let's say you want to execute program.exe. On Windows, make sure you are using cmd.exe; then executing the program would be a simple matter, just type program.exe and hit Enter.

In PowerShell and on Linux and macOS, however, things get more complicated. You will notice that program alone does not work, and will likely result in an error about a program not found.

You actually have to use ./program.exe. But what does this mean?

. means "the current directory"

That's right, you have to explicitly tell the shell that program.exe is in the current directory, and only then will it execute the program. This is for security reasons. What if you have 2 program.exes, one that's already installed in your system, and the other located in Desktop?

Once again, ./program.exe means "I want to execute program.exe and that it is in the current directory." And did I tell you that . means "the current directory"?


Let's expand our directory structure for a bit:

<user directory>
+-- Documents/
    +-- assignments.pdf
+-- Downloads/
    +-- archive.zip
+-- Desktop/ (we are here)
    +-- program.exe

and we are still in Desktop. Now suddenly you feel the urge to extract the archive archive.zip in Downloads. But we're in Desktop, so what do we do?

.. means "the parent directory"

The reason I'm introducing this is because there's no direct way to jump from Desktop to Downloads, and you have to go through the parent directory to access Downloads.

So now, from Desktop, we want to:

  • get to the parent directory
  • go to Downloads from the parent directory
  • access archive.zip in Downloads

The file, relative to Desktop, is accessed using the following syntax: ../Downloads/archive.zip. Let's break this down.

  • we need to get to the parent directory, so we used ..
  • we add a forward slash (/), which acts as a delimiter for directories
  • we need to get to Downloads, so we append that to get ../Downloads
  • we add a forward slash (/)
  • access the archive.zip file by appending that, resulting in ../Downloads/archive.zip.

Simple, isn't it? Now to extract the file, Linux has the command unzip that does it for us. So the prompt should look like this in Linux:

~/Desktop $ unzip ../Downloads/archive.zip

Now what if we actually want to change where we are at? Staying in Desktop all day seems boring, no?

cd means "change directory"

It does exactly what it says: give it a proper directory location and it will transport you there. Say you are still in Desktop and want to get to Downloads.

  • You can first go to the parent directory, so cd .. does that.
  • You can then get to Downloads, so use cd Downloads.
  • Or you can get there directly by using cd ../Downloads. Remember, / delimits parent-child directories.

And that's some brief command line usage tips. This helps every one of us inch closer and closer to being a power user, but we'll take it one step at a time. :)


Posted on by:

dienakakim profile

Dien Tran

@dienakakim

UTD '22, CS major. Fluent in rigorous mathematical reasoning, Bash-scripting, C++/Boost libraries, Python, Java, and Go.

Discussion

markdown guide