UNIX cheat sheat
Here are some basic UNIX commands to help you get around. Commands you type are in monospace, and arguments are in curly braces ({ }).
General hints
- Tab complete: When typing the path to a file or command, pressing the tab key will automatically complete it if there is only a single match. If there are multiple press again and see all of the possibilities.
- Command history: Pressing the 'up' arrow on the keypad lets you scroll through the history of commands you've typed recently and can save you some typing.
Basic
- man {command}
- Provides help (manual) information for any command, including a list of possible arguments and options.
- pwd
- Print working directory. Returns the full path to the directory you are currently in.
- cd {directory}
- Change to the specified directory. Shortcuts include:
cd ~/ (or often just cd) — go to your home directory
cd .. — go up one directory level
- ls {directory}
- List the files in the specified directory. If
directory isn't specified, list files in the current directory.
ls -A {directory} — list all files, even “hidden” files starting with .
ls -lh {directory} — list in long format, which lists file size, permissions, and date modified.
- mv {oldfile} {newfile}
- Move or rename a file (or directory). If you want to leave the file name the same, the second argument can be a directory in which to move the file, i.e.
mv myfile.txt /data/. If you want to rename a file in the process, simply specify the full path: mv myfile.txt /data/newfilename.txt.
- mkdir {directory}
- Create a new directory.
- cp {oldfile} {newfile}
- Copy a file to a new location. Just like
mv, if you want to leave the file name unchanged, the second argument can be a location: cp myfile.txt /newdirectory/ is the same as cp myfile.txt /newdirectory/myfile.txt.
cp -r — recursively copy a directory, i.e., copy a directory and all of its contents.
- rm {file}
- Remove (delete) a file. Note this does not put the file in your trash, it deletes it.
rm -r — Recursively delete a directory, i.e., delete a directory and anything it contains. Use with care.
- less {file}
- Read the contents of a text files. Press the space bar to get another page of the file, and q to quit. See also
more.
- file {file}
- Determine whether a file is a file, directory, executable, etc. Sometimes these things are color-coded, but if not, this is handy to have.
Intermediate
- top
- Updating list all of the processes currently running on a computer, including user, CPU usage, and memory usage. Press q to quit.
- ps -au
- List all processes (
-a) by user (-u). This is a static list, not updated like top. Provides the process ID (PID), useful for some other commands.
ps -aux — List all processes including processes without controlling terminals (system processes).
- kill {pid}
- Force a currently running process to stop. You can only do this for processes you own. The user owning a process, and the process id (pid), can be obtained from the
top or ps commands. If the process doesn't respond you can use the -9 option, but generally you should try it without this option first, which gives the process a chance to exit more normally.
- which {program}
- Tells you the path to the program in question. This is handy if you forget where you installed some software, or if you have two versions of the same program. For example, let's say you install your own version of
ls for some reason into the bin directory in your home directory (i.e. $HOME/bin). The system version is in /bin/ls. If you type ls, you can tell which one you are using.
A little advanced
- who
- Lists users currently logged onto your machine.
- nice {command}
- Runs the specified command at a lower priority level on the system, freeing up more resources for higher priority processes. Use
renice to change the priority of already-running processes.
- chmod [option] {file}
- Changes file access permissions. If you do a long listing of files (
ls -l), you will see 10 letters that indicate access levels. The first is either a d to indicate a directory, or a -. The next 9 indicate read, write, or execute access for the owner (the first 3), group (next 3), or all (last 3). If you see a file that has permissions of -rw-r--r--, this indicates that the owner has read and write access, but the group and others only have read access. To give everyone in the group write access, type chmod g+w {file}.
- chown {owner} {file}
- Changes the owner or group ownerwhip of a file.
Pipes and redirects
- The pipe (|) operator is generally accessed using shift and the backslash (\). The pipe directs the output of one command to the next. For example, let's say you have 5,000 files in a directory. If you look at them all using
ls, they go by too quickly to look at—perhaps you could look at them a page at a time. You know that less will look at text a page at a time. So, you simply need to pass the output of ls to less: ls | less.
- You can redirect output to a textfile using >, which is handy if you want to examine all items later on. For example, if your IT friend Ethan is asking about all of the 5,000 files in your directory and you just want to send him a list, you can type
ls > mylist.txt. This will take the output of ls, which would normally appear on the screen, and instead save it to the file you specify (in this case, mylist.txt).