Quick Reference

This page collects commands we have (or will) discuss throughout the course. Click on a command for more details on its operation and some examples of common or useful operations. We encourage students to contribute to this page through the advanced homework on git and open source contributions.

Basics

alias

alias instructs the shell to replace one string with another when executing commands.

$ alias rm='rm -i'
$ rm sample.txt
rm: remove regular empty file 'sample.txt'? Y
$ 

bg

bg resumes a stopped job in the background, so the terminal remains usable.

$ gedit file.txt
^Z
$ bg 'gedit file.txt'
$ jobs
[1]+ Running    gedit file.txt
$

cat

cat is a command that concatenates files and prints the concatenated files to the standard output.

$ echo "This is an:" > beginning.txt
$ echo "example" > end.txt
$ cat beginning.txt end.txt
This is an:
example

cd

cd is used to change directory. Unlike most commands, cd is not a separate program, it is a shell built-in. cd is a useful tool to navigate up and down the hierarchy of the file systems on your machine, and move into a given directory.

$ cd ~
$ cd /home

chmod

chmod (change mode) is the command to change permissions to system objects (files and directories).

$ chmod u+x filename

chown/chgrp

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

cp

cp is used to copy files or directories.

$ cp source destination
$ cp file1.txt file2.txt
$ cp -b file1.txt file2.txt
$ cp -v file1.txt file2.txt
$ cp * ~/Desktop

cut

cut is for cutting sections from each line of files and writing the result to standard output.

$ cut [OPTIONS] [FILE]

date

date returns a system time specified by the user.

$ date
Tue Feb  6 12:42:38 EST 2018

df

df displays the amount of disk space available on the file system containing each file name argument.

$ df [OPTIONS] [FOLDER]

dot (.)

The period (dot) is short hand for the bash built in “source”. It will read and execute commands from a file in the current environment and return the exit status of the last command executed.

$ echo "echo "hello world"" > hello
$ . hello
hello world

du

du, or disk usage, reports the sizes of directories, including of all of their contents and the sizes of individual files, and the sizes of each directory in the directory tree.

$ du [options] [directories and/or files]

echo

echo is used to display a message on the screen and write the given message to standard output with a newline at the end. echo is implemented as built-in command in many command shells like bash, ksh, csh.

$ echo "Hello World"
Hello World
$ echo Hello World
Hello World

expand

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

fg

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

gcc flags

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

gzip

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

halt

halt, together with poweroff, reboot are commands you can run as root to stop the system hardware. Specifically, halt instructs the hardware to stop all CPU functions. These commands require superuser privileges. If you are not logged in as root, you will need to prefix the command with sudo or the signal will not be sent.

$ sudo halt
[sudo] password for xxx: 

hash

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

head is a command-line utility tool that outputs the first lines of a given file to the standard output. By default, head will return the first 10 lines of the specified file.

$ head helloworld.txt
Hi World!
Hey World!
Hello World!
Howdy-do World!

history

history is used to display the most recently used commands.

$ history [options]

id

id is a command that prints out the effective and real user and group ids.

The syntax for the command is:

id [option(s)]

jobs

jobs is used to list the jobs running in the background of the current shell session.

The general format is

$ jobs [options] [jobID]

join

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

kill

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

less

less is a tool to view a text file one screen at a time. You can navigate through the file with j or RETURN, and backwards with k

$ less hello.txt
# displays hello.txt
q # to quit

ln

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

locate

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

ls

ls is used to list the contents of a directory. By default, ls will simply print file names.

$ ls
directory file

man

man provides manual pages for terminal commands

$ man git
# displays git man page
q # to quit

mkdir

mkdir is used to create directories (if they do not already exist) on a file system.

The general format is

$ mkdir [options] directories

more

The more command displays the file called name in the screen. The RETURN key displays the next line of the file. The spacebar displays the next screen of the file.

The syntax is:

$ more [options] [files]

mv

mv is used to move a source file or directory into a destination directory. It is also commonly used to rename files.

$ mv source desination

nl

nl is used to number the lines in a file.

$ nl filename 
$ echo "hello
> world
> hi" > hello.txt
$ nl hello.txt
   1  hello
   2  world
   3  hi

passwd

passwd is used to change the password of a user account

$ passwd [OPTION] [USER]

printf

printf is used to format and output data.

$ printf format [argument]
$ printf "%f\n" 7
7.000000
$ printf "%d\n " 0xF
15

pwd

pwd stands for print working directory. pwd outputs the full pathname of the current work directory.

$ pwd
/home/bo/Desktop

pwd can also be used to store the full path to the current directory.

x=$(pwd)

rm

rm is used to delete one or multiple files from your current directory. Be careful when using because this does not move the item to trash, it permanetly deletes it.

$ rm file1.txt file2.txt

rmdir

rmdir is used to remove empty directories.

The format is

$ rmdir [options] directories

rsync

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

seq

seq is used to produce a sequence of numbers

$ seq 3
1
2
3

sleep

The sleep command makes your script, terminal, or anything else that is bash executable go to sleep for however long you specify

$ sleep 5s

sort

sort is a standard command that sorts and outputs the input it was given.

$ sort names
  Aaron
  Brian

su

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

sudo

sudo allows a user to execute a command as a different user (with the default being the root user). sudo is useful when commands need to be performed with elevated privileges, such as installing system updates.

$    sudo apt-get update

tail

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

tar

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

time

time writes a message to standard output giving timing statistics about the program that was run. It outputs the times in 3 categories: real time, user time, and system time.

$ time ./executable
real    0m0.116s
user    0m0.004s
sys     0m0.008s

top

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

true

true a statement that logically evaluates to true. It is often used to check whether statements evaluated succesfully or not. It is the logical inverse of ‘false’, also a unix command.

$ true
$ rm -rf directory || true

uniq

uniq, often used with sort, allows a user to report, or filter, repeated lines that are adjacent in a file.

$ sort in.txt | uniq 
line1
line2
line3

A More In Depth Example:

Say we have a file called, myfile.txt, and it contains the following text:

This is a line. This is a line. This is a line.

This is also a line.

This is also also a line.

$ uniq myfile.txt

  This is a line.

  This is also a line.

  This is also also a line.

uptime

uptime is used to check how long your computer has been on since the last reboot. This is useful for the user as it allows the user to determine whether a restart or shut down is necessary for the computer.

$ uptime
11:31 up 1 day, 1:22, 2 users, load averages 1.97 2.04 2.14

wc

wc prints newline, word, and byte counts for each FILE.

$ wc tecmint.txt
12  16 112 tecmint.txt

whereis

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

which

which returns the first matching command(s) in $PATH

$ which echo
/bin/echo

who

Displays list of all users currently logged on

$ who
Username console  Oct 12 08:20
Username ttys000  Oct 20 09:35

whoami

whoami prints the username of the current user when invoked

$ whoami
mmdarden

Tutorials

Shells

Scripting

Tools

apropos

apropos searches the man database for entry that contains the string passed in as argument.

$ apropos whoami
whoami (1)           - print effective userid

asciinema

asciinema is a tool that allows you to record a terminal session with out having to use screen capture. It also allows for easy copy-pasting!

$ sudo apt-get install asciinema #install
$ asciinema rec #start session
$ your_commands_here
$ exit #exit session

cmp

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

comm

comm is used to check for the common lines between two sorted files line by line. This command will produce three columns of output, each showing the lines unique to file1, the unique to file2 and the common lines between file1 and file2. This is useful for checking the common content between two files.

$ printf "apple\ntomato\n"> MyFruits.txt
$ printf "carrot\ntomato\n"> MyVegetables.txt
$ comm MyFruits.txt MyVegetables.txt
apple
	carrot
		tomato

curl

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

dc

dc is a desk calculator that supports unlimited-precision arithmetic and postfix notation.

dc
2
3
+
p
5
*short* example of command usage and output

dd

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

diff

diff is used to check for the differences between two files line by line. This is very useful for checking a programs output against a file containing its expected output.

$ printf "this\ndifference\n" > first.txt
$ printf "this\nsimilarity\n" > second.txt
$ diff first.txt second.txt
2c2
< difference
---
> similarity

factor

factor prints all of the prime factors of a number.

$ factor 42
42: 2 3 7

ffmpeg

ffmpeg is a multimedia framework used to record, convert, and stream audio and video.

# Convert between filetypes
$ ffmpeg -i input.mp4 output.avi

file

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

find

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

git

git is a version control tool allowing for easy collaboration on projects.

$ git clone https://github.com/c4cs/c4cs.github.io.git
$ cd c4cs.github.io
$ echo "Modified File" >> README.md
$ git add README.md
$ git commit -m "Added to README.md"
$ git push origin master

grep

grep is a program for searching text files for lines that match regular exprssions. It can be used for all sorts of pattern-matching and text-based query analysis.

$ grep -i crab animals.txt
Crab
Crab-Eating Macaque
Hermit Crab
Horseshoe Crab
King Crab

ifconfig

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

ip

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

make

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

nslookup

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

objdump

objdump gives you information about an object file or executable.

$ gcc -std=c99 -c hello.c
$ objdump -r hello.o > hello_relocation.info

pdftk

pdftk is a command that is used to manipulate Portable Document Format (PDF) files. There are many ways to manipulate PDFs through the command such as merging, splitting, encrypting and much more.

$ pdftk in1.pdf in2.pdf cat output combinedIn.pdf

ping

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

ps

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

rev

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

scp

scp, or Secure Copy, is used to securely transfer files based on the Secure Shell protocol (ssh). Unlike cp, scp can transfer files from or to a remote host.

Syntax / Example
$ scp source user@host:destination
$ scp user@host:source destination
Basic Example (Local -> remote)
$ scp ~/Desktop/file uniqname@login-course-2fa.engin.umich.edu:~/Documents/

sed

sed is a stream editor utility which supports insertion, deletion, and search and replace of streams then prints the edited stream to standard output.

$ sed OPTIONS... [SCRIPT] [INPUTFILE...]

sftp

sftp is used to securely transfer files between a local machine and a remote machine that is accessed via SSH. SFTP stands for Secure File Transfer Protocol.

$ sftp user@hostname
Password: *********
Connected to hostname.
sftp> put filename.txt
sftp> mput file1.txt file2.txt file3.txt
sftp> get file.txt
sftp> mget file1.txt file2.txt file3.txt

sshfs

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

stat

Displays information about the file pointed to by [filename]

$ stat stat.md
16777217 14633638 -rw-r--r-- 1 [user] staff 0 439 "Feb  9 22:08:11 2018"
"Feb  9 22:08:08 2018" "Feb  9 22:08:08 2018" "Feb  9 21:29:18 2018"
4096 8 0x40 stat.md

tac

tac is used to concatenate files in reverse, line by line.

$ tac numberedlines.txt
Line number five
Line number four
Line number three
Line number two
Line number one

tcpdump

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

telnet

telnet is a command that uses the Telnet protocol. Telnet (short for TErminal NETwork) is a protocol designed to provide a command line interface for remotely communicating with a device or system. telnet can invoked on its own, or it can be invoked with a host and port argument.

$ telnet
telnet>
$ telnet examplesite.com 22
Trying 1.2.3.4...
Connected to examplesite.com

time

time outputs the runtime of a program that is given as an argument.

$ time cd

real 	0m0.008s
user 	0m0.008s
sys 	0m0.000s

tmux

‘tmux’ allows a user to use 1 terminal window to access multiple separate terminal sessions. It lets you keep running them in the background by detaching them and then reattach them to a different terminal.

$ tmux
$ tmux new -s myname
$ tmux a -t myname
$ tmux ls
$ tmux kill-session -t myname

touch

touch Creates a new empty file and can also change timestamps on existing files. Useful for creating empty files quickly.

$ touch main.cpp

tr

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

tree

Short description of the command

*short* example of command usage and output

units

units converts quantities from one unit to another unit. It can be used interactively or non-interactively.

To use interactively, type units into the command prompt:

$ units

To use non-interactively:

$ units [options] ['from-unit' 'to-unit']

wget

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

xclip

TODO: Add documentation for this command by submitting a pull request.

*short* example of command usage and output

Editors

Toys

Ubuntu

Os_x