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 is a command to create shortcut to commands and applications in the terminal.

$ alias l="ls"
$ l
Documents  Downloads  Music     Public     Videos
Desktop    Pictures   Templates

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

Changes the permissions of a file or directory. Stands for change file mode bits

$ ls -go --time-style=+
total 40
-rw-r--r-- 1 37465  index.html
$ chmod 666 index.html
$ ls -go --time-style=+
total 40
-rw-rw-rw- 1 74981  index.html

chown/chgrp

chown/chgrp changes the user that owns the file or the group that owns the file.

$ ls -l file
-rw-r--r--  1 ppannuto  ... file
$ sudo chown root file
$ ls -l file
-rw-r--r--  1 root      ... file

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

date

This command can get you the system time in terminal.

$ date
Thu Mar 24 15:09:46 EDT 2016

df

df (abbreviation for disk free) is used to display the amount of availbale disk space for file systems on which the invoking user has appropriate read access.

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            982M     0  982M   0% /dev
tmpfs           201M   22M  179M  11% /run
/dev/sda1        48G  4.9G   41G  11% /
...(Some more info)

dot (.)

A full stop can be synonymous for source in a shell, can reference a relative directory path, or indicate a hidden file.

du

The du (disk usage) command shows you the sizes of directory trees including all of their contents and sizes of individual files. This makes it usefull for taking down particular directories that are hogging disk space.

$ mkdir test
$ yes > test/bigfile.txt
$ du -h
51M    ./test
51M    .

echo

echo is used to output whatever it is given into stdout. Often useful see the value of environment variables.

$ echo "Hello World!"
Hello World!
$ echo $USER
<your username>

expand

expand a command that convert tabs to spaces.

$ expand [OPTION] [FILE]

gcc flags

gcc is a compiler command that has the ability to execute options or ‘Flags’ when compiling. These flags can be grouped and executed via the command line:

-o places output in a filename of your choice. If you do not use the -o flag the output file will have the name and extention: ‘a.out’.

-g turns on debugging information.

-Werror makes all warnings into errors.

-Wall enables all the warnings.

-O enables optimization.

NOTE: See below for details.

$ gcc test.c -o test
$ gcc -g test.c
$ gcc -Werror -Wall -O test.c

gzip

gzip reduces the size of the name files by compressing a file. It will only attempt to compress regular files. Compressed files can be restored to their original form using gzip -d or gunzip or zcat.

$ gzip archivefile.txt

hash

hash is used to maintain a hash table of full pathnames of recently executed programs. A full search of the directories in PATH is performed only if the command is not found in the hash table.

$ hash
  hits	command
   4	/bin/nano
   3	/usr/local/bin/bundle
   1	/usr/bin/pip
   3	/usr/bin/git
   2	/usr/bin/factor
   5	/usr/bin/sudo
  11	/bin/ls
   1	/usr/bin/figlet
   3	/usr/bin/python

head prints the first 10 lines of a file to the standard output. If more than one file is specified, head will print the filename first and then print the first ten lines of the file. Since a good programmer will always put comments at the beginning of a file, printing the first few lines of a file can help us identify what is in the file.

$ head [options] ... [file] ...

history

history is used to list command history. By default, history will print the last 500 commands typed into the terminal.

$ history
  ...
  498  third_most_recent_command
  499  second_most_recent_command
  500  most_recent_command
  501  history

id

id: id is used to get information about a user’s UID (user ID), the groups a user belongs to, groups associated with the user, and security context of the current user. The default id command shows the user and groups names and numeric ID’s. Use id with options to find more useful information.

$ id -G username
203 403 94 32 93

join

join is used to combine two files based on matching content in each file.

$ cat a.txt
1 h
2 t
3 e
4 a
$ cat b.txt
1 i
2 o
3 h
4 h
$ join a.txt b.txt
1 h i
2 t o
3 e h
4 a h

kill

kill is used to send signal to a process or to kill a process. We typically use kill PID, where you know the PID of the process. By default kill generates SIGTERM, to safely end a process.

$ kill 7243

ln

ln is used to create a link (symbolic or hard) to a file on disk. Default creates hard links.

$ ln file1.txt file2.txt
$ ln -s file1.txt file2.txt

locate

locate finds the locations of directories and files on Linux as well as other Unix-like OS.

$ locate hello.cpp

ls

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

$ ls
directory file

man

man allows users to format/display the user “manual” that is built into Linux. This manual holds a catalogue of commands and other aspects of the Linux OS.

$ man [command]
$ man ls
$ man strcmp

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 a command that numbers the lines of files and prints the result to the standard output.

$ echo "This is an:" > beginning.txt
$ echo "example" > end.txt
$ nl beginning.txt end.txt
   1	This is an:
   2	example

passwd

passwd is a command that is used to change the password of system users.

$ passwd
Changing password for user [current user]
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

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

rsync is file copying tool. It can be used locally or to and from a host. It is most commonly used to copy only the changes made to a file.

$ rsync dir1/file dir2/

seq

seqprints a sequence of numbers to standard ouput.

$ seq [options]... [*FIRST* [*STEP*]] *LAST*... 

sleep

sleep is a Unix command used to suspend program execution for a specified period of time. The default unit is seconds, but minutes, hours, or days may also be specified.

$ sleep 10

sort

sort is used to sort a file or input.

$ cat letters.txt
b
z
e
a
$ sort letters.txt
a
b
e
z

su

su (substitute user) is a command to change a login session’s owner without the owner having to first log out of the session. (as specified in the su Command). In other words, it is called the switch user command

Note: It is most commonly employed to change the ownership from an ordinary user to the administrative user. Therefore it is often refered as the superuser command.

$sudo su root

sudo

sudo (super user do) is a command that allows a permitted user to execute a command as a superuser or another user (as specified in the sudoers file). In other words, it runs the command with elevated privileges.

Note: The root user has permission to do anything on your system; acting as root can be dangerous! That is why it’s best practice to only use sudo when you need it, and to double check the command before running.

$ sudo apt-get install wbritish

tail

Tail displays the last part(s) of a file

$ tail input.txt
$ tail -f -n 15 input.txt

tar

tar is a command that is used to store and extract files from a tape or disk archive. The tar command which stands for tape archive simply archives a collection of files into one single file usually refered to as the tarball.

$ tar -cf tarball.tar file1 file2

top

top displays information on your Linux system, running processes, and system resources, including the CPU, RAM, swap usage, and total number of tasks being run:

$ top

true

true a command that returns the logical value of truth, which is 0.

#!/usr/bin/env bash
# The value of $? is the exit status code of the last program/command to run
$ true
$ echo $?
0
$ false 
$ echo $?
1

uniq

uniq is used to filter consecutive repeated lines from a file or input. The first line of each consecutive group of repeated lines is retained. Repeated lines that are not consecutive are treated as unique instances and will not be filtered.

$ cat numbers.txt
1
2
2
3
2
4
4
$ uniq numbers.txt
1
2
3
2
4

uptime

This command shows the current time followed by your computer’s uptime.

$ uptime
3:03 up 1 day, 6 hrs, 2 users, load averages: 1.38 1.43 1.42

wc

Print the number of bytes, words, and lines in files

$ cat a.txt 
I love EECS 398.
What a great class. 
Good.

$ wc [filename] 
$ wc a.txt
3 9 44 a.txt

whereis

whereis is used to locate source/binary and manuals sections for specified files. The supplied names are first stripped of leading pathname components and any (single) trailing extension of the form .ext.

$ whereis [options] filename

which

which is used to show the full path for each of the executables that you give it. Since command-line commands are also executables, which is usually used to find full paths to shell commands. By default, which will only give the first full path found for the given executable.

$  which ls
/bin/ls

who

who is a command that shows which users are currently logged on the machine.

$ who
alice    pts/0        2016-03-19 20:52 (:0)
bob      pts/1        2016-03-18 09:00 (:0)
mallory  pts/4        2016-03-19 22:48 (10.1.1.3)

whoami

whoami is used to print the current userid. Permissions for commands will be run with the level of permissions of userid.

$ whoami
current_userid

Tutorials

Shells

Scripting

Tools

apropos

apropos allows you to quickly figure which commands lead to the actions you want to perform

$ apropos "list directory"
dir (1) - list directory contents
ls (1) - list directory contents
ntfsls (8) - list directory contents on an NTFS filesystem
vdir (1) - list directory contents

curl

curl is used to transfer data from one server to another using a supported protocol such as HTTP or HTTPS

$ curl google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

dd

dd is a utility that can be used to convert and copy files in a number of ways. This can be used for backing up/restoring entire hard drives or partions, converting data formats, converting the case of a file, and more.

Warning: dd is often referred to by many as 'disk destroyer' or 'data deleter' due to its potential to erase large chunks of data when used improperly. If you plan on testing any of these commands exercise extreme caution to avoid losing any significant data.
$ dd if=[inputFile] of=[outputFile] [options]
0+1 records in
0+1 records out
6 bytes copied, 0.000101046 s, 59.4 kB/s

diff

diff is a tool used to compare files line by line.

$ diff [option]... [file1] [file2] 

factor

factor is used to list all prime factors of each integer number.

$ factor [NUMBER]...

ffmpeg

ffmpeg is a command line tool that is used for converting and editing audio and video files. Its reference page can be found here. Install by typing the following command:

$ sudo apt install ffmpeg

File Conversion

One of the simplest commands one can run using ffmpeg involves file type conversion. This is useful for scenarios in which you have, for example, a .mp4 and want to change it to a .avi. This can be done in the following way:

$ ffmpeg -i sample.avi sample.mp4

Break it down

The first argument, -i tells ffmpeg that the next file is the input to the tool. The argument after the input is implicitly interpreted as the output of the program. ffmpeg can tell what type of file you want to convert to just by looking at the extension of the filename that you give it for output. This command does not delete your original file.

The conversion command works for other file types too, just as long as they are compatible (audio to audio, image to image, etc.).

Converting File Quality

ffmpeg allows you to set how high of a quality you want your conversion to be. There are different settings depending on which file type you are writing to. For converting to .avi, use the -q argument followed by a value between 1 and 50. The lower the number, the higher the quality.

$ ffmpeg -i sample.mp4 -q 12 sample.avi

If instead you wish to convert to a .mp4, you must use the -crf argument.

$ ffmpeg -i sample.avi -crf 2 sample.mp4

Converting a Video to Many Images

It may be useful to take a video and cut it into a series of images. In order to do this, use the following command:

$ ffmpeg -i sample.avi -r 1 -s [WxH] -f image2 sample-%d.jpeg

Break it down

  • -i in this command specifies the input file of sample.avi.
  • -r specifies the framerate for the conversion, in this case we set it to one frame per second. -s is used to specify the desired width and height, denoted as [WxH]. So, for example, if you wanted a 1920x1080 image or video, you’d use the argument -s 1920x1080.
  • -f tells ffmpeg that you want to force the file conversion to the specified format encoding, in this case we chose image2 which is used for .jpeg and .png among others.
  • %d is called a sequence pattern. This allows you to convert the video into many image files with the same prefix, in this case sample-. In our case, if you have a 60 second video, you would end up with 60 images labeled sample-1.jpeg through sample-60.jpeg. You can 0-pad these sequences by using %d0N where N - 1 is the number of 0’s you want before the number. So if using %d03 you would see sample-001 instead.

Filters

The most powerful use of ffmpeg is its -filter flag. Let’s look at a few examples.

Volume

$ ffmpeg -i sample.avi -filter:a "volume=2" sample.avi

The -filter:a option specifically targets the audio channel of the video file. In this case, we are adjusting the volume by a multiplier of 2. Values smaller than 1 will reduce the volume of the file.

Channel Remapping

$ ffmpeg -i sample.mp3 -filter:a "channelmap=0-0|0-1" sample.mp3

Again we are using the -filter:a flag to target the audio channel of the file. channelmap allows us to map the input of an audio file to a different channel in the output file. A 0 specifies the left channel and a 1 is the right. The format is this: channelmap=input-input|output-output. So, you can flip the audio channels by a mapping like this channelmap=0-1|1-0. This will take the input left and map it to the ouput right, and vice versa for the input right.

In the above case, we are taking the left input channel and mapping it to the output’s left and right channel, which could be useful in “converting” a mono audio file to a stereo one.

file

file is used to find out the type of a file

$ file file.md
file.md: ASCII text

find

find is used to find items matching certain specified criteria in a given directory tree.

$ find ~/ -name "foo" -print

git

Git is a version control system that can be used for collaboration

grep

grep is used to search files for the occurrence os string of characters matching a string (egrep can support called regular expressions often abbreviated as regex).

$ grep string file
matches because it has string in it

ifconfig

ifconfig is used to configure a network interface or display information about all active or inactive connections.

Warning: While ifconfig is still used, it is recommended to use iproute2 (Linux) or iproute2mac [See reference on 'ip']. ip is a much more powerful tool that can do everything ifconfig can do, and lots more.

ip

ip is a powerful networking tool that when simply used will display information on kernel-resident interfaces. Much like ifconfig, it can turn devices on/off and set IP and MAC addresses. ip also wraps popular commands like netstat, ndp, arp, and route into its functionality.

# Close the first ethernet connection 
$ ip link set dev eth0 down

less

less is used to display contents of a file, as well as allow navigation. Its name is derived from being the opposite of more.

$ less file.txt

These are the contents of "file.txt".

The output will actually take up the entire screen, and you can navigate using Vim-like commands.

make

Simply put, this command can compile code into an executable without having to type a super long compilation sequence. We can use the make utility to automate many tasks in the development process, including compilation and regression testing.

$ make main
c++     main.cpp   -o main
$ ./main

nslookup


nslookup, which stands for “name server lookup”, is used to query internet name servers interactively. By default, nslookup tranlsates a domain name to an IP address, or vice versa.

nslookup www.google.com

objdump

TODO: Claim the issue for this command and add documentation for this command by submitting a pull request.

*short* example of command usage and output

ping

ping is used to test a connection to a network. You can use ping to check if your local machine can connect to a network or if a particular device is reachable from your machine. A ping may fail to reach a device if either device is blocked by a firewall.

$ ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=255 time=1.518 ms
$

ps

ps provides a snapshot of current processes

#Shows four items of information for each process currently under the current user and from this terminal
#PID: Process ID
#TTY: Terminal Type (name of console that user logged into)
#TIME: System time used on process
#CMD: Name of command that launched process
`ps`
  PID TTY          TIME CMD
 1612 pts/17   00:00:00 bash
25671 pts/17   00:00:07 gedit
26195 pts/17   00:00:00 ps

rev

Rev reverses the lines of a file. This utility will copy the files specified in the command to standard output while reversing the order of each of the characters in every line. If no files are specified, it will use standard input and read in lines.

The general format is:

$ rev [file...]

scp

scp is used to make a secure copy of files or directories to and from a remote host. Similar to “cp” except either the source or destination should be a remote host. scp uses secure shell protocol (ssh) to tranfer the file to or from the server.

$ scp source user@host:destination
$ scp user@host:source destination

sed

sed is used to make replacements in text files using regular expressions. It searches for regex matches and executes the provided replacement at all instances.

$ sed <options> string file

sftp

sftp is short for SSH File Transfer Protocol, also known as Secure File Transfer Protocol, enables secure file transfer capabilities between networked hosts. Unlike the scp, sftp additionally provides remote file system management functionality, allowing applications to resume interrupted file transfers, list the contents of remote directories, and delete remote files.

Useful Options / Examples

We can establish an SSH connection and then open up an SFTP session using that connection by issuing the following command:

$ sftp username@remote_hostname_or_IP

sshfs

sshfs is used to mount a remote ssh server to a local mountpoint to allow the secure transfer of files or directories to and from a remote host. Similar to “scp” except instead of having to manually transfer the files from the command line, files can be simply “mv” or “cp” into a folder. sshfs uses the secure shell protocol (ssh) to transfer the file to or from the server and the “FUSE” libraries to produce the mountpoint.

$ sshfs mnt_pnt user@host:dest_folder

stat

TODO: Claim the issue for this command and add documentation for this command by submitting a pull request.

*short* example of command usage and output

tac

tac concatenates each file or command line input to standard output in reverse (hence tac = cat in reverse).

$ tac filename

tcpdump

tcpdump is a tool that allows you to view a dump of traffic on a network. By default, tcpdump will let you view the dump on your local network. You can use packet analyzing tools like Wireshark to analyze tcpdumps and pcap files.

$ tcpdump 

telnet

telnet is used to communicate with another host using the TELNET protocol.

$ telnet
telnet>

time

time prints out the total time elapsed, the time used by system overhead and the time used to execute the utility.

tmux

tmux is a terminal multiplexer. It allows the user to access separate terminal sessions inside a single terminal window. tmux is especially useful in remote access.

$ tmux

touch

touch is used to create new, empty files or update the timestamp of an existing file.

$ touch newFile.txt

tr

tr is used to translate the characters given as input into new characters based on flags and strings given as input.

$ tr 'is' 'at'
this
that

units

units gives conversion rates from one scale to another.

$ units [-f unitsfile] [-q] [-v] [from-unit to-unit]...

wget

wget is a tool for downloading files using HTTP, HTTPS, or FTP using the command line.

$ wget https://c4cs.github.io/hw/c4cs-wk9-advanced.pdf

xclip

xclip can put the output of a command into the X-clipborad if we pipe the command into xclip.

ls -la | xclip

xclip can also directly copy the content of the following file:

xclip SomeOtherFile.txt

xclip -o can print the stuff in X-clipboard.

xclip -o > output.txt

If you’re on a Mac the equivalent command is pbcopy.

Editors

Toys

Ubuntu

Os_x