Linux Shell Commands and Vi Editor

Total Page:16

File Type:pdf, Size:1020Kb

Linux Shell Commands and Vi Editor __________________________________________________________________________ THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering ENG224 Information Technology Laboratory 1 Linux Shell Commands and vi Editor Objective: To practice the usage of Linux shell commands and vi editor Equipment: PC with Ubuntu Linux 5 preloaded. vi editor. Methodology: Start up the system. Login the system with username student. Consult your tutor for the password required. Answer all the questions in this lab sheet. 1. Introduction What is a shell? The shell is the command line interface to Linux. It is similar to the Microsoft DOS prompt as known to Windows users, but is much more powerful with its scripting languages. Currently the standard setup of Linux has included two shells, bash and tcsh. What is a shell command? Users coming from the DOS environment are probably familiar with the concept of commands that encompass core features of the operating system, such as DIR, COPY, and ATTRIB. These commands provide the base on which more complicated actions could be built and from which sophisticated batch files could be written. But in the DOS world, as in many operating systems, the number of available commands is limited and generally static: users cannot add new commands. In Linux, and also in Unix, the concept is different. A command is any executable file. This means that any executable file added to a system becomes a new command on the system. How to use Linux Shell inside the X-Window system? A terminal emulator is a program that opens up a window and then runs a shell in that window. It could be compared to the DOS prompt in Windows. You start a terminal emulator like any other X program, so it means that you can have a command line while you are still safely in the GUI! There are a bunch of different terminal emulators out there, like xterm, rxvt, gnome-terminal, konsole, kvt, eterm, and many others. We use gnome-terminal to practice the exercise in this tutorial. Open a console window by pressing the upper-left corner button "Applications", "System Tools" and then "Terminal" (Not Root Terminal). You may also open a new terminal by a single right click on the desktop, then choose Open terminal from the pop up menu. A terminal window similar to the one on Figure 1 will be shown. Page 1 __________________________________________________________________________ Figure 1 The Terminal in Ubuntu Linux 2. Common Linux Commands a. Find the usage of commands If you don’t know how to use the Linux shell commands, you can use man (which stands for MANual) command to show you the usage of them. Usage: man <command-name> Example: man ls (press q when you finish reading the manual of the command ls) b. Finding out where you are The pwd command (which stands for Present Working Directory) is the one of the most basic commands in Linux. By typing the command and hitting Enter, you will be informed of which directory you are currently in. Usage: pwd Exercise 2.1 Type “pwd” in the command line. What is the result? __________________________________________________________________________________ Page 2 __________________________________________________________________________ c. Change directory The cd command allows you to change your current directory to any accessible directory on the system. Usage: cd <directory> Example: cd /usr/X11R6/bin Note: using the command cd without applying any parameter will bring you back to your home directory. d. Listing the contents of a directory The ls command can be used to view the contents of the current directory. Usage: ls [option] <filename> Example: ls –al / Exercise 2.2 Type “cd /” and “ls” in the command line. What is the result? __________________________________________________________________________________ Exercise 2.3 Type these commands “cd /usr/X11R6/”, “cd ./bin/”, “cd ..”, and “pwd”. Give the current directory which you are located. What are the meanings of . and .. ? __________________________________________________________________________________ e. Find, Grep and Cat The find and grep commands are powerful tools for searching files. While both commands are used for searching, their purposes differ: find is used to search for files by a number of criteria, including name or date of creation. grep is used to search the contents of files. The cat command combines one or more files and displays them on the standard output. If no files are given, the standard input is written to the standard output. Usage of find command: find <starting-directory> <parameters> <actions> Usage of grep command: grep <text-pattern> <file-list> Usage of cat command: cat <options> <filename> Exercise 2.4 Type "cd" to go back to your home directory. Type “cat > testcat.txt” in the command line. After pressing return, type the following line of text “This is a test of cat.”, and then press crtl-d. Type “cat testcat.txt” again. What do you see? __________________________________________________________________________________ For the command “cat > testcat.txt” above, the symbol > allows the output of the command cat (the keyboard input of user) to store into a file namely testcat.txt. Page 3 __________________________________________________________________________ Exercise 2.5 Type “find . -name testcat.txt -print” in the command line. What is the result? What are the purposes of the options -name and -print (use man find to find out their meanings)? __________________________________________________________________________________ Exercise 2.6 Type “find . -name testcat.txt –print > list.lst” in the command line. You will find a file “list.lst” in your current directory. Use cat commands to show its contents. What is the result? What is the meaning of > list.lst? __________________________________________________________________________________ Exercise 2.7 Go to “/” directory by the command "cd /" and type “ ls –l | grep “bin” ” in the command line. What is the result? What is the function achieved by this line of commands? (The character | refers to pipe. It allows the output from the left hand side of | to become the input of the command on its right hand side.) __________________________________________________________________________________ 3. File Management Linux Commands a. Copying files and directories To copy files, you can use the mv or cp commands. The command line “mv file1 file2” allows one to move file1 to file2. After the move, file1 will no longer exist. So functionally it is similar to renaming a file in DOS. Command cp on the other hand copies one file to another. The following will copy file1 to file2. Note that if file2 does not exist, it will be created; however if it does exist, it will be overwritten: Usage: cp file1 file2 There is NO undo command in Linux, so accidentally overwriting an important file would probably make you pull your hair out. The risk of doing so is smaller if you use the -i option ("interactive") with cp. The following does the same as the above, but if file2 exists, you will be prompted before overwriting: Usage: cp –i file1 file2 If you want to copy file1 into directory dir1: Usage: cp file1 dir1/ The following would do the same as the above, copy file1 into dir1, but under a different name: Usage: cp file1 dir1/file2 You can also copy multiple files into one directory with a single command: Usage: cp file1 file2 file3 dir1 For copying directories, you can use the cp and mv commands just like you use them with files. If you have already tried to copy a directory with cp, you should have probably noticed that cp just complains to you. The cp command wants you to use the -r option if you want to copy a directory with its contents. The -r means "copy recursively": Usage: cp -r dir1 dir2 Page 4 __________________________________________________________________________ Exercise 3.1 Change to your home directory by using the "cd" command. Create a subdirectory called “studentsub” using the command “mkdir studentsub”. Copy the files testcat.txt and list.lst to studentsub. While staying at your home directory, copy ./studentsub to another directory called ./studentsub2? Give the command that you have used. __________________________________________________________________________________ __________________________________________________________________________________ __________________________________________________________________________________ __________________________________________________________________________________ Exercise 3.2 Rename the “studentsub2” directory to “studentsub3” using mv. Give the command that you have used. __________________________________________________________________________________ b. Deleting files and directories The rm command is used for removing files. To remove a file: Usage: rm file1 If you use the -i option, you'll be prompted before removing the file: Usage: rm -i file1 You can also delete more files at once: Usage: rm file1 file2 There are two commands you can use for removing directories. If the directory is empty, you can use rmdir: Usage: rmdir dir1 You can use rmdir only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. The -r option tells rm to remove a directory Recursively: Usage: rm -r dir1 It can cause a lot of trouble with rm -r if you are not careful. Therefore, it might be a good practice to use the -i option when deleting a directory with its contents so that you would be prompted before each file in the directory
Recommended publications
  • Tortoisemerge a Diff/Merge Tool for Windows Version 1.11
    TortoiseMerge A diff/merge tool for Windows Version 1.11 Stefan Küng Lübbe Onken Simon Large TortoiseMerge: A diff/merge tool for Windows: Version 1.11 by Stefan Küng, Lübbe Onken, and Simon Large Publication date 2018/09/22 18:28:22 (r28377) Table of Contents Preface ........................................................................................................................................ vi 1. TortoiseMerge is free! ....................................................................................................... vi 2. Acknowledgments ............................................................................................................. vi 1. Introduction .............................................................................................................................. 1 1.1. Overview ....................................................................................................................... 1 1.2. TortoiseMerge's History .................................................................................................... 1 2. Basic Concepts .......................................................................................................................... 3 2.1. Viewing and Merging Differences ...................................................................................... 3 2.2. Editing Conflicts ............................................................................................................. 3 2.3. Applying Patches ...........................................................................................................
    [Show full text]
  • A.5.1. Linux Programming and the GNU Toolchain
    Making the Transition to Linux A Guide to the Linux Command Line Interface for Students Joshua Glatt Making the Transition to Linux: A Guide to the Linux Command Line Interface for Students Joshua Glatt Copyright © 2008 Joshua Glatt Revision History Revision 1.31 14 Sept 2008 jg Various small but useful changes, preparing to revise section on vi Revision 1.30 10 Sept 2008 jg Revised further reading and suggestions, other revisions Revision 1.20 27 Aug 2008 jg Revised first chapter, other revisions Revision 1.10 20 Aug 2008 jg First major revision Revision 1.00 11 Aug 2008 jg First official release (w00t) Revision 0.95 06 Aug 2008 jg Second beta release Revision 0.90 01 Aug 2008 jg First beta release License This document is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License [http:// creativecommons.org/licenses/by-nc-sa/3.0/us/]. Legal Notice This document is distributed in the hope that it will be useful, but it is provided “as is” without express or implied warranty of any kind; without even the implied warranties of merchantability or fitness for a particular purpose. Although the author makes every effort to make this document as complete and as accurate as possible, the author assumes no responsibility for errors or omissions, nor does the author assume any liability whatsoever for incidental or consequential damages in connection with or arising out of the use of the information contained in this document. The author provides links to external websites for informational purposes only and is not responsible for the content of those websites.
    [Show full text]
  • Text Editing in UNIX: an Introduction to Vi and Editing
    Text Editing in UNIX A short introduction to vi, pico, and gedit Copyright 20062009 Stewart Weiss About UNIX editors There are two types of text editors in UNIX: those that run in terminal windows, called text mode editors, and those that are graphical, with menus and mouse pointers. The latter require a windowing system, usually X Windows, to run. If you are remotely logged into UNIX, say through SSH, then you should use a text mode editor. It is possible to use a graphical editor, but it will be much slower to use. I will explain more about that later. 2 CSci 132 Practical UNIX with Perl Text mode editors The three text mode editors of choice in UNIX are vi, emacs, and pico (really nano, to be explained later.) vi is the original editor; it is very fast, easy to use, and available on virtually every UNIX system. The vi commands are the same as those of the sed filter as well as several other common UNIX tools. emacs is a very powerful editor, but it takes more effort to learn how to use it. pico is the easiest editor to learn, and the least powerful. pico was part of the Pine email client; nano is a clone of pico. 3 CSci 132 Practical UNIX with Perl What these slides contain These slides concentrate on vi because it is very fast and always available. Although the set of commands is very cryptic, by learning a small subset of the commands, you can edit text very quickly. What follows is an outline of the basic concepts that define vi.
    [Show full text]
  • BSD UNIX Toolbox 1000+ Commands for Freebsd, Openbsd
    76034ffirs.qxd:Toolbox 4/2/08 12:50 PM Page iii BSD UNIX® TOOLBOX 1000+ Commands for FreeBSD®, OpenBSD, and NetBSD®Power Users Christopher Negus François Caen 76034ffirs.qxd:Toolbox 4/2/08 12:50 PM Page ii 76034ffirs.qxd:Toolbox 4/2/08 12:50 PM Page i BSD UNIX® TOOLBOX 76034ffirs.qxd:Toolbox 4/2/08 12:50 PM Page ii 76034ffirs.qxd:Toolbox 4/2/08 12:50 PM Page iii BSD UNIX® TOOLBOX 1000+ Commands for FreeBSD®, OpenBSD, and NetBSD®Power Users Christopher Negus François Caen 76034ffirs.qxd:Toolbox 4/2/08 12:50 PM Page iv BSD UNIX® Toolbox: 1000+ Commands for FreeBSD®, OpenBSD, and NetBSD® Power Users Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2008 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-37603-4 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Library of Congress Cataloging-in-Publication Data is available from the publisher. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permis- sion should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions.
    [Show full text]
  • Learning the Vi Editor
    Learning the vi Editor en.wikibooks.org December 29, 2013 On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. A URI to this license is given in the list of figures on page 103. If this document is a derived work from the contents of one of these projects and the content was still licensed by the project under this license at the time of derivation this document has to be licensed under the same, a similar or a compatible license, as stated in section 4b of the license. The list of contributors is included in chapter Contributors on page 101. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 107, since this book and/or parts of it may or may not be licensed under one or more of these licenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list of figures on page 103. This PDF was generated by the LATEX typesetting software. The LATEX source code is included as an attachment (source.7z.txt) in this PDF file. To extract the source from the PDF file, you can use the pdfdetach tool including in the poppler suite, or the http://www. pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you save the attachment to a file. After extracting it from the PDF file you have to rename it to source.7z.
    [Show full text]
  • Title VI Fact Sheet
    Title VI Fact Sheet Cleveland State Community College – P O Box 3570 – Cleveland, TN 37320-3570 RIGHTS UNDER TITLE VI OF THE CIVIL RIGHTS ACT OF 1964 The Civil Rights Act of 1964 was passed to ensure the people of the U.S. equal treatment, rights and opportunities regardless of race, color or national origin. Title VI of that Act prohibits discrimination in federally funded programs. It is important that all applicants and recipients of services know about their rights under the law. WHAT IS TITLE VI? “No person in the United States shall, on the ground of race, color or national origin, be excluded from participation in, be denied the benefits of, or be subjected to discrimination under any program or activity receiving federal financial assistance.” WHAT IS COVERED BY TITLE VI? • Veterans Administration educational benefits • equipment and building loans • employee or student recruitment • health care (i.e. Medicare, Medicaid, Tenncare) • social services • parks and recreation • construction • housing • transportation • contracting • the distribution of benefits and services tax benefits • the location of facilities enjoyed by private agencies, fraternal and non-profit • community development block grants organizations (i.e. 501 c3), as well as education • welfare services institutions • law enforcement • hiring • program effects on people in applicable communities List is not all-inclusive WHAT IS FEDERAL FINANCIAL ASSISTANCE? • Federal financial assistance means more than just money. Aid that enhances the ability to improve or expand allocation of a recipient’s own resources also constitutes assistance. Examples: o Student aid (releases recipient’s funds for other uses) or o Training of employees (permits better use of the employer) o Grants and Loans o Property o Loan or Personnel o Tax incentives o Technical assistance, etc.
    [Show full text]
  • Service Prioritization Decision Assistance Tool (VI-SPDAT)
    Vulnerability Index - Service Prioritization Decision Assistance Tool (VI-SPDAT) Prescreen Triage Tool for Single Adults AMERICAN VERSION 2.0 ©2015 OrgCode Consulting Inc. and Community Solutions. All rights reserved. 1 (800) 355-0420 [email protected] www.orgcode.com VULNERABILITY INDEX - SERVICE PRIORITIZATION DECISION ASSISTANCE TOOL (VI-SPDAT) SINGLE ADULTS AMERICAN VERSION 2.0 Welcome to the SPDAT Line of Products The Service Prioritization Decision Assistance Tool (SPDAT) has been around in various incarnations for over a decade, before being released to the public in 2010. Since its initial release, the use of the SPDAT has been expanding exponentially and is now used in over one thousand communities across the United States, Canada, and Australia. More communities using the tool means there is an unprecedented demand for versions of the SPDAT, customized for specifc client groups or types of users. With the release of SPDAT V4, there have been more current versions of SPDAT products than ever before. VI-SPDAT Series The Vulnerability Index – Service Prioritization Decision Assistance Tool (VI-SPDAT) was developed as a pre-screening tool for communities that are very busy and do not have the resources to conduct a full SPDAT assessment for every client. It was made in collaboration with Community Solutions, creators of the Vulnerability Index, as a brief survey that can be conducted to quickly determine whether a client has high, moderate, or low acuity. The use of this survey can help prioritize which clients should be given a full SPDAT assessment frst. Because it is a self-reported survey, no special training is required to use the VI-SPDAT.
    [Show full text]
  • Command Line Interface (Shell)
    Command Line Interface (Shell) 1 Organization of a computer system users applications graphical user shell interface (GUI) operating system hardware (or software acting like hardware: “virtual machine”) 2 Organization of a computer system Easier to use; users applications Not so easy to program with, interactive actions automate (click, drag, tap, …) graphical user shell interface (GUI) system calls operating system hardware (or software acting like hardware: “virtual machine”) 3 Organization of a computer system Easier to program users applications with and automate; Not so convenient to use (maybe) typed commands graphical user shell interface (GUI) system calls operating system hardware (or software acting like hardware: “virtual machine”) 4 Organization of a computer system users applications this class graphical user shell interface (GUI) operating system hardware (or software acting like hardware: “virtual machine”) 5 What is a Command Line Interface? • Interface: Means it is a way to interact with the Operating System. 6 What is a Command Line Interface? • Interface: Means it is a way to interact with the Operating System. • Command Line: Means you interact with it through typing commands at the keyboard. 7 What is a Command Line Interface? • Interface: Means it is a way to interact with the Operating System. • Command Line: Means you interact with it through typing commands at the keyboard. So a Command Line Interface (or a shell) is a program that lets you interact with the Operating System via the keyboard. 8 Why Use a Command Line Interface? A. In the old days, there was no choice 9 Why Use a Command Line Interface? A.
    [Show full text]
  • An Introduction to Display Editing with Vi
    An Introduction to Display Editing with Vi William Joy Mark Horton Computer Science Division Department of Electrical Engineering and Computer Science University of California, Berkeley Berkeley, Ca. 94720 ABSTRACT Vi (visual) is a display oriented interactive text editor. When using vi the screen of your terminal acts as a window into the file which you are editing. Changes which you make to the file are reflected in what you see. Using vi you can insert new text any place in the file quite easily. Most of the commands to vi move the cursor around in the file. There are commands to move the cursor forward and backward in units of characters, words, sentences and paragraphs. A small set of operators, like d for delete and c for change, are combined with the motion commands to form operations such as delete word or change paragraph, in a simple and natural way. This regularity and the mnemonic assignment of commands to keys makes the editor command set easy to remember and to use. Vi will work on a large number of display terminals, and new terminals are easily driven after editing a terminal description file. While it is advantageous to have an intelligent terminal which can locally insert and delete lines and characters from the display, the editor will function quite well on dumb terminals over slow phone lines. The editor makes allowance for the low bandwidth in these situations and uses smaller window sizes and different display updating algo- rithms to make best use of the limited speed available. It is also possible to use the command set of vi on hardcopy terminals, storage tubes and ‘‘glass tty’s’’ using a one line editing window; thus vi’s command set is available on all termi- nals.
    [Show full text]
  • How Do You Use the Vi Text Editor?
    How do you use the vi text editor? 1.What is the Visual Text Editor (vi)?...................................................................2 2.Configuring your vi session..............................................................................2 2.1.Some of the options available in vi are:................................................3 2.2.Sample of the .exrc file........................................................................3 3.vi Quick Reference Guide.................................................................................4 3.1.Cursor Movement Commands.............................................................4 3.2.Inserting and Deleting Text.................................................................5 3.3.Change Commands.............................................................................6 3.4.File Manipulation...............................................................................7 Institute of Microelectronics, Singapore FAQ for vi text editor E1 1. What is the Visual Text Editor (vi)? The Visual Test Editor, more commonly known as the vi text editor, is a standard “visual” (full screen) editor available for test editing in UNIX. It is a combination of the capabilities of the UNIX ed and ex text editors. It has specific modes for text insertion and deletion as well as command entering. Type view on the command line of the UNIX shell terminal to enter the read- only mode of vi. To switch between the line editor, ex, mode and the full screen wrapmargin vi mode: • To enter the ex mode from the vi mode, type Q • To enter the vi mode from the ex mode, type vi When vi is entered on the command line, you enter vi’s command (entering) mode. Positioning and editing commands can be entered to perform functions in this mode whilst advanced editing commands can be entered following a colon (:) that places you at the bottom of the file. To enter text insertion mode, a vi text insertion command i, o, or a is typed so that text will no longer be interpreted as positioning or editing commands in this mode.
    [Show full text]
  • Remote Command-Line Interface Installation and Reference Guide
    Remote Command-Line Interface Installation and Reference Guide Update 2 and later for ESX Server 3.5, ESX Server 3i version 3.5, VirtualCenter 2.5 Remote Command-Line Interface Installation and Reference Guide Remote Command-Line Interface Installation and Reference Guide Revision: 20090313 Item: EN-000082-01 You can find the most up-to-date technical documentation on our Web site at: http://www.vmware.com/support/ The VMware Web site also provides the latest product updates. If you have comments about this documentation, submit your feedback to: [email protected] © 2007–2009 VMware, Inc. All rights reserved. This product is protected by U.S. and international copyright and intellectual property laws. VMware products are covered by one or more patents listed at http://www.vmware.com/go/patents. VMware, the VMware “boxes” logo and design, Virtual SMP and VMotion are registered trademarks or trademarks of VMware, Inc. in the United States and/or other jurisdictions. All other marks and names mentioned herein may be trademarks of their respective companies. VMware, Inc. 3401 Hillview Ave. Palo Alto, CA 94304 www.vmware.com 2 VMware, Inc. Contents About This Book 5 Product Naming Conventions 5 1 Remote CLI Installation and Execution 9 List of Available Commands 10 Using the VMware Remote CLI 12 Installing and Executing Remote CLI Commands on Linux 13 Unpacking and Installing the Remote CLI Package 13 Executing Remote CLI Commands 14 Uninstalling the Remote CLI Package 15 Installing and Executing Remote CLI Commands on Windows 15 Installing
    [Show full text]
  • Gnu Coreutils Core GNU Utilities for Version 6.9, 22 March 2007
    gnu Coreutils Core GNU utilities for version 6.9, 22 March 2007 David MacKenzie et al. This manual documents version 6.9 of the gnu core utilities, including the standard pro- grams for text and file manipulation. Copyright c 1994, 1995, 1996, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled \GNU Free Documentation License". Chapter 1: Introduction 1 1 Introduction This manual is a work in progress: many sections make no attempt to explain basic concepts in a way suitable for novices. Thus, if you are interested, please get involved in improving this manual. The entire gnu community will benefit. The gnu utilities documented here are mostly compatible with the POSIX standard. Please report bugs to [email protected]. Remember to include the version number, machine architecture, input files, and any other information needed to reproduce the bug: your input, what you expected, what you got, and why it is wrong. Diffs are welcome, but please include a description of the problem as well, since this is sometimes difficult to infer. See section \Bugs" in Using and Porting GNU CC. This manual was originally derived from the Unix man pages in the distributions, which were written by David MacKenzie and updated by Jim Meyering.
    [Show full text]