Useful Unix Commands

CMSC 141: Introduction to Python • quick reference

A short reference for the commands you will use most often in the terminal. Anything in [brackets] is a placeholder you replace with a real name. Examples use the toy files cubs.py and white_sox.py.

Moving around the file system

pwd
Print working directory: shows the full path of the directory you are currently in.
cd [path]
Change directory to [path]. The path can be relative to where you are or an absolute path. cd labs   cd /home/student/cs141
cd ..
Move up one level, into the current directory's parent.
cd
With nothing after it, returns you to your home directory.
ls
List the files in the current directory (hides names beginning with .).
ls -l
Long format: shows permissions, owner, size, and modification date for each file.
ls -a
List all files, including hidden ones whose names begin with .
mkdir [dir]
Make a new directory called [dir].
rmdir [dir]
Remove a directory, which must already be empty.

Working with files

cp [a] [b]
Copy file [a] to a new file [b]. The original stays put. cp cubs.py white_sox.py
mv [a] [b]
Move or rename: turns [a] into [b]. Works on files and whole directories. mv cubs.py white_sox.py renames cubs.py
rm [file]
Remove (delete) a file. There is no undo, so check before you confirm.
cat [file]
Print a file's contents to the screen all at once. cat cubs.py
cat [a] [b] > [c]
Join the contents of [a] and [b] into a new file [c] (overwrites [c] if it exists). cat cubs.py white_sox.py > chicago.py
less [file]
View a long file one screen at a time. Press space to page down, q to quit.
grep [pattern] [file]
Search for [pattern] inside [file]. grep "print" cubs.py
chmod [mode] [file]
Change who can read, write, or run a file. chmod u+x cubs.py lets you run it; chmod 644 cubs.py sets owner read/write and read-only for everyone else.

Running programs and processes

python3 [file]
Run a Python 3 program. python3 cubs.py
Ctrl-C
Stop the program currently running in the foreground.
ps
List the processes running in your current shell.
history
Show a list of the commands you have run recently. The up and down arrow keys scroll through them.
!!
Re-run your most recent command.

Getting help and your session

man [command]
Show the manual page for a command. man ls
date
Show the current date and time.
passwd
Change your account password.
exit
Close the shell (and log out, if it is a login shell). Ctrl-d does the same thing.