Provides a foundation by going over directory structure and basic file manipulation but will begin with a brief intro.
In order to form a basis for the upcoming lessons it's helpful to define what a programming shell is and go over some terminology. A shell is a program which exposes the OS and is called so because it's the outermost layer around it. The program can either be a GUI (like the mac or windows interface you are familiar with), or a command line interface (CLI). For these lessons the focus will be on the CLI that Unix and Linux provide. This interface is both an interactive command-line interpreter and means for executing scripts.
To understand what that means, think of the shell as being somewhat analogous to the javascript console found in web browsers' developer tools. The console provides a means of executing arbitrary Javascript and can be considered as a shell to the browser itself. It's safe to assume that these console themselves have been heavily inspired by Unix shells.
Using the shell is sometimes simply called bash, which is the most popular shell for Unix & Unix like OS's. There are other shells like bash's predecessor sh and new variants like zsh (which we installed as a prereq), fish, etc. Fun fact, the proper name for sh is the Bourne Shell and bash is short for Bourne-Again shell.
- Getting familiar with Unix directory structure and the difference between absolute and relative paths.
- Directory structure is at the heart of all shell programming and is relevant for almost every command
- Directories (ie folders) is how all your files are organized on your computer and can be thought of as a tree structure
- Examples of a valid directory are
/,/tmp,/Users/Shared, and/Library/Apple/usr/share/ - The root directory
/is the "trunk" of the directories and all other directories are children or its descendants - Each occurrence of
/in a path means the item on the right of the/is inside the directory to the left of/.- In the example
/Users/Sharedcan be read Shared is inside the directory Users
- In the example
- There is also a special directory denoted by the tilde
~character, which is the users respective home directory (more on this later) - When using a command either absolute or relative paths may be used.
- An absolute path is a path that contains the root directory
/ - A relative path is one that is respective of your current location in the file system.
- Ex1: you are presently in the
/Users/homedirectory that contains a file called "myfile" and then relative path to it is simplymyfile - Ex2: you are presently in the
/Users/homedirectory that contains a folder named/mystuffthat has a file called "myfile" and the relative path to the file ismystuff/myfile
- Ex1: you are presently in the
- When relative pathing is used there are two special characters that can be used
- A single dot or period
.means the current directory, therefore the relative pathsmystuffand./mystuffare equivalent - Two dots or periods
..means the parent directory, therefore if you are in the folder/Users/homethen..is the/Usersdirectory
- A single dot or period
- The current directory:
pwd - Change directory:
cd-
cd / -
cd /Users/[your username] -
cd ~and trypwdto confirm -
cd ..
-
- Use tab completion
-
cdand press tab twice then make a choice followed by tab twice again
-
- Make directory:
mkdircd ~mkdir mystuffcd mystuff
- Delete directory:
rmdir-
cd .. -
rmdir mystuff
-
- Your first file manipulation:
touchmkdir mystufftouch firstfiletouch mystuff/myfile
- List Directory:
lsls- list form:
ls -l touch ./myfile2- list in modified order:
ls -lt - list in reversed modified order:
ls -ltr
- Move:
mv-
mv myfile2 mystuff/myfile2 -
touch myfile3 -
mv myfile3 mystuff/ -
touch movefile -
mv movefile anotherfilename -
Note: will overwrite files
-
Example GIF

-
- Copy:
cpcp firstfile myfilecopymkdir newfoldercp myfilecopy newfolder/- Lets try with a mix of relative & absolute paths
cp firstfile ~/firstfilecopy- Note: ~/ is an absolute path and is a special short-hand way of typing your home directory
- Note: will overwrite files
- Remove:
rm-
rm anotherfilename -
touch filetodelete1 -
touch newfolder/filetodelete2 -
rm filetodelete1 newfolder/filetodelete2- Note: you can remove any number of files in a single command as you like
-
- Using Copy & Remove with the recursive (
-r) option:- Normally copy & remove only respect files
- Using the
-roption will allow you to manipulate directories as well cp -r newfolder mystuffls -ltr mystuff/- Recursive option is also available for
rm rm -rf mystuff/newfolder- Note: The
-finrmskips the confirmation prompt
- Using
mvon entire directories- Unlike
cp&rmit doesn't need any special options to move folders mv newfolder mystuff/ls -ltrls -ltr mystuff
- Unlike
- The wildcard character
*-
The wildcard character is a simple regular expression that matches any character
-
It can be used to manipulate parts of or entire contents of a directory
-
cp mystuff/my* . -
rm myf*
-




