A Beginner’s Guide to Bash
Part I — Where Am I?
Bash, the terminal, the console, the command line, command prompt, a shell, an emulator, an environment, this simple program goes by many names. Bash stands for the Bourne-Again Shell. As it was intended to replace an earlier program called the Bourne Shell, this is an extremely funny joke. Most programming acronyms are like this. Keep in mind that if you are not using a Unix-based operating system (Windows), your terminal will be called something else.
Like most things related to software and the internet, the development of Bash traces a long and complicated history that extends back past the graphical user interface and at some point was worked on in Bell Labs. We are not going to talk about any of that as we simply do not have the time.
For me the largest stumbling block in getting comfortable with bash was… what is it? Most of your experience with a computer probably consists of “point-and-click” or “drag-and-drop” interactions. You use the mouse just as much as you use the keyboard. There are lots of colors, it’s nice. You open an app, close the app, forget what you had just done and open that same app again. You want to find something? You open a Finder window. The graphical user interface has spoiled us.
So we often forget that these windows we are peering through are, at the end of the day, a metaphor. A metaphor for the digital architecture of our computers — all its computery insides that we don’t really need to know about. Bash is the same thing! Looking at the command line is the same as looking at a folder anywhere on your computer, just with the information presented in a different way.
You can do all the same stuff you would normally do, and quite a bit more. The terminal is useful for all the housekeeping tasks that are not actually writing code but are absolutely necessary to make it all work. The part that feels so alien at first is that there is only one way to interact with the shell — by typing commands.
Part II — The Command Line
Staring back at you like a blank page, mysterious, inscrutable, possibly blinking. The prompt gives you the feeling of infinite possibilities… but for what exactly? We need some context before we get to the commands themselves.
In the shell almost everything in your computer can be represented by a symbol. The period itself .
is commonly used, as it refers to the directory (folder) that you are currently in. It’s sort of a “you are here” marker which includes all the files and folders that your location contains. Two periods ..
is the directory immediately above where you are and is used to to go “up” in the directory structure. Tilde ~
is your home directory, i.e. the folder on the top level with your name on it. These will be important later so don’t forget them. Here is an example:
- iTunes Media
- Some folder
- Another folder we don't care about
- Music
- Alice Cooper
- Killer
- Love It To Death
- School's Out
- Big Star
- #1 Record
- Radio City
- Third/Sister Lovers
- etc.
You can customize your shell like crazy, which is another blog post entirely, but your prompt may include your current directory, which is very handy. Maybe something like:
~/Users/Music/iTunes/iTunes Media/Music:~$
for the purpose of brevity in this tutorial the command prompt will be represented by just
$
The folder structure is represented by slashes /
. Each slash indicates that the folder on the right is located inside of the item on the left. So in this case your current location .
is Music and ..
is iTunes Media. These both change meaning as they depend on your current location, but ~
is special, it always refers to your home directory.
Part III — The Commands
Commands are just that, a word or two which can be customized further with flags, which is a fancy name for options. I’m not going to beat around the bash — here’s what you absolutely need to know.
Navigation Commands
pwd
(print working directory) shows your current location
ls
(list) shows all files in current folder
cd
(change directory) allows you to navigate through your file structure. It’s useful when combined with other elements:
cd ~
= go to my home directory
cd ..
= go up one level in the file structure
Use ls
to see what folders you can navigate into from where you are. If you typed ls
in the example above the terminal would print out a list of all the artist folders:
Alice Cooper Big Star Creedence Clearwater Revival
David Bowie Elvis Presley Fleetwood Mac
etc.
And then you can use cd <directory_name>
to move into any of these folders. You can navigate to any existing folder, but unless this is a well travelled path for you it won’t be obvious what the folder names are unless you use ls
.
Keep in mind that cd
is relative to your current location. You cannot cd
into a folder if you are not currently in the folder that contains it. Using the directory example from above:
$ pwd
/Users/you/music/iTunes/iTunes Media
$ ls
Music
$ cd "Alice Cooper"
-bash: cd: Alice Cooper: No such file or directory
— but —
$ pwd
/Users/you/music/iTunes/iTunes Media
$ cd Music
$ cd "Alice Cooper"
$ pwd
/Users/you/music/iTunes/iTunes Media/Music/Alice Cooper
(yay)
You can also use a shortcut and include more than one directory in the path you specify:
$ cd Music
$ cd "Alice Cooper"
— is the same as —
$ cd Music/"Alice Cooper"
Adam’s Top Tips: Bash isn’t so good with spaces in folder and file names. One easy way to
cd
into a folder with a space in the name is to wrap the folder’s name in quotation marks. For example:cd “iTunes Media”
Creating and Manipulating Files
So far you are probably thinking this is all well and good but what can I do? I didn’t come here just to look at all my files, while I surf through directories at lightning speed.
touch <filename>
creates a new file in the current directory. It can also be use to update a file’s “last modified” timestamp.
mv
(move) moves file from current directory to one specified.
usage: mv <file_to_move> [<new_name>] [<new_directory_path>]
cp <filename>
(copy) copies a file in the current directory to one specified.
usage:cp <file_to_move> [<new_name>] [<new_directory_path>]
mkdir <folder_name>
(make directory) seems pretty self-explanatory.
rmdir <folder_name>
(remove directory) only works on empty folders.
rm <file_or_folder_name>
(remove) can remove files or directories
These commands are used in conjunction with file names — the files you are creating, modifying, or deleting. So for instance if you typed: $ touch hello.txt
a new (blank) file called hello.txt would be created in your current directory. mkdir
is the version of this that creates a new folder. This is the fun part. Especially if you also have this directory open in a separate Finder window, where you can see the file or folder pop into existence, it feels like magic!
Here is a new imaginary file structure:
- Sandbox
- Apples Folder
- apples.txt
- Oranges Folder
- oranges.txt
mv
can actually move or rename a file, depending on what you specify. If you typed $ mv apples.txt bananas.txt
the file apples.txt would be renamed to bananas.txt. However if you typed$ mv apples.txt ../”Oranges Folder”
(remember ..
goes up one level!) your new file structure would now look like this:
- Sandbox
- Apples Folder
- Oranges Folder
- apples.txt
- oranges.txt
cp
works in exactly the same way, except when you specify a new directory it will not remove the original file, just make a copy in the new location, with the original file’s contents.
$ cp oranges.txt oranges-copy.txt- Sandbox
- Apples Folder
- Oranges Folder
- apples.txt
- oranges.txt
- oranges-copy.txt
rm
will remove any file you specify. Keep in mind that bash assumes you know what you are doing. It will not ask you “are you sure?”, but will delete it immediately.
Be careful with
rm
!
rm
is the command you need to use if you want to delete a folder that has anything inside of it. However, while using it do to so, you could also delete your entire hard drive. I’m not even going to type it all out here.
Other Common Commands
open
opens a file in it’s default program.
usage: open <filename>
cat
(concatenate) prints the contents of a file to the terminal.
usage: cat <filename>
find
searches for specified files or folders.
usage: find <search_term>
Working with Documentation
help
opens a basic quick reference guide.
man
(manual) has more detailed entries for commands and other topics.
info
contains exhaustive documentation pages.
These all kind of sound like the same thing so it’s good to know the differences. Type $ help
to bring up a single plain list of available commands and options. Type $ help <command_name>
for a short blurb about a single command. Type $ man <command_or_topic>
for a more in-depth look at an individual command. Type $ info
for the top level of the INFO directory, or $ info <command_or_topic>
to jump directly to that section.
Find commands with the help
command, find topics by typing info
and scrolling down with the arrow key (remember no mice allowed). Press q
to quit and search that topic with the info
command. Even navigating the manual pages and INFO directory can get pretty hairy if you are not familiar with the interface.
Remember: press
q
to quit
Some particularly useful combinations for beginners:
$ info info (followed by)
$ h$ info bash
Press spacebar to advance the pages while in the INFO directory.
Part IV — Reference
Keyboard Shortcuts
ctrl + L
= clear screen
ctrl + S
= stop output to screen (but not running processes)
ctrl + C
= kill current terminal process. Useful for accidental infinite loops, long evals, etc.
ctrl + Z
= suspend current terminal process. Later you can use $ fg <process_name>
to resume
ctrl + D
= closes the shell
ctrl + XX
= swap between cursor position and beginning of line
ctrl + H
= delete previous character (same as delete key)
ctrl + T
= swap the previous two characters
alt + T
= swap the current and previous word
ctrl + B
= go back one character
alt + B
= go back one word
ctrl + F
= go forward one characters
alt + F
= go forward one word
ctrl + A
= move cursor to beginning of line
ctrl + E
= move cursor to end of line
ctrl + -
= undo last keypress
ctrl + W
= cut word before cursor
ctrl + U
= cut line before cursor
ctrl + K
= cut line after cursor
ctrl + Y
= paste last cut item
Basic Commands List
pwd
= shows your current location
ls
= shows all files in current folder
cd <directory>
= allows you to navigate through your file structure
touch <filename>
creates a new file in the current directory. It can also be use to update a file’s “last modified” timestamp
mv <filename>
= moves file from current directory to one specified
cp <filename>
= copies a file in the current directory to one specified
mkdir <folder_name>
= used to create folders
rmdir <folder_name>
= used to remove folders (empty only)
rm <file_or_folder_name>
= used to remove files
open
= opens a file in it’s default program
cat
= prints the contents of a file to the terminal
find
= searches for specified files or folders
help
= opens a basic quick reference guide
man
= has more detailed entries for commands and other topics
info
= contains exhaustive documentation pages
Obviously there is much, much more one can do in the shell, but hopefully this is enough to get started poking around and figuring things out. Good luck out there!