The Command line
In the context of using a computer, the term interact simply means to exchange information or even simpler, send and receive information. Essentially, a computer sending data to you and you receive it. In turn, you also send some data to your computer and the computer receives it.
Computers have various input and output devices. The input devices include a keyboard, mouse, microphone, camera, touch-sensitive devices, and so on. The output devices are things like speakers, monitors, headsets, and haptic devices to name just a few.
You use all these devices to send data to a computer and receive data from it. But there’s something else that supports communication with your devices. These are graphical user interfaces or GUIs which facilitate your interactions.
GUIs are popular because they require very little training to use. GUIs offer an easy way to interact with devices, but they also somewhat limit the scope of human-computer interaction.
The command line is a very powerful alternative because it allows developers to perform tasks quicker and with enough experience, less potential for errors.
To use this powerful tool effectively, you need to have a certain level of knowledge. You might feel that the learning curve for the command line is a bit steep, but take it from me, the payoff is definitely worth it.
By learning just a few commands, you can perform various tasks, such as
- creating new directories,
- creating new files,
- combining directories,
- copying and moving files around different directories,
- and searching through files using various criteria and keywords.
As you become more advanced in using the command line, you will be able to perform tasks such as
- track software,
- access and control remote servers,
- search for files using specific criteria,
- unzip archives,
- access software manuals and display them in the command line
- install, upgrade, and uninstall software,
- and mount and unmount computer drives,
- or check disk space and so on.
Pretty advanced stuff, don’t you think? But the list goes on. You can
- write scripts to automate various tasks,
- control user access to files and programs,
- stop, start and restart programs
- create aliases of only a few characters long to initiate very long commands,
- download files from the Internet,
- run various software, and finally,
- run and control self-contained virtual software, which is also known as containerization.
Some basic commands to get you started
cd
Which stands for change directory.
This is used to point our command line to a specific directory.
I will point the command line to the desktop of my computer.
You will move out of the current directory and back into the parent directory.
touch
Which makes a new file of whatever type you specify.
mkdir
Make new folders using the mkdir
command.
history
To view a history of the most recently typed commands, you can use the history command.
What are Unix commands?
You interact with your phone and computer through a graphic user interface or GUI, which is just a layer above underlying commands that tells the device what to do.
Developers, however, need to know how to use specific commands to perform various types of tasks.
Having a grip on UNIX commands specifically is a great skill to have in today’s software development world.
NOTE
The majority of companies run their platforms on the Cloud, and 90 percent of these systems run on a platform called Linux.
Unix & Linux
Unix preceded Linux and was developed by Ken Thompson and Dennis Ritchie and team at AT&T Labs in 1969.
Linux came much later on and was originally developed as a hobby by Linus Torvalds, hence the name Linux.
Using the command line could seem a little intimidating at first, but you will quickly learn that Unix commands are simply a layer below the normal actions, such as opening file directories or renaming files.
Using the command line could seem a little intimidating at first, but you will quickly learn that Unix commands are simply a layer below the normal actions, such as opening file directories or renaming files. Windows, for example, became the dominant desktop operating system, mainly due to it’s easier to use GUI. Windows allowed non-technical users to perform tasks without having to learn a list of commands. But you, as an aspiring developer, will get to use Unix commands to develop those tasks.
NOTE
Each command has a set of helper instructions.
These helpers give detailed information about how the commands can be run and how something we call flags can be passed. One of these helpers is the man command.
man
Man is short for manual, and when called against a command, it will display a detailed manual of instructions for that given command.
We can also use something called flags in conjunction with Unix commands.
Flags
Flags are used to modify the behavior of a command. Think of them as options that can either change or extend the functionality of the given command.
cd
or change directory command is used to move from different directories of the file system.ls
is used to show the contents of the current working directory. The ls command can accept many different types of flags that will change what is returned in the response. For example,ls -l
, lists the file out, in list order, and shows the read or write permissions, owners and groups it belongs to.ls -a
, on the other hand, will list all files and directories including hidden ones.- The
pwd
or print working directory command shows the full path of the current working directory. - The copy or
cp
command copies files or folders from one destination to another. - The
mv
, move command, moves files from one directory to another.
Using Bash on Mac Terminal
Learning Objectives
- Learners will understand how to open the command line - terminal on Mac.
- Learners will become familiar with the most common commands.
Mac Terminal
The Terminal on Mac can be opened in one of three ways: Finder, Launch Pad and Spotlight.
Finder
- Scroll to the bottom of your desktop and click on the Finder icon.
- Click on Applications on the left-hand side of the screen.
- Locate the folder called Utilities and expand it.
- The Terminal app should be visible, select it to open.
Launch Pad
- Press the
F4
command. - Launch Pad view appears onscreen.
- Select the search bar and type
Term
(short forTerminal
). - The
Terminal
icon appears onscreen. - Select the icon to open.
Spotlight
- Press the command key and the space bar.
- The Spotlight modal appears.
- Type in the word Terminal or Term for short.
- The Terminal icon appears onscreen.
- Select the icon to open the Terminal.
Bash commands
Bash provides a list of commands for navigating through files, viewing the contents of files, and edit features for changing or updating the contents of a file. Below is a list of the most common commands:
Command | Used for |
---|---|
cd | Change Directory |
ls | List command used for showing the content of a directory. |
rm | Remove command used for removing a file or a directory |
mv | Used to move files or folders to another location |
touch | Allows creating of a new empty file or to update a timestamp on a file |
cp | Used to make a copy of a file or folder |
mkdir | Make a new directory |
pwd | Print work directory, shows the current location in the shell |
cat | Allows reading or concatenation of a file |
less | Displays the contents of a file one page at a time. |
grep | Global regular expression, allows for searching contents of files or folders |
Flags
Every bash command has flags for changing the output of the command itself. For example, the ls command prints out the list of contents inside a directory. If we wanted to show the list in a different view, we simply need to add a flag such as -l.
When the flag of -l is passed, it will show the output differently:
Man Pages
When first learning commands from bash, it can feel a bit daunting. Luckily, every command has its manual (or man pages for short). The man page lists all the flags and options that a particular command has to offer. Again, let’s use the ls command to demonstrate this. Type the following: man ls
The man pages are a great way to recall the different flags that are available and a great tool in your arsenal to becoming more fluent in bash.
Editing
There are many options for editing files in bash. The most common is usually VI or Vim. VI stands for visual editor. It’s used for making edits and changes to a file and saving them. It’s similar to what you may have used in applications like Word. VIM is a better version of VI with some improvements - hence its name: visual editor improved. Learning the commands in Vim will feel different from GUI applications, but once you practice, it will feel like second nature. Vim uses modes to determine the commands you can work with:
- Normal mode: Default mode
- Insert mode: Allows the contents of the files to be edited.
- Command line mode: Normal commands begin with :
Using Bash on Windows
Using Git Bash on Windows
The bashRC file is mainly for configurations. It is essentially a script file that’s executed when you first open the terminal window.
What’s in there will be configurations for the shell itself, for example, the types of colors that I’m using. I can also add in things around my shell history, like how much history of previous commands I want to store and so on.
Any configuration options that I put in here will be executed when the terminal session begins.
I press the Q key to exit the less environment.
This tends to be used more for environment variables. For example, I can use it for setting environment variables for my Java home directory or my Python home directory or whatever is needed during development.
Again, I press the Q key to exit.
Creating a shell script
This let’s to the operating system know that this is a bash script.
Press Escape
to get out of insert mode. Then I type :wq!
to save the file. Press Enter
.
WARNING
The other thing to notice is that this file can’t be run at the moment. In other words, it’s not executable. It’s just a read write file.
But I want it to be executable, which requires that I have an x being set on it.
In order to do that, I have to use another command which is called chmod
. After using this command, I type in the type of permissions that I want. I type in 755.
Running the file
Now you notice the words hello world are printed out on the screen.
Change directories and list contents
Link file
Always represented by l
Directory
Represented by d
and that means it is just a standard directory and you can change directory and open it.
Standard file
The association or symbol for it is the -
(hyphen).
The root
s written above, means “The owner” and “The group” that it’s associated to.
Pipes
I can check the content of a file by running another command called cat
.
Another command is the word count command, which is abbreviated as wc
.
The w flag tells the WC command to return the word count.
Pipes
Pipes allow you to pass the output from one command as the input to another.
You can also combine this command against the directory or multiple files.
Redirection
The basic workflow of any Linux command is that it takes an input and gives an output. The standard input device is the keyboard. The standard output device is the screen.
Redirection
With redirection, you can change the standard input and/or output.
There are three types of IO or input/output redirections:
- standard input
- standard output
- standard error
The shell keeps a reference of standard input, output, and error by a numbering system.
- The zero is for standard input
- one is for standard output
- two is for standard error
Standard input
Taking input normally refers to a user typing information from the keyboard. We use the less than sign <
for user input.
The cat command can be used to record user input and save it to a file. This command is actually set up to take in input.
Press Control D
to tell the cat command that’s the end of the file.
For output:
Standard output
A lot of the commands we have already used, for example, ls, send their output to a special file called the standard output
. Output direction is handled with a greater than sign >
. IO allows us to use redirection to control where the output goes.
NOTE
Everything in Unix, Linux is a file. This means every time you run a command like ls and press Enter, it sends the output of the file to an stdout file.
In Linux, if you want to control where the output goes, you can use a redirection.
There is no output.txt yet, but i will be built with this command.
Standard error
When using redirection, you also have to specify that the error should be written to a file. You can do that by explicitly setting the number 2 before the output arrow 2>
. And you can also combine it with the standard output of two greater than Ampersand 1 2>&1
, to print both the standard output and error if any occurs.
It may happen that an error occurs when you are outputting data to a text file. Remember that the error will not correspond with the output stream. It will change to use the error stream, which is represented by two.
Instead of using a directory that we know exists, I’m going to use one that doesn’t exist.
It prints the error on the screen.
Now the error is printed in error.txt.
If you want to handle both cases where it may find data or may not find data, you can pass in a different redirection. It handles each one, both for output and for error.
Grep
grep
Grep stands for global regular expression print, and it’s used for searching across files and folders as well as the contents of files.
WARNING
Keep in mind that Grep is case sensitive, which means if I run the same query with a lower case S, it returns a completely different set of results.
Fortunately I can pass in a flag to ignore case sensitivity.
We could also do an exact match by passing in a different flag, and that’s the -W
. Which means it’ll match exactly what I’m looking for.
Lastly, I can use a pipe command to combine different searches for Grep itself.
Additional Resources
Agile methodologies
https://www.planview.com/resources/guide/agile-methodologies-a-beginners-guide/
Installing git on mac and windows, detailed instructions.
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
Bash Reference Manual
https://www.gnu.org/software/bash/manual/html_node/index.html#SEC_Contents
Bash Redirections
https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Redirections
Bash Cheatsheet
Grep Cheatsheet
Grep Manual
https://man7.org/linux/man-pages/man1/grep.1.html
History and Timeline of Unix
https://unix.org/what_is_unix/history_timeline.html
History of Vim
https://en.wikipedia.org/wiki/Vim_(text_editor)
How to work with relative and absolute paths
https://www.geeksforgeeks.org/absolute-relative-pathnames-unix/
Unix Commands Cheatsheet
https://cheatography.com/jluis/cheat-sheets/bash-and-unix-commands/
Vim Cheatsheet
Previous one → 1.Introduction to Version Control | Next one → 3.Git and Github