Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection...

45
Introduction to Linux 賀⾀恆(Ho Yu-Heng) National Tsing Hua University

Transcript of Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection...

Page 1: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Introduction to Linux賀⾀恆(Ho Yu-Heng)

National Tsing Hua University

Page 2: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Source: http://i61.tinypic.com/1i0swk.png

1981 - MS-DOS

Page 3: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Source: http://i61.tinypic.com/1i0swk.png

1985 - 倚天

Page 4: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

1987 - Macintosh II

Source: https://upload.wikimedia.org/wikipedia/commons/d/d3/MacII.jpg

Page 5: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

1989 - Macintosh Portable

Source: https://upload.wikimedia.org/wikipedia/commons/c/c3/Macintosh_Portable-IMG_7541.jpg

Page 6: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Source: https://upload.wikimedia.org/wikipedia/en/7/73/Windows_3.11_workspace.png

1992 - Windows 3.1

Page 7: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Source: http://cdn.techalook.com.tw/wp-content/uploads/windows_95_by_clutch.jpg

1995 - Windows 95

Page 8: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Source: http://winsupersite.com/site-files/winsupersite.com/files/archive/winsupersite.com/content/content/127379/reviews/winme_rtm_0001.gif

2000 - Windows ME

Page 9: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Source: http://www.pcdiy.com.tw/assets/images/768/5ae8812fa0f0417ad5756f9e540aabea.jpeg

2001 - Windows XP

Page 10: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Outline• What is Linux?

- Background of Linux - Kernel & Shell

• Command Line Environment - Getting Started- SSH & SCP

• Basics of BASH Commands- Useful Tips- Frequently Used Commands

• Text Editors - Vi & Vim- Exercise

• Simple Shell Scripts

Page 11: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

What is Linux?• Wikipedia - Linux

A Unix-like computer operating system assembled under the model of free and open-source software development and distribution.

• Unix-like?

• Wikipedia - Unix A family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, developed starting in the 1970s at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and others.

Page 12: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

GNUhttps://www.gnu.org/

Page 13: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Free SoftwareFree Software Foundation, FSF!

Founded by Richard Matthew Stallman in 1985.

Four Essential Freedoms:!

0. To run the program.

1. To study and change the program in source code form.

2. To redistribute exact copies.

3. To distribute modified versions.

Creation of Linux!

By Linus Torvalds using GNU C compiler(From GCC) in 1991.

Page 14: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Linux Kernel• File Management

• Multitasking

• Memory Management

• I/O Management

• Device Management

• Network Management

• …etcSource: https://bash.cyberciti.biz/uploads/bashwiki/3/38/Rela.gif

Page 15: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Shell• Connection between the User Request and the Kernel Functions.!

1. Command-Line Interface(CLI)

2. Graphical User Interface(GUI)

• There are several types of shells commonly used by Linux!

1. Bourne Shell(sh)

2. Bourne-Again Shell(bash)

3. C-Shell(csh)

4. Tenex C-Shell(tcsh)

Page 16: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Shell• Connection between the User Request and the Kernel Functions.!

1. Command-Line Interface(CLI)

2. Graphical User Interface(GUI)

• There are several types of shells commonly used by Linux!

1. Bourne Shell(sh)

2. Bourne-Again Shell(bash)

3. C-Shell(csh)

4. TC-Shell(tcsh)

Page 17: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Let’s Begin!• For Windows Users, please download MobaXterm(Portable) from: http://mobaxterm.mobatek.net/download.html

Page 18: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Secure Shell(SSH)• Providing secure access for users and automated processes

• Interactive and automated file transfers

• Issuing remote commands

• Managing network infrastructure and other mission-critical system components.

Source: https://www.ssh.com/s/how-does-ssh-protocol-work-600x180-I1BIZnPs.jpg

Page 19: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Secure Shell(SSH)• ssh -Options account_name@host_name_or_IP

• Options:!

• -V : Displays product name and version information and exits.

• -X : Enables X11 connection forwarding and treats X11 clients as untrusted.

• -x : Disables X11 connection forwarding.

• -Y : Enables X11 connection forwarding and treats X11 clients as trusted.

• -f : Sends the specified command and has the session to run in the background.

Page 20: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Secure Shell(SSH)

The above command shows the login attempt with X11 forwarding enabled(-Y), so that GUI is available.

The above shows additional options for ssh, along with some shell commands.

Page 21: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Secure Copy(SCP)

• scp -Options Local_path/Local_file account_name@host_name_or_IP:Target_path/Target_file

• scp -Options account_name@host_name_or_IP:Target_path/Target_file Local_path/Local_file

• Options:

• -r : Recursive copying entire directories.

• -p : Preserves modification times, access times, and modes of the original files.

Page 22: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Secure Copy(SCP)

Forwarding commands to locate the target directory, then scp local file to the target file.

Scp the target file along with the parent directory back to local host.

Page 23: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Basics of BASH Commands

Page 24: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Useful Tips(Precautions)

• Some “Very Helpful” Commands

• whatis : Gives a short description of the command.

• man : Format and display system documentation.

• info : Display info documents.

• help : Display information about built-in commands.

Page 25: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Wildcards & Special Keys

• * : arbitrary string length

• ? : arbitrary character

• Tab Key : automatic completion

• Up(Down) Keys : previous(next) command

Page 26: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Working with Directories• cd : change directory

• pwd : print working directory

• df : disk free

• du : disk usage, estimate file space usage under current directory.

• mkdir : make directory

• -p : with parent directories

• rmdir : remove directory

• ls : list segment

• -l : long listing

• -a : list all

• -h : display in human readable format

• -F : reveals natures of files (appends * for executables, / for directories)

Page 27: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Working with Files• cp : copy

cp original_file target_file

• mv : move

mv original_file target_file

• chmod : change file mode

chmod +rwx target_file

chmod 755 target_file (r=4, w=2, x=1)

• file : determines file type

• touch : create empty file

• rm : remove file

• -r : recursive remove(can remove directories)

• -f : force remove

• rename: rename files(especially for multiple files)

rename original_keywords target_keywords target_files

Page 28: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Working with Contents• more & less : displaying

files(Space bar for next page, q to quit)

• head & tail : display the first(last) n lines of the file(n = 10 if not specified)

head -n target_file

• cat : concatenate, joining files to form a bigger one “Ctrl + d” to end file

Page 29: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Other Useful Commands• ln : create link

• -s : create soft link

ln -s original_file target_softlink

• find : find files with specified features

• sort : sorting contents of a text file line-by-line.

• -n : numerical sort

• -R : randomize

• -r : reverse order

• -k : sorting key positions

• grep : global regular expression print

• -d : search directories

• -i : ignore case

• -n : show line number

• -v : revert match

grep -Options keywords target_file

• history : show command history(history N = show latest N commands)

• locate : locate file with keywords provided by updatedb(Needs to be root)

Page 30: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Text Editors

Page 31: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Vi & Vim• Command Mode The Default Mode at the Beginning.

• Insert Mode Press “i”, “R”, “a”, or “Insert” keys to enter.Press “Esc” to leave Insert Mode.

• Command-Line Mode Press “:”, “/”, or “?” to enter.Modifying and Searching through the text file.

Page 32: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Vi & Vim• Use vim to check the text file “VIMhelp.txt”.

Page 33: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Useful Commands under Vim• Under the Default Mode!

• N + “arrow key” = Move the cursor N characters towards the specified direction.

• “Ctrl” + “f” = PageDown

• “Ctrl” + “b” = PageUp

• “Ctrl” + “d” = Half PageDown

• “Ctrl” + “u” = Half PageUp

• “H” = Top Row of the Screen

• “M” = Middle Row of the Screen

• “L” = Last Row of the Screen

• “G” = Last Row of the FileN + “G” = To N-th Row of the File

• “gg” = First Row of the File

• “u” = Undo

• “Ctrl” + “r” = Redo

Page 34: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Vi & Vim

Page 35: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Useful Commands under Vim• Under the Command-Line Mode

(Remember to press “Esc” first if you’re under Insert Mode)!

• “:w” = Save file

• “:q” = Close file

• “:q!” = Close without Save

• “:wq” = Close & Save

• “:w filename” = Save as filename

• “:N” = Go to N-th line.

• “?string” or “/string” = Search(Up/Down) file for “string” Press “n” to go to the next keyword.

Page 36: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Useful Commands under Vim• Under the Insert Mode!

• Insert or Replace?

Page 37: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Useful Commands under Vim• Under the Default Mode!

• “v” = Highlight character by character

• “V” = Highlight row by row

• “Ctrl” + “v” = Highlight block

• “y” = Copy highlighted area

• “d” = Delete highlighted area

• “p” = Paste highlighted area

Page 38: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Useful Commands under Vim

Images from ⿃哥的 Linux 私房菜:!http://linux.vbird.org/linux_basic/0310vi.php#vim

Page 39: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Let’s Play a Game!

• Find out my research specialty!

• Please open the text file “GAME.txt”

• Go to line 1195, then follow the instructions.

Page 40: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Scripting Shell Commands

Page 41: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Shell Script• Scripting Language, Interpreted Language, and Compiled Language?

• Shell commands can be written in a text file, then executed like an executable program directly, called a “shell script”.

• A simple shell script:

Modified version:

Page 42: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Pipe• “|” = pipe

• Example :!

• ls -lh /usr/bin/ | less Processing the output of “ls” with “less”.

• ls -lt | head -n 5 Show the newest 4 files under current directory.

• du -h | sort -nr sort files according to their sizes from small to large.

Page 43: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Some Powerful Tools• seq : generates sequence of numbers from first to last

• seq —format=%03g -10 10generates sequence of numbers from -10 to 010

• awk : data extraction and handling

• seq 15 | awk ‘BEGIN {a=1; b=1} {print a; c=a+b; a=b; b=c}’show the first 15 Fibonacci numbers

• bc : basic computing

• echo “scale=300; 4*a(1)” | bc -lqcalculates pi to 300th decimal number

• wc : word count

• cat /etc/passwd | wc -lget total number of users on the system

• cat text_file | wc -m get total number of characters of text_file

Page 44: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Exercise• The file “COLDGASS_DR3.ascii” is the catalogue of CO Legacy

Database for GASS(Galex Arecibo SDSS Survey) data release 3, which contains a total of 366 galaxies, with their spectra under the directory “COLDGASS_DR3_ASCII”.

• In the catalogue, column 1 is the GASS ID, and column 19 marks a flag for CO detection(1=detection, 2=non-detection), select all galaxies which has CO detection, and subtract their spectra from the directory.

• You can use a script written in python, “plot_spectra.py”, to evaluate the spectrum under ipython, simply use%run ‘plot_spectra.py’ filenameto get the result.

Page 45: Introduction to Linuxalma.astr.nthu.edu.tw/ucat/ussp2017camp_files/linux.pdfShell • Connection between the User Request and the Kernel Functions.! 1. Command-Line Interface(CLI)

Online Materials• ⿃哥的Linux私房菜 :

http://linux.vbird.org/

• Linux-training.be : http://linux-training.be/

• CO Legacy Database for GASS(COLD GASS) : http://wwwmpa.mpa-garching.mpg.de/COLD_GASS/index.shtml