This is a list of most frequently used unix/linux commands.
I've been a sys admin on Solaris and various unixes (HP-UX, Linux, BSDs, …) from 1997 to 2003. Then, recently started to explore linux again. Here's the list from my experience. The code here are based on Ubuntu Linux, but 99% of them work in any unix. (except a few that are specific to linux or distro. ⁖ {apt-get, shutdown, pstree, adduser})
| code | purpose |
|---|---|
ls | list files in current directory |
ls -al | list all files in current dir, including dot files |
ls -al | grep ‹string› | show file name matching ‹string› |
cd ‹dirpath› | change directory |
cd | go to $HOME dir |
pwd | show the current dir |
| code | purpose |
|---|---|
touch ‹filename› | create a new file (or updating timestamp of a existing file) |
rm ‹filename› | delete a file |
rm -r ‹dirname› | delete a dir |
cp ‹filename› ‹new filename› | copy a file |
cp -r ‹dirname› ‹new name› | copy a dir |
mkdir ‹new dir name› | create a new dir |
rmdir ‹dirname› | delete a dir only if it is empty |
mv ‹filename› ‹new name› | rename file, or move to a diff dir. |
du -sh ‹dirname› | show dir size. 〔☛ Linux: Show Directory Size: du〕 |
Emacs's dired lets you do these much easier. See: File Management with Emacs (dired tutorial).
Also, tree is a nice command for getting sense of dir structure. See: Linux: View Directory as Tree.
| code | purpose |
|---|---|
cat ‹filename› | view a file |
cat ‹filename› | more | view a file by page. Type 【q】 to exit. Type 【h】 for other keys. more can also be less; the latter is better. |
vi ‹filename› | view a file. Type 【Esc : q】 to exit. |
head ‹filename› | view the first few lines of a (big) file. (to get a idea what the heck the file contains) |
tail ‹filename› | view the last few lines of a file. |
tail -f ‹filename› | view the last few lines of a growing file, updated continuously. Typically used on log files. |
file ‹filename› | report what type of file it is. |
See also: Emergency vi (vi tutorial).
| code | purpose |
|---|---|
type ‹cmd name› | show if ‹cmd name› is a shell built-in or standalone program. ⁖ type kill. “type” is a bash built-in. |
which ‹cmd name› | show full path of a command, useful for checking if a program is installed, or if it's in the search path in $PATH environment variable. |
man ‹cmd name› | view documentation of a command. 【q】 to exit. 【h】 for help. |
apropos ‹string› | search man pages. |
locate ‹name› | find a file by name (using the database see man updatedb). This is similar to find ‹many dir paths› -name "*‹search string›*" but much faster. |
updatedb | update the database used by locate |
unix find executables by searching the $PATH environment variable. Try echo $PATH. It is a list of dir paths. They are searched in order. Excutable files must have executable bits on. That is, the “x”. For example, try ls -l /usr/bin.
| code | purpose |
|---|---|
apt-cache search ‹name› | find package ‹name› for install by “apt-get” |
apt-cache show ‹name› | describe package ‹name› |
apt-get install ‹name› | install a new program. (usually used with sudo in front) |
apt-get remove ‹name› | remove (un-install) a program. |
apt-get purge ‹name› | remove a program and its config files. |
dpkg -l | list all installed packages |
On Redhat based linuxes (⁖ Fedora, CentOS), the command to install apps is “yum”. See man yum.
Typical install from source code:
gzip -d ‹filename.tar.gz› tar xvf ‹filename.tar› cd ‹dirname› ./configure make sudo make install # optional. This basically copy the binary to /usr/local/bin
Example: Building Emacs on Linux — a Guide.
{uname, df}, see: Linux: How to Find {kernel version, Distro, CPU, RAM, Disk Size} Info
| code | purpose |
|---|---|
tar cvf ‹new name.tar› ‹dirpath› | archive a folder. |
tar xvf ‹filename.tar› | unarchive. |
gzip ‹filename› | To compress a file. |
gzip -d ‹filename› | Decompress a file. |
See: Linux Compression How-to: tar gzip bzip2 xz 7zip rar zip.
See: Unix Shell Text Processing Tutorial: grep, cat, awk, sort, uniq, find, xargs, …
See: Linux Tutorial: rsync, unison, wget, curl ◇ How to Use Unison for Syncing Files
| code | purpose |
|---|---|
ps -ef | view running processes |
ps -ef | grep ‹name› | find a particular process |
kill ‹pid› | quit a program that has process id ‹pid› |
kill -s 9 ‹pid› | force quit a process |
top | monitor processes with continuous update. q to quit. |
pstree | show the process parent-child relationship |
See: Linux: Monitor Processes, “top” Tutorial.
A much better tool is htop. See: Linux: Monitor Processes, “htop” Tutorial
see: Linux: Job Control Tutorial
| code | purpose |
|---|---|
sudo ‹command string› | run a command as “root” (“root” is the name of default admin account.) |
su | switch to “root” |
sudo su root | switch to “root”. (useful when root isn't setup as a login account. ⁖ default Ubuntu) |
| code | purpose |
|---|---|
useradd ‹user name› | create a new user account. (On Debian based linuxes, there's higher-level “adduser” written in perl.) |
passwd ‹user name› | change password for user. |
id ‹user name› | show the id number of a user, and all groups he belongs to. |
cat /ect/passwd | list all users |
getent group | list all groups. See getent --help |
See also: Linux: users and groups Admin.
| code | purpose |
|---|---|
chmod 664 ‹filename› | change the perm bits. (664 = rw-rw-r--; typical text file perm bits) |
chown ‹user name› ‹filename› | change owner of a file. |
chgrp ‹group name› ‹filename› | change the group of a file. |
ln -s ‹new name› ‹filename› | make a symbolic link of a file. (symbolic is file that contains the path of another file.) |
ln ‹new name› ‹filename› | create hard link of a file. (Hard link makes 2 files pointing to the same index in the file system (hard disk).) |
shutdown -r 0 | restart machine now. (power off is -P) |
| code | purpose |
|---|---|
date | show current date and time |
date --rfc-3339=seconds | show time stamp in this format: “yyyy-mm-dd hh:mm:ss-07:00” the last are time offset to UTC. |
w | show who is logged in. |
who -a | list all users that have logged in recently. |
uptime | show how long the system's been running. |
wc | count the number of chars, words, lines. useful with cat, grep |
| code | purpose |
|---|---|
source ‹shell file› | execute a shell file ‹shell file›. source ‹shell file› is equivalent to . ‹shell file› |
bash | start a new bash. 【Ctrl+d】 to exit when done. |
echo $PATH | View value of a environment variable. |
env | show all environment variables |
alias ‹str›="‹cmd›"; | make ‹str› as shortcut for ‹cmd›. ⁖ alias l="ls -al --color" |
| code | purpose |
|---|---|
‹cmd› *.txt | A asterisk “*” means any character. *.txt means all files ending in “.txt”. Can be used for any command that takes list of files or dir. See man 7 glob. |
‹cmd1› | ‹cmd2› | pass the output of ‹cmd1› to the input of ‹cmd2› |
cat ‹filename› | ‹cmd› | feed the content of ‹filename› to the input of ‹cmd› |
‹cmd› > ‹filename› | write the output to file |
‹cmd› >> ‹filename› | append output to file |
cat ‹filename1› ‹filename2› > ‹new filename› | join contents of ‹filename1› ‹filename2› to ‹new filename› |
‹cmd1›; ‹cmd2›; … | run several commands. |
‹cmd1› && ‹cmd2› | run ‹cmd1›, if success, then run ‹cmd2› (otherwise stop.) (the && is a logical “and” operator. Unix commands returns 0 if success, else a integer error code.) |
… `‹cmd›` … | generate the output of ‹cmd› and use it in your whole command. ⁖ ls -l `which more` |
… & | run a command in background. |
Linux: Bash/Terminal Keybinding List
Intro to Linux Window Manager & Desktop Environment (Gnome, KDE, Xfce, xmonad, …)