Quick viewing(Text Mode)

Introduction to Linux

Introduction to Linux

INTRODUCTION TO

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 : • Allows additional guest operating systems to run on of the host operating system. • Compatible guest OS - Windows, Linux, Solaris, BSD, Mac. • Other Virtualisation software - Hyper-(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 • /DVDs: • Host Drive • ISO Image • 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 • • Pause/Resume • Signal • Power off • Snapshots Introduction to Linux | Oliver Standford

Ubuntu Exercise

• Change your password – open a terminal and use the ‘’ 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 ‘ 2017’

• Open the following file: /etc/apt/sources.list using nano Introduction to Linux | Oliver Standford

Unix

• Unix was developed AT&T 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 than one job at a .

• 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 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 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 - X - Execute - - No Permission

User Group Others Modified Group Date.

-rwxr-xr-x 20 oliver stori 4096 2012-09-22 .c

Type Owner - - file File Size d - Directory (bytes) l - No. of links 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 are required). • Arguments are given in <>. Introduction to Linux | Oliver Standford

Basic Commands

- list files. • - files. • - files. • - remove (delete) files. • cd - Change directory. • - (create) directory. • - Remove (delete) directory. • - 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 - sizes in human readable . Introduction to Linux | Oliver Standford cp, mv, rm - Copy, Move, Remove

• cp Create a copy of file1, called file2. • -r - recursive copying of subdirectories. • -i - interactive, prompt before overwrite. • -u - update, copy only when source file is newer than destination.

• mv Move/rename file1 to file file2.

• rm Delete file1 file2. • -i - interactive prompting • -r - recursive subdirectories. • Beware, rm is potentially dangerous. A deleted file cannot be recovered. Introduction to Linux | Oliver Standford cd, mkdir, rmdir - Change, make, remove directories. • cd - Change directory. Move from current directory to another. • cd .. - goes up a level. • Argument can be relative or absolute.

• mkdir - Create a new directory called directory.

• rmdir - Delete a directory. • This will only work on an empty directory. • Use -r option to delete contents too. Introduction to Linux | Oliver Standford

Command Line tips

- 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 • Linux has an extensive help system, called the manual (Man). This consists of a set of manual pages. • To get the for a command we can do: • man • man ls • man mkdir • Man pages list command options, syntax and some examples of use.

• 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 of the current directory • Make a directory called linux01. • move into that directory. • Use the command to create the file myFile • Create a copy of myFile called myFile2 • rename myFile2 to oldFile. • delete oldFile • Use the man command to 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 :

$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; 10;date Introduction to Linux | Oliver Standford

Processes

• When a command is executed the shell creates a job which executes the specified task.

• A 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 % Introduction to Linux | Oliver Standford

Cancelling Jobs

• If you want to stop a job that is running in the foreground you can 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 % or • For example: • Kill %1 This would kill/quit/close the program with job number 1. • Kill 3678 This would kill/quit/close the program using its PID. Introduction to Linux | Oliver Standford

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 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. • - concatenate files and print them to the screen. • more - view the contents of a file bit by bit. • - same as more but with better navigation than more. • - view first 10 lines • - view last 10 lines. • - view text with line numbers. Introduction to Linux | Oliver Standford

Redirection

• We can use 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 is a mechanism for feeding the output from one program into another. • The symbol for this is | () • 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: • (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 (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 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: • 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 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 ~/.bashrc. Introduction to Linux | Oliver Standford

Command Line Editing

• See previous commands -

• 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 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.