Linux Tutorial by Example: Most Frequently Used Shell Commands

, , …,

This is a list of most frequently used unix/linux commands. Each is used multiple times per day. To work in Linux efficiently, you should memorize by reflex every command on this page.

The code here are based on Ubuntu Linux, but 99% of them work in any unix, including Mac OS X. (except a few that are specific to Linux or distro.)

File/Directory Manipulation

lslist files in current directory
ls -allist all files in current dir, including dot files
ls -al | grep stringshow file name matching string
cd dirpathchange directory
cdgo to $HOME dir
pwdshow the current dir

You can set bash to show current dir path and time in your prompt. 〔➤ Linux: Bash Shell Prompt Color & Timestamp

touch filenamecreate a new file, or update timestamp of the file if it exists already.
rm filenamedelete a file
rm -r dirnamedelete a directory. (careful!)
cp filename new filenamecopy a file
cp -r dirname new namecopy a dir
mkdir new dir namecreate a new dir
rmdir dirnamedelete a dir only if it is empty
mv filename new namerename file, or move to a diff dir.
du -sh dirnameshow dir size. 〔➤ Linux: Show Directory Size: du

Many of these command's argument can be either a file name (relative to current directory), or a full file path.

Emacs's dired lets you do these much easier. 〔➤ Emacs: File Management (dired tutorial)〕 But you have to learn emacs first. 〔➤ Xah Emacs Tutorial

Also, tree is a nice command for getting sense of directory structure, useful for big projects. 〔➤ Linux: View Directory as Tree

Viewing Files

cat fnameview a file
cat fname | moreview a file by page. Type 【q】 to exit. Type 【h】 for other keys. more can also be less; the latter is better.
vi fnameview a file. Type 【Esc : q】 to exit. 〔➤ Emergency vi (vim tutorial)
head fnameview the first few lines of a (big) file. (most useful for log files.)
tail fnameview the last few lines of a file.
tail -f fnameview the last few lines of a growing file, updated continuously. Typically used on log files.
file fnamereport what type of file it is.

Locating Commands

type cmdshow if cmd is a shell built-in or standalone program. ⁖ type kill. type is a built-in bash command, not a standalone shell util. Try type type
which cmdshow full path of a command, useful for checking if a program is installed (if it's in the search path in $PATH environment variable.)
man cmdview documentation of a command. 【q】 to exit. 【h】 for help.
apropos stringsearch man pages.
locate fnamefind a file by name (using the database see man updatedb). This is similar to find dir paths -name "*fname*" but much faster.
updatedbupdate the database used by locate. (this is doen automatically. Useful only if you just installed bunch of new commands.)

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, type ls -l /usr/bin to see them. 〔➤ Intro to Linux File Permission System

Install Programs/Debian-Package

The following are for Ubuntu derived Linux distro. (Ubuntu is derived from Debian.)

apt-cache search namefind package name for install by “apt-get”
apt-cache show namedescribe package name
apt-get install nameinstall a new program. (usually used with sudo in front)
apt-get remove nameremove (un-install) a program.
apt-get purge nameremove a program and its config files.
dpkg -llist all installed packages
apt-get updatesync package index files from sources. (need to do this regularly)
apt-get upgradeupgrade all installed packages to latest versions (if any).
apt-get dist-upgradeupdate OS kernel, and others.

〔➤ Linux: Package System {dpkg, apt-get} Tutorial

Typical install from source code:

gzip -d filename.tar.gz
tar xvf filename.tar

cd dirname
sudo make install # optional. This basically copy the binary to /usr/local/bin

Example: How to Build Emacs on Linux.

〔➤ Linux: How to Find {kernel version, Distro, CPU, RAM, Disk Size} Info

Archive, Compression {tar, gzip}

tar cvf new name.tar dirpatharchive a folder.
tar xvf filename.tarunarchive.
gzip filenameCompress a file.
gzip -d filenameDecompress a file.
bzip2 filenameCompress a file.
bzip2 -d filenameDecompress a file.
xz filenameCompress a file.
xz -d filenameDecompress a file.

〔➤ Linux: Compression How-to: tar gzip bzip2 xz 7zip rar zip

Text Processing

Version Control


Fetching & Sync Remote Files: rsync, unison, wget, curl

Managing Process

ps -efview running processes
ps -ef | grep namefind a particular process
kill pidquit a program that has process id pid
kill -s 9 pidforce quit a process
topmonitor processes with continuous update. q to quit. 〔➤ Linux: Monitor Processes, “top” Tutorial
pstreeshow the process parent-child relationship

A better program for monitoring processes than “top” is “htop”. 〔➤ Linux: Monitor Processes, “htop” Tutorial

Job Control

Linux: Job Control Tutorial

Sys Admin

sudo command stringrun a command as “root” (“root” is the name of default admin account.)
suswitch to “root”
sudo su rootswitch to “root”. (useful when root isn't setup as a login account. ⁖ default Ubuntu)
chmod 664 filenamechange the perm bits. (664 = rw-rw-r--; typical text file perm bits)
chown user name filenamechange owner of a file.
chgrp group name filenamechange the group of a file.
ln -s new path existing pathmake a symbolic link of a file. (symbolic link is file who's content is a path of another file.)
ln new path existing pathcreate hard link of a file. (Hard link makes 2 files pointing to the same index in the file system (hard disk).)
shutdown -r 0restart machine now. (power off is -P)

〔➤ Linux: File Permission System Tutorial

useradd user namecreate a new user account. (On Debian based Linuxes, there's higher-level “adduser” written in perl.)
passwd user namechange password for user.
id user nameshow the id number of a user, and all groups he belongs to.
cat /ect/passwdlist all users
getent grouplist all groups. See getent --help

〔➤ Linux Sys Admin: Managing Users & Groups

dateshow current date and time
date --rfc-3339=secondsshow time stamp in this format: “yyyy-mm-dd hh:mm:ss-07:00” the last are time offset to UTC.
wshow who is logged in.
who -alist all users that have logged in recently.
uptimeshow how long the system's been running.
wccount the number of chars, words, lines. useful with cat, grep
source fnameexecute a file fname. source fname is equivalent to . fname
bashstart a new bash. 【Ctrl+d】 to exit when done.
echo $PATHView value of a environment variable PATH.
envshow all environment variables
alias str="cmd";make str as shortcut for cmd. ⁖ alias l="ls -al --color"

〔➤ Linux: Show Open Files, 「lsof」 Tutorial

Image Processing

Generic Useful Bash Syntax

cmd *.txtA 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 | cmd2pass the output of cmd1 to the input of cmd2
cat filename | cmdfeed the content of filename to the input of cmd
cmd > filenamewrite the output to file
cmd >> filenameappend output to file
cat filename1 filename2 > new filenamejoin contents of filename1 filename2 to new filename
cmd1; cmd2; …run several commands.
cmd1 && cmd2run 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.

Bash Keys

Linux: Bash Keys, Terminal Keys, Man Page Keys ⌨

Linux {Desktop, Window Manager} Overview

Intro to Linux Window Manager, Desktop Environment (Gnome, KDE, Xfce, xmonad, …)

blog comments powered by Disqus