Introduction to Linux
INTRODUCTION TO LINUX
Oliver Standford STOR-i, Lancaster University Introduction to Linux | Oliver Standford
Virtual Box • Virtual Box - virtualisation software package by Oracle. • Virtualisation is when we use software to create a virtual version of hardware (computer) - HDD, RAM, CPU
• Installed on an existing host operating system: • Allows additional guest operating systems to run on top of the host operating system. • Compatible guest OS - Windows, Linux, Solaris, BSD, Mac. • Other Virtualisation software - Hyper-V(Microsoft), VMware, Parallels (Mac). Introduction to Linux | Oliver Standford
Virtual Media & Networking • Virtual Hard drives: • VDI - virtual disk image • VMDK - Vmware disk image • VHD - Microsoft disk image • CD/DVDs: • Host Drive • ISO Image file • Empty • Networking: • NAT - takes on the hosts IP address etc. • Bridged - gets its own IP, name etc on the network. • Internal • Host • USB Devices • Shared Folders Introduction to Linux | Oliver Standford
Operating a virtual Machine
• Create • Start • Pause/Resume • Shutdown Signal • Power off • Snapshots Introduction to Linux | Oliver Standford
Ubuntu Exercise
• Change your password – open a terminal and use the ‘passwd’ command.
• Change the desktop background.
• Open Firefox and browse to lancs.ac.uk
• Try to install software from the software centre
• View a calendar for 2017 in the terminal by typing ‘cal 2017’
• Open the following file: /etc/apt/sources.list using nano Introduction to Linux | Oliver Standford
Unix
• Unix was developed at AT&T Bell Labs in the 1970s.
• Not designed as a commercial Operating System but as a toolset for programmer’s.
• Source code was given out for a nominal fee.
• The growth and success of Unix was a result of thousands of programmers contributing to the project.
• One of the last popular operating systems that do not force you to work behind a Graphical Interface. Introduction to Linux | Oliver Standford
Unix Features
• Multitasking - processing more than one job at a time.
• Multi-User - supporting more than one user at a time.
• Networking - inter-machine communication and sharing.
• X-windows - a server oriented graphics system and interface. Introduction to Linux | Oliver Standford
Linux
• An open source version of Unix.
• Started from Linus Torvalds in 1991
• Uses tools developed by the Free Software Foundation.
• One of the most prominent examples of free software and open source development.
• Source code can be modified, used and redistributed.
• Available on a variety of platforms - Intel, Alpha, Mac. Introduction to Linux | Oliver Standford
Operating Systems
• We can think of an operating system as consisting of four components:
• Kernel
• Utilities
• Libraries
• File System Introduction to Linux | Oliver Standford
Kernel • The core of the operating system.
• Manages devices, memory, user and system processes.
• Schedules the use of system resources - RAM,CPU,HDD.
• Stores information about arrangement of computer system and network. Introduction to Linux | Oliver Standford
Shell
• The shell is a utility that provides an interface between the user and the operating system. • Shells act as command interpreters of user input. Introduction to Linux | Oliver Standford
Filesystem
/bin - All executable Binaries
/boot - boot loader / Root /etc - Configuration files Directory
/home - Contains the home directories
/tmp - A place for temporary files. Introduction to Linux | Oliver Standford File Attributes & Permissions Permissions R - Read W - Write X - Execute - - No Permission
User Group Others Modified Group Date.
-rwxr-xr-x 20 oliver stori 4096 2012-09-22 sum.c
Type Owner - - file File Size d - Directory (bytes) l - Link No. of links Filename Introduction to Linux | Oliver Standford
User Accounts
• A user account consists of three things:
• Username - a username associated with a unique identifier (User ID: UID).
• Password - your personal key to the account.
• Home Directory - disc space allocated to you on the system. Typically /home/username. Introduction to Linux | Oliver Standford
Command Syntax
• Most UNIX commands can be given options and arguments. • Unix commands are often described with a notation like:
command [-options]
• Square brackets refer to options (none of which are required). • Arguments are given in <>. Introduction to Linux | Oliver Standford
Basic Commands
• ls - list files. • cp - copy files. • mv - move files. • rm - remove (delete) files. • cd - Change directory. • mkdir - Make (create) directory. • rmdir - Remove (delete) directory. • ln - create a link to file. Introduction to Linux | Oliver Standford ls - Listing files
• Used to list files and their attributes in a directory.
• To list files in the current directory (relative): • ls • ls ./scripts • To list files in another directory (absolute): • ls /home/oliver/mydirectory.
• Useful list options: • -l - reveals much more information about files. • -a - lists all files, including hidden files. • -c - list files in columns • -h - print sizes in human readable format. Introduction to Linux | Oliver Standford cp, mv, rm - Copy, Move, Remove
• cp
• mv
• rm
• mkdir
• rmdir
Command Line tips
• pwd - present working directory - shows what directory you are currently in. • . - current directory. • tab key - auto completes a file name. • Up arrow - scroll through previous commands. Introduction to Linux | Oliver Standford
Getting Help • Linux has an extensive help system, called the manual (Man). This consists of a set of manual pages. • To get the man page for a command we can do: • man
• The whatis command gives a brief description of a command. • Whatis ls • Google! Introduction to Linux | Oliver Standford Exercises
• List files in your current directory. • List all files in your directory (including hidden) • Display the full path of the current directory • Make a directory called linux01. • move into that directory. • Use the touch command to create the file myFile • Create a copy of myFile called myFile2 • rename myFile2 to oldFile. • delete oldFile • Use the man command to find out what ls -p does. • What are the permissions of myFile. Introduction to Linux | Oliver Standford
SHELL USAGE Introduction to Linux | Oliver Standford
Bash Shell
• The shell is a program that:
• Interprets commands, and
• Acts as an intermediary between the user and the kernel.
• In practice we use a terminal emulator.
• We interact with the shell via a command line interface.
• A command is either a file, or an instruction internal to the shell. Introduction to Linux | Oliver Standford PATH • The shell retains an internal list of directories to search for executable files, called PATH.
• To view the PATH type:
• echo $PATH
• To execute a command not in the PATH you must type the absolute name:
• /home/oliver/bin/ls
• Commands for sequential execution can be entered on a single line, separated with a ; (semi-colon).
• date;sleep 10;date Introduction to Linux | Oliver Standford
Processes
• When a command is executed the shell creates a job which executes the specified task.
• A process can exist in one of four states:
• Foreground: All jobs run in foreground unless otherwise stated.
• Background: Append ‘&’ to the command. Job runs but shell remains interactive.
• Stopped: A stopped jobs can be resumed in different states.
• Terminated: A terminated job cannot be resumed. Introduction to Linux | Oliver Standford
Running jobs in the background
• Running jobs in the background allows to continue using the parent shell.
• The shell returns a job number (1) and a process number (2271). • To obtain a list of jobs currently running use jobs command. Introduction to Linux | Oliver Standford
Process Management
• You can stop a job by typing CTRL-z while it is running. • This will suspend the job. • The job can then be started running in the background using the following command. • bg %
Cancelling Jobs
• If you want to stop a job that is running in the foreground you can kill it by pressing CTRL-C. • If you want to stop a job that is running in the background you can kill it with the kill command. • The syntax of the kill command is:
Kill %
Process management
• The operating system maintains a list (called the process table) of all processes. • Each process has a large amount of information related to it. • PID - unique process ID. • Owner • Start time • Execution priority level etc. • The command for examining the process table is ps or top. Introduction to Linux | Oliver Standford
Exercise
• Start emacs, firefox, libreoffice in the background. • Terminate emacs, firefox & libreoffice with the kill command. • Find the PID of the current shell. • List all the processes running as root. • Start emacs. use top to determine how much memory is being used by emacs. Kill emacs using top. • Quit top. Introduction to Linux | Oliver Standford
Viewing Files
• It’s helpful to be able to move around the file system, create files etc. At some point we will want to view these files. • cat - concatenate files and print them to the screen. • more - view the contents of a file bit by bit. • less - same as more but with better navigation than more. • head - view first 10 lines • tail - view last 10 lines. • nl - view text with line numbers. Introduction to Linux | Oliver Standford
Redirection
• We can use Redirection to direct the output of a command to a file. • ls > listing.txt • This will create a text file with the output from the list command.
• Similarly, for a command that requires input, we can make it run from a file with appropriate commands. • Com < ins.txt Introduction to Linux | Oliver Standford
Appending
• Appending is used to append a redirection output to an already existing file. • The new output is glued to the end of a file.
• This is achieved with:
• ls >> myfile.txt Introduction to Linux | Oliver Standford
Metacharacters
• Metacharacters are keyboard characters with special meanings, and are a powerful feature of any shell.
; $ % > < ! ~ [ ] ( ) | / ‘ “ *
• Do not use these characters in naming files or directories: • They are used to interact with commands to manipulate files and directories. • We have already used ; < > & $. Introduction to Linux | Oliver Standford
Wildcards
• Wildcard characters represent all other characters. • * - represents zero or more characters. • ? - represents any single character. Introduction to Linux | Oliver Standford
Pipelines
• A pipeline is a mechanism for feeding the output from one program into another. • The symbol for this is | (vertical bar) • It is placed between the commands. Introduction to Linux | Oliver Standford
Filters
• Filters are commands that read an input (from a pipeline), processes it and produces an output. • Some simple filters include: • Sort (reorder the input) • Tail (extract lines of input) • More (pass through the input page at a time)
• Sort the contents of a file and save results in a new file:
cat file.dat | sort -n > newfile.dat Introduction to Linux | Oliver Standford Exercise • run the following command: • wget www.lancs.ac.uk/~standfor/linux/cathedral-bazaar.txt • view the contents of the downloaded file. • view the last 15 lines of the file using tail. • Run the previous command but put the output in a txt file. • list all files and permissions in current directory and append to the previously created txt file. • Run the following command • wget www.lancs.ac.uk/~standfor/linux/names.txt • View the file using cat. • Sort into alphabetical order • sort into reverse order, remove duplicates and write last 10 lines to new_names.txt • List all files in /bin (including hidden (-al)) and pipe the list to the sort command and sort by size. (hint: -k -n flags). Introduction to Linux | Oliver Standford
Archiving
• It is useful to be able to collect a group of files together into a single file. E.g. Backup, file transfer etc. • Linux provides the tar (tape archive) to do this. [oli@ubuntu ~]$ tar cvf filename.tar a.txt b.txt c.txt
• Options: • c - create a new archive • v - verbose mode (list files processed) • f - use archive file. (filename.tar) Introduction to Linux | Oliver Standford
tar
• The list of files to be archived can be specified with wildcards and metacharacters.
[oli@ubuntu ~]$ tar cvf filename.tar f*.txt
• To extract an archive use the x option.
[oli@ubuntu ~]$ tar xvf filename.tar Introduction to Linux | Oliver Standford Compression • tar can also be used to compress files and directories. • tar can filter archive through gzip(.gz) and bzip2(.bz2). • gzip compression - append z to the options. • bzip2 compression - append j to the options.
[oli@ubuntu ~]$ tar zxvf filename.tar.gz
• Extraction from gzip file.
[oli@ubuntu ~]$ tar jcvf filename.tar.bz2 *.txt
• Compression using bzip2. Introduction to Linux | Oliver Standford Environment Variables • Environment variables are variables that store information about your Linux environment . They have two parts: • Variable Name. • Variable Value (content). • To view the long set of environment variables type: env • SHELL, HOME, USER, LANG
• The echo command can be used to display the value of specified variables: • echo $SHELL • Note the variable name is prefixed with $. • To create your own variable: • myvar=‘myvalue’ Introduction to Linux | Oliver Standford Command Aliases • Shells provide a shorthand mechanism, called aliases, that enable you to create new commands. • A command alias allows substitution of a long command for a short one or a single command to represent a series of commands. • Alias alias-name=‘command [options]’ • Alias rm=‘rm -i’ - prompt before removing. • Alias home=‘cd ~;ls’ - change directory and list. • Aliases are only valid for the shell in which they are created. • To make them global, place them in the configuration file ~/.bashrc. Introduction to Linux | Oliver Standford
Command Line Editing
• See previous commands - history
• Execute command with number N in history - !N
• Search your history for commands - Ctrl - r ‘ls’
• Scroll through recent commands - up arrow.
• Execute the most recent command starting with ‘e’ - !e
• Run previous command - !! Introduction to Linux | Oliver Standford
Shell Initialisation
• You can setup files that automatically configure your working environment every time you login.
• The three major configuration files for the bash shell are:
• /etc/bash.bashrc - generic system-wide profile.
• .bash_profile - personal profile that is processed every time you login.
• .bashrc - setup file processed every time you open a non-login shell. Introduction to Linux | Oliver Standford
Exercises • View the contents of the variables SHELL, USER, LANG. • Create a variable called ‘myage’ and give it a value… • Print the value of your variable to screen. • Create a backup of everything in your home directory using tar. • Delete that archive, create it using gzip compression. • view all the files in the archive (hint: -t flag). • extract everything in the archive to /tmp/restored (use -C). • Create an alias called home that takes you to your home directory and automatically lists all files. • Make the alias permanent. • View all your command history, one page at a time.