Introduction to Linux

很泛但是也提纲挈领的点名Linux是什么?基础操作:Bash Shell,I/O重定向,文件系统,进程和作业控制,编辑器vim,如何编译c程序等。
展开查看详情

1.Introduction to Linux Robert Putnam Research Computing, IS&T p utnam@bu.edu

2.What is Linux? The Bash shell I/O redirection (pipes, etc.) Navigating the file system Processes and job control Editors Hello,world in C Introduction to Linux - agenda

3.What is Linux? T he Most Common O/S Used By BU Researchers When Working on a Server or Computer Cluster

4.Where is Linux?

5.What is Linux? http://en.wikipedia.org/wiki/Darwin_%28operating_system%29

6.Linux is a Unix* clone begun in 1991 and written from scratch by Linus Torvalds with assistance from a loosely-knit team of hackers across the Net . 64% of the world’s servers run some variant of Unix or Linux. The Android phone and the Amazon Kindle run Linux. What is Linux? *kernel

7.Linux is an O/S core written by Linus Torvalds and others AND a set of programs written by Richard Stallman and others . They are the GNU utilities. http://www.gnu.org/ What is Linux? Linux + GNU Utilities = Free Unix

8.Bird’s eye view: What is Linux? K ernel H ardware Shell Utilities multitasking gcc emacs grep cat sort awk f ile system bash sh tcsh d evice access wc

9.From The Unix Programming Environment , Kernighan and Pike: What is Linux? “Small programs that do one thing well” … at its heart is the idea that the power of a system comes more from the relationships among programs than from the programs themselves. Many UNIX programs do quite trivial things in isolation, but, combined with other programs, become general and useful tools.

10.awk Pattern scanning and processing language c at Display file(s) cut Extract selected fields of each line of a file diff Compare two files grep Search text for a pattern head Display the first part of files less Display files on a page-by-page basis od Dump files in various formats sed Stream editor (esp. search and replace) sort Sort text files split Split files tail Display the last part of a file tr Translate/delete characters uniq F ilter out repeated lines in a file wc Line, word and character count tar File archive (similar to zip) What is Linux: Selected text processing utilities

11.You need a “ xterm ” emulation – software that emulates an “X” terminal and that connects using the “SSH” Secure Shell protocol. Windows Recommended: MobaXterm ( http ://mobaxterm.mobatek.net / ) Also available at BU, Xwin32 (http://www.bu.edu/tech/services/support/desktop/distribution/xwindows/xwin32 /) Connecting to a Linux Host – Windows Client Software

12.You need a “ xterm ” emulation – software that emulates an “X” terminal and that connects using the “SSH” Secure Shell protocol. Windows Recommended: MobaXterm ( http ://mobaxterm.mobatek.net / ) Also available at BU, Xwin32 (http://www.bu.edu/tech/services/support/desktop/distribution/xwindows/xwin32 /) Connecting to a Linux Host – Windows Client Software

13.MobaXterm From Windows Desktop Double-click MobaXterm_Personal_6.5.exe Double-click saved session scc1.bu.edu [SSH] Login: < userID > Password: <password> Connecting to a Linux Host - Windows Client

14.Terminal Type ssh –X scc1.bu.edu or ssh –Y scc1.bu.edu Connecting to a Linux Host - Mac OS X Client

15.At the command prompt, type the following: c d t ar xf /scratch/linux-materials.tar Get supplementary files

16.A shell is a computer program that interprets the commands you type and sends them to the operating system. On Linux systems (and others, like DOS/Windows), it also provides a set of built-in commands and programming control structures, environment variables, etc. Most Linux systems, including BU’s Shared Computing Cluster, support at least two shells: TCSH and BASH. The default shell for your account is BASH. (Which is best? Caution: flame war potential here!) “BASH” = “Bourne-again Shell ” (GNU version of ~1977 shell written by Stephen Bourne) The Shell

17.V ariables are named storage locations. So-called “environment variables” are conventionally used by the shell to store information such as where it should look for commands (i.e., the PATH). Environment variables are shared with programs that the shell runs. To see the current value of PATH, do: e cho $PATH To see all currently defined environment variables do: printenv Bash environment variables

18.To create a new variable, use the assignment operator ‘=‘: f oo=“this is foo’s value” The foo variable will now be shown if you run the ‘set’ command (or type ‘echo $foo’). To make foo visible to programs run by the shell (i.e., make it an “environment variable”), use export: e xport foo Variables are used extensively in shell scripts (about which, more later) Bash variables

19.After you connect, type s hazam # bad command w hoami # my login h ostname # name of this computer e cho “Hello, world” # print characters to screen e cho $HOME # print environment variable e cho my login is $( whoami ) # replace $(xx) with program output d ate # print current time/date c al # print this month’s calendar Commands have three parts; command , options and parameters . Example: cal –j 3 1999 . “ cal ” is the command, “-j” is an option (or switch), “3” and “1999” are parameters. Options have long and short forms. Example: date –u date --universal Using the Shell What is the nature of the prompt? What was the system’s response to the command?

20.Try the history command Choose from the command history by using the up ↑ and down ↓ arrows To redo your last command, try !! To go further back in the command history try !, then the number as shown by history (e.g., !132). Or, !ls, for example, to match the most recent ‘ls’ command. What do the left ← and right → arrow do on the command line? Try the < Del> and < Backspace> keys Command History and Simple Command Line Editing

21.Type date –-help man date info date [And yes, you can always Google it] For a list of BASH built-in commands, just type the command ‘help’ (and see also ‘man bash’) Help with Commands

22.The ‘man’ command generally pipes its output through a pager called ‘less’, which supports many ways of scrolling through text: Space, f # page forward b # page backward < # go to first line of file > # go to last line of file / # search forward (n to repeat) ? # search backward (N to repeat) h # display help q # quit help On using ‘man’ with ‘less’ Plug: emacs has a man page mode that is convenient.

23.Many Linux commands print to “standard output”, which defaults to the terminal screen. The ‘|’ (pipe) character can be used to divert or “redirect” output to another program or filter . w # show who’s logged on w | less # pipe into the ‘less’ pager w | grep ‘ tuta ’ # pipe into grep , which will print only lines containing ‘ tuta ’ w | grep –v ‘ tuta ’ # print only lines not containing ‘ tuta ’ w | grep ‘ tuta ’ | sed s/ tuta /scholar/g # replace all ‘ tuta ’ with ‘scholar’ I/O redirection with pipes

24.Try the following (use up arrow to avoid retyping each line): w | wc # count lines, words, and characters w | cut –d’ ‘ –f1 | less # extract first column, page with ‘less’ w | cut –d’ ‘ –f1 | sort # sort users (with duplicates) w | cut –d’ ‘ –f1 | sort | uniq # eliminate duplicates We can also redirect output into a file: w | cut –d’ ‘ –f1 | sort | uniq > users Note that ‘ awk ’ can be used instead of ‘cut’: w | awk ‘{print $1;}’ | sort | uniq > users Quiz: How might we count the number of distinct users currently logged in? For extra credit, how can we avoid over-counting by 2? (Hint: use ‘tail’.) More examples of I/O redirection

25.The structure resembles an upside-down tree Directories (a.k.a. “folders” in Windows) are collections of files and other directories. Every directory has a parent except for the root directory. Many directories have subdirectories. Unlike Windows, with multiple drives and multiple file systems, a Unix/Linux system only has ONE file system. The Linux File System

26.A Typical Linux File System The Linux File System

27.Essential navigation commands: pwd print current directory ls list files cd change directory Navigating the File System

28.We use “pathnames” to refer to files and directories in the Linux file system. There are two types of pathnames: Absolute – the full path to a directory or file; begins with / Relative – a partial path that is relative to the current working directory; does not begin with / Special characters interpreted by the shell for filename expansion: ~ your home directory (e.g., /usr1/tutorial/tuta1) . c urrent directory .. p arent directory * wildcard matching any filename ? wildcard matching any character TAB try to complete (partially typed) filename Navigating the File System

29.Examples: cd / usr /local/lib # change directory to / usr /local/lib cd ~ # change to home directory (could also just type ‘cd’) p wd # print working (current) directory cd .. cd / (root directory) ls –d pro* # (a listing of only the directories starting with “pro”) Navigating the File System