Top Banner
Linux OS(Part І: Basics) CoSharif University of Technology mputer Workshop 88-89 1
35

CoSharif University of Technology mputer Workshop 88-89 1.

Dec 26, 2015

Download

Documents

Sheryl Cole
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Slide 1
  • CoSharif University of Technology mputer Workshop 88-89 1
  • Slide 2
  • Shell A program that takes the commands you type and translates them into instructions to the operating system. It is like Command Prompt in windows. A shell is just one interface to Linux. There are many possible interfaces--like the X Window System, which lets you run commands by using the mouse and keyboard. 2
  • Slide 3
  • Shell or Kernel? The shell manages the technical details of the operating system kernel interface, which is to the lowest-level, or 'inner-most' component of an operating system. The program is called a "shell" because it hides the details of the underlying operating system behind the shell's interface 3
  • Slide 4
  • SSH Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two networked devices. Used primarily on Linux and Unix based systems to access shell accounts. SSH was designed as a replacement for Telnet and other insecure remote shells. 4 Telnet is a protocol that allows you to connect to remote computers (called hosts) over a TCP/IP network (such as the Internet). You use software called a telnet client on your computer to make a connection to a telnet server (i.e., the remote host). http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
  • Slide 5
  • Get Started 1 Using Tab and Arrow keys. Everything is a file. files are case sensitive. bookstobuy.txt BooksToBuy.txt BoOkStObUy.txt Maximum filename length is 256 char. Files are identified by their file names. Directory is a file, tooa special kind of file that contains information about other files. 5 Getting familiar with current directory
  • Slide 6
  • Common shell commands: File & directory commands: cd,ls,cp,mv,rm,mkdir,pwd,ln touch,cat, vim, head, tail Searching for files: find, locate, updatedb whereis, which Permission-related: chmod, chgrp, chown Security-related: su, passwd, logout System-resources related: ps, kill, free, df, du Other commands: echo who, whoami,finger, write, talk, mesg gcc 6
  • Slide 7
  • Commands cd: change directory cd [with no arguments] ---> change to user home directory cd / ---> root directory Cd ~ home directory cd - (back) ls: list directory contents -a : all,-A,-d -l : long listing format(All infos) -n : numeric uid/gid (this way, students can find others' STUDENT IDs) -F, --classify Append indicator (one of */=@|) to entries cp: copy, mv: move (can be used to rename files, too) -r : recursive -v : verbose -I (warn you about overwriting) -R (for copying directories) 7
  • Slide 8
  • Commands 8
  • Slide 9
  • To show long listing information about the file/directory. $ ls l -rw-r----- 1 ramesh team-dev 9275204 Jun 13 15:27 mthesaur.txt.gz 1st Character File Type: First character specifies the type of the file. In the example above the hyphen (-) in the 1st character indicates that this is a normal file. Following are the possible file type options in the 1st character of the ls -l output. Field Explanation - normal file d directory s socket file l link file Field 1 File Permissions: Next 9 character specifies the files permission. Each 3 characters refers to the read, write, execute permissions for user, group and world In this example, -rw-r indicates read-write permission for user, read permission for group, and no permission for others. Field 2 Number of links: Second field specifies the number of links for that file. In this example, 1 indicates only one link to this file. Field 3 Owner: Third field specifies owner of the file. In this example, this file is owned by username ramesh. Field 4 Group: Fourth field specifies the group of the file. In this example, this file belongs to team-dev group. Field 5 Size: Fifth field specifies the size of file. In this example, 9275204 indicates the file size. Field 6 Last modified date & time: Sixth field specifies the date and time of the last modification of the file. In this example, Jun 13 15:27 specifies the last modification time of the file. Field 7 File name: The last field is the name of the file. In this example, the file name is mthesaur.txt.gz. 9 Ls -l
  • Slide 10
  • Help me! --help man : manual -k : find the command by its funcitionality. -f : shows the syntax for a command info: a little hard for novices to understand whatis: same as man f. apropos: same as man k. 10 http://kb.liquidweb.com/neccw-user-tutorial-whois/
  • Slide 11
  • Commands chgrp: change the group owning files. Make Oracle the owner of the database directory $chgrp oracle /usr/database chown: change the owner of a file. chown : change owner 11
  • Slide 12
  • Permission Letters and Their Meanings File AttributeAbbrivatio n Meaning for FileMeaning for Directory ReadablerCan view.Can list with ls. WritablewCan edit.Can delete, rename, or add files. ExecutablexCan run as program.Can access to read files and subdirectories or to run files. 12 The root user can always do anything to any file or directory
  • Slide 13
  • Change Permissions on Files chmod: Alphabetic: chmod [ugo][+-=][rwx] Example: chmod go+w sample.txt Make a file readable and writable by the group and others: chmod go+rw file Numeric: chmod [0-7][0-7][0-7] Example: chmod 664 sample.txt 13 OwnerGroupWorld Permissionsr; w; x Numeric representatio n 4; 2; 1
  • Slide 14
  • Commands Rm,rmdir: remove, rmdir: remove empty directory -d : directory -f : force -r : recursive mkdir: make directory -p (make n-level directory and subdirectory) pwd: print working directory ln: make links between files -s : make symbolic links instead of hard links 14
  • Slide 15
  • Link Hard linkSoft link Pointers to programs and files, but NOT directories If the original program or file is renamed, moved, or deleted, the hard link is NOT broken Hard links cannot span disk drives, so you CANNOT have a hard link on /dev/hdb that refers to a program or file on /dev/hda To create a hard link called myhardlink.txt that points to a file called myfile.txt, use this: ln myfile.txt myhardlink.txt Pointers to programs, files, or directories located elsewhere (just like Windows shortcuts) If the original program, file, or directory is renamed, moved, or deleted, the soft link is broken. If you type ls -F you can see which files are soft links because they end with @ To create a soft link called myfilelink.txt that points to a file called myfile.txt, use this: ln -s myfile.txt myfilelink.txt 15
  • Slide 16
  • Commands touch: update file timestamp touch can create new files -t : change a file to any desired time cat: concatenate and view (Concatenate FILE(s), or standard input, to standard output. ) less: Display output one screen at a time, Search through output, Edit the command line. Usage : | less [options] /: when you are in less mode you can find the special pattern which you want V : edit files when you view head: view first 10 lines of a file tail: view the last 10 lines of a file -f : shows the last 10 lines of a file as the file changes 16 Display a file : $ cat myfile.txt Concatenate two files: $ cat file1.txt file2.txt > union.txt
  • Slide 17 &1; done 17">
  • Touch Command Examples Create/datestamp one file called sample.txt: touch sample.txt Create/datestamp 10 files called file1, file2 etc: touch file{1,2,3,4,5,6,7,8,9,10} Create/datestamp 100 files called file1, file2 etc: for i in $(seq 1 100); do echo -n ">&1; done 17
  • Slide 18
  • Commands Vim Different modes of operation (Command Mode, Insert Mode) How to create a file How to save a file (using :w) How to exit (using :q) How to save a file and exit (using :wq) How to exit without saving (using :q!) How to search for texts (using / operator) 18
  • Slide 19 finds all files with txt extension in curren"> finds all files with txt extension in current (.) directory # find / ! -user me---> finds all files in the root directory whose owner is not me # find. -size +10M ----> find all files in the current directory whose size are beyond 1MB - whereis: locate the paths for a command's executable, source files, and man pages Search the user's $path, man pages and source files for a program -b : locate binary files -m : locate man pages -s : locate source files which: find out which version of as command will run 19 Locate : Search database(s) of filenames and print matches. *, ?, [, and ] are treated specially; / and. are not. Matches include all files that contain pattern, unless pattern includes metacharacters, in which case locate requires an exact match."> finds all files with txt extension in curren" title="Commands Locate : find files -i : case-insensitive search find Sample usages: # find. -name "*.txt" ----> finds all files with txt extension in curren">
  • Commands Locate : find files -i : case-insensitive search find Sample usages: # find. -name "*.txt" ----> finds all files with txt extension in current (.) directory # find / ! -user me---> finds all files in the root directory whose owner is not me # find. -size +10M ----> find all files in the current directory whose size are beyond 1MB - whereis: locate the paths for a command's executable, source files, and man pages Search the user's $path, man pages and source files for a program -b : locate binary files -m : locate man pages -s : locate source files which: find out which version of as command will run 19 Locate : Search database(s) of filenames and print matches. *, ?, [, and ] are treated specially; / and. are not. Matches include all files that contain pattern, unless pattern includes metacharacters, in which case locate requires an exact match.
  • Slide 20
  • Commands ps: report process status Common usages: ps aux ------> view all currently running processes ps auxf ------> view a process tree df, du Usage : with/without argument -k : in kilobytes -h : print file sizes in human-readable format (using K,M,G,etc for KB,MB,GB,etc) who: print who is currently logged in -a : all (detailed) whoami, su Echo : print out s.th 20
  • Slide 21
  • Vim 21 6.2.2. Basic commands 6.2.2.1. Moving through the text Moving through the text is usually possible with the arrow keys. If not, try: h to move the cursor to the left l to move it to the right k to move up j to move down SHIFT-G will put the prompt at the end of the document. 6.2.2.2. Basic operations n dd will delete n lines starting from the current cursor position. n dw will delete n words at the right side of the cursor. x will delete the character on which the cursor is positioned :n moves to line n of the file. :w will save (write) the file :q will exit the editor. :q! forces the exit when you want to quit a file containing unsaved changes. :wq will save and exit :w newfile will save the text to newfile. :wq! overrides read-only permission (if you have the permission to override permissions, for instance when you are using the root account. /astring will search the string in the file and position the cursor on the first match below its position. / will perform the same search again, moving the cursor to the next match. :1, $s/word/anotherword/g will replace word with anotherword throughout the file. yy will copy a block of text. n p will paste it n times. :recover will recover a file after an unexpected interruption. 6.2.2.3. Commands that switch the editor to insert mode a will append: it moves the cursor one position to the right before switching to insert mode i will insert o will insert a blank line under the current cursor position and move the cursor to that line. Pressing the Esc key switches back to command mode. If you're not sure what mode you're in because you use a really old version of vi that doesn't display an "INSERT" message, type Esc and you'll be sure to return to command mode. It is possible that the system gives a little alert when you are already in command mode when hitting Esc, by beeping or giving a visual bell (a flash on the screen). This is normal behavior.
  • Slide 22
  • Find Command Examples List all filenames ending in.mp3, searching in the current folder and all subfolders: $ find. -name "*.mp3" List all filenames ending in.mp3, searching in the music folder and subfolders: $ find./music -name "*.mp3" List files with the exact name: Sales_document.doc in./work and subfolders: $ find./work -name Sales_document.doc List all files that belong to the user Maude: $ find. -user Maude List all the directory and sub-directory names: $ find. -type d List all files in those sub-directories (but not the directory names) $ find. -type f List all the file links: $ find. -type l List all files (and subdirectories) in your home directory: $ find $HOME Find files that are over a gigabyte in size: $ find ~/Movies -size +1024M Find files have been modified within the last day: $ find ~/Movies -mtime -1 Find files have been modified within the last 30 minutes: $ find ~/Movies -mmin -30 Find.doc files that also start with 'questionnaire' (AND) $ find. -name '*.doc' -name questionnaire* List all files beginning with 'memo' and owned by Maude (AND) $ find. -name 'memo*' -user Maude Find.doc files that do NOT start with 'Accounts' (NOT) $ find. -name '*.doc' ! -name Accounts* 22
  • Slide 23
  • Common techniques The meaninig of tilde (~): home directory Running several commands using semicolon ; : for do some commands sequentially && : run commands only if the perevious ones succeed || : run a command only if the perevious one fails Running commands in the background using & Syntax:bg [PID...] example: bg %1 Using backslash (\) for commands that span more than one line 23 ampersand
  • Slide 24
  • Multitasking To run a job (program) in the background, type an ampersand (&) at the end of the command line. To suspend the currently running job, Ctrl-Z. To force a suspended job to run in the background, type bg. To bring a background job into the foreground, type fg. To see a list of jobs currently running (or suspended) type jobs. If you exclude the argument on these commands, it defaults to whatever job was last running in the foreground (not the one currently in the foreground). 24
  • Slide 25
  • Common techniques $(): as though you had typed that output in directly | : it takes the output from the first and uses it as input for the second. > : redirects the output of a command to a file. >> : append a commands output to a file. < : use a file as input for a command 25
  • Slide 26
  • Sort Command Sort text files. Sort, merge, or compare all the lines from the files given (or standard input.) Syntax sort [options] [file...] Examples: Character Sort: $ sort countries.txt Numeric sort: $ sort -n numbers.txt To sort the file below on the third field (area code): Jim Alchin 212121 Seattle Bill Gates 404404 Seattle Steve Jobs 246810 Nevada Scott Neally 212277 Los Angeles $ sort -k 3,3 people.txt> sorted.txt or using the 'old' syntax: $ sort +2 -3 people.txt> sorted2.txt To sort the same file on the 4th column and supress duplicates: (should return 3 rows) $ sort -u -k 4,4 people.txt> sorted3.txt 26
  • Slide 27
  • Sample Qs I have several directories as below /home/user/ dir1 dir2 dir3 Each directory has different size. I want to print each directory sizes sortedly. What can I do ? 27
  • Slide 28
  • Wildcards Imagine that you have the following files: libby1.jpg libby2.jpg libby3.jpg libby12.jpg libby1.txt You want to delete these files using the rm command,what will you do? 28
  • Slide 29
  • Wildcards There are three wildcards: *: matches any character zero or more times. ?: matches a single character. [ ]: match either a set of single characters ([12], for instance) or a range of characters separated by a hyphen (such as [1-3]). 29
  • Slide 30
  • Escaping special characters $ rm Why\ don\ 't\ I\ name\ files\ with\ \*\?.txt $ rm "Why don't I name files with *?.txt" 30 CharacterAdvice /Never use. Cannot be escaped. \Must be escaped. Avoid. -Never use at beginning of file or directory name. [ ]Must be escaped. Avoid. { }Must be escaped. Avoid. * ? ' "
  • Slide 31
  • Examples for using wildcards Examples for using *: 31 Command Matches rm libby1*libby10.jpg through libby12.jpg, as well as libby1.txt rm libby*.jpglibby1.jpg through libby12.jpg, but not libby1.txt rm *txtlibby1.txt, but not libby1.jpg through libby12.jpg rm libby*libby1.jpg through libby12.jpg, and libby1.txt rm *All files in the directory
  • Slide 32
  • Examples for using wildcards Examples for using ?: 32 Command Matches rm libby1?.jpg libby10.jpg through libby12.jpg, but not libby1.txt rm libby?.jpglibby1.jpg through libby9.jpg, but not libby10.jpg rm libby?.*libby1.jpg though libby9.jpg, as well as libby1.txt
  • Slide 33
  • Examples for using wildcards Examples for using [ ]: 33 Command Matches rm libby1[12].jpglibby11.jpg and libby12.jpg, but not libby10.jpg rm libby1[0-2].jpglibby10.jpg through libby12.jpg, but not libby1.jpg rm libby[6-8].jpglibby6.jpg through libby8.jpg, but nothing else
  • Slide 34
  • $ Clear 34
  • Slide 35
  • 35