Essential Administrative Tools  Commonly used utilities Grep, Awk, Find, Xargs  Communication commands  Editor Vi 1.

Slides:



Advertisements
Similar presentations
Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
Advertisements

การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
Cosc 4750 Getting Started in UNIX Don’t be afraid of the prompt, in linux it can be your best friend. In some cases, the only way to do certain things.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Unix – CS 21 Lecture 13. Lecture Overview Finding files and programs which whereis find xargs Putting it all together for some complex.
Guide To UNIX Using Linux Third Edition
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
Introduction to Linux and Shell Scripting Jacob Chan.
Linux Commands LINUX COMMANDS.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Advanced File Processing
Va-scanCopyright 2002, Marchany Unit 6 – Solaris File Security Randy Marchany VA Tech Computing Center.
Help session: Unix basics Keith 9/9/2011. Login in Unix lab  User name: ug0xx Password: ece321 (initial)  The password will not be displayed on the.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
Unix programming Term: III B.Tech II semester Unit-II PPT Slides Text Books: (1)unix the ultimate guide by Sumitabha Das (2)Advanced programming.
Linux+ Guide to Linux Certification, Third Edition
Working with Linux Lab 1 1. Login and logout Account – username & password – Note: Linux is case-sensitive Administrator: username = root Logout: exit,
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Session 2 Wharton Summer Tech Camp Basic Unix. Agenda Cover basic UNIX commands and useful functions.
INTRODUCTION TO LINUX Jacob Chan. GNU/Linux Consists of Linux kernel, GNU utilities, and open source and commercial applications Works like Unix –Multi-user.
Week 3 Exploring Linux Filesystems. Objectives  Understand and navigate the Linux directory structure using relative and absolute pathnames  Describe.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Lesson 9-Setting and Using Permissions. Overview Describing file permissions. Using execute permissions with a file. Changing file permissions using mnemonics.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 10 Linux.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
Introduction to Unix – CS 21 Lecture 4. Lecture Overview * cp, mv, and rm Looking into files The file command head and tail cat and more What we’ve seen.
BIF713 Basic Unix/Linux Commands Getting Help with Commands.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Chapter Four I/O Redirection1 System Programming Shell Operators.
UNIX An Introduction. Brief History UNIX UNIX Created at Bell Labs, 1969 Created at Bell Labs, 1969 BSD during mid 70s BSD during mid 70s AT&T began offering.
Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.
Linux+ Guide to Linux Certification, Third Edition
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Linux Commands C151 Multi-User Operating Systems.
CS 245 – Part 1 Using Operating Systems and Networks for Programmers Jiang Guo Dept. of Computer Science California State University Los Angeles.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
File Management commands cat Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Learning Unix/Linux Based on slides from: Eric Bishop.
Tutorial of Unix Command & shell scriptS 5027
Lesson 5-Exploring Utilities
Quality Thought Technologies
Chapter 11 Command-Line Master Class
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
Some Linux Commands.
C151 Multi-User Operating Systems
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands:
Vi Editor.
CSE 374 Programming Concepts & Tools
INTRODUCTION TO UNIX: The Shell Command Interface
Tutorial of Unix Command & shell scriptS 5027
Basic UNIX OLC Training.
Tutorial of Unix Command & shell scriptS 5027
Guide To UNIX Using Linux Third Edition
Tutorial of Unix Command & shell scriptS 5027
Chapter Four UNIX File Processing.
Tutorial Unix Command & Makefile CIS 5027
LPI Linux Certification
Presentation transcript:

Essential Administrative Tools  Commonly used utilities Grep, Awk, Find, Xargs  Communication commands  Editor Vi 1

2 Utility grep  The grep command searches its input for lines containing a given pattern.  The grep is commonly used to search files  #grep mysql /etc/passwd  Use grep with pipe is very useful  Example: find out about a process owned by one certain user $ps –ef | grep chavez $ps –aux | grep chavez $ps –aux | egrep ‘chavez|PID’ $alias pu “ps –aux | egrep ‘\!:1|PID’ ” $ pu chavez Exercise: find all process owned by root

3 Utility awk  Manipulate the output of another command Picking out the columns  List the users that run dooms. $ps –ef | grep “[d]oom” | awk ‘{print $1}’ $ps –ef | grep doom| grep –v grep | awk ‘{print $1}’  Create a file to store the users that run dooms, include the data, cpu time $ (date ; ps –ef | grep “[d]oom” | awk ‘{print $1 “ [ “ $7 “]” }’ | sort | uniq) >> doomed.users Sum up a column of numbers Example: search files owned by chavez and calculate the total size. #find. -user chavez –ls | awk ‘{sum+=$7}; END {print “User chavez total disk use =“ sum}’

4 Utility awk  Background You want to create a log file that include the hostname (wopr.csl.mtu.edu) and the date ( 24Aug2010).  Exercise: Create a file with name hostname.mmddyyyy.log  #date  #date | awk ‘{print $3 $2 $6}’  #echo `hostname`.`date | awk ‘{print $3 $2 $6}’`.log  #touch `hostname`.`date |awk ‘{print $3 $2 $6}’`.log

5 Utility find  Find locates files having certain characteristics on where you tell it to look. Basic syntax #find starting-dir(s) criteria-and-action Matching criteria  Very flexible Action  What to do with the files matches all the criteria

6 Utility find -atime nFile was last accessed n days ago -mtime nFile was last modified exactly n days ago -newer fileFile was modified more recently than file was -size nFile is exactly n 512-byte blocks long -type cSpecifies file typeL f, d -name namThe filename is nam -perm pThe file’s access mode is p -user usrThe file’s owner is usr -group grpThe file’s group owner is grp -nouserThe file’s owner is not listed in the password file -nogroupThe file’s group owner is not listed in the group file

7 Utility find  Use +, - to indicate more than, less than -mtime +7 last modified more than 7 days ago -atime –2 last accessed less than 2 days ago -size +100 larger than 50k  Use wildcards with –name option -name “*.dat”  Join more condition together Use () to change the precedence Expr1 – o expr2 Example: \( -atime +7 –o –mtime +30 \) ! Expr Example: ! –name gold.dat Expr1 – a expr2 (as same as Expr1 expr2) Example: ! –name gold.dat –name \*.dat

8 Utility find  Check for a specific access mode with – perm Exact permission  -perm 75 At least permission with “‘-” sign  -perm –002 world writable  -perm –4000 SUID access is set  -perm –2000 SGID access is set

9 Utility find  Actions optionMeaning -printDisplay pathname of matching file -lsDisplay long directory listing for matching files -exec cmdExecute command on file -ok cmdPrompt before executing command on file -xdevRestrict the search to the file system of the starting directory -pruneDon’t descend into directories encountered

10 Utility find  Default is –print Example: $ find. –name \*.c -print  -exec and –ok must end with \;  {} may be used in commands as a placeholder for the pathname of each found file. -exec rm –f {} \;

11 Utility find  The usage of find for administration Monitoring disk usage Locating file that pose potential security problems Performing recursive operations  Example: $find /chem –size –mtime +30 –exec ls –l {} \; $find /chem –size \( -mtime +30 –o –atime +120 \) –ls $find / \( -perm –2000 –o –perm –4000\) –print | diff – files.secure $find /chem –name ‘*.c’ –exec mv {} /chem1/src \;

12 Utility xargs  Repeating Commands: xargs Command find is limited to files Command xargs can accept any objects Example  Send all the arguments to one commands.  Low all the priority of doom processes by increasing nice number by 10. #ps –ef | grep “[d]oom” | awk ‘{print $2}’ | xargs renice +10  Send the arguments in groups by using –n option  Warn each user #ps –ef | grep “[d]oom” | awk ‘{print $a}’ | xargs –n1 warn_user

13 Utility xargs More xargs examples  Place the argument in a specific position #ps –ef | grep “[d]oom” | awk ‘{print $a}’ | xargs –I chargefee {} 100 -t option to display each constructed command before executing -p to allow you to selectively execute commands by prompting you before each one.

14 Utility mkdir  Creating several directory levels at once The command mkdir has option –m, -p  Set the mode with creating a file $mkdir –m 775./phone.list $mkdir –m g+w./things  Create any missing parents required for the subdirectories $mkdir –p./a/b/c

15 Utility cp,tar,cpio  Duplicating an entire directory tree while keeping the ownership, mode settings and timestamp Command tar, cpio, cp Example  Copy the directory /chem/olddir to /chem1/newdir  Use tar # cd /chem1 # tar –cf - -C /chem olddir | tar –xvpf – #mv olddir newdir -p option of tar restores the ownership and access modes.  Use cpio #mkdir /chem1/newdir #cd /chem1/olddir # find. –print | cpio –pdvm /chem1/newdir

16 Exercise  On your local machine in the lab Make a copy of /etc to /etc.copy with the original settings preserved  #cp -pr /etc /etc.copy Compare nsswitch.conf under /etc and /etc.copy  #ls –l /etc/nsswitch.conf /etc.copy/nsswitch.conf  Same size?  Same date?  Same mode?

17 Exercise for fun  Background Sometime files with strange names were created. These command could ruins the use of the regular commands. Deleting Pesky Files can be a little something to do.  Exercise:  Create a file named -foo  # cat “Remove me” > “-foo”  Remove it  The answer is in the man page

18 More utilites  wc count the number of characters, words and lines  cat display the contents of a file or join files  more and less Display the contents of a file a page at a time  head display the first few lines of a file  tail Display the last few lines of a file  $tail –f  What does –f mean?

19 Essential Administrative Tools  sort sort the content of a file into order  uniq Remove duplicate lines from a file  cut remove columns of characters from a file  paste join columns of files together  tr translate specific characters  split split files evenly

20 Communicating with Users  Command write  Write username [tty]  who  rwho  Ctrl – D end it  Reply with write will create a two-way communication  Command wall  Send message to all users  Disable message  Command mesg n  Root account can override the setting  The message of the Day  /etc/motd  Command talk  Separate window for sender and receiver

Exercise  Log on to icu0.csl.mtu.edu %ssh icu0.csl.mtu.edu -lusename  Send a greeting message to all users %wall 21

22 vi  vi is an editor. It is the editor I strongly suggest you start using Why?  it's always available on UNIX  it includes access to an ex command line  it is hugely powerful  it will make stuff later easier

23 vi  Command format is normally [ count] command [where] count: number of times to repeat a command (optional) Command: the actual command Where: how much to act on or where to take the cursor depending on the command (optional) Examples  23x Delete 23 characters  25dd Delete 25 lines  d$ Delete from current position to the end of the line

24 Cutting and Pasting/Deleting text Key stroke Purpose DDelete to the end of the line from the current cursor position. PPaste the specified buffer before the current cursor position or line. If no buffer is specified (with the " command.) then 'P' uses the general buffer. XDelete the character before the cursor. YYank the current line into the specified buffer. If no buffer is specified, then the general buffer is used. dDelete until where. "dd" deletes the current line. A count deletes that many lines. Whatever is deleted is placed into the buffer specified with the " command. If no buffer is specified, then the general buffer is used. pPaste the specified buffer after the current cursor position or line. If no buffer is specified (with the " command.) then 'p' uses the general buffer. xDelete character under the cursor. A count tells how many characters to delete. The characters will be deleted after the cursor. yYank until, putting the result into a buffer. "yy" yanks the current line. a count yanks that many lines. The buffer can be specified with the " command. If no buffer is specified, then the general buffer is used. vi

25 vi Moving the Cursor Within the File $Move the cursor to the end of the current line. A count moves to the end of the following lines. %Move the cursor to the matching parenthesis or brace. ^Move the cursor to the first non-whitespace character. (Move the cursor to the beginning of a sentence. )Move the cursor to the beginning of the next sentence. {Move the cursor to the preceding paragraph. }Move the cursor to the next paragraph.

26 Replacing Text Key stroke Purpose CChange to the end of the line from the current cursor position. RReplace characters on the screen with a set of characters entered, ending with the Escape key. SChange an entire line. rReplace one character under the cursor. Specify a count to replace a number of characters. sSubstitute one character under the cursor, and go into insert mode. Specify a count to substitute a number of characters. A dollar sign ($) will be put at the last character to be substituted. vi

27 Searching for Text or Characters Key stroke Purpose,Repeat the last f, F, t or T command in the reverse direction. /Search the file downwards for the string specified after the /. ;Repeat the last f, F, t or T command. ?Search the file upwards for the string specified after the ?. FSearch the current line backwards for the character specified after the 'F' command. If found, move the cursor to the position. NRepeat the last search given by '/' or '?', except in the reverse direction. TSearch the current line backwards for the character specified after the 'T' command, and move to the column after if it's found. fSearch the current line for the character specified after the 'f' command. If found, move the cursor to the position. nRepeat last search given by '/' or '?'. tSearch the current line for the character specified after the 't' command, and move to the column before the character if it's found. vi

28 Manipulating Character/Line Formatting Key stroke Purpose ~Switch the case of the character under the cursor. <Shift the lines up to where to the left by one shiftwidth. "<<" shifts the current line to the left, and can be specified with a count. >Shift the lines up to where to the right by one shiftwidth. ">>" shifts the current line to the right, and can be specified with a count. JJoin the current line with the next one. A count joins that many lines. vi