34
Introduction to Linux Fang (Cherry) Liu, PhD Scientific Computing Consultants PACE, GATECH [email protected] 1

Introductionto* Linux- - Home | PACE | Georgia Institute ...pace.gatech.edu/sites/default/files/intro-linux_1.pdf · o `diff ` ... • Vi(m) stands for

Embed Size (px)

Citation preview

Introduction  to  Linux

Fang (Cherry) Liu, PhD Scientific Computing Consultants

PACE, GATECH

[email protected] 1

Outline •  What is Linux operating system •  How to access to Linux •  Common commands on Linux

o  What is a shell o  Manipulating files o  I/O redirection

•  How to use man pages •  Text editors •  Environment Variables •  Modules •  Some usage tips •  References

[email protected] 2

Why  Linux? •  Linux is an implementation of UNIX •  A simple description of the UNIX operating system,

also applicable to Linux, is this: o  On a Unix system, everything is a file; if something is not a file, it is a

process

•  Linux uses GNU tools, a set of freely available standard tools of handling the operating system

•  Linux pros o  It is free o  It is portable to any hardware platform o  It was made to keep on running o  It is secure and versatile o  It is scalable

[email protected] 3

PACE  Computing  Center

[email protected] 4

A  Partnership  for  Advanced  Computing  Environment •  Provides  faulty  and  researchers  vital  

tools  to  accomplish  the  institute’s  vision  to  define  the  technological  research  university  of  the  21st  century.

•  A  strong  HPC  environment  through  a  tight  partnership  with  our  world-­‐‑class  students,  researchers  and  innovators  to  help  them  make  the  greatest  impact  with  their  work.

Access  to  Linux •  From windows machines:

o  For those without remote system to access, need to build Linux locally •  LiveCD/USB Stick http://livecdlist.com/ubuntu (for person who wants

to try out a Linux distribution, needs to booting up from CD) •  Run linux in a Virtual Machine and like any other program in Windows

(for person who wants to run Linux more frequently) •  Access javascript based Linux from web browser http://bellard.org/

jslinux o  To access the remote cluster:

•  Putty or Xwin32– command line access •  Tightvnc – GUI/Desktop access

•  From MacOSX: o  Using SSH Client such as iTERM2, Terminal, Xquartz to remote cluster o  Tightvnc – GUI/Desktop access

•  `ssh –AX <userid>@<remotecluster>`

[email protected] 5

What  is  a  Shell? •  The most important command-line tool is the command

shell o  A shell manages the interaction between the system and its users by reading

commands from the keyboard, executing the commands, displaying the output

o  A shell can best be compared with a way of talking to the computer, a language

•  Shell types o  Sh or Bourne Shell: the original shell still used on Unix system o  Bash or Bourne Again shell: the standard GNU shell, on Linux, it is standard shell

for common user o  Csh or C shell: the syntax of this shell resembles that of the C programming

language o  Tcsh or Turbo C shell: a superset of the common C shell, enhancing user-

friendliness and speed o  Ksh or the Korn shell: a superset of the Bourne shell, but it is hard for the

beginning users.

[email protected] 6

Shell  Execution  Cycle •  When you type a

command like `ls -l`, the OS reads characters from the keyboard and passes them to the shell

•  The shell breaks the line of text into words, looks for program with the same name as the first word and runs that program

•  Program’s output goes back to the shell, and passes to the OS, then displays on the screen

[email protected] 7

User  type  in  the  command  ‘ls  -­‐‑l’

Shell  parses  the  input  text,  determines  the  command  and  runs  

it

Program  output  gets  sent  back  to  shell  and  passes  to  the  OS  to  

display  on  the  screen

Linux  File  System •  The Linux file system is the

set of files and directories the computer can access

•  Data is stored in files •  Files are stored in

directories, and directories can contain other directories

•  On Linux, the file system has unique root directory “/”

[email protected] 8

Linux  File  System  (Cont.) •  A path is a description of how to find something in a

file system, there are absolute path and relative path o  “.” is the current directory o  “..” is the directory one level up o  “~/” is an alias for the current users home directory o  An absolute path describes a location from the root directory down o  An relative path describes a location from the working directory

[email protected] 9

Linux  Path Windows  Path Internet  Path /root/to/path C:\root\to\path hTp://www.address.com/

root/to/path

Common  Commands  on  Linux

•  cd - change directory •  ls – list of files •  file <filename> - display file type of file with name •  cat textfile - throws content of textfile on the screen •  pwd – display present working directory •  exit or logout – leave this session •  man <command> - read man pages command •  Info <command> read info pages on command

[email protected] 10

Manipulating  Files •  Command `ls`

o  Show the name of the file `ls <filename>` o  Show permissions on a file, file size, inode number, creation date and time,

owners and amount of links to the file `ls –l <filename>`

•  Command `file` o  Show the format of a file `file <filename>`

[email protected] 11

Manipulating  Files  (Cont.) •  Creating and deleting files and directories

o  Creating directories `mkdir <dirname>` o  Show the directories `ls –ld <dirname>` o  Creating directories and subdirectories in one step `mkdir –p

<dirname>/<subdirname>`

•  Moving files o  `mv <sourcedir>/<sourcefile> <destdir>/<destfile>`

[email protected] 12

Manipulating  Files  (Cont.)   •  Copying files

o  `cp <sourcefile> <destfile>` o  Copy directories `cp –r <sourcedir> <destdir> `

•  Removing files o  `rm -i <filename>` o  Remove directories `rm –ri <dirname>`

[email protected] 13

Manipulating  Files  (Cont.)   •  Finding files

o  Find all files and subdirectories contained in a given path, and print the names of the files containing the search string in their name `find <path> -name <searchstring>`

o  Find all files in the path and subdirectories, that are bigger than 5MB `find <path> -size +5000k`

[email protected] 14

Manipulating  Files  (Cont.) •  Filtering input lines and returning certain patterns to

the output o  `grep –r <searchstring> <files>` o  `grep –ri <searchstring> <files>` o  `find` is often used in combination with `grep` to define more serious

queries

[email protected] 15

Manipulating  Files  (Cont.) •  More ways to view file content

o  `cat <filename>` simply sending files to the standard output o  `less <filename>` is the GNU version of more and has extra features

allowing highlighting of search strings and scrolling back.

o  `tail –n <filename>` displays the last n lines of a file o  `head –n <filename>` displays the first n lines of a file

[email protected] 16

Manipulating  Files  (Cont.)     •  Transfer files:

o  The SSH suite provides scp for data transfer, scp will ask for passwords or passpharases if they are needed for authentication:

o  `scp filename username@remotemachine:/path/`

•  Compare two files: o  `diff <file1> <file2>` o  `sdiff <file1> <file2>` side-by-side comparison between two files.

[email protected] 17

Manipulating  Files  (Cont.)   •  File security

o  (rwx)(rwx)(rwx) 1: owner 2: group 3: other o  `chmod 600 <filename>` A private file only changeable by the user who

entered this command.

[email protected] 18

I/O  redirection •  Standard input, output and errors •  Redirection operators •  How to use output of one command as input for another

(pipe) •  How to put output of a command in a file for later

reference •  How to append output of multiple commands to a file •  Input redirection •  Combining redirection of input, output and error streams •  Output filters

[email protected] 19

I/O  redirection  (Cont.) •  Keyboard (stdin), screen (stdout) •  Redirecting output with > and |

o  > output to a file o  | output to another command (pipe) o  `cat file1 file2 >newfile` concatenates files and puts them all together to

the standard output

[email protected] 20

I/O  redirection  (Cont.) •  Combining redirections

o  `less –help | grep –i examine > examine-files-in-less`

•  Append text to an existing file >> o  `date >> file`

[email protected] 21

I/O  redirection  (Cont.) •  Filtering output

o  `cat file | sort` o  `sort file | uniq`

[email protected] 22

How  to  read  the  document

•  man – manual pages in Linux o  `man <command>` o  The first line contains the name of the command o  The name of the command and a short description are given o  The synopsis of the command provides a technical notation of all the options/

arguments o  A longer description of the command is given o  Options with their descriptions are lists o  Environment describes the shell variables that influence the behavior of this

command o  A reference to other man pages is given in the “SEE ALSO” section o  There might also be author and copyright information

•  In addition to the man pages, you can read the info pages about a command, these usually contain more recent information and are somewhat easier to use. o  `info <command>`

•  --help option gives a short explanation about how to use the command and a list of available options (cheat sheet). o  `<command> --help`

[email protected] 23

Text  Editors •  GNU nano – clone of the Pico text editor with some

enhancements. It is easy to use since it displays commends at the bottom of the editor.

•  GNU Emacs is the extensible, customizable, self-documenting, real-time display editor. o  It provides facilities that go beyond simple insertion and deletion:

controlling subprocesses; automatic indentation of programs, viewing two or more files at once; supporting the formats in different programming languages.

•  Vi(m) stands for Vi Improved, it is screen-oriented text editor originally created for the Unix operating system.

[email protected] 24

Text  Editor  (vim) •  It operates in two modes: command mode and insert mode.

o  Commands move you through the text, search, replace, mark blocks and perform other editing tasks

o  Each key has two meanings: it can either represent a command for the editor when in command mode, or a character that you want in a text when in insert mode.

o  Switching between two modes by clicking <esc>

•  Basic operations o  Delete n lines starting from the current cursor position - n dd o  Delete n words at the right side of the cursor – n dw o  Delete the character on which the cursor is positioned – x o  Move to line n of the file - :n o  Save (write) the file - :w o  Exit the editor - :q o  Save the text to newfile - :w newfile o  Save and exit - :wq! o  Search the string in the file - /searchstring o  Replay word with anotherword - :%s/word/anotherword/ o  Copy a block of text – y<n>y o  Paste it - p

[email protected] 25

Example:  Creating  Files  and  Directories

•  `mkdir tmp` •  `cd tmp` •  `ls` •  `vi file1.txt` “This is my first file created at vim editor!” •  `cp file1.txt file2.txt` •  `nano file2.txt` •  `ls -t` (-t tells ls to list files by modification time)

[email protected] 26

Example:  Looking  At  Files •  `cat file1.txt` •  `ls -l` (“-l” means “long form”) •  `wc *` columns show lines, words, and characters •  `sdiff file1.txt file2.txt`

[email protected] 27

Shell  Scripts •  A shell script is a text file containing shell commands •  The first line of a scripts begins with the two

characters #!, and the remainder of the line specifies an interpreter for the program, bash shell script starts with`#!/bin/bash`

•  Wildcards o  * matches zero or more characters `ls *.txt` o  ? Matches any single character `ls file-?.txt`

[email protected] 28

Bash  Script  Building  Blocks

•  The first line in the script should be always as o  #!/bin/bash (bash shell)

•  Passing arguments to the bash script o  echo $1 $2 $3 ' -> echo $1 $2 $3’ o  use $@ to print out all arguments at once

•  Executing shell commands o  echo `hostname`

•  If statement : if [ condition ]; then statements; else statements; fi

•  For loop: for i in `ls`; do echo item: $i; done

[email protected] 29

Environment  variables •  Get list of environment variables defined for the current

session o  printenv

•  Display the variables o  echo $PATH

•  Change the content of a variable o  export PATH=$PATH:/new/path

•  Important environment variables o  COLUMNS the width in characters of the current display window o  HOME the current user’s home directory o  HOSTNAME this computer’s name o  LINES the height in characters of the current display o  SHELL what shell is being run o  USER the current user’s ID o  PATH where to look for programs

[email protected] 30

Configuration •  Shell setup files ~/.bashrc ~/.bash_profile •  Add personal tools directory to PATH

o  PATH=$HOME/bin:$PATH

•  Add personal settings o  Export EDITOR=/usr/bin/vim

•  Alias (put in ~/.bash_profile) o  Alias cherrypace=‘ssh –A [email protected]’ o  Alias ll=‘ls -lrt’ o  Alias ..=‘cd ..’

•  Unalias o  Unalias ll

[email protected] 31

PACE  Modules •  Configuration for software environment and switching

between different versions, no need to edit PATH, LD_LIBRARY_PATH, etc!

•  Commands:

o  module avail : Lists all available modules o  Module list : displays all the modules that are currently loaded o  Module load: loads a module to the environment o  Module rm : removes a module from the environment o  Module purge: removes all loaded modules (buggy)

•  Module may depend on, or conflict with, each other

•  For more details, please read o  http://www.pace.gatech.edu/using-software-modules

[email protected] 32

Key  combinations  in  Bash

•  Ctrl + A – move cursor to the beginning of the command line

•  Ctrl + E – move cursor to the end of the command line

•  Ctrl + C – end a running program and return the prompt

•  Ctrl + D – log out of the current shell session, equal to typing exit or logout

•  Ctrl + L – clear this terminal. •  Ctrl + Z – suspend a program

[email protected] 33

Reference •  General Linux

o  “Linux in a Nutshell” by Ellen Siever, etc. o  “Running Linux” by Matt Welsh, etc.

•  Editors o  “Learning the Vi Editor” by Linda Lamb, etc. o  “GNU Emacs Manual” by Richard M. Stallman

•  Shells o  “Unix Shell Programming” by Stephen G. Kochan etc. o  “The Complete Linux Shell Programming Training Course” by Ellie Quigley,

etc. o  “Learning the Bash Shell” by Cameron Newham, etc.

[email protected] 34