Linux: Basic Shell Commands

By Xah Lee. Date: . Last updated: .

This is a list of most frequently used linux commands.

These are essential commands. Most of them are used everyday by every linux user.

The code here are based on Ubuntu Linux, but 99% of them work in any unix, including Mac OS X.

Navigate Directory

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
cd ..go up one dir
pwdshow the current dir

Many of these command's argument can be either a file name (relative to current directory), or a full file path. For example, ls /usr/local/bin

You can set bash to show current dir path and time in your prompt.

[see Linux: Bash Prompt Setup]

File/Directory Manipulation

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. [see Linux: Show Directory Size: du]

Viewing Files

cat fnamedisplay file content
cat fname | moreview a file by page. Type 【q】 to exit. Type 【h】 for other keys.
vi fnameview a file. Type 【Escape : q】 to exit. [see vim Basics]
head fnameview the first few lines of a file. (most useful for big files, e.g. log file.)
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. (For example, text, jpg, png, pdf, ….)

Locating Commands

type cmdshow if cmd is a shell built-in or standalone program. For example, type kill. The command 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 done automatically. Useful only if you just installed bunch of new commands.)

Linux find executable files 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. [see Linux: File Permission System]

Install Program/Package

The following are for Ubuntu Linux.

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.

[see Linux: How to Install/Remove Packages]

To install and compile a new command, here's the typical steps:

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.

[see Linux: Get System Info]

Archive, Compression {tar, gzip}

tar cvf new name.tar dirpathtar up a dir.
tar xvf filename.taruntar.
zip -r new dirnamezip a dir
unzip filenameunzip
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.

[see Linux: Compression How-to: tar gzip bzip2 xz 7zip rar zip]

Text Processing

Version Control

Fetching and 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. [see Linux: Monitor Processes: top]
pstreeshow the process parent-child relationship

A better program for monitoring processes than “top” is “htop”. [see Linux: Monitor Processes, “htop”]

Job Control

Linux: Job Control

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. For example, 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 (aka soft link) of a file. (symbolic link is basically a file path.)
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)

[see Linux: File Permissions]

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

[see Linux: Users and 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. For example, alias l="ls -al --color"

[see Linux: Show Opened Files, lsof]

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 take 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. For example, ls -l `which more`
cmd &run the command cmd in background.
echo $?show exit status of previous command.

Bash Keys

Linux: Bash Keys, Terminal Keys, Man Page Keys

Linux {Desktop, Window Manager} Overview

Linux: How to Switch to LXDE, Xfce

Linux Shell Basics

  1. Get System Info
  2. Shell Basics
  3. grep, cat, awk, uniq
  4. sort
  5. find, xargs
  6. diff Files/Dir
  7. dir size: du
  8. dir tree
  9. tar gzip bzip2 xz 7zip rar zip
  10. wget, curl, GET, HEAD
  11. rsync
  12. Install Packages

Sys Admin

  1. Job Control
  2. ps
  3. top
  4. htop
  5. RAM stat
  6. Users and Groups
  7. File Permission
  8. Opened Files: lsof
  9. shutdown, sleep


  1. Bash Keys, Terminal Keys, Man Page Keys
  2. Bash Prompt Setup
  3. Bash Color Prompt
  4. .bashrc, .profile, .bash_profile
  5. Virtual Console
  6. Terminal Control Sequence Keys
  7. Reset Terminal
  8. tmux
  9. man page
  10. Bash Manual in Chapters
  11. BASH Shell Misc Tips
  12. Log Terminal Session

Linux Desktop

  1. Most Useful GUI Apps
  2. Set Default App
  3. Shell Commands for GUI Apps
  4. Image Viewers
  5. Music Players
  6. Move File to Trash by Command
  7. X11 Selection and Clipboard
  8. How to Switch to LXDE, Xfce
  9. LXDE Keyboard Shortcuts
  10. LXDE/Openbox, Change Keyboard Shortcuts
  11. LXDE Set Key Repeat Rate
  12. LXDE/OpenBox, Disable Mouse Scroll Wheel Hide Window
  13. Xfce Keyboard Shortcuts
  14. Xfce Good Themes
  15. xmonad Keybinding
  16. How to Restart X11
  17. Why Tiling Window Manager Sucks
  18. Standard Fonts
  19. How to Install Font

If you have a question, put $5 at patreon and message me.