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

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
cd ..
Go up one dir
pwd
Show 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 fileName
Create a new file, or update timestamp of the file if it exists already.
rm fileName
Delete a file
rm -r dirName
Delete a directory. (careful!)
cp fileName newFileName
Copy a file
cp -r dirName newName
Copy a dir
mkdir newDirName
Create a new dir
rmdir dirName
Delete a dir only if it is empty
mv fileName newName
Rename file, or move to a diff dir.
du -sh dirName
Show dir size. [see Linux: Show Directory Size: du]

Viewing Files

cat fname
Display file content
cat fname | more
View a file by page. Type q to exit. Type h for other keys.
vi fname
View a file. Type Escape : q to exit. [see vim Basics]
head fname
View the first few lines of a file. (most useful for big files, e.g. Log file.)
tail fname
View the last few lines of a file.
tail -f fname
View the last few lines of a growing file, updated continuously. Typically used on log files.
file fname
Report what type of file it is. (For example, text, jpg, png, pdf, ….)

Locating Commands

type cmd
Show 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 cmd
Show 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 cmd
View documentation of a command. q to exit. h for help.
apropos string
Search man pages.
locate fname
Find a file by name (using the database see man updatedb). This is similar to find dir_paths -name "*fname*" but much faster.
updatedb
Update 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 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
apt-get update
Sync package index files from sources. (need to do this regularly)
apt-get upgrade
Upgrade all installed packages to latest versions (if any).
apt-get dist-upgrade
Update 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
./configure
make
sudo make install # optional. This basically copy the binary to /usr/local/bin

Example: How to Build Emacs on Linux .

uname -a
Print your OS info.
df --si
Print disk usage.

[see Linux: Get System Info]

Archive, Compression (tar, gzip)

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

Text Processing

Version Control

Git Basics

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

Managing Process

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. [see Linux: Monitor Processes: top]
pstree
Show 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 commandString
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. For example, default Ubuntu)
chmod 664 fileName
Change the perm bits. (664 = rw-rw-r--; typical text file perm bits)
chown userName fileName
Change owner of a file.
chgrp groupName fileName
Change the group of a file.
ln -s newPath existingPath
Make a symbolic link (aka soft link) of a file. (symbolic link is basically a file path.)
ln newPath existingPath
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)

[see Linux: File Permissions]

useradd userName
Create a new user account. (On Debian based Linuxes, there's higher-level “adduser” written in perl.)
passwd userName
Change password for user.
id userName
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 Linux: Users and Groups]

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
source fname
Execute a file fname. source fname is equivalent to . fname
bash
Start a new bash. Ctrl+d to exit when done.
echo $PATH
View value of a environment variable PATH.
env
Show 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 *.txt
A 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 | 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 > newFileName
Join contents of fileName1 fileName2 to newFileName
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. For example, ls -l `which more`
cmd &
Run the command cmd in background.
echo $?
Show exit status of previous command.

Bash Keys

Linux: Bash/Terminal Keys

Linux {Desktop, Window Manager} Overview

Linux: How to Switch to LXDE, Xfce

Linux Shell Basics

Sys Admin

Bash/Terminal

Linux Desktop