Using the command line (Terminal)

Useful commands

Command Description
ls List files in the current folder (directory)
ls -l List files, show file size and modification time etc.
ls -a List files, show also hidden files (filenames starting with a dot)
ls -F List files, display directories with a trailing slash
pwd Print current working directory
Observe that / is used to separate the individual components (directory names) of the path
cd DIRECTORY Change working directory to DIRECTORY
cd .. Change working directory to parent directory
mkdir DIRECTORY Create new directory
rmdir DIRECTORY Remove (empty) directory
rm FILENAME Remove the file named FILENAME
rm -rf DIRECTORY Recursively remove the directory and all its contents. m( Be very careful! There is no "undo" for this!
cp FILENAME DESTINATION Copy the file named FILENAME file/directory to DESTINATION
mv SOURCE DESTINATION Move or rename the file or directory named SOURCE to DESTINATION.
If DESTINATION is the name of an existing directory, the source file/directory will be moved to the new directory and keep its current name.
If DESTINATION does not exist, the source file/directory will be renamed.
less FILENAME View the contents of the file. Press q to quit. See more shortcuts below.
nano FILENAME Edit the plaintext file (doesn't work for binary files). See more shortcuts below.
man COMMAND Show the help page for the given command. Same keyboard shortcuts as less.

Keyboard shortcuts for the command line

Ctrl+a means: Press the Control key, press and release the a key, then release Control.
Escape, b means: Press and release the Escape key, then press and release the b key.

Keyboard shortcut Function
Up/down arrows Navigate your command history
Left/right arrows Move cursor
TAB Filename completion
Ctrl+a Move cursor to start of line
Ctrl+e Move cursor to end of line
Escape, b Move cursor one word to the left (backwards)
Escape, f Move cursor one word to the right (forwards)
Escape, Backspace Delete from cursor to start of word
Delete previous word
Escape, d Delete from cursor to end of word
Delete next word
Ctrl+u Delete from cursor to start of line
Ctrl+k Delete from cursor to end of line
Ctrl+l Clear screen

Keyboard shortcuts for less

Keyboard shortcut Function
Space Jump one page forward
b Jump one page back
g Jump to beginning
G Jump to end
q Quit
/ Search forwards: Press / followed by your search term and press ENTER
n Jump to next occurrence of search string
N Jump to previous occurrence of search string

The search string can be a regular expression.

Keyboard shortcuts for nano

See full list at nano-editor.org.

Ctrl+x means: Press the Control key, press and release the x key, then release Control.

Keyboard shortcut Function
Ctrl+x Exit. If you have made changes, it asks if you want to save first.
Ctrl+s Save
Ctrl+o Save as
Ctrl+k Cut current line into cutbuffer
Ctrl+u Paste contents of cutbuffer
Ctrl+w Search

More on pathnames

The tilde (~) is an alias for your home directory, that can simplify navigating to the right directory: If your home directory is /Users/johndoe, these are two equivalent ways of referring to your Documents folder:

  • /Users/johndoe/Documents
  • ~/Documents

To navigate back to your home directory, you can either run cd ~, or just cd without any parameters.

Also note the difference between absolute paths and relative paths:

  • An absolute path always starts with a / or ~, and specifies the full location of a file/folder as observed from that computer. An absolute path is not constant across different computers.
  • All other paths are relative paths, which specify the location of a file/folder relative to your current working directory.
    If you change your current working directory with cd SOMETHING, a relative path will no longer refer to the same place, but given a specific directory these are still portable across different computers.

You should avoid using absolute paths in your code, this will make it difficult to run the code on a different computer:

  • On your laptop, your home directory will typically be /Users/<USERNAME> (macOS) or /home/<USERNAME> (Linux).
  • On our computing servers, your home directory will be somewhere else: Typically /home/ahomeX/Y/<USERNAME> or similar.
  • When connecting to your server home directory from your laptop, it will typically appear as /Volumes/<USERNAME> (macOS) or something completely different (Linux).
  • This means that if your program code is using absolute references when reading datasets or writing results, your computation will not run without modifications after being transferred from your laptop to our servers. Using relative references instead eliminates this problem.

Some examples:

  • If your code lives in /Users/johndoe/Documents/project3/ and wants to save output to a file in a results folder, for example /Users/johndoe/Documents/project3/results/output.csv,
  • Then you should refer to the output file as results/output.csv instead. (Or ./results/output.csv, which has the same meaning.)
  • Similarly: From the […]/project3/src/ folder, you can refer to the […]/project3/results/ folder as ../results (one level up from src/, then one level down again into results/).
  • These paths can be nested: From the /Users/johndoe/Documents/project/src/version2/ folder, the expression ../../output/sim2/ will refer to /Users/johndoe/Documents/project/output/sim2/ (the ../../output/sim2/ expression can be read as "first two levels up, then back into the output/ directory, and further in to the sim2/ subdirectory inside the output/ directory).

If you are using macOS, we recommend changing these settings:

  • FinderPreferences:
    • GeneralShow these items on the desktop: Tick the box Show Connected servers on the desktop.
      • This will make network drives appear as an icon on your desktop, similar to an external disk.
    • SidebarShow these items in the sidebar: Tick the box which contains your username and a picture of a house.
      • This will make your home directory appear among the bookmarks in the left column in Finder.
  • FinderView:
    • Enable Show Path Bar.
      • This will add a line to the bottom of each finder Window, showing the path of the current folder.
      • To copy the folder location as text, right-click on the folder in the Path Bar, then select Copy "…" as Pathname. This text can be pasted into Terminal or into your code editor.
      • To open a new Terminal window in a given folder, right-click the folder and select ServicesNew Terminal at Folder.
  • System PreferencesSecurity & PrivacyPrivacy
    • Add Terminal to the list of apps in the Full Disk Access section.
      • This will make all the contents of your home directory available to the Terminal window.
      • Without full disk access, you'll get a pop-up window the first time you navigate to your Documents folder, to a network folder, to your Downloads folder, etc., asking you to confirm that you want to allow Terminal to access these files.

Changing the look of the command prompt (macOS and Linux)

Especially on macOS, the default command prompt isn't very useful – it only shows the last path component of your current working directory.

To change the appearance of the command prompt so that it shows the current working directory, first check which shell you are using. This will print out a single line, usually saying /bin/bash or /bin/zsh.

echo $SHELL

Changing the bash prompt

Edit your .bashrc file in your home directory, for example by using nano:

nano ~/.bashrc

Add this line at the end:

PS1="\w \$ "

See the bash man page for other options (run man bash and search for ^PROMPTING).

Changing the zsh prompt

Edit your .zshrc file in your home directory, for example by using nano:

nano ~/.zshrc

Add this line at the end:

PROMPT="%~ %# "

See the zshall man page for other options (run man zshall and search for SIMPLE PROMPT ESCAPES).

Other useful stuff

  • Avoid special characters in filenames: Don't use /, \, $, {[<()>]}, space, ", ', :, …
  • Letters, digits, underscore, minus, and extra dots are OK.
  • In macOS, you can open files in graphical program from the command line with:
    • open FILENAME
      Open the file in its default graphical program.
    • open -a RStudio FILENAME
      Open the file in the RStudio program (regardless of which is the default program).
    • open .
      Open ".", the current folder, in Finder.
2022-10-27, Per Kristian Hove