Introduction to the command line
The command line has an unfortunate talent for looking much more frightening than it actually is.
You open a window, get some text and a cursor, and apparently everybody expects you to know what to type. Rude.
If you're learning Python, Git, Linux or quite a lot of other developer tools, you're going to meet the command line sooner or later. You do not need to become a command-line wizard. You just need to understand what is happening.
Terminal, shell and command line
A terminal is the interface or application through which you interact with a command-line environment. A shell is the program that reads commands and carries them out. Command Prompt, PowerShell and Bash are examples.
The command line is the text-based way you interact with that shell. You mainly need to know this so that when one tutorial says "terminal" and another says "PowerShell", the words don't immediately send you running for the hills.
Your first command
cd Documents
Here, cd is the command. Documents is an argument telling it which directory we are interested in.
You can think of it a little like calling a function: there is the thing you want to do, followed by information it needs to do the job.
Arguments and options
Commands can accept arguments and options. An argument is commonly a value the command works with, such as a filename. An option changes how the command behaves.
On Unix-like systems you will often see options beginning with - or --. Windows tools have several conventions of their own. Check the command's help rather than assuming they are all identical.
The current working directory
Your shell is always working from somewhere. That place is the current working directory.
Imagine you're standing in a room in a house. If I tell you to go through "the door", which door I mean depends on the room you're standing in. Relative file paths work in much the same way.
Relative and absolute paths
A relative path starts from your current location. An absolute path gives the full location from the root of the drive or filesystem.
C:\Users\Nathan\Documents\projects\mygame
Linux and other Unix-like systems use a different path style, which we'll cover later.
Moving around
cd means change directory. If a folder called Documents exists in your current location:
cd Documents
To move to the parent directory, commonly:
cd ..
Fancy!
Listing contents depends on the shell. Command Prompt commonly uses dir, while Bash commonly uses ls.
Running a program
If the shell knows where a program lives, you can run it by entering its command.
python my_program.py
The first part launches Python. The second tells Python which script to run. On Windows you may also encounter the py launcher.
When a command is not found
At some point you will type a perfectly sensible command and the computer will announce that it cannot find it. Lovely.
That does not automatically mean the program is broken. The shell may simply not know where its executable lives. This is where the PATH environment variable comes in.
Stopping a running command
If a command or script is running in the foreground and you need to interrupt it, Ctrl+C is commonly used in terminals on Windows and Unix-like systems.
Getting help
You do not need to memorise hundreds of commands. Many programs provide help using an option such as --help, and shells have their own help systems.
Learn the commands you use regularly. Look up the rest. That is not cheating; that is software development.
A small exercise
- Find out which directory you are currently in.
- List the files and folders there.
- Move into a folder.
- Move back to its parent.
The exact commands depend on your operating system, so continue with either the Windows or Linux tutorial.
Image Description