Presentation is loading. Please wait.

Presentation is loading. Please wait.

Learning Linux Shell. Outline Introduction to Linux Learning the Shell Writing Shell Scripts.

Similar presentations


Presentation on theme: "Learning Linux Shell. Outline Introduction to Linux Learning the Shell Writing Shell Scripts."— Presentation transcript:

1 Learning Linux Shell

2 Outline Introduction to Linux Learning the Shell Writing Shell Scripts

3 Unix A multitasking, multi-user computer operating system.

4 Linux Linux is a Unix-like OS. Linux is open source and free. Popular distributions (distros) are Ubuntu, Fedora/Red Hat, OpenSUSE, Mandriva, Mint, ArchLinux, Slackware… The mascot is a “slightly overweight Tux penguin”.

5 Benefits of UNIX/Linux Low cost and very stable (some Linux servers are not rebooted for over a year, try that with Windows server!). Best multi-user, multitasking OS. Most secure OS. Hence, it’s popularity as a server OS. Best computing power and inbuilt network support Fastest developing OS, with the most number of developers.

6 Learning the Shell What is “The Shell” Navigation Looking Around Text Editor Manipulating Files Help I/O Redirection Permissions Search

7 The Shell The Shell is a program used to interface between you and the Linux. The bash is the most popular and default shell. sh (Bourne shell) ssh (Secure shell) bash (Bourne Again shell) User Linux Shell Program Linux kernel

8 Features of bash Tab completion: the program automatically fills in partially typed commands Wild cards *: matches any character and any # of character. ?: matches any single character. […]: matches any single character content within the […]. [yzhang@dias01 ~]$ cat /proc/version Linux version 2.6.32-573.3.1.el6.x86_64 (mockbuild@sl6-kojislave01.fnal.gov) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) #1 SMP Thu Aug 13 12:55:33 CDT 2015

9 A Terminal It's a program called a terminal emulator. This is a program that opens a window and lets you interact with the shell. Testing the keyboard Wonderful! Now press the up-arrow key. Watch how our previous command “blabla" returns. Yes, we have command history. Press the down-arrow and we get the most recent command. [yzhang@dias01 ~]$ blabla blabla: Command not found.

10 Learning the Shell What is “The Shell” Navigation Looking Around Text Editor Manipulating Files Help I/O Redirection Permissions Search

11 File System Organization

12 Three Commands pwd : print working directory. ls : list files and directories. cd : change directory.

13 pwd The directory you are standing in is called the working directory. To find the name of the working directory, use the pwd command. When you first log on to a Linux system, the working directory is set to your home directory (or your Desktop). [yzhang@dias01 ~]$ pwd /net/people/faculty/cs/yzhang

14 ls To list the files in the working directory, use the ls command. [yzhang@dias01 ~]$ ls bin Downloads mail Pictures research Trinity Desktop Music profile-bck rmi Videos Documents index.htm note.txt Public Templates www-docs

15 cd To change your working directory (where you are standing in the maze), type cd followed by the pathname of the desired working directory. The pathname of the directory is research, which is under your home directory. Notice how your prompt has changed? As a convenience, it is usually set up to display the name of the working directory. [yzhang@dias01 ~]$ cd Desktop [yzhang@dias01 Desktop]$ pwd /users/yzhang/Desktop

16 Two Kinds of Pathname An absolute pathname begins with the root directory “/” and leads to its destination. A relative pathname starts from the working directory. It uses three special notations to represent relative positions in the file system tree. The "." notation refers to the working directory itself. The ".." notation refers to the working directory's parent directory. The “~" notation refers to your home directory.

17 [yzhang@dias01 Desktop]$ cd.. [yzhang@dias01 ~]$ pwd /users/yzhang [yzhang@dias01 ~]$ cd./Desktop [yzhang@dias01 Desktop]$ pwd /users/yzhang/Desktop

18 Two Shortcuts If you type cd followed by nothing, cd will change the working directory to your home directory. If type cd ~user_name, cd will change the working directory to the home directory of the specified user. [yzhang@dias01 ~]$ cd Local/HTML-Documents/teaching/spring2016/1320 [yzhang@dias01 1320]$ pwd /users/yzhang/Local/HTML-Documents/teaching/spring2016/1320 [yzhang@dias01 1320]$ cd [yzhang@dias01 ~]$ pwd /users/yzhang [yzhang@dias01 ~]$ cd Desktop/ [yzhang@dias01 Desktop]$ cd [yzhang@dias01 ~]$ cd Desktop/ [yzhang@dias01 Desktop]$ cd ~yzhang [yzhang@dias01 ~]$ pwd /users/yzhang

19 Important Facts About File Names File names that begin with a period character (.) are hidden. File names are case sensitive. File.txt and file.txt refer to different files. Do not embed spaces in file names. If you want to represent spaces between words in a file name, use underscore characters. For example, this_is_a_file_of_linux_command.pdf.

20 Learning the Shell What is “The Shell” Navigation Looking Around Text Editor Manipulating Files Help I/O Redirection Permissions Search

21 Looking Around ls (list files and directories) less (view text files) file (classify a file's contents)

22 ls command -options ls –l  List the files in the working directory in l ong format. ls –la  List a ll files (including hidden files) in the working directory in long format. [yzhang@dias01 ~]$ ls –la [yzhang@dias01 ~]$ ls –l

23 -rw------- 1 yzhang cs 50 Feb 24 09:21 a_test.txt drwx------ 2 yzhang cs 4096 Dec 3 2012 bin -rw-r--r-- 1 yzhang cs 120 Feb 24 08:49 C:\nppdf32Log\debuglog.txt drwxr-xr-x 3 yzhang cs 4096 Feb 19 13:57 Desktop drwxr-xr-x 2 yzhang cs 4096 Aug 27 12:58 Documents drwxr-xr-x 2 yzhang cs 12288 Dec 10 12:54 Downloads drwx------ 5 yzhang cs 4096 Jan 20 10:53 HiWorld File permissionsOwnerGroupSize (in bytes)Modification timeFile name

24 less less is a program that lets you view text files. This is very handy since many of the files used to control and configure Linux are human readable. You may use the Page Up and Page Down keys to move through the text file. To exit less, type " q ". [yzhang@dias01 ~]$ less a_test.txt This is a test file for the Linux "less" command. a_test.txt (END)

25 file file will examine a file and tell you what kind of file it is. [yzhang@linfac6 108 ~]$ file a_test.txt a_test.txt: ASCII English text

26 A Guided Tour cd into each directory. Use ls to list the contents of the directory. If you see an interesting file, use the file command to determine its contents. For text files, use less to view them.

27 Learning the Shell What is “The Shell” Navigation Looking Around Text Editor Manipulating Files Help I/O Redirection Permissions Search

28 vi Text Editor The default editor that comes with the UNIX operating system is called vi ( vi sual editor). Three modes of operation Command mode is the default when you enter vi. In command mode, everything that you type will be interpreted as commands.  Press Esc to return to command mode. In insert mode, whatever you type is inserted in the file at the cursor position.  Press Esc to return to command mode.  Type :a (lowercase letter a, for a ppend) to enter insert mode from command mode;  Press Esc to end insert mode, and return to command mode. Use line mode to enter line oriented commands.  In command mode, type a colon ( : ). Your cursor moves to the bottom of the screen, by a colon prompt.  Type a line mode command, then press Enter. See Manual: vi for a complete manual of vi.

29 Manipulating Files mkdir - create directories cp - copy files and directories mv - move or rename files and directories rm - remove files and directories

30 mkdir The mkdir command is used to create directories. Create a directory CSCI1320 in your home directory. [yzhang@dias01 ~]$ mkdir CSCI1320

31 Practice Read Practice: Linux Commands and do practices A1-8.

32 cp The cp program copies files and directories. Copy HelloWorld.scala to home directory. [yzhang@dias01 CSCI1320]$ cp HelloWorld.scala../.

33 mv The mv command moves or renames files and directories depending on how it is used. It will either move one or more files to a different directory, or it will rename a file or directory. Rename HelloWorld.scala to HowdyWorld.scala. [yzhang@dias01 CSCI1320]$ mv HelloWorld.scala HowdyWorld.scala

34 rm The rm command removes (deletes) files and directories. Remove HowdyWorld.java. [yzhang@dias01 CSCI1320]$ rm HowdyWorld.java

35 Practice (cont’d) Refer to Practice: Linux Commands and do the following two tasks: practices A9-15. On your own. Key concepts regards files and directories / root directory ~ home directory. current directory.. parent directory absolute path /dir/file vs. relative path../file

36 Learning the Shell What is “The Shell” Navigation Looking Around Text Editor Manipulating Files Help I/O Redirection Permissions Search

37 Help which - Display information about command type man - Display an on-line command reference  To exit man, type " q ". [yzhang@dias01 ~]$ which which alias which='alias | /usr/bin/which --tty- only --read-alias --show-dot --show-tilde' /usr/bin/which [yzhang@dias01 ~]$ which man /usr/bin/man

38 I/O Redirection We can redirect the output of many commands to files, devices, and even to the input of other commands. Standard Output By default, standard output directs its contents to the display. To redirect standard output to a file, the ">" character is used like this The scala command is executed and the result is written (redirected) in a file named output. So no result appear on the display. [yzhang@dias01 CSCI1320]$ scala HelloWorld.scala > output [yzhang@dias01 CSCI1320]$ less output Hello World!

39 I/O Redirection Standard Input By default, standard input gets its contents from the keyboard, but like standard output, it can be redirected. Download Redirect_Input_Output.scala from course website to your CSCI1320. Run it in terminal. Interact with the program by providing the input information. scala Redirect_Input_Output.scala Use vi to create a txt file called input. The content is: Run it again by these commands: Yu Zhang 12345 [yzhang@dias01 CSCI1320]$ scala Redirect_Input_Output output [yzhang@dias01 CSCI1320]$ less output What is your name? What is your phone number? Nice to meet you Yu Zhang. I will call you at 12345.

40 Learning the Shell What is “The Shell” Navigation Looking Around Text Editor Manipulating Files Help I/O Redirection Permissions Search

41 Permissions [yzhang@dias01 CSCI1320]$ ls -l /bin/bash -rwxr-xr-x 1 root root 941720 Jul 22 09:12 /bin/bash

42 Permissions chmod - modify file access rights. It is easy to think of the permission settings as a series of bits (which is how the computer thinks about them). Here's how it works: rwx rwx rwx = 111 111 111 rw- rw- rw- = 110 110 110 rwx --- --- = 111 000 000 and so on... rwx = 111 in binary = 7 rw- = 110 in binary = 6 r-x = 101 in binary = 5 r-- = 100 in binary = 4

43 Permissions [yzhang@dias01 CSCI1320]$ chmod 600 some_file Now, if you represent each of the three sets of permissions (owner, group, and other) as a single digit, you have a pretty convenient way of expressing the possible permissions settings. For example, if we wanted to set some_file to have read and write permission for the owner, but wanted to keep the file private from others, we would:

44 Permissions

45 Learning the Shell What is “The Shell” Navigation Looking Around Text Editor Manipulating Files Help I/O Redirection Permissions Search

46 The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. grep is considered as one of the most useful commands on Linux. grep 'word' filename grep 'word' file1 file2 file3 grep 'string1 string2' filename grep --color 'data' fileName

47 Search Download file_for_grep.txt from the course website to your CSCI1320. You can force grep to ignore word case i.e match abc, ABC and all other combination with the -i option: [yzhang@dias01 CSCI1320]$ grep abc file_for_grep.txt abcdefg [yzhang@dias01 CSCI1320]$ grep -i abc file_for_grep.txt abcdefg ABCDEFG

48 Search You can search recursively i.e. read all files under each directory for a string “abc“, with the -r option. Or combine options –r and –i. Use grep to search words only with the -w option. [yzhang@dias01 CSCI1320]$ cd [yzhang@dias01 ~]$ grep -r abc CSCI1320 CSCI1320/file_for_grep.txt:abcdefg [yzhang@dias01 ~]$ grep -ri abc CSCI1320 CSCI1320/file_for_grep.txt:abcdefg CSCI1320/file_for_grep.txt:ABCDEFG [yzhang@dias01 ~]$ grep -riw Inauguration CSCI1320 1Linux/file_for_grep.txt:The Inauguration of Trinity's 19th president, Danny Anderson, is just a 1Linux/file_for_grep.txt:little more than a month away now. The Inauguration Committee and other 1Linux/file_for_grep.txt:participate in all the Inauguration events starting with the Day of 1Linux/file_for_grep.txt:Inauguration will be a great way to show your TigerPride.

49 Search Use grep to search 2 different words. Use the -l option to list file name whose contents mention “college”: you can force grep to display output in colors [yzhang@dias01 ~]$ grep -riw 'Danny Anderson' CSCI1320 1Linux/file_for_grep.txt:The Inauguration of Trinity's 19th president, Danny Anderson, is just a [yzhang@dias01 ~]$ grep -rl Trinity CSCI1320 CSCI1320/file_for_grep.txt [yzhang@dias01 ~]$ grep -r --color Trinity CSCI1320 1Linux/file_for_grep.txt:The Inauguration of Trinity's 19th president, Danny Anderson, is just a 1Linux/file_for_grep.txt:celebrate this important moment in Trinity's history. I hope you plan to 1Linux/file_for_grep.txt:Service as "Trinity Gives Back" on Feb. 13. The "Great Trinity 1Linux/file_for_grep.txt:our classrooms. On Friday, Feb. 19, plan to wear your Trinity maroon and


Download ppt "Learning Linux Shell. Outline Introduction to Linux Learning the Shell Writing Shell Scripts."

Similar presentations


Ads by Google