Linux command line usage tutorial

Shell keys -- Basic commands -- Documentation -- I/O redirection -- Permissions -- Installing software -- Configuration -- User accounts -- File systems -- More commands -- Custom commands -- References

Shell key shortcuts

Command-line editing

Handled by readline library compiled into bash shell; also available as frontend rlwrap / rline.

Key Action   Key Action
Tab Auto-complete command or file name Alt-!, Alt-$, Alt-/ Autocomplete command / shell variable / file name
Alt-B, Alt-F Word backward / forward Alt-Backspace Backspace word
Ctrl-A, Ctrl-E To start / end of line Ctrl-K, Ctrl-W Delete to end of line / to previous space
Ctrl-R, Alt- > Search history backwards / go to latest Ctrl-Y, Insert; Alt-Y Insert latest kill / replace by previous

Terminal shortcuts

Key Action Signal Notes
Ctrl-C Interrupt (permanently) INT See kill (1), signal (7)
Ctrl-Z Stop (interrupt temporarily) STOP Cannot be caught
jobs, fg View / continue stopped job -- Shell commands, not terminal shortcut
Ctrl-S, Ctrl-Q Stop / continue terminal output -- Doesn't work with PuTTY
Shift-PageUp, Shift-PageDown Scroll terminal output --  
Ctrl-D Exit shell --


Basic commands

Command Action Type
cp, mv, rm Copy / move or rename / remove file Binary exe
mkdir, rmdir Create / remove directory Binary exe
cd Change current directory (cd - back) Shell command
ls, ll Display files and directories Binary exe / alias for ls -l
top, ps, pstree Display running processes Binary exe

Common options

-h Print usage message (help)
-i Interactive (user confirmation); or case insensitive
-r, -R Recursively process subdirectories
-q Quiet (less or no output)
-v Verbose (more output)



help Shell commands man Most binary exe's
info Gnu project programs /usr/share/doc/... Dummyware (HTML or PDF)

Manual page sections (see man man): 1) user commands, 2) system calls, 3) library functions, 5) configuration files, 8) administration commands (etc.). Nomenclature: "see ls (1)". Explicitly give section or display manual pages from all sections:

man 1 ls       # view manual page about ls from section 1
man 1 intro    # view introduction to manual page section 1
man -a kill    # view manual page from all sections
man -k kill    # search headlines

Manual pages are displayed with less by default. Key summary:

Cursor keys, Page up/down, Home, End as usual
< > To top / to bottom
/ Search (regular expressions allowed; history)
n N Next / previous search result
q Quit


Input / output redirection

cmd > file Redirect output to file cmd < file Get input from file
cmd &> file Redirect stdout and stderr cmd << EOF Get input from "here document"
cmd1 | cmd2 Pipe: output of cmd1 = input of cmd2 = following lines until EOF
cmd `cmd2` Put output of cmd2 on command line of cmd1 cmd <<<"String" Get input from string

Commands useful in pipes

cat Pass data through tee Copy to file and stdout
wc Count characters, words, lines head, tail First or last n lines
grep Filter lines by regular expression cut, tr Extract columns / transliterate


ls -1 | wc -l    # number of files and subdirectories
ps -lf | cat     # prevent right cutoff


Access permissions

UNIX file permissions: Read, write and execute for file owner, group and others.

Command Action Argument / Syntax Notes
ls -l List file(s) -rwxr-x--- Readable and executable by owner and group, writable only by owner, no access for others
chmod Change file permissions a+x / o=r Enable execution by all / make read-only for others
chgrp Change file group Group ID
chown Change file ownership User ID Only root (admin) may do this
id List groups of a user User ID Argument defaults to current user
newgrp Change user's current group Group ID This determines group of new files
umask Change file permission mask as for chmod This determines permissions of new files

Frequent use: Restricting access to devices by assigning a dedicated group to the device file and disabling access for others:

crw-rw---- 1 root lp 6, 0 2011-11-07 09:27 /dev/lp0


Installing software

Distribution packages

Ubuntu offers ~37000 packages -- enough for most purposes.

Package managers differ between distros. Ubuntu has the high-level command-line tool aptitude. Syntax:

aptitude [<options>] <command> <arguments>
search <keyword> Search for package (-w 170 recommended) show <package> Show package details
install <package> ... Install package(s) and their dependencies remove <package> ... Remove package(s)

Compile from source

Compile as normal user:

Install as root:

Exercise: Print out (exclusively) the Ubuntu packages that start with "abc" (Hint: \< is the regular expression for a left word boundary)

Configuration information

Configuration files

Global configuration files are text files in /etc. Notable config files:

/etc/passwd Users and their primary group, no password /etc/shadow Users and hashed password
/etc/network/interfaces Network interface configuration /etc/ssh/sshd_config and .../ssh_config SSH server and client configuration
/etc/init.d/ Scripts starting / stopping services /etc/profile.d/... Settings read by shell at startup
/etc/apt/apt.conf.d/ Settings for aptitude /etc/X11/xorg.conf X11 (GUI) server configuration

See section 5 manual pages for documentation. Edit with any text editor as root, except for passwd and friends (see below).

Environment variables

Like shell variables, they are dereferenced with "$" in the shell.

$PATH Executable search paths separated by ":" (prettyprint: echo $PATH | tr : '\n')
$EDITOR Default text editor $PAGER Program to view text page by page
$PRINTER Default printer $http_proxy, $ftp_proxy, $no_proxy HTTP / FTP proxy / exceptions

Listing environment variables:


To set an environment variable globally, add something like the following to a file ending .sh in /etc/profile.d: (For per-user settings, put the line in ~/.profile or ~/.bashrc.)

export PATH="new/path:$PATH"


User administration

adduser, deluser, addgroup, delgroup high-mid-level user / group creation / modification / removal according to /etc/adduser.conf
useradd, userdel, usermod, groupadd, groupdel mid-level user / group creation / modification / removal
vipw, vigr, visudo Edit passwd / group / sudoers file with $EDITOR; with syntax check
adduser --ingroup users newuser  # give --ingroup to prevent usergroup creation
adduser newuser othergroup       # additional group membership

Administrator access

Shell Command Password Notes
su -l su -c root Still safe if user password is compromised
sudo -i sudo user Access can be revoked without root password change

/etc/sudoers syntax:

<user / %group>  <host(s)> = ( <target user> )  <command(s)>
thisuser ALL = (root) ALL          # allow thisuser to do anything as root
%admin ALL = (root) ALL            # allow admin group to gain root privileges
thisuser ALL = (otheruser) opera   # allow thisuser to run opera as otheruser

File systems

No drive letters as in Windoze, but one large directory tree. File systems are made available at designated directories called mount points.

Informational commands and files

/proc/partitions Disk partitions and sizes
/etc/fstab Configured partitions, file systems and mount points
df -h, /proc/mounts, /etc/mtab Mounted file systems

Mounting and unmounting a file system: (root only, except with the "user" option in /etc/fstab)

mount /dev/sdc1 /mnt/disk   # mount <device representing partition> <mount point>
umount /mnt/disk            # historical misspelling

Device /dev/sdc1 = sd SCSI disk, c third disk, 1 first partition.

Device files (traditionally in /dev) map to Linux device drivers. File-like access is usually possible:

cp /dev/sdc1 /tmp/image Create partition image (better use dd)
less /dev/ttyS0 Read from serial interface (assumes baud rate etc. have been set correctly)

Symbolic links refer to other files by name. Create with:

ln -s path/to/target link_name

Use ls -F to identify special files, and ls -l or readlink to see target.

Exercise: List all symlinks in /usr/bin with the help of ls -l and grep.

More commands


In order of decreasing suitability for dummies.

GUI editors nedit, emacs / xemacs
Text-based editors running in a terminal pico, emacs -nw, vi
Automated editing in pipes sed, awk

Finding and identifying files

find Search for files by various criteria grep -rl Search for files by content (-i case insensitive)
locate Search for files by name in database dpkg -L / dpkg -S List files in package / package of file
file Identify file type identify Identify image type
find . -iname \*ircam\*   # Search current dir for file names containing "ircam" (case insensitive)
grep -irl ircam .         # Search current dir for files containing the string "ircam"

Science and engineering

bc -l Arbitrary precision calculator units Unit conversion
socat Network socket and serial interface access hexdump, hd, od Hex dump
hexedit Hex editor; supports bit searches with Volker's patch convert Image conversion and automated processing


Creating new commands

Aliases alias newcmd="..." Substituted by string by shell "Batch files" Execute with source or .
Shell functions function newcmd() { ... } Can take arguments Shell scripts, binary exe's Put somewhere in $PATH

Interpreter scripts

If the first line of a text file is #!/path/to/interpreter and it has its executable bits set, the system will execute it by calling the interpreter with the script file name as an argument (and following command line arguments as applicable).

Shell scripts

Interpreter line is #!/bin/sh, or #!/bin/bash if bash-specific features are required.

Command line arguments are $1, $2 and so on; number of arguments is $#. Use "$1" to preserve spaces; "$*" is all parameters as one string, "$@" is all parameters as separate strings. Further shell variables are created by assignment: varname=value.

Control flow:

if [ -n "$var" ]; then do_that ; fi             # help if;  help test
for var in 1 2 foo bar ; do echo $var ; done    # help for
output_files | while read f ; do cat $f ; done  # help while; help read

Shell wrapper scripts for argument handling

bc -l <<EOF
($1 - 32)*5/9


Further reading

man bash | wc -l      # ...and all worth reading

UNIX tutorial with book recommendations

Introductory UNIX course with exercises

Comprehensive guide to programming the shell bash

List of useful command-line programs sorted by topic

Alphabetical list of shell commands and frequently used utilities

TOS / Impressum