Linux: Basic Shell Commands

By Xah Lee. Date: . Last updated: .

This is a list of most frequently used linux commands.

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. 〔➤see Linux: Bash Prompt, Add Color, DateTime

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

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. 〔➤see Emacs: File Management (dired tutorial)〕 But you have to learn emacs first. 〔➤see Xah Emacs Tutorial

Also, tree is a nice command for getting sense of directory structure, useful for big projects. 〔➤see 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. 〔➤see vim Basics
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. (➢ for example: text, jpg, png, pdf, ….)

Locating Commands

type cmdshow if cmd is a shell built-in or standalone program. ➢ for example: 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. 〔➤see 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.

〔➤see 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.

〔➤see Linux: How to Find 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 & 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” Tutorial
pstreeshow the process parent-child relationship

A better program for monitoring processes than “top” is “htop”. 〔➤see 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. ➢ 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, Groups, Tutorial

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 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. ➢ for example: ls -l `which more`
… &run a command 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