--- title: "Learn Basic Command Line Skills and Rock Your Workflow" date: "2022-04-19" categories: - "learn-to-code" coverImage: "matrix_command_line.jpg" --- # Learn Basic Command Line Skills and Rock Your Workflow I firmly believe that every software developer should know how to use the command line. At the same time that this is my position, it feels strange to have to advocate for it since the alternative of ignoring the command prompt altogether is a very recent development. For us dinosaurs born in the last century, the command line _was_ the computer's interface, so using a computer without knowing the command line wasn't an option. My first computer didn't come with a mouse. The user interface looked like this: ```bash A:\ ``` It wasn't a badge of honor to learn the command line back then -- any more than being able to swipe a phone makes you a rock star today. It was just an essential skill you needed to use the device. Things are different now, of course. Most of the programmers I work with today grew up with graphical user interfaces: Windows, macOS, and even Linux all feature excellent interfaces that make my old A:\\ prompt look as quaint as a rotary dial phone. However, for all the convenience of windowing interfaces, I've only met two developers during a long career who were utterly ignorant of how to get around in a command line (or "terminal," as it's also called). Most developers are also quite adept at using a computer generally, including knowing their way around a terminal. In the sections that follow, we'll begin with a list of core command-line skills that we want to cover. For completeness, I'll discuss both the Windows "`cmd`" version of the command line and the terminals like bash. You'll notice that I'm not planning to cover Windows PowerShell. Though it's quite powerful and certainly worthy of discussion if you're planning a career as a Windows system administrator, I don't know it well enough to do it justice. ## The Core Command Line Skills Here are what I consider the top command-line skills you should master: - 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. With that list to guide us as a broad outline, let's begin a whirlwind tour of the basics. There are many types of command terminals available to us, and because of this, we're going to have to be a bit selective about what we cover. For Mac and Linux, I will discuss commands that will generally work on two popular shells: bash and zsh. There are some subtle differences between the two, but we won't need to discuss the differences in much detail, especially at the beginner level. On Windows, these same "bash" commands will also work on WSL (Windows Subsystem for Linux) and in git bash. I'll also discuss some basics of the Windows command-line interface, "cmd." For serious work in the terminal on Windows, however, I recommend using git bash. WSL is also quite good, but it makes integrating other Windows programs a bit more complicated. ## Starting a Terminal Session ### Opening a Command Prompt From Your Computer To get to a command line terminal, you should have by default, from the Windows search bar, you can type "`cmd`" to locate the "Command Prompt" application. Running this will open a command-line terminal. ![](/images/basic-command-line-skills-to-rock-your-development-world/CmdClip-1024x898.png) On the Mac, use Launchpad and search for "term" to find and run the terminal program, as shown here below: (You probably won't see ITerm, unless you've installed it separately). ![](/images/basic-command-line-skills-to-rock-your-development-world/TerminalCip.jpg) On Linux, you'd also search for "Terminal," but the way you locate programs depends on the distribution. One of the first things I do on a new computer once I've run a command line terminal for the first time is to right-click on the icon of the running terminal, then select "Pin to taskbar" (Windows) or "Options / Keep in Dock" (Mac). This way, I'll always have a terminal program handy. ### Opening a Command Prompt From Your Editor or IDE It's also a good idea to understand how to open a command-line terminal from your editor or IDE. In Visual Studio Code, you can reach for the Terminal menu item, or from the keyboard, use CTRL-SHIFT-\`. (That's a "backtick" key, typically above the tab key). In PyCharm, you'll reach for Alt-F12 on Windows or Option-F12 on the Mac. You can also get there through the much longer menu sequence "View / Tool Windows / Terminal." In Jupyter Lab, you can open a terminal via the menu sequence "File / New / Terminal." As far as I know, you don't have a separate terminal available in Jupyter Notebook. ## Navigating the File System from the Command Line ### Where Am I? Generally speaking, if you open a terminal from your computer, your initial location or "current working directory" will be your user's home directory. Your prompt may be set up to show you where you are, but if it isn't, in CMD on Windows, you can use "cd" to print the current working directory. You're likely to see "C:\\Users\\YourUserName." On Linux/Mac, you would use "pwd" and you're likely to see "/Users/YourUserName". From now on, I'll try to simply refer to "the Mac" to mean Mac and Linux (as well as git bash and other bash environments on Windows). In an IDE or editor, your terminal will probably open up where your project is located or where you opened your editor. Again, however, you can use `pwd` on the Mac or `cd` on Windows to print out your current working directory. ### Creating Folders and Moving Around Let's begin by creating a folder, moving into it, and then moving out again. The following sequence works in all environments: ```bash # Make a directory mkdir myfolder # Move into it cd myfolder # Move back cd .. ``` On Windows "`cmd`"it's possible to substitute the shorter "`md`" command for "`mkdir`". Other than that, so far, things have been the same, cd ("Change directory") moves into a directory by name. ".." represents the parent directory, so if we start in /User/me and create a folder there called "myfolder," navigating into it lands us in /User/me/myfolder, and "`cd ..`" gets us back where we started. Paths are separated differently in Windows and on Linux or Mac. We use a backslash, "\\", on Windows and a forward slash, "/" on a Mac. To navigate to our "grandparent" directory, so to speak, we'd use "`cd ../../`" on Mac and "cd ..\\..\\" on Windows and Mac. To go all the way to the root, we use: ```bash cd / # Mac cd \ # Windows ``` ### Navigating to Your Home Directory from the Command Line On the Mac, the tilde character is an alias for the current user's home directory. So to go there, you can use "`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: ```bash cd %HOMEPATH% ``` ## Working With Environment Variables and Setting the Path The operating system on your computer launches a command-line terminal program (or any other program) with a default set of environment variables. These are key-value pairs that store important information. When we used the command cd %HOMEPATH% in the last section on Windows, we said, in effect, "change directory to directory stored in the environment variable called HOMEPATH." One of the most important environment variables is the "PATH" variable, which is set to a list of directories, separated either by colons (Mac) or semi-colons (Windows). Generally speaking, outside of some system defaults, you can run a program easily if it's located on your PATH. Otherwise, you have to type the whole path to the program to run it, and terminal-wielding cool kids like us HATE typing long paths. To find out if a program is runnable, you can generally try something like this on any system: ```bash program-name --version ``` You should get information about the program if it's available; if not, you'll get an error. Given a non-existent program, "badprogram," on Windows, you'll see: ```bash 'badprogram' is not a recognized internal or external command, operable program or batch file ``` ```bash zsh: command not found: badprogram ``` If you know that the program is installed, you'll need to check the online documentation for it to see where it gets installed and add the directory where it lives to the PATH variable. Note that this is not the full path to the executable file itself, just the full path to the directory where it lives. ### Adding a Path to the User Path on Windows To add a path to the system path on Windows, follow these steps, run the Control Panel system administration app by typing this command in the terminal: ```bash control sysdm.cpl ``` In the Window that opens, click the Advanced tab: ![](/images/basic-command-line-skills-to-rock-your-development-world/ControlPanel.png) Next, click on environment variables, which will bring up an editor. ![](/images/basic-command-line-skills-to-rock-your-development-world/ControlPanel2.png) Here you can search for the "Path" variable underneath the User variables section to edit it. Clicking edit will bring up a list editing window, where you can click New to enter your new path. ![](/images/basic-command-line-skills-to-rock-your-development-world/ControlPanel3.png) After you've added your new path to this list, click OK on this dialog box and all the ones behind it to save your changes. You'll also need to open up a new terminal to pick up the changes as a final step. ### Adding a Path to the User's Path on Mac or Linux On Mac or Linux, you can modify the path where programs are loaded for the user by editing a user's profile file. The name of that file depends on the shell you're using. For newer Mac machines using the zsh shell, the file will be located at ~/.zprofile. For older Macs and on Linux running the bash shell, the file is at ~/.bash\_profile. In either case, you can add a single directory named "/my/new/path" to the user's PATH variable by adding a line like this: ``` export PATH="$PATH:/my/new/path" ``` You can then save the file and then either open a new terminal or use the following command to reload the file's contents into the current shell environment: ``` source ~/.zprofile # For example ``` By the way, that $PATH is the way to get the PATH variable's value (or any other named environment variable) on Linux. Remember that on Windows, the equivalent would be %PATH%. Therefore, these commands will show you the whole PATH on either system: ``` # Mac: echo $PATH # Windows echo %PATH% ``` If you want to see all the environment variables that are currently set in the terminal, use "`env`" on a Mac and "`set`" on Windows. Knowing how to set the PATH variable is a crucial skill for making the programs you need available to you in the terminal. Some install utilities will set things up correctly for you -- but many others won't. By the way, if you already know you have access to a program but need to find out where it's coming from, you can use "`which program-name`" on the Mac or "`where program-name`" on Windows. For example, here's some info about where I'm running Python as of today: ```bash % which python /Users/johnlockwood/.pyenv/shims/python ``` ## Exploring Folders and Files and Their Contents Let's create and manipulate some files and folders to get an idea of how we would do this at the command line. First, to make sure we're looking at the same files, let's create a directory and go to it: ```bash mkdir files cd files ``` At this point, we won't have any files to look at, but let's try the command to see what would happen in that case. On Windows, use `dir` (directory). You'll see some output like this. ```bash C:\Users\CodeS\files>dir Volume in drive C is Windows Volume Serial Number is 3885-2FC1 Directory of C:\Users\CodeS\files 04/16/2022 10:49 AM
Task | Mac / Linux | Windows |
Create a directory | mkdir name | mkdir name md name |
Move to a directory underneath this one | cd name | cd name |
Navigate to "home directory." | cd ~ | cd %HOMEPATH% cd %HOMEDRIVE%%HOMEPATH% |
Print the contents of a text file | cat filename | type filename |
Print something to the screen | echo something | echo something |
Print something to a file (appending) | echo something > filename | echo something > filename |
Copy a file | cp source target | xcopy source target |
Copy a set of files recursively from one directory to another | cp -R source/* target | xcopy /s/e/k/c/h source\* target |
Move a file or directory | mv source target | move source target |
Getting help | command-name /? | command-name --help man command-name |