📖 Deeper dive reading: MDN Command line crash course
Before the creation of graphical user interfaces, all computing systems were simple console environments consisting of a prompt for inputting a command and the display of the command output. All of the original programming tools ran as console applications. The console tradition is still actively used by professional developers and most programming tools execute within a console window.
Also known as the command line, shell, or terminal, the console window is an essential web development tool. The console provides access to the file system and allows for the execution of command line applications.
There are many console applications that you can choose from. All operating systems come with a default console, but you will probably want to install one in order to get the best experience. Here is an example of Warp running on a Mac.
In order for you to successfully use the console for web programming it must be POSIX compliant. POSIX compliance means that it supports a standard set of console commands. Both Mac and Linix support POSIX. That means any necessary console commands will work on those operating systems.
If you are using Microsoft Windows you can get a reasonable approximation of a POSIX compliant console by installing Git Bash. When installing, use all the default settings.
Once it is installed, hit the Windows button on your keyboard, and type Git-Bash. This should open a console window that looks something like the following. Make sure you look for the Git Bash icon so that you don't accidentally use an non-POSIX compatible console application.
Caution
Do not use Git Command, cmd, or Powershell. That will not work for the commands and scripts we will run in this class.
You can use Windows Subsystem for Linux (WSL) (e.g Ubuntu on Windows) for your work in this class, but you must be very careful to do all of your work actually within WSL. Don't download files to your Windows partition and then access them from WSL. It is suggested that you use Git Bash instead of WSL, unless you are really familiar with WSL.
Once you have an acceptable console application on your development computer, open it up and make sure you can run a simple POSIX compliant command such as printf 'hello\n'.
If this displays hello then you are on the right track. If that doesn't work then you are using a console application that is not POSIX compliant. For example, Windows Powershell will not work.
One of the primary purposes of a console application is to view the files on the computer. The files on a computer are organized into a tree structure of nodes called directories. At any given point in time your console is located at one of the directories in the file system. You can see which directory you are in with the pwd (present working directory) command.
➜ pwd
/Users/student/byu//webprogramming260You can list all of the files in the directory with ls (list files). Most command line applications take parameters that are specified after you type the application name. For example, ls can list all files (even hidden ones) in a long format if you provide the parameter -la.
➜ ls -la
total 16
-rw-r--r-- 1 lee staff 1.0K Nov 19 08:47 LICENSE
-rw-r--r-- 1 lee staff 82B Nov 19 08:47 README.md
drwxr-xr-x 4 lee staff 128B Nov 19 08:48 profile
drwxr-xr-x 4 lee staff 128B Nov 19 08:47 reactThe other primary purpose of the console is to execute commands. You already did this in the previous section when you executed commands for working with the file system. However, console commands can perform many different operations. Here are some basic commands that you should experiment with.
- echo - Output the parameters of the command
- cd - Change directory
- mkdir - Make directory
- rmdir - Remove an empty directory
- rm - Remove file(s)
- mv - Move file(s)
- cp - Copy files
- ls - List files
- curl - Command line client URL browser
- grep - Regular expression search
- find - Find files
- top - View running processes with CPU and memory usage
- df - View disk statistics
- cat - Output the contents of a file
- less - Interactively output the contents of a file
- wc - Count the words in a file
- ps - View the currently running processes
- kill - Kill a currently running process
- sudo - Execute a command as a super user (admin)
- ssh - Create a secure shell on a remote computer
- scp - Securely copy files to a remote computer
- history - Show the history of commands
- ping - Check if a website is up
- tracert - Trace the connections to a website
- dig - Show the DNS information for a domain
- man - Look up a command in the manual
You can also chain the input and output of commands using special characters
|- Take the output from the command on the left and pipe, or pass, it to the command on the right>- Redirect output to a file. Overwrites the file if it exists>>- Redirect output to a file. Appends if the file exists<- Pass the input on the right to the program on the left
For example, you can list the files in a directory, pipe it into grep to search for files created in Nov, and then pipe that into wc to count the number of files found with a date of Nov.
ls -l | grep ' Nov ' | wc -l
There are also keystrokes that have special meaning in the console.
CTRL-R- Use type ahead to find previous commandsCTRL-C- Kill the currently running command
Experiment with console commands. Open a console window and create a test directory that you can play with by executing the command mkdir test (make directory). Then execute the command cd test to change into that directory. Then execute the following sequence of commands:
mkdir test
cd test
pwd
ls
ls -la
ls ../
printf "x" > test.txt
ls -l
cat test.txt
cp test.txt other.txt
ls
cat test.txt >> other.txt
rm test.txt
less other.txt # press `q` to exit less
for i in {1..2}; do printf 'y' >> other.txt; done;If your section of this course requires that you submit assignments for grading: Submit in the Canvas assignment the text that results from running cat other.txt.
The better you get at working with the console the more efficient your programming will be.


