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
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
Caen Connect Tutorial
Caen Connect Tutorial walks through with you the way to sync your local project folder to your remote CAEN (Linux) machine instantly using SFTP, so you could ssh into CAEN, and do the stuff that you want CAEN to do.
Optimizing Your Virtual Machine
A few things Professor Darden did to spruce up his virtual machine that you can try!
Shells
clear
clear
clears the terminal screen
$ clear
$
export
export
marks an environment variable to be exported with any child process. Any child process will will inherit the variable along with any other marked variables
$ NAME=alice
$ echo $NAME
alice
$ cat test.sh
#!/usr/bin/env/bash
echo $NAME
$ ./test.sh
$ export NAME
$ ./test.sh
alice
$ export NAME=bob
$ ./test.sh
bob
help
help
is a program that takes a linux program as an input and prints a helpful message about the input program’s behavior, its optional/required flags, and how to successfully use the program.
$ help echo
<info about echo>
$ help cd
<info about cd>
Scripting
$_
$_
is a special shell variable which always holds the last argument of the most recent command.
$ touch example.txt
$ echo $_
example.txt
$ (Dollar sign prefix to a variable)
‘$variable’ The dollar sign prefix allows the shell to interpret the phrase as a variable.
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
>>
‘>>’ is a shell operator that you can use to append the output in an existing file.
$ echo line 1 >> example.txt
$ cat example.txt
line 1
$ echo line 2 >> example.txt
$ cat example.txt
line 1
line 2
| (pipe operator)
command1 | command2 [| command3 ...]
The pipe operator is used in bash to chain multiple commands together. The two commands are run sequentially and the ouput of command1 becomes the input of command2.
$ ls | wc -l
3
> (standard output redirection operator)
command1 -[options] [arguments] > <output file name>
The standard output redirection operator is used in bash to redirect the output of one command to a specified file.
$ ./a.out > output.txt
yes
yes
will continuously output lines with y
until user uses ctrl+c to stop. yes
Can be useful for automating
scripts.
$ yes
y
y
^C
$ yes n
n
n
n
^C
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
Atom
Atom is a IDE like editor for programmers, designed by Github to make programming more enjoyable.
Atom is free and can be downloaded straightly from Github website.
ed
ed
is a standard, minimal and not a terribly intuitive text editor for Unix systems.
To open ed
simply type the following:
$ed
emacs
emacs
is used to edit files in emacs.
$ emacs filename
Then file specified by filename
will be opened in emacs, and you can use emacs to edit it.
gedit
gedit
is used to edit files in gedit.
$ gedit filename
Then file specified by filename
will be opened in gedit, and you can use gedit to edit it.
nano
nano
is a simple text editor for unix systems, with a much smaller learning curve than vi and emacs. It is great for people who are new to the command line, and need to quickly make small changes to files.
$ nano filename
vi
vi
is a text editor for programmers, designed to facilitate optimal efficiency
when writing code.
To open vi
, simply type the following:
$ vi [filename]
Toys
asciiquarium
asciiquarium
is a script that doubles as a cool screensaver for the terminal.
To start asciiquarium
, simply type the following:
$ asciiquarium
asciiview
TODO: Add documentation for this command by submitting a pull request.
*short* example of command usage and output
cal
TODO: Add documentation for this command by submitting a pull request.
*short* example of command usage and output
cmatrix
cmatrix
is a program that simulate the cool scrolling lines from the movie ‘The Matrix’.
To start cmatrix
, simply type the following:
$ sudo apt install cmatrix
$ cmatrix
Cowsay
cowsay
is a program that shows a ASCII picture of a cow saying a message inside the terminal.
To use cowsay
, simply type:
$ cowsay "your message"
fortune
TODO: Add documentation for this command by submitting a pull request.
*short* example of command usage and output
linuxlogo
linuxlogo
is a Linux command line utility that generates a color ANSI picture of logo with a few system information.
To use linuxlogo
, simply type:
$ sudo apt install linuxlogo
$ linuxlogo
sl
TODO: Add documentation for this command by submitting a pull request.
*short* example of command usage and output
snake
TODO: Add documentation for this command by submitting a pull request.
*short* example of command usage and output
t
TODO: Add documentation for this command by submitting a pull request.
*short* example of command usage and output
Ubuntu
Os_x
brew
brew
, or known by its longer name as Homebrew, bills itself as the “missing package manager for OS X”.
$ brew install rsync
==> Installing rsync from homebrew/dupes
==> Downloading https://homebrew.bintray.com/bottles-dupes/rsync-3.1.2.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring rsync-3.1.2.el_capitan.bottle.tar.gz
🍺 /usr/local/Cellar/rsync/3.1.2: 8 files, 748.2K
caffeinate
caffeinate
is used to caffeinate your computer, i.e. prevent it from automatically going to sleep when idle, it is primarily used by processes, applications like ‘Caffeine’ and ‘Amphetamine’ are wrappers to this command.
$ caffeinate
Ditto
ditto
is an OSX command that copies the entire contents of a directory to another, new location.
$ ditto /old/directory/ /new/directory/
open
open
is used to open files, directories or URLs from the terminal.
$ open http://www.google.com
pbcopy
pbcopy
is used to copy text from the terminal into the clipboard.
The Linux equivalent of pbcopy
is xclip
.
$ pbcopy < example.txt
Say
The say
command will speak whatever you type
$ say "hello c4cs"
screencapture
screencapture
is used to capture an image of the whole, or part of the screen, saving it into the current working directory.
$ screencapture screencap.png