- Presented by
- Victor Gedris
- In Co-Operation with
- The Ottawa Canada Linux Users Group
- and
- ExitCertified
- Markdown formatted version with minor changes by
- Dirk Eismann, 2018-07-28
This manual was written with the intention of being a helpful guide to Linux users who are trying to become familiar with the Bash shell and basic Linux commands. To make this manual useful to the widest range of people, I decided to release it under a free documentation license, with the hopes that people benefit from it by updating it and re-distributing modified copies. You have permission to modify and distribute this document, as specified under the terms of the GNU Free Documentation License. Comments and suggestions for improvement may be directed to: vic@gedris.org .
This document was created using an Open Source office application called Open Office. The file
format is non-proprietary, and the document is also published in various other formats online.
Updated copies will be The original Open Office document is available on Vic Gedris' web site http://gedris.org/LinuxShellIntro.html [http://vic.dyndns.org/].
For more information on Open Office, please visit http://www.openoffice.org/ .
Copyright © 2003 Victor Gedris.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU
Free Documentation License, Version 1.1 or any later version published by the Free Software
Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
Texts. A copy of the license is available from the Free Software Foundation's website:
http://www.fsf.org/copyleft/fdl.html
Document Version: 1.2, 2003-06-25
The purpose of this document is to provide the reader with a fast and simple introduction to using the Linux command shell and some of its basic utilities. It is assumed that the reader has zero or very limited exposure to the Linux command prompt. This document is designed to accompany an instructor-led tutorial on this subject, and therefore some details have been left out. Explanations, practical examples, and references to DOS (nowadays Windows Command Promt) commands are made, where appropriate (for the readers how are familiar with DOS).
-
A program that interprets commands
-
Allows a user to execute commands by typing them manually at a terminal, or automatically in programs called shell scripts.
-
A shell is not an operating system. It is a way to interface with the operating system and run commands.
Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. Released in 1989, it has been distributed widely as the shell for the GNU operating system and as a default shell on Linux and OS X.
-
BASH
=is an acronym for Bourne Again SHell -
Bash is a shell written as a free replacement to the standard Bourne Shell (/bin/sh) originally written by Steve Bourne for UNIX systems.
-
It has all of the features of the original Bourne Shell, plus additions that make it easier to program with and use from the command line.
-
Since it is Free Software, it has been adopted as the default shell on most Linux systems.
-
Case Sensitivity:
In Linux/UNIX, commands and filenames are case sensitive, meaning that typing "EXIT" instead of the proper "exit" is a mistake. -
"\" vs. "/":
In DOS, the forward-slash "/" is the command argument delimiter, while the backslash "" is a directory separator. In Linux/UNIX, the "/" is the directory separator, and the "" is an escape character. More about these special characters in a minute! -
Filenames:
The DOS world uses the "eight dot three" filename convention, meaning that all files followed a format that allowed up to 8 characters in the filename, followed by a period ("dot"), followed by an optional extension, up to 3 characters long (e.g.FILENAME.TXT). In UNIX/Linux, there is no such thing as a file extension. Periods can be placed at any part of the filename, and "extensions" may be interpreted differently by all programs, or not at all.
Before we continue to learn about Linux shell commands, it is important to know that there are many symbols and characters that the shell interprets in special ways. This means that certain typed characters: a) cannot be used in certain situations, b) may be used to perform special operations, or, c) must be "escaped" if you want to use them in a normal way.
-
Character Description \Escape character. If you want to reference a special character, you must "escape" it with a backslash first.
Example:touch /tmp/filename\*/Directory separator, used to separate a string of directory names.
Example:/usr/src/linux.Current directory. Can also "hide" files when it is the first character in a filename. ..Parent directory ~User's home directory *Represents 0 or more characters in a filename, or by itself, all files in a directory.
Example:pic*2002can represent the filespic2002,pic anuary2002,picFeb292002, etc.?Represents a single character in a filename.
Example:hello?.txtcan representhello1.txt,helloz.txt, but nothello22.txt[ ]Can be used to represent a range of values, e.g. [0-9], [A-Z], etc.
Example:hello[0-2].txtrepresents the nameshello0.txt,hello1.txt, andhello2.txt|“Pipe”. Redirect the output of one command into another command.
Example:ls | more>Redirect output of a command into a new file. If the file already exists, over-write it.
Example:ls > myfiles.txt>>Redirect output of a command onto the end of an existing file.
Example:echo “Mary 555-1234” >> phonenumbers.txt<Redirect a file as input to a program.
Example:more < phonenumbers.txt;Command separator. Allows you to execute multiple commands on a single line.
Example:cd /var/log ; less messages&&Command separator as above, but only runs the second command if the first one finished without errors.
Example:cd /var/logs && less messages&Execute a command in the background, and immediately get your shell back.
Example:find / -name core > /tmp/corefiles.txt &
- Most common commands are located in your shell's “PATH”, meaning that you can just
type the name of the program to execute it. Example: Typing
lswill execute the "ls" command. - Your shell's “PATH” variable includes the most common program locations, such as
/bin, /usr/bin, /usr/X11R6/bin, and others. - To execute commands that are not in your current PATH, you have to give the complete
location of the command.
Examples:
/home/bob/myprogram
./program (Execute a program in the current directory)
~/bin/program (Execute program from a personal bin directory)
- Commands can be run by themselves, or you can pass in additional arguments to make them do
different things. Typical command syntax can look something like this:
command [-argument] [-argument] [--argument] [file]
Examples:
lsList files in current directory
ls -lLists files in “long” format
ls -l --colorAs above, with colourized output
cat filenameShow contents of a file
cat -n filenameShow contents of a file, with line numbers
When you're stuck and need help with a Linux command, help is usually only a few keystrokes away! Help on most Linux commands is typically built right into the commands themselves, available through online help programs (“man pages” and “info pages”), and of course online.
Many commands have simple “help” screens that can be invoked with special command flags. These flags usually look like -h or --help. Example: grep --help
The best source of information for most commands can be found in the online manual pages, known as “man pages” for short. To read a command's man page, type man command.
Examples:
man ls Get help on the “ls” command.
man man A manual about how to use the manual!
To search for a particular word within a man page, type /"word”. To quit from a man page, just type the Q key.
Sometimes, you might not remember the name of Linux command and you need to search for it. For example, if you want to know how to change a file's permissions, you can search the man page descriptions for the word “permission” like this: man -k permission
If you look at the output of this command, you will find a line that looks something like:
chmod (1) - change file access permissions
Now you know that chmod is the command you were looking for. Typing man chmod will show you the chmod command's manual page!
Some programs, particularly those released by the Free Software Foundation, use info pages as their main source of online documentation. Info pages are similar to man page, but instead of being displayed on one long scrolling screen, they are presented in shorter segments with links to other pieces of information. Info pages are accessed with the info command, or on some Linux distributions, pinfo (a nicer info browser).
For example:
info df Loads the “df” info page.
The Linux filesystem is a tree-like hierarchy hierarchy of directories and files. At the base of the filesystem is the “/” directory, otherwise known as the “root” (not to be confused with the root user). Unlike DOS or Windows filesystems that have multiple “roots”, one for each disk drive, the Linux filesystem mounts all disks somewhere underneath the / filesystem. The following table describes many of the most common Linux directories.
-
Directory Description /The nameless base of the filesystem. All other directories, files, drives, and devices are attached to this root. Commonly (but incorrectly) referred to as the “slash” or “/” directory. The “/” is just a directory separator, not a directory itself. /binEssential command binaries (programs) are stored here ( bash,ls,mount,tar, ...)./bootStatic files of the boot loader. /devDevice files. In Linux, hardware devices are acceessd just like other files, and they are kept under this directory. /etcHost-specific system configuration files. /homeLocation of users' personal home directories (e.g. /home/susan)./libEssential shared libraries and kernel modules. /procProcess information pseudo-filesystem. An interface to kernel data structures. /rootThe root (superuser) home directory. /sbinEssential system binaries ( fdisk,fsck,init, ...)./tmpTemporary files. All users have permission to place temporary files here. /usrThe base directory for most shareable, read-only data (programs, libraries, documentation, and much more). /usr/binMost user programs are kept here ( cc,find,du, ...)./usr/includeHeader files for compiling C programs. /usr/libLibraries for most binary programs. /usr/local“Locally” installed files. This directory only really matters in environments where files are stored on the network. Locally-installed files go in /usr/local/bin,/usr/local/lib, ...). Also often used for software packages installed from source, or software not officially shipped with the distribution./usr/sbinNon-vital system binaries ( lpd,useradd, ...)/usr/shareArchitecture-independent data (icons, backgrounds, documentation, terminfo, man pages, etc.). /usr/srcProgram source code. E.g. The Linux Kernel, source RPMs, etc. /usr/X11R6The X Window System. /varVariable data: mail and printer spools, log files, lock files, ...
The first thing you usually want to do when learning about the Linux filesystem is take some time to look around and see what's there! These next few commands will: a) Tell you where you are, b) take you somewhere else, and c) show you what's there. The following table describes the basic operation of the pwd, cd, and ls commands, and compares them to certain DOS commands that you might already be familiar with.
-
BASH
CommandDOS
CommandDescription pwdcd"Print Working Directory" shows the current location in the directory tree. cdcd,chdir"Change Directory", when typed all by itself, it returns you to your home directory. cd ~“~” is an alias for your home directory. It can be used as a shortcut to your “home”, or other directories relative to your home. pwdpwd
