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.
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 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 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
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
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 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
This command can get you the system time in terminal.
$ date Thu Mar 24 15:09:46 EDT 2016
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)
A full stop can be synonymous for
source in a shell, can reference a relative
directory path, or indicate a hidden file.
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 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 a command that convert tabs to spaces.
$ expand [OPTION] [FILE]
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 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 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 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 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 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 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.
kill generates SIGTERM, to safely end a process.
$ kill 7243
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 finds the locations of directories and files on Linux as well as other Unix-like OS.
$ locate hello.cpp
ls is used to list the contents of a directory. By default,
ls will simply print file names.
$ ls directory file
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 is used to create directories (if they do not already exist) on a file system.
The general format is
$ mkdir [options] directories
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 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 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 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 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.
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 is used to remove empty directories.
The format is
$ rmdir [options] directories
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/
seqprints a sequence of numbers to standard ouput.
$ seq [options]... [*FIRST* [*STEP*]] *LAST*...
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 is used to sort a file or input.
$ cat letters.txt b z e a $ sort letters.txt a b e z
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 (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
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
when you need it, and to double check the command before running.
$ sudo apt-get install wbritish
Tail displays the last part(s) of a file
$ tail input.txt $ tail -f -n 15 input.txt
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
$ tar -cf tarball.tar file1 file2
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:
true a command that returns the logical value of truth, which is
#!/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 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
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
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 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
$ whereis [options] filename
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 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 is used to print the current userid. Permissions for commands will be run with the
level of permissions of userid.
$ whoami current_userid
clear clears the terminal screen
$ clear $
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
$_ 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 is used to print the value of ‘variable’.
echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
> (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
‘>>’ 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
yes will continuously output lines with
y until user uses ctrl+c to stop.
yes Can be useful for automating
$ yes y y ^C $ yes n n n n ^C
| (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
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 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>
dc stands for ‘desk calculator’. Typing this command into a terminal will open a postfix notation calculator.
$ dc 2 15 * p 30
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.
$ 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 is a tool used to compare files line by line.
$ diff [option]... [file1] [file2]
factor is used to list all prime factors of each integer number.
$ factor [NUMBER]...
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
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
$ 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
-iin this command specifies the input file of sample.avi.
-rspecifies the framerate for the conversion, in this case we set it to one frame per second.
-sis 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
-ftells 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.
%dis 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
%d0Nwhere N - 1 is the number of 0’s you want before the number. So if using
%d03you would see sample-001 instead.
The most powerful use of ffmpeg is its
-filter flag. Let’s look at a few examples.
$ ffmpeg -i sample.avi -filter:a "volume=2" sample.avi
-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.
$ 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 is used to find out the type of a file
$ file file.md file.md: ASCII text
find is used to find items matching certain specified criteria in a given directory tree.
$ find ~/ -name "foo" -print
Git is a version control system that can be used for collaboration
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 is used to configure a network interface or display information about all active or inactive connections.
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
route into its functionality.
# Close the first ethernet connection $ ip link set dev eth0 down
less is used to display contents of a file, as well as allow navigation. Its name is derived from being the opposite of
$ 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.
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, 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.
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 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 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 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 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 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 is short for SSH File Transfer Protocol, also known as Secure File Transfer Protocol, enables secure file transfer capabilities
between networked hosts. Unlike the
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 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
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 concatenates each file or command line input to standard output in reverse (hence tac = cat in reverse).
$ tac filename
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.
telnet is used to communicate with another host using the TELNET protocol.
$ telnet telnet>
time prints out the total time elapsed, the time used by system overhead and the time used to execute the utility.
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.
touch is used to create new, empty files or update the timestamp of an existing file.
$ touch newFile.txt
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 gives conversion rates from one scale to another.
$ units [-f unitsfile] [-q] [-v] [from-unit to-unit]...
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 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 -o can print the stuff in X-clipboard.
xclip -o > output.txt
If you’re on a Mac the equivalent command is
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.
Short description of the command
*short* example of command usage and output
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.
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 is a text editor for programmers, designed to facilitate optimal efficiency when writing code.
vi, simply type the following:
$ vi [filename]
asciiquarium fills your terminal with a lively aquarium in all ascii text
asciiview generates an ASCII image based on any png file provided by the user.
$ asciiview filename.png
cal is used to display the calendar of the current, past, and future month of any year. By default, it displays the month of the current month, current year.
$ cal March 2016 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
cmatrix draws a Neo style matrix on your terminal and makes you feel a little more geekier.
$ sudo apt-get install cmatrix
After install cmatrix, just type in the command in command line window. Like
$ cmatrix -option
cowsay generates an ASCII picture of a cow saying something provided by the user.
$ cowsay Hi There __________ < Hi There > ---------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
fortune prints a randomly-selected quip from its “cookie files”.
$ fortune Good news, everyone!
Prints a train whenever you mistype ls as sl.
Play the classic arcade game
snake in Terminal.
While in emacs, hold
x, then type
M-x is the default emacs prompt.
t lets you interface with Twitter from the command line.
$ t update "tweet tweet" $ t stream timeline -l
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 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.
#Prevents your computer from going to sleep caffeinate
ditto is an OSX command that copies the entire contents of a directory to another, new location.
$ ditto /old/directory/ /new/directory/
open is used to open files, directories or URLs from the terminal.
#Opens google.com in your default browser open http://www.google.com
pbcopy is used to copy text from the terminal into the clipboard.
The linux equivalent of
# Copies the contents of a file into the clipboard $ pbcopy < example.txt
say command will speak whatever you type
say "hello c4cs"
screencapture is used to capture an image of the whole, or part of the screen.
#Takes a screenshot of the entire screen and saves it to the file screencap.png in the cwd screencapture screencap.png