COMP1600 - Software Development Workshop I
Workshop 1.3: Editors and File Utilities
Learning Objectives
In this workshop, you will learn how to use some common editors and file utilities in Linux.
Editor
You use an editor to edit text files (e.g., input source programs, change the configuration files in /etc). You should be familiar with at least one editor in Linux. Some common editors include emacs, vi and pico. On X-windows, some common editors are xedit, gedit and kedit.
Basics of vi vi is one of the common editor in Linux/Unix. It has two modes: insert mode and command mode. To get into insert mode: A, a, I, i, O, o To get out of insert mode: To move around in command mode: h, l, j, k, Ctrl-U, Ctrl-D, Ctrl-F, Ctrl-B Editing in command mode: x : delete 1 character
3x : delete 3 characters X : backspace to delete the last character dd : delete 1 line
3dd : delete 3 lines d^ : delete from the beginning of line D : delete up to the end of line u : undo last command . : redo last command yy : yank 1 line (copy into buffer) 10yy : yank 10 lines p : paste the buffer below the current line P : paste the buffer above the current line J : joint two lines : : go into ex mode
/something/ : find something forward ?something? : find something backward
Text File Format in Linux/DOS For text files in DOS, appears at end of each line, where the control
1
COMP1600 - Software Development Workshop I characters and stand for "carriage return" and "line fit". For text files in Linux, appears at the end of each line. In FTP, when the transfer mode is ASCII, the FTP program will automatically change to ; when the transfer mode is binary, it will not do the conversion.
Common File Utilities grep : to find the content inside the file find : to find the name of a file od : octo-dump (may also do hex dump) cat : display the file content diff : find the difference patch : update a file with a diff file tr : translate a file (tr ‘[a-z]’ ‘[A-Z]’)
Other common file utilities include: head, tail, wc, sort. Use the command man to find out their purposes.
Workshop
1. Login as root and then change into the directory "/tmp".
2. Play around with following vi command and get a feel of how it works.
Command
Description k Move up j Move down h Move left l Move right
$
Move cursor to the end of line
Ctrl-B
Scroll up
Ctrl-F
Scroll down i Insert at the current position
I
Insert at the beginning of the line a Append after the current position
A
Append at the end of the line x Delete a character dw Delete a word dd Delete the current line
D
Delete all to the right of the line
/pattern
Searching for a pattern yy Copy the current line into a buffer
P
Paste contents of buffer
:w
Save the current file
:q!
Quit without saving the file
:wq
Save and then quit
:r filename
Read in a file
2
COMP1600 - Software Development Workshop I
Handling MS-DOS File
3. In this exercise, you cooperate with a classmate because you cannot access both the Linux and the Windows platforms. Use the notepad to create a MS-DOS file called "dosfile.txt". Type something in this file and then use a floppy disk to transfer this file to your Linux computer. You can type the command mcopy a:dosfile.txt to copy the file from the floppy disk.
4. Type the command od –ax dosfile.txt . You will see that the end of each line is CR and NF.
5. Type the command pico dosfile.txt such that you use the pico editor to open the file "dosfile.txt". Add some content and then delete it again (e.g., add a blank line and remove this line again). Press Ctrl-x to quit pico.
6. Type the command od –ax dosfile.txt again. How is the end of each line changed?
Commands find, whereis and tr
7. Use the command find / -name ls to locate the program file ls. Then use an alternative command whereis ls to locate the same program file.
8. The command tr can be used to translate characters. For example, use the command find to locate the log file "boot.msg" and copy this file to the directory "tmp". Type the command tr ‘[a-z]’ ‘[A-Z]’ < boot.msg >result.log to translate all the lower-case letters in "boot.msg" to upper-case and save the resulting letters in "result.log".
Commands diff and patch
9. Use any editor to create a file called "helloworld.c" and input the following: main( ) { printf (“hello world!\n”); printf(“how are you today?\n”); }
10. Copy the file "helloworld.c" as another file "newhelloworld.c".
11. Edit the file "newhelloworld.c", changing the word today to yesterday. Then exit the editor.
12. Type the command diff helloworld.c newhelloworld.c > patch1.pat.
13. Type the command cat patch1.pat to see the content of patch1.pat.
14. Type the command cat helloworld.c.
15. Type the command patch helloworld.c patch1.pat.
3
COMP1600 - Software Development Workshop I
16. Type the command cat helloworld.c again. What has happened to "helloworld.c"?
Exercises
1. Locate a file called "boot.msg".
2. Copy this file to the directory "/tmp".
3. Use the editor vi to open this file.
4. Move the last 5 lines to the beginning of the file.
5. Search the phrase “Setting up network interfaces” and then copy it to the end of the file.
6. Save the file as "exercise.txt" in the directory "/tmp".
References
http://www.novell.com/documentation/suse101/index.html
4