Note
- References: Much of the content of these sessions is summarized at our Command line quick reference page.
- Credit: Our materials are based on the Software Carpentry Unix Shell course
- For the exercises: Download the repository files
We will work with the same setup as the Software Carpentry lessons.
- Most of the time we work with a device, we use a graphical user interface – made of windows, icons, etc. with which we typically interact using a combination of mouse+keyboard or by touching the screen.
- Many personal assistants, such as home assistants, operate via voice interfaces: commands are executed by voice and responses are provided through audio.
- There is now a lot of discussion about brain-computer interfaces.
- Maybe many other types of interfaces will be developed in the future...
- ... but before we get carried away with our hypotheses, we would like to go back to the "basics": the text-based interface, typically referred to as the command line or the shell.
-
The shell is a program that runs other programs.
- The shell is still a program, which takes input and gives output. The input is a command, though, so it seems as though we’re doing something different. In reality, using command line is no different than using any other program.
-
There are shells that use a GUI (graphical user interface), for example the Windows Shell, and shells that use a CLI (command-line interface), like cmd in Windows.
-
The command-line-based window that runs a shell is called a console or a terminal.
-
The Unix philosophy is that you can pipe (chain) together small commands, each of which does one thing well, to do something complex. You can’t do this in a GUI.
-
bash is a very popular shell on Unix-like operating systems, like Linux and macOS, but we can also install it on Windows.
-
bash = ‘Bourne again shell’ (the original Bourne shell is sh; others include csh, ksh, tcsh, zsh).
-
Learn the shell on a need-to-know basis.
- There are commands you’ll use every day, some you’ll use for special purposes (and you’ll look up how they work when you need them), and some that you’ll never need.
-
There are a lot of tools available
- Some of these tools do not have GUIs, so the only way to use them is by learning the command line.
- Some of these tools have GUIs, but these are sometimes more limited in functionality and less reliable.
- e.g., exiftool (for reading and editing metadata in images), grep (regular expressions), git (we'll learn about git in the next lesson), pandoc (for converting between a lot of formats).
-
Those tools can be combined and thus provide a lot of flexibility.
-
It has a very high action-to-keystroke ratio
- i.e., you can perform much with just little typing.
-
You can automate most of the boring stuff
- e.g., how to write down a list of all file names in a folder? or how to rename 1000 files?
-
It is the main way of interacting with remote machines
- for example, when you have to connect to the server of your university.
-
It can be very useful for DIY/maker projects (e.g., if you work with Arduino/Raspberry Pi).
-
And a lof of other tasks...
- Installing Python packages (e.g.
pip install pandas) - Installing software (especially when this involves Docker: see e.g. the instructions for Editing Tools for Digital Epigraphy)
- Installing Python packages (e.g.
-
Finally, shell scripts can be a great introduction to programming languages
The standard shell on macOS, starting with macOS Catalina (10.15), is zsh (Z shell). There are some differences compared to bash (some of them listed here), but none that affect us during these lessons. You can still use bash by simply typing
bashin the command line, or you can change the default shell by following these instructions. Not sure which shell you're using? Typeecho $SHELLat the command prompt.
- When you downloaded Git, you also downloaded Git bash (screenshot), the command line interface.
- You may have been using
cmd.exe(the so-called Command Prompt: screenshot) or Windows PowerShell (screenshot), both native to Windows. Though each has its own benefits and drawbacks, for the purposes of this course we will have all Windows users learn and use Gitbash.Git bash vs. Windows command lines vs. regular bash
- The Git
bashadopts a Unix-like convention for paths: it uses/as the path separator, and absolute paths start with/c/,/d/, etc. (So,/c/Users/usernameinstead ofC:\Users\usernameincmd.)- Git
bashcomes with a lot of commands, but some others that are usually available on Unix systems are missing, e.g.,nano,sudo, andman.
nanois a text editor. You can substitute Notepad, which you can run usingnotepad myfile.txt.sudoallows you to change features that require elevated administrative permissions. (If you ever get to try it, use it with caution.)mandisplays a command manual. You can look up commands on line at https://manpages.ubuntu.com.- Git
bashrequires the use ofwinptywhen running certain Windows applications that are meant to run withincmd. For example, to run an interactive Python session, you should executewinpty python.
Tip
Launching the shell
- For macOS: the Terminal.app that you will find in the Applications → Utilities folder
- For Ubuntu Desktop: you can hit
Ctrl-Alt-Tor you can typeTerminalinto the Search box. You can also right-click on an empty part of a window in the file explorer (Nautilus) and select "Open in Terminal". - For Windows go to the start menu and run Git bash
The corresponding software carpentry lesson is 1. Introducing the Shell.
-
The prompt:
- bash on Windows:
username@yourpc MINGW64 ~ $ - zsh on macOS:
username@yourpc ~ % - bash on Ubuntu Desktop:
username@yourpc:~$
- bash on Windows:
-
A first simple command:
whoami -
If you write someting it does not know, it tells you so
-
The shell is case sensitive
-
Check what is your current working directory:
pwd- What you get is the path (i.e., position in the file system) of the directory
- Directory is another way of saying folder
- Folder focuses more on the graphical metaphor, while directory is the more technical term
-
Go to your home directory:
cd ~orcd -
Go to Desktop (provided that the Desktop directory is in the current directory):
cd Desktop -
How do we check what is in this directory?
ls -
Options:
-r,--reverse,-t,-l,-h -
Distinguish between files, directories, etc.:
ls -F(option-Fwill add the following characters after pathnames:/for directories,*for executable files,@symbolic links etc.)
Command + options (with -) + arguments
For example: ls -F exercise-data
- Options are also called flags or switches
- In some cases, options can be expressed in a longer form, with a double hyphen (
--) - You can also combine options together (e.g.,
ls -l -horls -lh) - Some options require an argument immediately after them
- In general, options are case-sensitive (
ls -sis different fromls -S)
For example ls:
ls --help(only on Linux)man lswhatis ls(short description)
Resources on the web:
- Command line reference: https://ss64.com/bash/
- Or just google the command (followed by
unix,terminal, or the like)
How to navigate in `man` pages
| Key | Action |
|---|---|
| space | forward one window |
| b | backward one window |
| d | forward one half-window |
| u | backward one half-window |
| g | go to first line |
| G | go to last line |
| /pattern | search forward for pattern |
| ?pattern | search backward for pattern |
| n/N | next/previous search result |
| q | exit |
The corresponding Software Carpentry lessons are 2. Navigating Files and Directories and 3. Working with Files and Directories.
- Absolute path:
/Users/mamo/Desktop/shell-lesson-data(starts with a slash, which means "root directory", i.e. uppermost directory of the filesystem)- This is the one you get when you type
pwd
- This is the one you get when you type
- Relative path
shell-lesson-data(starting from/Users/mamo/Desktop/)Desktop/shell-lesson-data(starting fromUsers/mamo/Desktop)..(starting from/Users/mamo/Desktop/shell-lesson-data/exercise-data)../shell-lesson-data(if we imagine to start from another directory contained in/Users/mamo/Desktop/)
- Command and filename completion with the
Tabkeytab: 1) Filename completion, 2) Command completion
- Command history with the arrow keys
- Some useful shortcuts:
- Delete the whole line:
Ctrl+u - Move to beginning of line:
Ctrl+a - Move to end of line:
Ctrl+e Option + click(Mac only)
- Delete the whole line:
- If the screen has become a bit cluttered:
clear(orCtrl+l) history(up and down arrows)!580(execute the 580th command from the history)!!(execute the previous command)!$: last word of last commandCtrl+r: initiate (or continue) history search
cd = change directory
cd ~(go to home directory)cd ..(go to parent directory)cd ../..(go up two directories)cd -(go back to previous directory; like "Back" button in file explorer)cd /(go to root directory; in Git bash the installation folder will act as root)cd; cd Desktop(cdwill change to the home directory)cd data-shell/data/s...(tab completion)cd /Users/djb/Desktop/shell-lesson-data/exercise-data(absolute path; it has a leading slash = root directory)
ls = listing
ls -a: include hidden files (filenames starting with.)ls -l: show enhanced file information, including date and time stamps, owner and group, permissionsls -t: list in timestamp orderls -r: list in reverse order (try to combine it with-t)ls -G: colored outputls -lh: human readable file sizels -F: decorate filenames according to filetypels -1: single-columnls -d: don’t recurse into directoriesls -d */: list only directoriesls -R: recursive listing inside directories
mkdir: make directory- What's a good name for a directory? And more in general: what's a good filename?
- Avoid spaces (otherwise you'll be forced to use quotes
"..."for arguments or backslashes\) - Avoid beginning hyphens, otherwise file and directory names might be mistaken for options/flags
- Avoid spaces (otherwise you'll be forced to use quotes
- What's a good directory structure for a project?
- What's a good name for a directory? And more in general: what's a good filename?
mkdir -p a/b/c: create intermediate directoriesrmdir: remove empty directoryrm -rf:remove directory and its contents recursively (careful!)rm -ri: remove directory asking for confirmation (for single files)cp: copycp oldfile newfilecopy a filecp oldfile1 oldfile2 newdirectory: copy multiple filescp -r olddirectory newdirectorycopy directory recursively
mv: rename / move- Rename a file or directory
- Move a file or directory to a different location (optionally rename)
- Be careful! It will automatically overwrite files with the same name, unless you add
-ito make it interactive (will ask for permission)
- An easy way to create an empty file:
touch test.txt rm: delete (careful: deletion is forever!)rm -i: delete after asking permission
- The file system is responsible for managing information on the disk.
- Information is stored in files, which are stored in directories (folders).
- Directories can also store other directories, which form a directory tree.
cd pathchanges the current working directory.ls pathprints a listing of a specific file or directory;lson its own lists the current working directory.pwdprints the user’s current working directory.whoamishows the user’s current identity./on its own is the root directory of the whole file system.- A relative path specifies a location starting from the current location.
- An absolute path specifies a location from the root of the file system.
- Directory names in a path are separated with
/on Unix (including macOS) ..means ‘the parent directory = the directory above the current one’.on its own means ‘the current directory’. Why would we need this?- Most filenames have conventional extensions:
.txt,.xml, etc. - Most commands take options (flags) which begin with a
-.
- If you type a command and find yourself on a blank line with nothing happening: you typed an incomplete command, and should abort it with
Ctrl+c. - When you see
$(shell prompt): the shell is waiting for you to provide input. - When you type
$: you’re beginning to type a variable name. - When you see
>(shell continuation prompt): you’ve started entering multiple-line input, and the shell is waiting for the next line. Abort if this not what you meant to do.- To enter a multiple-line command, use the backslash
\at the end of every line except the last one.
- To enter a multiple-line command, use the backslash
- When you type
>: you’re writing output into a file, instead of displaying it on the screen. More on this in Command Line 2.
