FILE MAINTENANCE COMMANDS

Similar documents
Command Line Interface The basics

Unix File System. Learning command-line navigation of the file system is essential for efficient system usage

Week Overview. Unix file system File types and file naming Basic file system commands: pwd,cd,ls,mkdir,rmdir,mv,cp,rm man pages

Refresher workshop in programming for polytechnic graduates General Java Program Compilation Guide

Physics REU Unix Tutorial

Editors in Unix come in two general flavours:

Introduction. File System. Note. Achtung!

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Helpful Tips for Labs. CS140, Spring 2015

Introduction to Unix - Lab Exercise 0

Getting Started With UNIX Lab Exercises

Basic vi Commands. What is vi? To Get Into and Out Of vi

Emacs Tutorial. Creating or Opening a File. Geog 405/605 Computer Programming for Environmental Research Fall 2018

vi filename edit filename starting at line 1

Mills HPC Tutorial Series. Linux Basics I

CHE3935. Lecture 1. Introduction to Linux

Tutorial 1: Unix Basics

Using the Zoo Workstations

vi Primer Adapted from:

Introduction to the UNIX command line

Using the Unix system. UNIX Introduction

VIM Tips and Tricks. Search and replace a string in a document : :.,$s/search_string/replacement_string/g

Essential Linux Shell Commands

Bok, Jong Soon

Short Read Sequencing Analysis Workshop

FREEENGINEER.ORG. 1 of 6 11/5/15 8:31 PM. Learn UNIX in 10 minutes. Version 1.3. Preface

h/w m/c Kernel shell Application s/w user

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

Introduction. SSH Secure Shell Client 1

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering

AMS 200: Working on Linux/Unix Machines

Carnegie Mellon. Linux Boot Camp. Jack, Matthew, Nishad, Stanley 6 Sep 2016

Getting Started. Running Utilities. Shells. Special Characters. Special Characters. Chapter 2 Unix Utilities for non-programmers

Statistics 202A - vi Tutorial

Linux Shell Script. J. K. Mandal

Lab 3a Using the vi editor

CHEM5302 Fall 2015: Introduction to Maestro and the command line

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Lab Working with Linux Command Line

BEG 6. 50p. Getting Started with the Emacs Screen Editor. An introduction to the Emacs screen editor, which is available on Unix systems.

Files and Directories

Getting Started with UNIX

CSE115 Lab exercises for week 1 of recitations Spring 2011

Practical Session 0 Introduction to Linux

Introduction to Linux

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Introduction to Unix and Linux. Workshop 1: Directories and Files

Linux/Cygwin Practice Computer Architecture

CSCI 2132 Software Development. Lecture 4: Files and Directories

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

Exploring UNIX: Session 3

Lab 1 Introduction to UNIX and C

CS CS Tutorial 2 2 Winter 2018

Introduction to EMACS. Harriet Borton Academic and Research Computing

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

Outline. Structure of a UNIX command

Std: XI CHAPTER-3 LINUX

Introduction to Linux (Part II) BUPT/QMUL 2018/03/21

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

You should see something like this, called the prompt :

Tiny Instruction Manual for the Undergraduate Mathematics Unix Laboratory

Intro. To Unix commands. What are the machines? Very basics

When you first log in, you will be placed in your home directory. To see what this directory is named, type:

Chapter 1 An Introduction to C++, Unix, SSH and Komodo Edit

Introduction: What is Unix?

CS101 Linux Shell Handout

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

Introduction to Linux Environment. Yun-Wen Chen

STA 303 / 1002 Using SAS on CQUEST

Connecting to ICS Server, Shell, Vim CS238P Operating Systems fall 18

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Helsinki 19 Jan Practical course in genome bioinformatics DAY 0

VIP Quick Reference Card

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Text Editors for Programmers. Dr. Prasad Kulkarni Michael Jantz Jamie Robinson

Getting started with Hugs on Linux

Getting started with Hugs on Linux

Table Of Contents. 1. Zoo Information a. Logging in b. Transferring files 2. Unix Basics 3. Homework Commands

Linux Bootcamp Fall 2015

RH033 Red Hat Linux Essentials

Introduction to Linux for BlueBEAR. January

Basic File Attributes

genome[phd14]:/home/people/phd14/alignment >

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Chapter-3. Introduction to Unix: Fundamental Commands

Introduction to Linux Workshop 1

CSE 391 Lecture 3. bash shell continued: processes; multi-user systems; remote login; editors

CENG393 Computer Networks Labwork 1

Introduction of Linux

Session 1: Accessing MUGrid and Command Line Basics

7.1 INTRODUCTION 7.2 STARTING Emacs 7.3 Emacs SCREEN Menu Bar Text Mode Menu Mode Line Echo Line

A Brief Introduction to Unix

Using CRISP. I. CRISP Basics To create a new file, at the UNIX prompt type: cr newfilename

The Unix Shell & Shell Scripts

15-122: Principles of Imperative Computation

Development Environment. ICLAB NCTU Institute of Electronics

Transcription:

Birla Institute of Technology & Science, Pilani Computer Programming (CS F111) Lab-2 ---------------------------------------------------------------------------------------------------------------------- Objectives: 1. File maintenance Command 2. Creating files using vi editor 3. File Editing in vi ---------------------------------------------------------------------------------------------------------------------- INTRODUCTION In Lab1 we have learned few basic commands like cd (change directory), ls (list), mkdir (make directory), and we have learned how to navigate through the directory structure. We have also seen what all files are present in the directory. In this lab session we will learn how to create files, display its contents, file maintenance commands, and file permissions. Apart from this we will learn some more UNIX commands. In UNIX there are many text editors like vim, nano, emacs, etc. We shall be using the VI editor for all the labs. FILE MAINTENANCE COMMANDS 1. Copy a file using cp command a. The cp command copies the content of one file into another file. b. Create a new file with a name first.txt If there is no file by the name first.txt, you can temporarily create one using [user@localhost: Lab2]$ echo the first file > first.txt c. Do the following and copy the content of first.txt into firstcopy.txt [user@localhost: Lab2]$ cp first.txt firstcopy.txt d. Now see the contents of the file first.txt and firstcopy.txt (Hint: use the cat command), you can observe that the contents of the two file are same. e. You now have two copies of the file, each with identical contents. They are completely independent of each other and you can edit and modify either as needed. f. To copy entire directory, you should use the -r option of cp command. i. Come back to home directory. ii. To copy Lab2 directory to Lab2copy directory [user@localhost: ~]$ cp -rv Lab2 Lab2copy (The option v gives verbose output.) 2. Moving a file with the mv command a. The mv command is used to move files between the directories. b. So you are in directory Lab2, create a new directory inside Lab2 with a name mvtest (use mkdir command). c. Let us move firstcopy.txt into mvtest directory [user@localhost: Lab2]$ mv firstcopy.txt mvtest (If the directory mvtest does not exist, then a file named mvtest will be created.) d. Now go the directory mvtest (use cd command) and see that firstcopy.txt file is present in the directory. e. Now come back into the directory Lab2, list the contents of the directory you will see that firstcopy.txt is not there. f. Now try the following command [user@localhost: Lab2]$ cp first.txt mvtest Page 1 of 8

g. Now list the contents of both Lab2 and mvtest directory using the ls command and you will be able to see that first.txt is present in both the directories. h. What you can observe is that mv command moves the file into other directory, while cp command will create a copy of file and then put that copy into the other directory. 3. Rename a file with mv command a. UNIX does not have a command specifically for renaming files. Instead, the mv command is used both to change the name of a file and to move a file into a different directory. b. Let us change the name of file first.txt to myfirstfile.txt, do the following: [user@localhost: Lab2]$ mv first.txt myfirstfile.txt (Please give a space between mv, first.txt, and myfirstfile.txt) c. List the content of the directory Lab2 as a result of executing the above command you will find that there is no longer a file called first.txt, but a new file called myfirstfile.txt contains what was previously in first.txt. You can see that by displaying the contents of the file using cat command. 4. Removing a File with rm command a. Use the rm command to remove a file. b. Go to the directory mvtest and type in the following command: [user@localhost:~/ Lab2]$ rm myfirstfile.txt c. The file will be deleted. If, rather you want a prompt for confirmation of whether you really want to remove the file, use rm -i rm: remove firstfile (y/n)? y Type y or yes to remove a file; type n or no to leave it. d. List the contents of the directory and you will not find myfirstfile.txt file. e. To remove all files with extension.txt: [user@localhost:~/ Lab2]$ rm *.txt f. A directory can be deleted using the command rm r 5. Removing a directory with rm / rmdir command a. Create a temperory directory called temp b. Goto temp directory c. Create two directories temp1 and temp2 d. To remove directory temp2 [user@localhost: temp]$ rmdir temp2 Check the result by typing ls before and after the rmdir command. e. Come back to temp directory f. To remove directory temp and all its contents [user@localhost: temp]$ rm -rvf temp ii. In the option set, r indicates recursive, v indicates verbose, and f indicates forced deletion. CREATING AND WRITING INTO FILE USING VI EDITOR 1. Create a directory with a name Lab2 and go to that directory (Hint: use mkdir and cd command). 2. You should be in the following working directory [user@localhost: Lab2]$ 3. You can verify that the current working directory is initially empty (Hint: use ls command)

Page 2 of 8

4. Let us create a simple text file using VI editor. To create a file you should specify a file name that uniquely identifies that file in the directory. Two files cannot have same filename if they are in the same directory. 5. Let us assume first.txt to be the name of the file that you want to create. 6. To open the VI editor and create the file first.txt follow the following commands [user@localhost: Lab2]$ vi first.txt 7. Refer to Figure-1 that s what you will see when VI editor opens. 8. You are now ready to create/edit the file named first.txt. 9. Ok, now you just press the ESC button on your keyboard and then while holding the SHIFT button down press the colon key (:) release shift button and type wq on keyboard and press ENTER key. The VI editor will exit and you will again come to the command prompt. 10. Verify the contents of your directory again (use ls command) 11. You will see that a file with a name first.txt is created and is present in your working directory. 12. Surprised!!! What happened when you pressed ESC button and while holding SHIFT button typed colon key (:) and then wq? Doing this you instructed the VI editor to write and quit (wq stands for write and quit). Seeing this instruction the VI editor creates a file with the name that you specify and quits after saving that file in your directory. 13. Try to create one more file using vi editor let the name be second.txt, but this time while exiting the VI editor just press ESC and while holding the SHIFT button press the colon key (:) and then type q. Check the contents of your directory again, you will not find second.txt. 14. Can you tell why the second.txt file is not created??? Because you instructed the VI editor to quit (q stands for quit) without writing/saving the file second.txt. 15. Congratulations!!!, you know how to create a file using VI editor. Figure-1: VI editor opens given the file name as first.txt Page 3 of 8

16. Now Let us write something inside the file you have just created (first.txt). 17. Open the file using vi editor [user@localhost: Lab2]$ vi first.txt a. Do you observe the cursor blinking on the upper left hand corner of the screen? The editor is waiting for the instruction from the user to do one of the many things that it can do (you will learn them gradually). b. Press i on your keyboard, and observe the bottom of the screen as shown in Figure- 2. It reads --INSERT -- c. Type the following: i. My journey begins from here. ii. It s going to be an enjoyment Figure-2: Press i to insert text 18. Again press ESC and while holding the SHIFT button press the colon key (:) and then type wq to write the contents to the file first.txt and quit the VI editor. DISPLAY THE CONTENTS OF A TEXT FILE Now if you want to display the contents of the file use the cat command, see below: [user@localhost: Lab2]$ cat first.txt Basic Operating Modes in vi We mentioned while in the process of creating file, that we are switching between two operating modes. The vi Editor at any instant can be found to be in one of the following 3 modes. Command Mode Command Line Mode Insert mode The navigation among the modes is best illustrated by the following figure. Page 4 of 8

Insert Mode ESC : a,a, i,i, o,o Command Mode ESC Command Line Mode ( ex command mode) Default mode when you open the editor for editing a file Fig. 3 Modes in vi The technical details of each of the modes are as follows: Command Mode: Once you finished entering or changing the text, you can move to command mode by pressing ESC key. Unlike in Insertion mode, when a key is pressed in the command mode, it doesn t show up on the screen but it performs some function based on the key pressed. Refer to the Table-1 for the basic cursor movement commands and Table-2 for commands for editing a text. Table: 1 Basic Cursor Movement commands h or left arrow key l or right arrow key j or down arrow key k or up arrow key ^ Move cursor to the left one character. Move cursor to the right one character. Move cursor down one line. Move cursor up one line. Move cursor to the beginning of the line. $ Move cursor to the end of the current line. G w b Table: 2 Commands for Editing text Move cursor to the last line of your file. Move to first character of next word Move to first character of previous word i Inserts text to the left of the cursor. I Inserts text at the beginning of the line, no matter where the cursor is positioned on the current line. a Begins inserting after the character (append) on which the cursor is positioned. A Begins inserting at the end of the current line, no matter where the cursor is positioned on that line. o O Begins inserting text on a new line below the current line, no matter where the cursor is positioned on that line. Begins inserting text on a new line above the current line. Ins Toggle between insert mode and replace mode. If not currently in Insert Mode, works key as same as pressing i Insert Mode: vi editor opens in command mode. To perform the said activities on the file, we should change the current working mode to insert mode. Insert mode allows editing the contents of the current file, making a new file and other manipulations with the file content. This can be done by pressing the Page 5 of 8

keys i, a, o, O at command mode (Table-2). Pressing i on the keyboard at command mode allows to insert character in the page on the left side of the cursor, press a to insert characters on the right side of the cursor. To open a new line under the cursor, type o in command mode and pressing O (Uppercase letter) allows opening a new line above the cursor. Once we are changed to insert mode, any character of the keyboard can be typed and written on the screen. Task: Practice to use i, a, o, O with the file you have created i.e. will. While working with it, try to keep track of the current mode you are in with the help of Fig. 2. Command Line Mode: The bottom line of the vi editor is called command line. This mode allows giving command at the command line. (This mode is also called as ex command mode, as the commands issued at the command line are compatible with ex editor.) All commands issued in command line mode are displayed in the command line. Let us introduce a minimal set of commands issued at command line mode here. Command Purpose :w To save your file but not quit vi :w <filename> To save the contents in the screen (buffer) into the file named filename. :q To quit if you haven't made any edits :q! To quit with out saving the changes made in the current edit :wq To both quit and save your edits. Exercise-1 Fig. 4 Directory Structure Refer the directory structure shown in Fig-4. Accomplish the following tasks, in sequence. 1. Create the directory structure as in Fig-4 in your home directory 2. Rename each file in Personal with the prefix P. That is, the file accounts in Personal directory should be renamed as Paccounts. 3. Allow others from modifying the file Pairticket in Personal and Official directories by setting appropriate file permissions. 4. The address file in the Official directory contains some confidential addresses. To protect it, a. Remove all permissions to this file by anyone other than the owner. b. Move the file named address to a newly created directory in Myhome (you may create your own directory under Myhome) from its current location. 5. You may also rename the above mentioned file as medicalreport, Page 6 of 8

6. Create a directory Backup under Myhome directory and move all the contents of Personal into Backup. 7. Remove all the contents of Personal directory. You should do it from the Official directory. 8. Now rename the Backup directory as Personal. File editing Exercise-2: Create a file using vi. Add few lines of text and tryout the following vi commands. Copying and Pasting Text The commands for copy/paste are issued in command mode. Make sure that you are in command line mode. If not, first switch to command mode. Command Explanation yy Copy (yank) the current line 6yy Copy Six lines, beginning with the current line p Put the copied text in next line P Put the copied text above the current line Once you issue the copy command (yy, 2yy, 4yy etc), the copied text goes into a temporary memory area (buffer) that is replaced each time when you copy (or delete) more text. Only the current contents of the temporary buffer can be put back into your file. As a result, when you use copy (yy), use the put (p) command immediately. Showing Line number: Command :set number :se nu :set nonumber :se nonum :set number! :se nu! Explanation Shows line numbers Shows line numbers // short cut for set number Hides line numbers Hides line numbers // short cut Toggle between showing and hiding line numbers Toggle between showing and hiding line numbers Searching for a Text Search for a string or a character is possible with vi in command mode. For a string search, the / and? commands are used. When you start these commands, the command just typed will be shown on the bottom line as shown in Fig.1 where you type the particular string to look for. These two commands differ only in the direction where the search takes place. The / command searches forwards (downwards) in the file, while the? command searches backwards (upwards) in the file. List of commands are as follows: Command Explanation /text Search forward (down) for text?text Search backward (up) for text fchar Search forward for a character on current line (e.g. fb) Fchar Search backward for a character on current line Page 7 of 8

Fig. 5 Searching text editor in forward direction Substituting the Text If you want to do substitutions over a range of lines, or throughout the file, the s command is used as follows: :n1,n2s/old/new/gc n1 is the beginning line number n2 is the ending line number s means to substitute text matching the pattern (old) with text specified by (new) g stands for global (optional). Indicates you want to substitute all occurrences on the indicated lines. If you don t use g, the editor substitutes only the first occurrence on the indicated lines. c stands for confirm (optional). It indicates you want to confirm each substitution before vi completes it. Examples: :%s/old/new/g :.,$s/old/new/g Substitutes old with new throughout the line Substitutes old with new from the current cursor position to the end of the line How to recover your work if something goes wrong!!! The vi editor edits a temporary copy of your file, and after the editing is complete, or when you tell it to save, it puts the contents of the temporary copy into the original file. If something goes wrong while you are editing your file, the vi editor will attempt to save whatever work you had in progress, and store it for later recovery. If you were editing the file, and you accidentally got logged out, then the -r option of the vi editor helps. Use the following command to open the file: vi -r first It will show you the temporary file options for recovery. The -r option stands for recovery. After using the -r option once, you MUST save what you have recovered to the actual file. Page 8 of 8