The Core Command Line Skills
Here are what I consider the top command-line skills you should master:
Advertisements
- Navigating and managing folders. Having known and easy-to-use locations for your files and repositories.
- Setting the PATH variable to manage what programs you can launch from the command line by default. That is to say, given a program you’ve installed, you should generally be able to run it from the command line.
- Listing folder contents and locating files in subfolders, either based on name or contents.
- Listing file contents and re-directing program output.
- Writing simple shell scripts or batch files to automate tasks. I’m not talking about advanced bash, CMD, or PowerShell scripts here, just basic task launchers. One could also write aliases in place of simple scripts (depending on the environment).
- Setting and using environment variables.
- Running career-relevant command-line tools. In Python, of course, that means python and pip, and perhaps conda, not to mention being able to launch pip-installed tools like Jupyter notebook or IPython. (For front-end folks, we could talk about tools like npm, node, yarn, etc.). Top on my list of career-agnostic tools would be git and — optionally —
gh
(the GitHub command-line tool). You should be able to run other tools as needed, such as the AWS command-line interface.
Let’s start our quick overview of the essentials with the help of this list as a general guide. We need to be discriminating in our selection of the various command terminals that are accessible to us. I will go over instructions that are generally compatible with two commonly used shells, bash and zsh, on Mac and Linux. We don’t need to go into great depth regarding the distinctions between the two, especially for beginners.
The same set of “bash” commands can be used on Windows via WSL (Windows Subsystem for Linux) and git bash. I will also talk about the fundamentals of the Windows command-line program, “cmd”. Nonetheless, if you are looking to do some serious work in the terminal on Windows, I would recommend utilizing git bash. WSL is quite satisfactory, however it can be complicated to incorporate other Windows applications.
Advertisements
Starting a Terminal Session
Opening a Command Prompt From Your Computer
To access a command line terminal, you should be able to type “cmd” into the Windows search bar to find the “Command Prompt” application. Running this will open a command-line terminal.
To use the terminal program on a Mac, use Launchpad and type in “term” to find it. The program should appear, although you may not see ITerm unless it was installed separately.
Advertisements
In Linux, you would look for the “Terminal” too, however the method in which you find applications depends on the version that you are using.
One of my first steps when using a new computer is to click on the icon for the command line terminal, then choose “Pin to taskbar” (in Windows) or “Options / Keep in Dock” (on a Mac). This way, I’ll always have a terminal program handy.
Opening a Command Prompt From Your Editor or IDE
It would be beneficial to learn how to launch a command-line interface from your editor or integrated development environment. In Visual Studio Code, you can access the Terminal by selecting the Terminal menu item or by pressing CTRL-SHIFT-` on the keyboard. (That’s a “backtick” key, typically above the tab key). On Windows, you would press Alt-F12 in PyCharm, while on a Mac the shortcut would be Option-F12. In Jupyter Lab, you can access a terminal by navigating to “View / Tool Windows / Terminal” or “File / New / Terminal”. However, it does not seem like there is a terminal available in Jupyter Notebook.
Advertisements
Navigating the File System from the Command Line
Where Am I?
Generally speaking, if you access the command line from your computer, the directory you will be placed in initially, also known as your “current working directory”, will be your user’s home directory. In CMD on Windows, if your prompt is not setup to show you where you are, you can use the “cd” command to display the present working directory. On Windows computers, when you use the command “pwd”, you will probably view “C:UsersYourUserName”. For Linux/Mac systems, the command “pwd” will usually show you “/Users/YourUserName”.
From here on out, I’m going to attempt to just call “the Mac” Mac, Linux, git bash, and other bash environments on Windows.
Your terminal should open in your IDE or editor, in the same place where your project is situated or where you launched the editor. On a Mac, you can use the command pwd to show your current working directory, and on Windows you can use the command cd to do the same.
Creating Folders and Moving Around
We should start off by making a directory, going into that directory, and then leaving it. The following sequence works in all environments:
# Make a directory
mkdir myfolder
# Move into it
cd myfolder
# Move back
cd ..
You can use the abbreviated command “md” instead of “mkdir” on Windows with the “cmd” program. Apart from that, cd (“Change directory”) still functions as it always has- to move into a directory by name. The “..” symbol stands for the parent directory, so if we currently are in /User/me and make a directory named “myfolder” in that location, navigating into it will take us to /User/me/myfolder and “ cd .. ” will take us back to our starting point.
Advertisements
The way in which paths are divided is distinct between Windows and Linux/Mac operating systems. On Windows, a backslash, “”, is used, while a Mac employs a forward slash, “/”. To go up to the directory above the one we’re currently in, we would type “cd ../../ ” on Mac and “cd …. ” on Windows and Mac. To go all the way to the root, we use:
cd / # Mac
cd # Windows
Navigating to Your Home Directory from the Command Line
On the Mac, the tilde symbol stands for the home folder of the current user. To get to that location, you can type in “cd ~”. On Windows, things are a bit more verbose. On Windows, the different drive letters look like “ C: “, “ E: “, etc. Your user home directory is probably in C: , so unless you moved to another drive using the command “ E: ” (for example), you can probably use the following to get home again:
cd %HOMEPATH%
What Is Command Line Interface/CLI?The command line, or command-line interface (CLI), is a text-based interface that allows you to type commands via lines of text that are then processed by the computer.
For example, you can find out how to utilize command line capability to rapidly rename several hundred files in a single directory, totally erase a drive, move through files and folders, or generate a list of all the programs installed on your computer – without ever using your mouse!
The command line interface (CLI) will vary depending on if you use Windows or Mac/Linux.
Michael mentions that many Linux and Mac systems utilize bash as the shell for the terminal, although all the commands are consistent across all Unix machines.
Michael would not suggest that someone learn the command line interface of Windows unless they are employed in IT or Penetration Testing. The way Windows operates its Command Line Interface is distinct from that of Linux and Mac’s CLI.
What is the Difference Between Shell, Bash, Terminal, and Command Prompt?
When discussing the command line, it is important to be familiar with a few related terms.
The expression “shell” is usually employed to allude to any type of command line interface. It’s the software that interprets and executes the commands. On Apple devices, the program for using the command line is called “Terminal”, whereas on Windows machines it is known as “Command Prompt”.
Bash is an instance of a shell program and is an acronym for “Bourne Again Shell.” It is usually the default type of shell found on Linux systems, however there are other varieties of shell available, such as ksh, tcsh, dash, and zsh.
What is the Difference Between the CLI and GUI?
Users are able to communicate with a computer system through both the Command Line Interface and the Graphical User Interface. However, there are a few differences.
As Michael explains,
When using programs with a graphical user interface (GUI), certain buttons are responsible for certain actions on the computer. The command line interface also allows you to do the same thing, but with much more accuracy and strength. When you type in some words and press the enter key, the shell will interpret those words and collaborate with the operating system kernel and records to carry out the order.
Okay, this may sound really confusing. So let me break it down.
Most people utilize the graphical user interface 95% of the time to command their computer. Here is how you can open the folder “myapp” on my desktop using the graphical user interface.
According to Google, a Kernel is:
The main element of most computer operating systems is the operating system itself; it serves as a link between the applications and the actual data processing that happens at the hardware level.
The bottom line is, both the Graphical User Interface and Command Line Interface are methods of interacting with your computer. When it comes to using the command line, it is speedier and more effective to move around your computer once you have learned the commands. That’s why many programmers prefer it.
You need to give your computer instructions via the command line if you want it to carry out any action. That is an additional explanation why coders should become knowledgeable in using it.
Why Should I Learn the Command Line?
Why is learning command line skills important?
According to Michael,
Many helpful applications work best when using the command line interface, and it is simpler to configure and execute them from this platform. An example of this is git, a popular software versioning system, which was specifically built for the command line (although it is likely that someone has created a graphical user interface for it). Using it makes working on the code base simpler and more efficient.
No matter if you have employed it or not, I’m certain many of you reading this have come across Github or Bitbucket previously. This is what Michael is talking about above. Many programmers, either by themselves or in a joint setting, upload their repository to either Github or Bitbucket. Git provides numerous other advantages and benefits.
Moreover, Michael explains:
The Command Line Interface gives you the ability to make modifications to the internal workings of your system and code in a more precise manner. A command line interface provides more adaptability and authority than a graphical user interface no matter what type of operating system is being utilized. If you are looking to use software from Github, you may need to execute certain commands in the command line in order for it to function correctly.
Essentially, it is imperative that you possess a rudimentary knowledge of the command line. To become a professional developer, one must be proficient in using the command line interface.
What Does the Command Line Look Like?
It is very straightforward to begin utilizing the command line with any type of computer, as they are all provided with it already set up on Windows, Linux, and Mac systems. No extra command line utilities are necessary, just your expertise!
To access the Command Prompt in Windows, begin by opening the Start menu, then select the Windows logo, scroll down to locate the Windows System folder, and finally click on Command Prompt.
To access the terminal on a Mac, press the Command and Space bar keys together to open the Spotlight search. Type in “terminal” and select the terminal application.
Grasping the elements of a command is the initial step in utilizing a command line interface. Coding with a command line interface involves a few components: an indication to start, an instruction to execute, an optional modifier, and an input value.
The beginning of each command line is marked by a prompt, which the terminal automatically provides and is usually finished with a $ or %. The command line that appears when you turn on your machine depends on your system’s specifications and can be changed if desired (though this requires an understanding of more complex concepts).
Instructions: This is where you give the computer directions on what to do. Some examples of commands include:
- cd: change directory
- open: open files
- mv: move a file
- mkdir: create a directory
- top: list actively running computer processes
- ditto: copy contents of folder to a new folder
Possibilities: Pieces of data that tailor how a command functions. For example, for the Linux ls (ls) command that allows you to view a list of files and folders in a given directory, options include:
- -l: shows permissions, the user who made it, and other details
- -a: shows hidden files
- -S: sort by file size, largest first
- -C: list entries by columns
A dispute could be the sequence of symbols you wish to display, or paths to files or directories. Not all programs need arguments or parameters.
Leave a Reply