Skip to content

Shell Basics

Simply put, a shell is a program that takes commands from the keyboard and gives them to the operating system to perform. In the old days, it was the only user interface available on a Unix-like system such as Linux. Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such as the shell. Nevertheless, CLIs are still often used, because they make it easier to see and understand what the computer is doing on your behalf.

On most Linux systems, a program called bash (which stands for Bourne Again Shell, an enhanced version of the original Unix shell program, sh, written by Steve Bourne) acts as the shell program. Bash is an sh-compatible command language interpreter that executes commands read from the standard input in a shell terminal or from a file.

Bash incorporates many useful functions: * Output can be redirected using > and <. * Commands can be combined using pipelines - “|”. * More options can be applied for one command (i.e. ls -l and ls -a to ls -la) * The “history” command can be used to view and repeat previous operations, while tab completion can be used to save re-typing. * Use grep to find elements in files and to find files themselves. * Programs can be paused, run in the background, or run on remote machines. * The shell has variables like any other program and these can be used to control how it behaves.

Remember the UNIX/Linux command line is case sensitive!

Helpful commands

Exploration

pwd               # "Print working directory"; show your current path
ls                # "List" contents of current directory
ls -l             # Similar to ls, but provides additional info on files and directories
ls -a             # List all files, including hidden files (.name) as well
ls -R             # Lists subdirectories recursively
ls -t             # Lists files in chronological order
cd <dir_name>     # "Change directory" to specified path
cd                # Brings you to your home directory
cd ~              # Also bring you to your home directory
cd ..             # Moves one directory up
cd ../../         # Moves two directories up (and so on)
cd -              # Go back to you were previously (before the last directory change)

Files and Directories

mkdir <dir_name>     # Creates specified directory
rmdir <dir_name>     # Removes empty directory
rm <file_name>       # Removes file_name
rm -r <dir_name>     # Removes directory including its contents, but asks for confirmation
rm -rf <dir_name>    # Same as above, but turns confirmation off – use with caution
cp <name> <path>     # Copy file/directory as specified in path (-r to include content in directories)
mv <name1> <name2>   # Renames directories or files
mv <name> <path>     # Moves file/directory as specified in path

Help

Not all commands have help documentation available, however, one of these methods will likely work:

help <COMMAND>     # Show help for a Bash command
man <COMMAND>      # Show the manual page for a program (press the 'q' key to exit)
<COMMAND> --help   # Show help documentation for command
<COMMAND> -h       # Show help documentation for command

Shortcuts

Command History * ↑ # Up arrow key scrolls backwards through command history * ↓ # Down arrow key scrolls forwards through command history * history # Shows all commands you have used recently

Auto-completion

The tab (⇥) key auto completes commands or file names if there is only one option. Hitting the tab (⇥) key twice will list multiple options. Keep in mind that there are no spaces between the tab (⇥) keys and the partial names of commands or files.

Show all directories under my home that I can cd into:

cd ~/⇥⇥

Show all files that I can ls with names that start with “myfile”:

ls myfile⇥⇥

Show all commands that I can run with names that start with “sp”:

sp⇥⇥