Introduction To Linux in HPC
Introduction To Linux in HPC
By
Karuna Prasad
SSDG, CDAC-Bangalore
Agenda
● Background
● Linux Directory Structure
● Files
● Text Display and Search
● Users and Permissions
● Processes
● Shell scripting
● Environment variables
● Remote file transfer
Historical Background
- 1969: Unix (Bell Laboratories)
- Written in C
- Already a successor to Multics
- Over time, many variations
- 1990: POSIX (Portable Operating System Interface) Standard
- Interface that all unix systems implement
- Adopted by Unix-like systems (including Linux)
- Linux is an operating system that evolved from a kernel created by Linus
Torvalds when he was a student at the University of Helsinki.
- Open Source, GNU General Public License
- Linux is the most popular OS used in a Supercomputer
- Security and stability
- Flexible and scalable
OS used in top 500 Supercomputers
Linux is the most
popular OS used in a
Supercomputer
○ Security and stability
○ Flexible and scalable
Ref :https://en.wikipedia.org/wiki/Supercomputer_operating_system
Distribution of OS in Supercomputers
Ref- https://www.top500.org/statistics/list/
Architecture of Linux
● User space
○ The space in memory where user processes
run
○ This space is protected
○ The system prevents one process from
interfacing with another process
○ Only kernel processes can access a user
process
● Kernel space
○ The space in memory where kernel processes run
○ The user call has access to it only through the
system call
● System call
○ User space and kernel space are different spaces
○ When a system call is executed the arguments to
the call are passed from user space to kernel
space
○ The user process becomes a kernel process when
it executes a system call
Architecture of Linux
● Shell
○ It is an interface among the kernel
and user.
○ It can afford the services of kernel.
○ It can take commands through the
user and runs the functions of the
kernel.
○ The shell is available in distinct
types of OSes.
○ These operating systems are
categorized into two different
types, which are the graphical
shells and command-line shells.
Command Line Interface
● A line where you type commands
● Other terms:
○ CLI (command line interface)
○ Console / Terminal
○ Shell
● Advantages
○ Simple (nearly always works)
○ Easy to program (in comparison to GUI)
○ Fast and efficient to use (if you know the commands)
● Disadvantage: lots of memorizing (vs. GUI buttons)
Command Line Interface
Command Line Interface
Navigating the Command Line
● <Enter> run command
● <Up-Arrow> navigate command history back in time
● <Down-Arrow> navigate command history forward in time
● <Tab> auto-completion (a.k.a. “tab completion”)
○ If enough letters to identify command: completes command
○ More than one possibility: nothing shown
○ Second Tab to list possible completions
● <Ctrl-C> abort current command.
● Always case-sensitive
○ Popular source for errors
● Command line options:
○ Usually start with dash
○ Common convention: single dash for “short options”, double dash for “long options”
Navigating the Command Line
● Manual Page
○ Man <command Name>
● Built-in help
○ -h or –help option
○ Similar to man pages
● CLI no “UNDO” button
● No confirmation Dialog box
● Careful, if root can have destroy entire system
● Make sure you are in right directory
● Make sure you are not “root”, if not required
Directory Structure
● Directory tree structure different from Windows
○ No drive letters ( C:\ )
○ Top level identical on every Linux system
○ “Mounting points”: location of hard drive in tree structure
● “Path”: location inside file system
○ Example /home/karuna/SamplePrograms
○ Absolute path: the location of a file or directory from the root
directory(/) and provides the full path (starts with / )
○ Relative path: relative to current working directory
● Print working directory : pwd
Directory Structure
Directory Structure
Directory
● Linux - everything is a file. Directory is also a file and stores file names and
related information
○ /dev : Device files
○ /proc : System information files
● Every command is a program or script somewhere
○ which <commandname>
● Directories abbreviations
○ Current Directory: . (dot)
○ Parent directory: .. (2 dots)
○ Your home directory: ~ (tilde)
Navigating Directories
● Linux - everything is a file
● cd Command (change directory)
● Use : cd <path>
○ Path can be relative or absolute path
○ Absolute path
○ Relative Path
● Common mistake: cd.. (no space in between)
● ls Command
○ List directory contents
○ ls -l : ll(shortcut)
○ ls -a : show hidden files
○ ls -t : to sort by time modified
Files
● Linux: extensions do not matter
○ But: conventions to help humans
○ Some programs also look at extensions
● Most important: text file or not?
○ Configuration files
○ Scripts
○ System information files
● Binary file: generally not searchable
● Use file <filename> to identify file type
File Manipulation Commands
● Rename file/directory: mv <oldname> <newname> (move)
● Copy file/directory: cp <filename> <newname> (copy)
○ Needs -r for directories
●
● Create directory: mkdir <dirname>
●
● Create empty file: touch <filename>
●
● Remove fire/directory: rm <filename>
○ Check access permissions
○ Recursively delete sub-directories: rm -r
○ Common option: -f (force) - Never prompt for confirmation
● Select multiple files according to patterns
○ * zero or more characters
○ ? exactly one character
○ [ ] range of characters
Console specific commands
● <Ctrl-C> stop current command
● <Ctrl-Z> suspend current command
● <Ctrl-D> send “End-of-File” to application
○ Will usually quit console when on an empty command line
○ exit to Quit console
○ clear command to clear screen
● Paste selected text
■ NOT Ctrl-C / Ctrl-V, see below
Searching Files
● find command
● Syntax: find <targetdir> <options>
●
● Eg: find . -name "ex1.txt" -type f
●
● Allows very complex searches
○ Wildcards
○ Only files modified after X
● Allows executing command for every found file: -exec
● Ex: find command -name option
● $ find . -type f -name “*test*”
Console Input and Output
● Console has three main ways of
communicating with process
(streams)
○ Standard input ( stdin)
○ Standard output ( stdout )
○ Standard error ( stderr )
● stdin : What you type into
console
● stdout + stderr : what you
see in console
○ Separate stream so that error
messages are separated from
normal output
Console Input and Output
● Input / output streams can be redirected to
○ Other commands
○ Files
● Redirect stdout
● Redirect stderr
● Redirect stdin
command1 | command2
Redirection
● Append to file without overwriting
command >> filename
● Streams are numbered
○ 0 : stdin
○ 1 : stdout
○ 2 : stderr
● Examples
command > out.log 2> err.log
command 2>&1 > out_err.log
Text Display
● Different ways to display and edit text
● cat command
○ Outputs contents to the console
● less command
○ Allows going back and forth
○ Also used by man pages
● Others
○ head : display first lines
○ tail : display last lines
Searching File Contents
● Search using grep command
● Syntax: grep <options> <string> <filename>
○ Ex: grep -i -r “test” example*.txt
● Can use wildcards
● Options
○ -r Recursive ( include subdirectories)
○ -i Ignore upper / lower case
○ -I Ignore binary files (uppercase i)
● Situation: command with lot of text output and need to search something
inside output
● Solution: pipe output into grep
○ $ ll | grep - i test
○ ps -aef | grep mysql
Users and Permissions
● Linux is a multi-user system
○ Everyone should only be able to access own files
○ Others only see / changes what you want them to
○ Some files / directories should only be accessible to admins
● Everyone is logged in as specific user (account)
○ and every user has certain permissions
● Only admins can set permissions for others
File / Directory Permissions
● Each file and directory has certain permissions
○ Determines what you can do
● root user (superuser) can do everything
● Users can have temporary root permissions
● sudo <command>
● Users belong to groups, each user has a primary group
● Read : who can read the contents of file /directory
● Write : who can change contents of file / directory
● Execute :
○ File : who can execute file ( or program)
○ Directory : who can traverse directory
■ who can execute files inside but not see them
Example output of ls -l
Changing Permissions
● Modify owner / group (needs root ) :
● chown <newowner> <filename>
● chown <newowner> : <newgroup> <filename>
●
● Modify permissions
● chmod u+x <filename>
● u = User, g = Group, o = Other, a = All
● + or -
● r = Read , w = Write, x = Execute
Processes
● Process : running instance of a program
○ System
○ User
○ User (manually launched)
● Like windows
○ Equivalent to Task Manager: top
○ Short Overview: pstree
● Each process has an owner
○ Process can/can't do what owner can/can't do
● Each process has an ID number ( PID)
Top command output and Navigation
Top command output and Navigation
● Single-letter commands to navigate top
● u : filter processes from a specific user
● K : kill a specific process
● h : show help
● f : toggle displayed columns
● x : highlight current sort column
● <> : select column to sort
● R : Reverse sorting
● q : quit top
Process- Background and Foreground
● If you enter command, it runs in the shell
● Enter <command> & to start it in background
● Send foreground command to background by Ctrl-Z (pauses it) and typing
bg
● Bring to foreground with fg <jobID> - Jobid is different from process ID
● Can be displayed with jobs
VIM Text Editor
● Default Linux text editor: vi
● Usually: vim (vi improved)
● includes syntax highlighting
● Completely inside console
● Advantages:
○ Always available
○ Very fast once you know commands
● When in doubt which Mode you are in , use Esc key and u for undo
Test Editor
Common Vi Commands
● $ vi filename
● :w write(save) file
● :w filename -write as new filename
● :wq or :x or zz write (save) file and quit
● :q! Close file without saving