Unix Basics
What is Unix?
Unix refers to a family of proprietary operating system.
Different from Windows which presents a graphical user interface (GUI), Unix uses a command line interface (CLI) where you type commands you want to run into a terminal.
Unix was designed to be used by many users at the same time.
Command line interpreters are called shells (two commonly used ones are bash [Bourne-again shell] and zsh [Z-shell])
Most neuroimaging packages need to be run from a command line
How to run Unix commands
To use Unix commands on Windows, you would need to install a Terminal emulator (e.g. Git for Windows), or to enable Windows Subsystem for Linux (WSL) feature.
Your default shell is the Terminal program. To locate Terminal, click the Launchpad icon in the Dock, enter “Terminal” in the search field, and click on Terminal. A user guide for Terminal is available here.
You already have Terminal installed, so you can open a terminal directly and start typing commands.
Command Structure
A command typically consists of the following four components:
Note
You can practice using Unix online with JupyterLab (see https://neuroimaging-core-docs.readthedocs.io/en/latest/pages/unix.html for an excellent tutorial).
An excellent tutorial on the use of the Unix Shell is available here.
Basic Unix Commands
manDisplay the manual page of a given commandpwdPrint Working Directory, tells you the current directory you are incd <directory>Change Directory, takes to you the directory specifiedcd ~Go to home directorycd /Go to root directorycd ..Go to the parent directorycd -Go to the last directorylsList, lists the contents of the directory you are inls <directory>Lists contents of the specified directoryls -aLists all files and directories in the directory you are in (including hidden ones which start with an “.” and don’t appear is you just you the “ls” command)ls -FAnnotate different types of files, directories (ended with “") and links (ended with “@”)ls -lLong list, lists all files and directory with ownership and user permissionsls -alLong list (including hidden files and directories)ls -tSort files by last-modified timels -rReverse the order of the sortls -hDisplay file sizes in human readable formatmkdir <name>Creates a directory with that namemkdir .<name>Creates a hidden directory with that namehistoryLists all recently run commands!<number>Re-runs command specified (by number from history list)cp -r <from> <to>Copies a directory from one path to another pathmv <from> <to>Move a file to a new location, or rename a file or directory to a different namerm <file>Deletes a filerm -rf <directory>Deletes a directorylnCreate a link to an existing file or directoryln -sCreate a symbolic (or soft) link that can point to a path of a file or directorycatConcatenate the contents of the given files, or simply to view the content of a single filediff <file1> <file2>Compare two files and mark the lin numbers where they are different. Can also compare two different directories.
Tip
Tab completion
Pressing the “Tab” key can auto-complete commands, files and directories or, when multiple entries are identified, suggest possible options. This would allow faster typing and is less likely to introduce typos.
Important Unix Directories
/binWhere built-in Unix commands (e.g.ls,mkdir, etc…) are stored./etcWhere system profiles are stored (e.g. users and passwords)./usr/local/binWhere user-installed programmes are often stored, unless user specifies a different install location