1 / 54

Introduction to Linux OS AUBG ICoSCIS Team Prof. Volin Karagiozov

Introduction to Linux OS AUBG ICoSCIS Team Prof. Volin Karagiozov. March, 09 – 10, 2013 SWU, Blagoevgrad. Outline. Some basic mostly used command ls, pwd, mkdir, cd, man, cp, mv, rm Text editors (vi, nano) Input/output redirection, pipes

ginata
Download Presentation

Introduction to Linux OS AUBG ICoSCIS Team Prof. Volin Karagiozov

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Linux OSAUBG ICoSCIS TeamProf. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad

  2. Outline • Some basic mostly used command ls, pwd, mkdir, cd, man, cp, mv, rm • Text editors (vi, nano) • Input/output redirection, pipes • File system and access modes • Protecting files and directories • Sharing files • The power of the “command line”

  3. Entering commands Command options and arguments $ date $ ls –a $ ls –al $ cal The who command The finger command $ finger $ finger user_name The write command The talk command The mesg command $ mesg y $ mesg n $ mesg Command name Option(s) Argument Allows receiving the messages Denies receiving the messages

  4. Specifying a Terminal Terminal characteristics $ echo $TERM $ stty $ TERM=vt100 $ export TERM $ echo $TERM Environment variable Displays value(s) on the screen Definition of the variable TERM Makes the new value known to the system

  5. Listing the content of a Directory $ ls -l total 1272 drwxr-xr-x 2 volin users 4096 Jan 21 11:23 COS120 drwx------ 5 volin users 4096 Sep 3 10:31 Desktop -rwxr-xr-x 1 volin users 1504 Oct 10 13:48 a -rw-r--r-- 1 volin users 108 Dec 16 22:41 awk1 -rw-r--r-- 1 volin users 68 Dec 16 22:59 awk2 -rw-r--r-- 1 volin users 57 Dec 14 22:50 awktest -rwxr-xr-x 1 volin users 1472 Oct 10 13:48 b -rw-r--r-- 1 volin users 1561 Nov 27 17:08 bank.pl $ ls $ ls –a $ ls -al $ ls -al|more total 1396 drwxr-xr-x 21 volin users 4096 Jan 21 11:17 . drwxr-xr-x 117 root root 4096 Jan 21 10:59 .. -rw------- 1 volin users 0 Dec 3 18:11 .ICEauthority -rw------- 1 volin users 107 Dec 3 18:11 .Xauthority -rw-r--r-- 1 volin users 0 Sep 4 21:54 .addressbook drwxr-xr-x 2 volin users 4096 Jan 21 11:23 COS120 drwx------ 5 volin users 4096 Sep 3 10:31 Desktop -rwxr-xr-x 1 volin users 1504 Oct 10 13:48 a -rw-r--r-- 1 volin users 108 Dec 16 22:41 awk1

  6. Displaying the contents of a Text File $ cat f1 f2 $ more ffff File name(s) Displays the content page by page

  7. More commands $ date $ cal Options? How to learn about the commands? $date Mon Jan 28 11:43:14 EET 2002 $cal January 2002 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

  8. Searching the man-pages with Keywords • If you do not know the name of the command to carry out a particular task, you can search the man-pages with keyword. $ man –k sort or $ apropos sort • The output from these commands might be more than one screen. How can you manage to get page-by-page output? • Texinfo database in Linux contains full description of the commands. $ info <name_of_the_command> “Play” for a couple of minute with these commands

  9. More commands There are three vital things to know when you're working on a Unix system: • Who am I? • Where am I? • What am I? $ whoami $who am i $ pwd (print working directory) $hostname $uname –a Try this commands now!

  10. Getting Around • You can move around from directory to directory using the cdcommand. Give the name of the directory you want to move to, or give no name to move back to your home directory. Use the pwd command to see where you are (or rely on the prompt, if configured): $ cd $ pwd $ cd / $ pwd $ cd /home $ ls –F $ cd ~ $ pwd $ cd .. $ pwd Try this commands and tell what “/”, “~” and “..” symbols mean? • Each directory contains two special hidden • directories named "." and "..". • "." refers always to the directory in which • it is located. • ".." refers always to the parent of the • directory.

  11. Absolute path of directory john /users/john/work

  12. Relative pathname from work to play ../play

  13. Files in the directory tree

  14. Exercise: Exploring the filesystem Take a tour of the directory system, hopping one or many steps at a time, with a mixture of cd and pwd commands. Go to your home directory. Enter cd Find your working directory. Enter pwd Change to new working directory. Enter cd /etc List files in new working directory. Enter ls Change directory to root and list files. Enter cd /; ls Change to a new directory. Enter cd usr Give a wrong pathname. Enter cd xqk Change to a new directory with its absolute pathname. Enter cd /etc List files in another directory. Enter ls /bin Find your working directory (notice that ls didn't change it) Enter pwd Return to your home directory. Enter cd

  15. Environment Variables • Unix commands and other programs have many options. Some options are controlled by configuration files, others by command-line switches, and still others by environment variables. Each environment variable has a name and a bit of text (usually one line's worth) associated with it. • To Examine one Environment Variable use command $ echo $var_name

  16. Environment Variables … • The following environment variables contain important and/or useful information: PATH - A colon-delimited list of directories that the shell will search through when you type a command. TERM - The current terminal type, which in term controls whether the screen can do colors, scrolling, etc. DISPLAY - The machine on which X-windows based applications will display their windows. Usually of the form: hostname:0.0, where hostname is the name or IP address of your computer. (Don't worry about the meaning of the 0.0 -- it's "nothing") HOME - Path to your home directory. USER - Your user name. $ echo $HOME $ printenv | more

  17. Using echo to display text on the terminal • The echo utility copies to the terminal anything you put on the command line after echo. $ echo Hi Hi $ echo Hello World! Hello World! $ echo date date $ echo ”date” date $ echo ’date’ date $ echo `date`

  18. Copy file cp cp source-file destination-file $ cd $ cat > file1 This is a source file named file1 Ctrl+D $ ls $ cp file1 file2 $ ls $ cp file2 file2.0130.week2 $cd $cp /home/vkaragiozov/cos231/memo01. File manipulation commands (copy)

  19. File manipulation commands (rename) Changing the name of the file mv mv existing-file new_file $ mv file1 file1.week1 $ ls

  20. grep to Find a String $ cat memo01 $ grep quiz memo01 $ grep quizzes memo01 $ grep –v quiz memo01 wc(word count) command – print the numbers of bytes, words and lines in file $ wc memo01 $ wc –w memo01 $ wc –c memo01 $ wc –l memo01 More commands …

  21. More commands … • head to look at the top of the file $ head memo01 $ head –1 memo01 $ head –3 memo01 • tail to look at the end of the file $ tail memo01 $ tail –1 memo01 $ tail –4 memo01

  22. More commands … • sort to display a File in Order $ sort mem01 • uniq to remove adjacent duplicated lines in a file $ cat > test $ cat test Line3 $ uniq test Line1 What about to use first sort command Line3 and after it uniq? Line2 Line1 Ctrl+D

  23. Recap … • Searching the man-pages with Keywords and TexInfo database (apropos, info) • More commands (whoami, pwd, hostname, uname) • Getting around from directory to directory (cd, “..”, “.”, “/”, “~”) • Absolute and relative path of directory • Environment variables • Using echo to display text on the terminal (“…”, ‘…’, `…`) • File manipulation commands (copy, mv, head, tail) • More commands (grep, wc, sort, uniq)

  24. Using echo to display text on the terminal • The echo utility copies to the terminal anything you put on the command line after echo. $ DATE=tomorrow; export DATE $ echo $DATE tommorow $ echo DATE DATE $ echo ”$DATE” tommorow $ echo ‘$DATE’ $DATE $ echo ‘$DATE$’ $DATE$ $ echo \$DATE $DATE $ echo “$DATE$” ???

  25. Special Characters & Quoting Characters • Special characters are those which have a special meaning to the shell: & ; | * ? ‘ “ ` [ ] ( ) $ < > { } ^ # / \ ! • Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.

  26. Quoting mechanisms There are three quoting mechanisms: the escape character, single quotes, and double quotes • A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the exception of <newline>. • Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash. • Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, and \. The characters $ and ` retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or <newline>. A double quote may be quoted within double quotes by preceding it with a backslash.

  27. Using whereis and which to Find Utilities • Purpose – to know the full pathname associated with the command. Why? • The shell searches for a command in so called search path • How can you display the search path? Do it now! • /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/java/jdk1.3/bin:/usr/X11R6/bin:/usr/local/mysql/bin:/usr/local/netscape:/soft/u01/app/oracle/product/8.1.7/bin:/home/volin/bin $whereis grep  grep: /bin/grep /usr/man/man1/grep.1.gz $which grep  /bin/grep

  28. Using diff to Compare Two Files • Copy two files named colors.1 and colors.2 from ~vkaragiozov@aubgin.local/cos231 to your home directories. • Compare the files: $ diff colors.1 colors.2 4d3 < yellow

  29. Compressing the files • file utility gives the information about the content of the file $ file * • compress (uncompress) utilities $ compress –v memo01 $ file * $ zcat memo01 $ uncompress memo01 $ file *

  30. File and Directory Wildcards • When you have a number of files named in series (for example, chap1 to chap12) or filenames with common characters (like aegis, aeon, and aerie), you can use wildcards (also called metacharacters) to specify many files at once. These special characters are: • * (asterisk) - An asterisk is replaced by any number of characters in a filename. For example, ae* would match aegis, aerie, aeon, etc. if those files were in the same directory. You can use this to save typing for a single filename (for example, al* for alphabet.txt) or to name many files at once (as in ae*).

  31. File and Directory Wildcards … • ? (question mark) - A question mark is replaced by any single character (so h?p matches hop and hip, but not help). • [] (square brackets) - Square brackets can surround a choice of characters you'd like to match. Any one of the characters between the brackets will be matched. For example, [Cc]hapter would match either Chapter or chapter, but [ch]apter would match either capter or hapter. Use a hyphen (-) to separate a range of consecutive characters. For example, chap[1-3] would match chap1, chap2, or chap3.

  32. Using Wildcards - practice Let’s create quickly the files we need: $ touch chap10 chap2 chap5 cold chap1a.old chap3.old chap6 haha chap1b chap4 chap7 oldjunk And try the following commands: $ ls chap? $ ls chap?? $ ls chap[5-8] $ ls chap* $ ls *old $ ls *a*a*

  33. Creating Directories • To create a new directory, use the mkdir command. The format is: $ mkdir dirname(s) dirname is the name of the new directory. To make several directories, put a space between each directory name. To continue our example, you would enter: $ mkdir spy boston.dine

  34. Removing Files and Directories The rm command removes files. The syntax is simple: $ rmfilename(s) rm removes the named files, as the following examples show: $ ls $ rm *old chap10 $ ls $ rm ch* $ ls Caution: rm * rm –rf * DO NOT TRY THIS!

  35. Removing Directories • Just as you can create new directories, you can also remove them with the rmdir command. As a precaution, the rmdir command will not let you delete directories that contain any files or subdirectories: the directory must first be empty. (The rm -r command removes a directory and everything in it. It can be dangerous for beginners, though.) • The syntax is rmdirdirname(s) If a directory you try to remove does contain files, you will get a message like "rmdir: dirname not empty". • To delete a directory that contains some files: • Enter cddirname to get into the directory you want to delete. • Enter rm * to remove all files in that directory. • Enter cd .. to go to the parent directory. • Enter rmdirdirname to remove the unwanted directory.

  36. Exercise: Manipulating files In this exercise, you'll create, rename and delete files Go to home directory. Enter cd Copy distant file to working directory. Enter cp /etc/passwd myfile Create new directory. Enter mkdir temp List working directory. Enter ls -F Move file to new directory. Enter mv myfile temp Change working directory. Enter cd temp Copy file to working directory. Enter cp myfilemyfile.two List filenames with wildcard. Enter ls -l myfile* Remove files. Enter rm myfile* Go up to parent directory. Enter cd .. Remove directory. Enter rmdir temp Verify that directory was removed. Enter ls -F

  37. Standard Input, Standard Output and Standard Error • In general, if no filename is specified in a command, the shell takes whatever you type on your keyboard as input to the command (after you press the first [RETURN] to start the command running, that is). Your terminal keyboard is the command's standard input. • As a command runs, the results are usually displayed on your terminal screen. The terminal screen is the command's standard output. • The error messages are usually displayed on your terminal screen, also. We call it standard error stream.

  38. Standard Input, Standard Output and Standard Error • Standard input is associated with so called file descriptor 0. • Standard output is associated with so called file descriptor 1. • Standard error is associated with so called file descriptor 2. • So, by default, each command takes its input from the standard input, sends the results to the standard output and sends the error messages (if any) to the standard error

  39. Input/Output Redirection • Two default cases of input/output can be varied. This is called input/output redirection. • In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is <, the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is >, the redirection refers to the standard output (file descriptor 1).

  40. Input/Output Redirection • You can use a given file as input to a command that doesn't normally accept filenames by using the "<" (less-than symbol) operator. • The > operator - When you add "> filename" to the end of a command line, the results of the command are diverted from the standard output to the named file. The > symbol is called the output redirection operator.

  41. The >> operator • You can add more text to the end of an existing file, instead of replacing its contents, by using the >> (append redirection) operator. Use it like the > (output redirection) operator. So, catfile2>>file1 appends the contents of file2 to the end of file1. • Exercise: $ date > today $ who > users $ cat today > today_users $ cat users >> today_users $ cat today_users

  42. Redirecting Standard Output and Standard Error • Bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file • There are two formats for redirecting standard output and standard error: &>file_name and >&file_name Of the two forms, the first is preferred. This is semantically equivalent to >file_name 2>&1

  43. Redirection - examples :::::::::::::: memo01 :::::::::::::: Due on Tuesday, 4th you should submit by e-mail to your Instructor the first homework which will be published on our course Web-page. My e-mail address is: vkaragiozov@aubg.bg Use the Subject: COS231a/or b - HW 01 Greetings, Volin Karagiozov Mon Jan 28 12:05:08 EET 2003 unexist_file: No such file or directory $ cd; cat /etc/passwd > passwd $ cat passwd $ who > users $ date > today $ ls $ cat users $ cat today $ more memo01 unexist_file $ more memo01 unexist_file 2>test $ cat test $ more memo01 unexist_file &>test $ cat test Error message unexist_file: No such file or directory

  44. Pipes • In addition to redirecting input/output to a named file, you can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe. • To make a pipe, put a vertical bar (|) on the command line between two commands. When a pipe is set up between two commands, the standard output of the command to the left of the pipe symbol becomes the standard input of the command to the right of the pipe symbol. • Any two programs can form a pipe as long as the first program writes to standard output and the second program reads from standard input. • Each command in a pipeline is executed as a separate process (i.e., in a subshell).

  45. Named Pipes • The other sort of pipe is a “named” pipe, which is sometimes called a FIFO. FIFO stands for “First In, First Out” and refers to the property that the order of bytes going in is the same coming out. The “name” of a named pipe is actually a file name within the file system. Pipes are shown by ls as any other file with a couple of differences: $ mkfifo fifo1 $ ls -l fifo1 prw-r--r-- 1 AUBGIN\vkaragiozov AUBGIN\domain users 0 Feb 4 09:42 fifo1 • One very useful application of named pipes is to allow totally unrelated programs to communicate with each other. For example, a program that services requests of some sort (print files, access a database) could open the pipe for reading. Then, another process could make a request by opening the pipe and writing a command.

  46. Example of using named pipes • Two virtual consoles. fifo1 is a named pipe • On the first console execute $ ls -l > fifo1 • On the second console: $ cat < fifo1

  47. Filters • The grep program searches a file or files for lines that have a certain pattern. The syntax is: greppattern file(s) • The name "grep" derives from the ed (a UNIX line editor) command g/re/p which means "globally search for a regular expression and print all lines containing it." • A regular expression is either some plain text (a word, for example) and/or special characters used for pattern matching. When you learn more about regular expressions, you can use them to specify complex patterns of text.

  48. Exercise: Redirecting input/output, pipes and filters • In the following exercises you'll redirect output, create a simple pipe, and use filters to modify output. Redirect output to a file. Enter who > users Sort output of a command. Enter who | sort Append sorted output to a file. Enter who | sort >> users Display output to screen. Enter more users or less users Display long output to screen. Enter ls -l /bin | more or ls -l /bin | less

  49. Output fromls -al

  50. Protecting and Sharing Files • Directory Access Permissions A directory's access permissions help to control access to the files in it. These affect the overall ability to use files and subdirectories in the directory. (Once you have access to a directory, the ability to read or modify the contents of specific files is controlled by the file access permissions • File Access Permissions The access permissions on a file control what can be done to the file's contents. The access permissions on the directory where the file is kept control whether the file can be renamed or removed.

More Related