Unix and Linux CheatSheet
GeneralGo to video for this category
lowercase-italics, <generic item type>, [optional item], keyboard shortcuts
Throughout this CheatSheet we will use the below symbols and conventions to document commands:
lowercase-italics Words in italics indicate the item is a user provided variable. You must substitute the appropriate names or values.
Example:
help command_name The user enters the specific command name they want help on.
help mkdir mkdir is the command name to get help on.
< > Angle brackets contain a generic item type. You must substitute it with the appropriate values. The angle brackets themselves are not typed.
Example:
find <path> -name <filename> The user enters the path, and the filename.
find /users/steve/documents -name myfile.txt Replace path with the path to the documents directory. Replace filename, with the file to find.
[ ] Square brackets contain optional items. The square brackets themselves are not typed.
Example:
command [-options] [arguments] The options and arguments are optional
ls Here I execute the ls command with no options or arguments
ls /users/steve/documents Here I execute the ls command with the path argument.
ls -a /users/steve/documents Here I execute the ls command with the -a option and the path argument.
| Items separated by a single vertical bar are alternative items. You can select only one of the group of items separated by single vertical bars. The vertical bar itself is not typed.
Example:
--color | colour For the color option, you can enter color or colour.
Note: don't confuse this with the Unix pipe symbol.
Keyboard Shortcuts:
lowercase-italics Words in italics indicate the item is a user provided variable. You must substitute the appropriate names or values.
Example:
help command_name The user enters the specific command name they want help on.
help mkdir mkdir is the command name to get help on.
< > Angle brackets contain a generic item type. You must substitute it with the appropriate values. The angle brackets themselves are not typed.
Example:
find <path> -name <filename> The user enters the path, and the filename.
find /users/steve/documents -name myfile.txt Replace path with the path to the documents directory. Replace filename, with the file to find.
[ ] Square brackets contain optional items. The square brackets themselves are not typed.
Example:
command [-options] [arguments] The options and arguments are optional
ls Here I execute the ls command with no options or arguments
ls /users/steve/documents Here I execute the ls command with the path argument.
ls -a /users/steve/documents Here I execute the ls command with the -a option and the path argument.
| Items separated by a single vertical bar are alternative items. You can select only one of the group of items separated by single vertical bars. The vertical bar itself is not typed.
Example:
--color | colour For the color option, you can enter color or colour.
Note: don't confuse this with the Unix pipe symbol.
Keyboard Shortcuts:
- Mac keyboard shortcuts generally use the Command key.
- Windows keyboard shortcuts generally use the Command key.
- By convention, keyboard commands are written like Command+C. This means to press the Command key and the lower case "c" key simultaneously.
- Mac and Windows usually, but not always, use the same key combinations for the shortcuts, so they could be expressed as commandOrControl+C.
Terms
Unix, kernel, UNIX trademark, POSIX, Unix-like, Linux, Darwin/Apple
- Unix: A family of operating systems first published by AT&T's Bell Labs in 1971.
- Characteristics:
- Kernel: A centralized operating system "kernel" manages system and process activities.
- Modular: Non-kernel programs are organized into modular kernel-managed processes.
- Shell: Has a "shell" that acts as an interface between the user and the kernel.
- Has a hierarchical file system with the root
/
at then top. - Built-in networking (TCP/IP is standard).
- Everything is a file. Programs are executed from binary files. Scripts are run from script files. Etc.
- Multiuser: Multiple users can log in and use the OS at the same time.
- Multitasking: Multiple programs or processes can run simultaneously (thread management).
- Portable: Uses a high-level language, which along with its modularity makes it flexible and modifiable to adapt to multiple types of hardware and implementations.
- Unix timeline diagram: Wikimedia.org:Unix_timeline
- Unix is written almost entirely in C.
- Kernel: The central part of an operating system. It manages the core tasks of the computer including memory and CPU time, and I/O devices like keyboard, mouse, monitor, hard drive, printer. A computer user never interacts directly with the kernel. It runs behind the scenes.
- Critical kernel code is loaded on startup to a protected section of computer memory called the kernel space. Applications that are launched occupy a different area of memory called user space.
- Different Unix variants use different kernel designs. A microkernel only runs key processes in the kernel space and runs everything else in the user space (for modularity). A monolith kernel runs all the operating system instructions in the kernel space (for speed). Most commercial OSs use a hybrid kernel that is like a microkernel with some additional processes included in the kernel space for performance reasons.
- UNIX trademark and Single UNIX Specification: UNIX is a trademarked name. The UNIX business and trademark was sold by AT&T to Novell in 1993, who transferred the UNIX trademark and certification rights to a neutral industry consortium now called the Open Group. The Open Group publishes a standard called the Single UNIX Specification (SUS). They offer a compliance check and certification (there's an initial and annual fee) that allows companies to use the UNIX trademark. Some prominent OSs that are compliant and can therefor use the UNIX trademark include macOS, HP-UX (HP Enterprise), AIX (IBM), and Solaris (Sun/Oracle).
- POSIX (Portable Operating System Interface): A family of standards for Unix-like operating systems to ensure compatibility. It is specified by the IEEE and is a major (but not only) component of the Single UNIX Specification.
- Unix-like operating systems: Unofficial umbrella term for operating systems that behave like a Unix system, whether or not they are certified as following the Single UNIX Specification. They are fully or mostly POSIX compliant.
- Market share: All mainframe computers. Nearly all smartphones (used in Android and iOS). Approx 70% of servers. PCs/laptops: MacOS and about 2% of the rest. Windows is the main competitor in servers and the dominant OS for PCs.
- Linux: A family of open source Unix-like operating systems built around the Linux kernel.
- Linux kernel: A reimplementation of the Unix kernel from scratch. Created by Linus Torvalds and initially released in 1991.
- Linux is the leading Unix-like OS apart from macOS (Darwin). Android uses a Linux kernel.
- Darwin: A free open-source Unix-like operating system released by Apple in 2000. It makes up the core components of macOS and iOS. It uses a hybrid kernel called XNU. It is derived from the BDS variant of Unix. It is mostly POSIX compliant, while the full macOS is both POSIX and Single UNIX Specification compliant.
Shell
Definitions: shell, command-line interface CLI, terminal, console, bash, zsh, ssh.
- Use a shell application, like Bash or zsh, to execute commands on a Unix or Linux operating system. You will generally open a terminal application such as Terminal on Mac that emulates the shell and provides a command-line interface where you type in commands.
- There are differences between the below terms but you can often use them interchangeably. Ex: "Enter the command in the shell/the CLI/the terminal/the console/Bash."
- Shell: A command-line interpreter program that executes commands on the operating system's services. It can use a graphical user interface (GUI) but the term is more commonly used for a command-line interface (CLI). It is called a shell because it is the outermost layer around the operating system kernel.
- REPL (Read-Eval-Print-Loop): Another term used for a language shell, used explicitly for Database shells like psql, the Node.js shell and others. The name refers to the nature of the program. It reads user typed in commands, evaluates them, prints the response to the terminal, then returns a prompt waiting for the user to type in another command.
- Command-line interface (CLI): Interface for entering text-based commands. It provides a prompt such as $ that tells you it is ready for you to enter a command. Commands are sent by pressing enter.
- Terminal: Old days: a monitor with a keyboard that connected to a mainframe (i.e., a thin client). Nowadays: a GUI application that emulates a terminal for interacting with the shell. It provides a command-line interface, as well as GUI features like tabs, copy/paste, etc. It doesn't process the input though. That is done by the shell.
- Console: Essentially the same thing as terminal.
- GNU Bash / Bash: An open source Unix command-line shell.
- It is the default command shell for most Linux distributions and for MacOS up to Mojave. It can also be downloaded on Windows machines. You access it through a terminal application.
- It is POSIX-compliant with a number of extensions.
- Bash is a replacement for the Bourne shell. Bash is an acronym for Bourne-again shell.
- Z shell: An open source Unix command-line shell.
- Used in MacOS starting with the Catalina OS, at least in part because it has a less restrictive license (MIT instead of GNU). Mostly compatible with Bash.
- Secure Shell (SSH): A protocol for securely connecting to a remote computer/server over a network or over the internet. Includes login and encryption. An example is if you use a web hosting server for your website, you would access it over the internet from the command line using SSH. Commonly uses port 22.
Command Syntax
command [-options] [arguments] Optional parameters: options, arguments.
- Command: The command name comes first.
- Options (aka flags): Modify how the command does its job.
- -<character> Unix options use single dash and one character.
- -<char1><char2> or -<char1> -<char2> Apply multiple options.
- --option-keyword Some Unix versions allow long options.
- Arguments: Pass information to the program you want to run. Typically zero, one, or multiple arguments specify the data upon which the program will perform an action.
Examples
The ls list command:
Long Option example: Node version
- ls The list command lists the items in the current directory.
- ls -l ls has a number of options you can add to it. Applying the -l long option will return extra information about each item including the permissions, owner, size, date last modified, and name.
- ls -lh Applying the -h human readable option will show the item size as B bytes, K kilobytes, M megabytes, or G gigabytes which make them easier to read. You can add both options individually ls -l -h but it is more common to combine them after a single dash.
- ls -lha Applying the -a option will show all items including hidden files that start with a dot.
- ls -lha ~/Documents ls works on the current directory by default. If you want to show the items in a different directory you need to add the path as an argument. To show the items in the Documents directory add the absolute or relative path as an argument.
- ls -lha ~/Documents ~/Desktop A command can have multiple arguments. To show the items in both the Documents and Desktop directory list them both as arguments separated by a space.
Long Option example: Node version
- Assuming Node.js is installed on your machine, the node command you launches the node program.
- The version option returns the version of the node program instead of launching it. Version has both a short and long option:
- node -v
- node --version
Case: Unix/Linux is case sensitive. Generally use lower case for everything.
Quotes: Either single or double quotes around strings: echo 'Hello'; echo "Hello"Interpolation: Must be in double quotes echo "My user name is $USER"
Single line Comments: # comment goes here
When typing a command:
enter/return Enter the command. Cursor can be anywhere on the line you want to enter.left | right arrow Move the cursor one character to the left or right.
Ctrl+A Move the cursor to the beginning of the line.
Ctrl+E Move the cursor to the end of the line.
up | down arrow Up gets the previously entered command. Down goes back down.
partial command + tab Unix tries to guess the rest of the command or filename.
Multiple commands on same line:
cmd1 && cmd2 Separate commands with && to run sequentially.cmd1; cmd2 Or separate commands with semicolons.
Example
Put two commands on one line: Change directories into the Documents directory, then list the contents of the directory.
- cd Documents && ls
- cd Documents; ls
Help / Manual
man command_name Display the online manual page for a command.
Man uses the less paging program:
Man uses the less paging program:
- Navigate: enter | downArrow down one line, upArrow up one line
u scroll up half the screen, d scroll down half the screen.
f | space forward one screen, b back one screen, g to start, G to end.
- Search: /search-term, n next instance, N previous instance
- Quit: q
help Display shell name and version and all the builtin shell commands.
help command_name Display info about a command.
Useful Commands
Terminal keyboard shortcuts:
Cmd+N Open new terminal window.
Cmd+T Open new terminal tab.
Cmd+F Find.
Cmd+C Copy selected text.
Cmd+V Paste.
Cmd+K Clear the screen. All previous lines removed.
Unix commands:
history List of recent commands.
clear Clear screen by scrolling to last line without removing previous lines.
User/Group/Host Info:
echo arg Returns the argument(s). echo 'Hello'
date Display the date and time.
cal Display a calendar. Specific year: cal 1982. Specific month: cal 01 2020
Run Other Shell Programs in the Terminal:
program_name Start the program
Ctrl+C Close an operation in the program (without exiting the program).
Ctrl+D Exit the program.
program_name --version Returns the version of the program if it is installed.
which program_name Returns the executable file path which is called by the command.
Cmd+N Open new terminal window.
Cmd+T Open new terminal tab.
Cmd+F Find.
Cmd+C Copy selected text.
Cmd+V Paste.
Cmd+K Clear the screen. All previous lines removed.
Unix commands:
history List of recent commands.
clear Clear screen by scrolling to last line without removing previous lines.
User/Group/Host Info:
whoami Returns the current logged in user's id.
id -p Returns the current logged in user's id and groups.
users Lists the user ids of all users on the system.
uname Name of the operating system.
hostname Name of the current host system.
Utilities:id -p Returns the current logged in user's id and groups.
users Lists the user ids of all users on the system.
uname Name of the operating system.
hostname Name of the current host system.
echo arg Returns the argument(s). echo 'Hello'
Hello
. echo $USER User_id
date Display the date and time.
cal Display a calendar. Specific year: cal 1982. Specific month: cal 01 2020
Run Other Shell Programs in the Terminal:
program_name Start the program
Ctrl+C Close an operation in the program (without exiting the program).
Ctrl+D Exit the program.
program_name --version Returns the version of the program if it is installed.
which program_name Returns the executable file path which is called by the command.
File SystemGo to video for this category
The Unix file system is like an upside down tree with the root at the top and branches going down.
Directories
pwd Present working directory: Returns the absolute path of the directory you are in.
/ Root directory
~ | $HOME Home directory (on Mac: /Users/username)
./ the current directory
Make Directories:
mkdir directory_name Create directory
mkdir existing/path/directory_name Create directory in a lower directory.
mkdir -p newdir1/newdir2/newdir3 Create a whole directory path.
Move Directory (and all its subdirectories and files):
mv directory_name new_location Down one level.
Copy Directory:
cp directory_name new_location Down one level.
cp -r directory_name new_location With all its subdirectories and files recursively.
Rename Directory:
mv old_dirname new_dirname "Move" directory to same location w/ different name.
Remove Directories: BE CAREFUL!
rmdir directory_name | rm -d directory_name Remove empty directory.
rmdir directory1 directory2 Remove multiple empty directories.
rmdir -p subdir/subsubdir Remove whole path of directories. Must be empty.
rm -r directory_name removes directory all its files and subdirectories recursively./ Root directory
~ | $HOME Home directory (on Mac: /Users/username)
./ the current directory
Make Directories:
mkdir directory_name Create directory
mkdir existing/path/directory_name Create directory in a lower directory.
mkdir -p newdir1/newdir2/newdir3 Create a whole directory path.
Move Directory (and all its subdirectories and files):
mv directory_name new_location Down one level.
Copy Directory:
cp directory_name new_location Down one level.
cp -r directory_name new_location With all its subdirectories and files recursively.
Rename Directory:
mv old_dirname new_dirname "Move" directory to same location w/ different name.
Remove Directories: BE CAREFUL!
rmdir directory_name | rm -d directory_name Remove empty directory.
rmdir directory1 directory2 Remove multiple empty directories.
rmdir -p subdir/subsubdir Remove whole path of directories. Must be empty.
Change Directories
Absolute path: starts from the root directory /.
Relative path: starts from your present working directory.
cd path Change directory to the path argument.Relative path: starts from your present working directory.
./ is the current directory.
cd .. Up one directory.
cd ../../path/to/directory_name Up two directories then down three.
cd /path Start from the root directory.
cd ~ | cd $HOME | cd Go to user's home directory (on Mac: /Users/username).
cd ~/path Start from user's home directory.
cd - Go back to the last directory you were in.
cd ../../path/to/directory_name Up two directories then down three.
cd /path Start from the root directory.
cd ~ | cd $HOME | cd Go to user's home directory (on Mac: /Users/username).
cd ~/path Start from user's home directory.
cd - Go back to the last directory you were in.
Forward vs backslashes, folder names with spaces, c: optional, case sensitivity
- While Windows allows you to use either forward slash or backslash, Linux only allows forward slash. As such you may want to use only forward slash as a habit.
- If a folder has a space in it, you must put it in quotes: /"program files"/directoryname
- Including c: when referencing the root directory in Windows is optional.
- Case sensitivity: Unix is case sensitive, but case sensitivity for directory and file names depends on the underlying operating system's file system. MacOS's file system, for instance, is not case sensitive.
Files
Create file:
rm filename
touch filename Create new file. If file already exists, changes the last update date.
code filename Open file in your default text editor.
Move file:
mv filename subdirectory Down one directory.
mv filename .. Up one directory.
mv filename ../subdirectory Up one then down a different directory.
mv file1 file2 file3 subdirectory Move multiple files.
mv * subdirectory Move all files (except hidden files)
Copy file:
cp filename different_filename Copy to same directory with other name.
cp filename sub_directory Copy to directory one level down.
Rename file:
mv old_filename new_filename "Move" file to same directory with different name.
Remove file: BE CAREFUL!code filename Open file in your default text editor.
Move file:
mv filename subdirectory Down one directory.
mv filename .. Up one directory.
mv filename ../subdirectory Up one then down a different directory.
mv file1 file2 file3 subdirectory Move multiple files.
mv * subdirectory Move all files (except hidden files)
Copy file:
cp filename different_filename Copy to same directory with other name.
cp filename sub_directory Copy to directory one level down.
Rename file:
mv old_filename new_filename "Move" file to same directory with different name.
rm filename
rm file1 file2 Multiple files.
rm * All files in the directory.
Options:
- -d Directory option removes directories as well as files.
- -r Recursive option removes all subdirectories and files. No warning messages.
- -i Interactive. Requires confirmation on each file regardless of file permissions.
- -f Force. Removes files w/o prompting confirmation regardless of permissions.
List Files and Directories
ls List all files and directories in the current directory.
ls path List all files & directories in the given path.
Options:
ls path List all files & directories in the given path.
Options:
- ls -1 Show 1 file or directory per line.
- ls -p Distinguish directories by appending a / after the name.
- ls -d List the given directory only.
- ls -a | ls -A All files & directories including hidden files (with dot in front of them).
- ls -S Sort by file size highest to lowest.
- ls -l Long version. Shows permissions, ownership, size on the files & directories.
- ls -lSh -l Long version, -S sort by size, -h human readable file sizes.
- ls -l filename Long version on a specific file or directory.
Show/hide system files
In Unix, system files start with a dot and are hidden.
In the Mac Finder program you can press Cmd+Shift+. to show or hide the system files.
In the Mac Finder program you can press Cmd+Shift+. to show or hide the system files.
Find
find <path> -name <filename> Find filename in or below the provided path.
Wildcard characters can be used in the filename:
Wildcard characters can be used in the filename:
- * 0, 1, or more characters.
- ? Any 1 character.
- [] Any character in the brackets.
Examples
- Find file named myfile.txt anywhere in or below the current directory:
- Find file named myfile.txt anywhere in the file system:
- Find file named myfile.txt anywhere in your home directory:
- Find files ending in .txt anywhere in or below the current directory:
- Find files named myfile with any three character extension:
Permissions | OwnershipGo to video for this category
View Permissions
ls -l path Shows file or directory permissions and ownership.
Each file or directory is listed on a separate line with the following info:
-rwxrwxrwx ownerName groupName fileSize dateModified fileName
-rw-r--r-- Steve Staff 103000 Jan 1 202X myfile.txt
The first column is 10 characters.
- First character identifies type:
-
regular file,d
directory - Next 9 characters: 3 Permissions classes containing three character positions each.
- Permission classes: owner, group, and other.
- Each class has three character positions indicating permissions:
r
readable,-
not readable.w
writable,-
not writable.x
file is executable or directory is traversable,-
not executable/traversable
Rest of the columns
- Owner name (usually you)
- Group name
- File size in bytes
- Date of last modification (and time if less than 6 months ago)
- File name
Change Permissions
Mode Numbers: 7=
rwx
, 6=rw-
, 5=r-x
, 4=r--
, 3=-wx
, 2=-w-
, 1=--x
, 0=---
chmod 751 file1 file2 file3 Change permissions for the given files.
- Don't give executable access to non-executable files.
chmod -R 700 subdir Change permissions recursively for directory and its children.
Examples
chmod 644 file1 file2 Change permission to the default:chmod 751 executable_file Changes permissions to:chmod 700 subdir Changes permissions to:
- Owner class, Mode 6. Owner has
rw-
read and write permission.
- Group class, Mode 4. Group has
r--
read only permission.
- Others class, mode 4. Others (not Owner or Group member) have
r--
read permission.
- Owner class, Mode 7. Owner has
rwx
read, write, and execute permission.
- Group class, Mode 5. Group has
r-x
read and execute permission.
- Others class, mode 1. Others (not Owner or Group member) have
--x
execute permission.
- Owner class, Mode 7. Owner has
rwx
read, write, and traverse permission.
- Group class, Mode 0. Group has
---
no access.
- Others class, mode 0. Others (not Owner or Group member) have
---
no access.
MacOS Permissions
Default permissions: non-executable files: 644, executables: 755, directories: 755
- Use Mac's default permissions unless there is a strong reason to modify.
In addition to using chmod to change permissions you can do it in Finder:
- Open Finder > Right Click on a file or directory > Get Info > Sharing & Permissions: Change the Privilege value.
- Owner class is listed first, then group, then others.
Change Ownership
chown username path Changes the owner for the given path (file or directory).
chown username:groupname path Changes the owner and group for the given path.
chown -R username directory Changes directory owner and its children recursively.
sudo chown username path Add sudo if root permission is required.
chown username:groupname path Changes the owner and group for the given path.
chown -R username directory Changes directory owner and its children recursively.
sudo chown username path Add sudo if root permission is required.
Ownership on macOS
- Home directory
/users/yourUsername
- Owner: the signed in Mac user.
- Group: staff, is regular macOS users.
- Root directory
/
- The root directory holds system files and subdirectories not meant to be accessed directly by the user.
- Owner: Root, which is the system administrator user.
- Group: Wheel, which is the system administrator group. Root is the only member of the wheel group.
- If you have to do something that requires wheel permission, use the sudo command.
- Admin
- Admin users have write access to the applications folder.
- Admin users are put in both the admin and staff groups.
- To see if you have admin permission: Open Apple menu > System Preferences > Users & Groups > Find your username on the left panel. If it says Admin under your username, you have Admin permissions.
Change Group
groups username Returns all groups a user is a member of.
groups returns current user's groups.
chgrp groupname path change group ownership of the path (file or directory).
groups returns current user's groups.
chgrp groupname path change group ownership of the path (file or directory).
Sudo
Accessing some system files and directories requires root user access. Preface these commands with sudo (superuser do). Sudo is a Unix/Linux command that allows programs to be executed as a superuser (i.e., root user).sudo command Ex: sudo mkdir /dirname
More info
- When you use sudo to run a command as root you will be asked to enter your password.
- To use sudo you must be on a special list, kept in the file /etc/sudoers. It can only be changed by the superuser.
- On a Mac, users with admin privileges (members of the admin group) can use the sudo command. This includes the computer owner.
Environment Variables | AliasGo to video for this category
Startup File
Put environment variables (including PATH), aliases, functions, etc. in a startup file.
Bash Shell:
Z shell:
To make file changes effective immediately (instead of having to restart the shell):
source ~/.filename such as: source ~/.zshrc
Bash Shell:
~/.bash_profile
(loads when you log in) or ~/.bashrc
(loads when you open Bash shell).Z shell:
~/.zshenv
or ~/.zshrc
To make file changes effective immediately (instead of having to restart the shell):
source ~/.filename such as: source ~/.zshrc
Details
Overview:
MacOS:
- Startup file gets loaded on login.
- Files are in the Home directory
~/
. - Bash will look for a startup file in the home directory named bash_profile, bash_login, or profile in that order, and open the first one it encounters.
- Zsh Ref: Zsh:Startup_files
MacOS:
- To persist environment variables, aliases, etc. that you want to access from the shell, put them in the startup file.
- Hidden files are prefixed with a dot and are not shown in Finder by default.
- Cmd+Shift+. Show/hide hidden files and folders in Finder.
- Mojave and earlier: Uses Bash Shell
- Unlike other Bash systems, Mac's Terminal application reloads the .bash_profile every time a new window or tab is opened.
- Create startup file: touch ~/.bash_profile.
- Catalina and later: Uses zsh Shell.
- Create startup file: touch ~/.zprofile.
Most other systems use two files:
- Startup file: Loaded on login but not reloaded when a new shell tab is opened.
- Put commands you want to execute on log in. Include environment variables.
- File name depending on the system: .bash_profile, .bash_login, .profile, .zprofile
- Environment file:
- Put commands you want executed every time a new shell starts. Include shell options, aliases and functions.
- File name depending on the system: .bashrc, .zshrc
Variables
Variable: a name-value pair stored in memory. The variable name is an identifier used to reference the value.
Syntax:
- Characters: Can contain letters, numbers and underscores, but can only start with a letter or underscore.
- Case: Uppercase name is not required but is the convention.
- Quotes: Optional unless the value has a space in it. Can be single or double quotes. Single quotes are literal, while double quotes allow interpolation of other variables.
- Call Variables: $VAR_NAME
Local Variables
Local variables are only accessable in the current shell instance.
declare VAR_NAME=value Set a variable. Ex: declare GREETING=Hello
echo $VAR_NAME Print the variable value. Ex: echo $GREETING
Environment Variables
The outermost (i.e., global) scope for a variable is the "environment".
Set an environment value:
export VAR_NAME=value Ex: export GREETING="Hello $USER"
Get an environment value: Echo prints the value
echo $VAR_NAME Ex: echo $GREETING
printenv VAR_NAME also prints the variable value.
Print all environment variables:
printenv or env
Persist Environment Variables:
Set an environment value:
export VAR_NAME=value Ex: export GREETING="Hello $USER"
Get an environment value: Echo prints the value
echo $VAR_NAME Ex: echo $GREETING
printenv VAR_NAME also prints the variable value.
Print all environment variables:
printenv or env
Persist Environment Variables:
- To persist them, save them to the startup file (e.g.,
.bash_profile
)
(Bash) Shell Variables
- The Shell has some built in environment variables. Some are read only, some can be changed but generally shouldn't be, others have default values you may want to customize.
- Different Unix shells use many of the same variables but there are differences. Below relates specifically to the Bash shell.
- echo $VAR_NAME Returns the value.
Below variables are built-in to the shell program:
SHELL: Path to the Shell's executable file (e.g.,
/bin/bash
)HOME: Path to the user's home directory. Equivalent to echo ~.
USER: The current username.
HOSTNAME: Name of the current host.
PWD: The present working directory (full path).
Below Variables are Customizable: They have a default value set by the shell.
- Change the value in the (
.bash_profile
) startup file withexport VAR_NAME=new_value
PS1: Sets the Shell prompt. Ref: Gnu.org:Controlling-the-Prompt
HISTSIZE: Maximum number of values returned by the history command.- echo $PS1 Get the current prompt setting.
- History command: history Lists most recent commands entered in the Shell.
- echo $HISTSIZE Default:
500
. export HISTSIZE=100
Customize Bash and Z shell prompts
To customize your shell prompt, set the PS1 environment variable in the shell startup file. The syntax varies by shell.
Bash:
Bash:
- Default is:
\h:\W \u\$
- Which translates to: hostname:current_directory_name username$
export PS1="\w \$ "
Sets it to current_directory_path $export PS1="\W \$ "
Sets it to current_directory_name $
- Default is
%n@%m %1~ %#
- Which translates to: username@hostname current_directory_name %
export PS1="%1~ \$ "
Sets it to current_directory_name $
PATH
PATH: An environment variable containing a set of paths to executable files.
- To execute a command (run a program) either:
- Use the full path to the executable file with the command (e.g., /bin/ls).
- If its directory is included in PATH (e.g., /bin), use just the command (e.g., ls).
- When a command is entered in the Shell the system looks for it in PATH. It searches each path in order until it finds an executable file with the same name as the command. It may be a Unix command, another program preloaded with the computer, or a program the user loaded or created.
- echo $PATH Returns the PATH value. Each path is separated by a colon.
MacOS PATH
MacOS PATH values are loaded from the following locations:
/etc/paths
file: Holds the preloaded system paths./etc/paths.d
directory: The system will look for files in this directory that contain a single path. These are for user installed software executables.- The shell startup file (e.g.,
~/.zsh
).
System paths are automatically added to PATH. Typical Unix-like system paths:
- /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
Details
System paths (loaded from /etc/paths):
- Note:
- To see a list of the executables in each of the below files: ls path
- Bin is short for binary, although bin files can hold any executable file, binary or script.
/usr/bin
The primary directory for executable programs./usr/sbin
Like above but requires superuser (root) privileges./bin
Contains executable programs which are needed in single user mode and to bring the system up or repair it./sbin
Like above but requires superuser (root) privileges.usr/local/bin
System programs are loaded in the above directories. Programs used by the user are either loaded in this folder or an alias pointing to their executable file is. This folder is not overwritten when system software is updated.- Aliases point to the actual location of the executable file or folder.
- To see the location of the aliased executable: Right click alias > Show Original.
- Programs installed with Homebrew add aliases to this directory.
- The actual Homebrew installed executables are in
usr/local/Cellar/
. - Packages installed globally by npm (node.js) are aliased in this directory.
- The actual npm installed executables are in
usr/local/lib/node_modules/
.
Add your own paths:
You can add a path to the startup file (e.g.,
.zshrc
)export PATH="$PATH:path"
New PATH=copy of existing PATH plus :
and new path.Example
Ex: Make a directory in the home directory to hold user created executable files.
mkdir ~/bin Create the directory.
mkdir ~/bin Create the directory.
export PATH="$PATH:$HOME/bin"
Copy PATH. Appends :
and path $HOME/bin
.Set your own environment variables
export OWNER_EMAIL="me@example.com"
Reasons to set your own environment variables.
- Enviroment variables can be used when developing web applications to store sensitive data or data that differs in the development vs production environments.
- Some frameworks have special files for those. Node.js, for example, has the dotenv package which uses a .env file.
- For others you can add them to the Shell's startup file.
Shell Alias
Shell alias is a command shortcut.
Use them to shorten frequently used commands or to correct incorrect input.
Return list of all shell aliases: alias
Set Alias: Add aliases to your startup file (e.g.,
Execute alias: alias_name
Use them to shorten frequently used commands or to correct incorrect input.
Return list of all shell aliases: alias
Set Alias: Add aliases to your startup file (e.g.,
.bash_profile
) or the environment file (e.g., .bashrc
or .zshrc
).alias name=value
Execute alias: alias_name
Examples
- Make a cd shortcut to the favprojects directory (or whatever directory you want):
alias cdfav='cd ~/Documents/favprojects'
- cdfav Executes the shortcut.
- Always add the -A option to the ls command, which will include hidden files:
alias ls='ls -A'
- ls Runs the ls command. It will run as ls -A.
Useful Unix CommandsGo to video for this category
Command Documentation:
- man <command> Shows the command's documentation.
- f go to next screen. b go back one screen. q quit.
Examples
- Get the manual page for the cat command: man less
- Get help on the less command: less --help
Cat
cat <filename> View the contents of the file.
cat -n <filename> View the contents of the file with numbered lines.
cat <file1> <file2> View the contents of two files one after the other.
cat <file1> <file2> > combined_file Concatenate file1 and file2 into a combined file.
cat > filename Create a new file, enter content on next line(s), exit command with Ctrl+D.
cat << 'EOF' > filename Create new file, enter content next line(s), enter EOF as last line.
cat >> <filename> Append text to a file. Takes you to a screen to enter content.
cat -n <filename> View the contents of the file with numbered lines.
cat <file1> <file2> View the contents of two files one after the other.
cat <file1> <file2> > combined_file Concatenate file1 and file2 into a combined file.
cat > filename Create a new file, enter content on next line(s), exit command with Ctrl+D.
cat << 'EOF' > filename Create new file, enter content next line(s), enter EOF as last line.
cat >> <filename> Append text to a file. Takes you to a screen to enter content.
Explanation and examples
Cat is a utility command that allows you to concatenate and print files.
Examples:
It reads files sequentially, writing them to the standard output.
Examples:
- View the contents of myfile.txt: cat myfile.txt
- View the contents of myfile.txt with the lines numbered: cat -n myfile.txt
- View the contents of myfile1.txt and myfile2.txt: cat myfile1.txt myfile2.txt
- Concatenate myfile1.txt and myfile2.txt into a file named combined.txt:
cat myfile1.txt myfile2.txt > combined.txt
- Create a file named myfile3.txt and enter the text for it using the cat command:
cat > myfile3.txt
Some text. Enter lines of text that go in the file.
enter Hit enter on last line so last line is blank.
Ctrl+d Exit the command.
- Create a new file, enter content, enter EOF as the last line.
cat << 'EOF' > myfile4.txt In the cat command add argument << 'EOF' to indicate when the file ends. EOF is short for "End of file". You don't have to use EOF for this value, it can be any characters you want.
Some text. Enter lines of text that go in the file.
EOF Enter EOF on the last line, then hit enter.
- Append a line to myfile3.txt using the cat command:
cat >> myfile3.txt Then enter the text line. Hit enter on last line so last line is blank. Ctrl+d
Head / Tail
head -n <filename> Show the first n lines of a file. Default is 10.
tail -n <filename> Show the last n lines of a file. Default is 10.
tail -n <filename> Show the last n lines of a file. Default is 10.
Examples
- Show the first 10 lines of myfile.txt:
head myfile.txt
- Show the first 20 lines of myfile.txt:
head -20 myfile.txt
- Show the last 50 lines of myfile.txt:
tail -50 myfile.txt
Less
less <filename> View the contents the file one page at a time.
less -N <filename> Number each line.
Navigation key commands:
less -N <filename> Number each line.
Navigation key commands:
- One line: downArrow or return down | upArrow up
- Half screen: d down | u up
- One screen: f or space forward | b back
- First/Last line: g Go to first line | G Go to last line.
- /< search_term> Search for a term in the file.
- n Go to the next occurrence of that term.
- N Go to the previous occurrence of that term.
Explanation and examples
Less is a paging program used to view (but not change) the contents of a text file one screen at a time. Once in the program, it has keyboard commands to search and navigate the file.
The less command is mostly used for opening large files. Less is also used internally by Unix and Git for displaying pages. The man online manual command uses less to show the command documentation.
Examples:
The less command is mostly used for opening large files. Less is also used internally by Unix and Git for displaying pages. The man online manual command uses less to show the command documentation.
Examples:
- View the contents of myfile.txt: less myfile.txt
- View the contents of myfile.txt with the lines numbered: less -N myfile.txt
- Go forward one window: f
- Go forward another window: f
- Go back one window: b
- Go to the first line of the file: g
- Go to the last line of the file: G
- Find the term Waldo in the file: /Waldo
- Find the next occurrence of that term: n
- Find the previous occurrence of that term: N
- Quit less: q
Grep
grep <pattern> <filename> Search the file for a pattern. Returns the line text.
Options:
Options:
- -n Include the line number in the result.
- --color or --colour Color the search term in the results.
- -i Case insensitive.
- -w Only search for complete words
- -c Return the number of occurrences.
- -r Search recursively in all files in a directory.
- -l List filenames that contain the pattern
Explanation and examples
Grep stands for global regular expression print. It allows you to search for text patterns in a file.
Examples:
Examples:
- Search for "Waldo" in myfile.txt: grep Waldo myfile.txt
- Search for "Waldo" in myfile.txt. Include the line number in the result: grep -n Waldo myfile.txt
- Search for "Waldo" in myfile.txt. Color the search term in the results: grep --color Waldo myfile.txt
- Search for "Waldo Smith" in myfile.txt: grep 'Waldo Smith' myfile.txt
- Do a case insensitive search for "Waldo" in myfile.txt: grep -i Waldo myfile.txt
- Do a whole word search for "Waldo" in myfile.txt: grep -w Waldo myfile.txt
- Count the number of occurrences of the word "Waldo" in myfile.txt: grep -c Waldo myfile.txt
- Search the entire mydir directory for the term "Waldo": grep -r Waldo mydir
- Search the entire mydir directory for the term "Waldo". Return the filenames that contain the term: grep -lr Waldo mydir
Standard I/O and Pipes
Standard I/O:
- Standard Input: Commands generally typed with the keyboard into the terminal.
- Standard output: Command results generally printed to the terminal screen.
- Standard error: Error result, generally printed to the terminal screen.
Pipes
<command1> | <command2> Run commands consecutively. Output of first is input for second.
Explanation and examples
Use pipes to create a series of commands, like an assembly line. The programs you pipe to/from are called filters as they take input, filter/process it, then give the output.
Get a list of all users from the users.txt file, then sort the usernames with the sort command, then remove any duplicates with the uniq command:
cat users.txt | sort | uniq
Get a list of all users from the users.txt file, then sort the usernames with the sort command, then remove any duplicates with the uniq command:
cat users.txt | sort | uniq
- The cat command input is typed into the terminal by the user.
- The output is not printed to the screen. Rather it is piped to the sort command as input.
- The sort output is also not printed to the screen. Instead it is piped to the uniq command as input.
- The uniq command's output is printed to the screen.
Disk Usage
df -h Returns the amount of free space on the disk(s).
du -h <path> Returns each directory in the path and the space it uses.
du -h -d<n> <path> The -d depth option with a number argument goes n layers deep.
du -h <path> Returns each directory in the path and the space it uses.
du -h -d<n> <path> The -d depth option with a number argument goes n layers deep.
Examples
- Show the space used and available for all disks (aka volumes) on your computer, in human readable form: df -h
- Show the total space used by the ~/Documents directory: du -h -d0 ~/Documents
- Show the space used for the directories in ~/Documents but not their children: du -h -d1 ~/Documents
- Show the space used for all directories in the ~/Documents directory: du -h ~/Documents
Get users and terminals open
users Returns username of all users logged into the system.
whoami Returns the username of the current user.
who Returns list of all user terminals open.
w Like who. Also includes name and arguments of their current process running.
whoami Returns the username of the current user.
who Returns list of all user terminals open.
w Like who. Also includes name and arguments of their current process running.
Explanation
who and w return a list of all terminals open.
If the Terminal application is open it will be listed first with the name "Console".
Then each individual shell instance is listed with names starting with "tty".
If the Terminal application is open it will be listed first with the name "Console".
Then each individual shell instance is listed with names starting with "tty".
Processes
ps Process status: return list of running processes owned by the current user.
ps au Return list of running processes owned by all users (includes root).
ps aux Return list of running processes owned by all users and system processes.
top Return list of running processes sorted by process id.
top -o cpu Sort by CPU usage.
top -o mem Sort by memory usage.
kill -9 <pid> Kill process with the given process id.
ps au Return list of running processes owned by all users (includes root).
ps aux Return list of running processes owned by all users and system processes.
top Return list of running processes sorted by process id.
top -o cpu Sort by CPU usage.
top -o mem Sort by memory usage.
kill -9 <pid> Kill process with the given process id.
Explanation and examples
A process is an executing program. Each command launches a program. The kernel allocates memory and resources for the program to run. Each process has an id called pid, a command name, and an owner name and id.
- Show all processes owned by the user: ps
- Show all processes owned by all users. Includes processes that are user initiated but root owned: ps au
- Show all processes owned by all users and system processes: ps aux
- Show list of all running processes sorted by pid: top
- Show list of all running processes sorted by CPU usage: top -o cpu
- Show list of all running processes sorted by Memory usage: top -o mem
- Kill the process with pid 268: kill -9 268