LAB 8 (Aug 4/5) Unix Utilities

Similar documents
LAB 8 (Aug 4/5) Unix Utilities

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Linux Essentials. Programming and Data Structures Lab M Tech CS First Year, First Semester

Chapter-3. Introduction to Unix: Fundamental Commands

Introduction to Linux

Files and Directories

On successful completion of the course, the students will be able to attain CO: Experiment linked. 2 to 4. 5 to 8. 9 to 12.

Files

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Introduction to Unix: Fundamental Commands

CSC209. Software Tools and Systems Programming.

EECS2301. Lab 1 Winter 2016

A Brief Introduction to Unix

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

UNIX File Hierarchy: Structure and Commands

CS246 Spring14 Programming Paradigm Notes on Linux

Consider the following program.

Commands are in black

Practical Session 0 Introduction to Linux

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

2) clear :- It clears the terminal screen. Syntax :- clear

CSCI 2132 Software Development. Lecture 4: Files and Directories

Introduction to Linux Basics

Lecture 3. Unix. Question? b. The world s best restaurant. c. Being in the top three happiest countries in the world.

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Practical 02. Bash & shell scripting

Set 1 MCQ Which command is used to sort the lines of data in a file in reverse order A) sort B) sh C) st D) sort -r

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

Linux Shell Script. J. K. Mandal

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

The Unix Shell & Shell Scripts

CISC 220 fall 2011, set 1: Linux basics

Laboratory 1 Semester 1 11/12

Unix Tools / Command Line

Perl and R Scripting for Biologists

Week 2 Lecture 3. Unix

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

18-Sep CSCI 2132 Software Development Lecture 6: Links and Inodes. Faculty of Computer Science, Dalhousie University. Lecture 6 p.

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

Introduction to UNIX command-line

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

CHAPTER 1 UNIX FOR NONPROGRAMMERS

A Brief Introduction to the Linux Shell for Data Science

5/20/2007. Touring Essential Programs

Unix Filesystem. January 26 th, 2004 Class Meeting 2

Linux & Shell Programming 2014

Unix Handouts. Shantanu N Kulkarni

Introduction to UNIX command-line II

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]

CS Fundamentals of Programming II Fall Very Basic UNIX

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Introduction to the Linux Command Line

Introduction: What is Unix?

Unix File System. Class Meeting 2. * Notes adapted by Joy Mukherjee from previous work by other members of the CS faculty at Virginia Tech

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

CENG 334 Computer Networks. Laboratory I Linux Tutorial

File permission u owner of file/directory (user) g group of the file/directory o other a all

The Unix Shell. Pipes and Filters

The Unix Shell. Permissions

GNU/Linux 101. Casey McLaughlin. Research Computing Center Spring Workshop Series 2018

PESIT Bangalore South Campus

UNIX files searching, and other interrogation techniques

5/8/2012. Exploring Utilities Chapter 5

CST8207: GNU/Linux Operating Systems I Lab Six Linux File System Permissions. Linux File System Permissions (modes) - Part 1

Welcome to Linux. Lecture 1.1

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

Introduction to Linux

UNIX. Contents. Overview of UNIX Structures File systems

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Introduction to Linux

Unix Introduction to UNIX

Mills HPC Tutorial Series. Linux Basics I

commandname flags arguments

CSN08101 Digital Forensics. Module Leader: Dr Gordon Russell Lecturers: Robert Ludwiniak

Computer Systems and Architecture

Lab #2 Physics 91SI Spring 2013

Exercise Sheet 2. (Classifications of Operating Systems)

Unix Internal Assessment-2 solution. Ans:There are two ways of starting a job in the background with the shell s & operator and the nohup command.

Find out where you currently are in the path Change directories to be at the root of your home directory (/home/username) cd ~

Working with Basic Linux. Daniel Balagué

Introduction to Linux

The input can also be taken from a file and similarly the output can be redirected to another file.

SECTION -C. Getting Started with UNIX

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

Outline. Structure of a UNIX command

Open up a terminal, make sure you are in your home directory, and run the command.

Unix Basics. UNIX Introduction. Lecture 14

CENG393 Computer Networks Labwork 1

S E C T I O N O V E R V I E W

Introduc)on to Linux Session 2 Files/Filesystems/Data. Pete Ruprecht Research Compu)ng Group University of Colorado Boulder

Introduction to Linux Workshop 1

Introduction to UNIX. Introduction EECS l UNIX is an operating system (OS). l Our goals:

Transcription:

Aug 4/5 Due: Aug 11 in class Name: CSE number: LAB 8 (Aug 4/5) Unix Utilities The purpose of this lab exercise is for you to get some hands-on experience on using some fundamental Unix utilities (commands). After this lab, you are expected to be able to accomplish lots tasks using command line utilities, without resorting to your GUI based utilities such as JEditors or File Manager. In some systems such GUI tools are not available and thus using command line utilities is your only option. We covered in class the following basic utilities/commands: man, pwd, ls, cd, mkdi, rmdir, cat, more, head, tail, cp, mv, rm, wc, chmod, grep/egrep. We will also cover uniq, sort, cut, find and tr etc this week. You are also expected to explore some advanced utilities such as tr, awk, sed. You can get the details of each utility by 1) going through the class slides. 2) going through the provided Guided Lab Tour for CSE1020. 3) using utility man. E.g., man sort Go through the following 95 (small) practices. Write down your answers besides the questions. Hand in this sheet. Note: Each solution should only involve one entry of utility (e.g., mv file1 file2) or a pipeline of utilities (e.g., cat file1 sort wc). 0. Login to your home directory, and change to Bourne shell by issuing sh.the prompt should change from % to $ 1. There is a file called xxx in directory /cs/dept/course/2014-15/s/2031/ Copy this file to your current directory with the same name, using one entry of utility (command). 2. Check that the file is copied here. sh-4.1$ your_command xxx sh-4.1$ 3. There are two files named xfile2 and xfile3 in directory /cs/dept/course/2014-15/s/2031/ copy these two files to your home directory using one entry of utility, and using absolute path. Assume these two files are the only files whose names begin with xfile. Hint: so you can use xfile* or File? to match these two files. (If you don t understand * and?, look for page 10 of the Guided Lab Tour for CSE1020). Note, don t confuse that with * and? that are used in (extended) regular expression.. 4. Verify that the 2 files are copied successfully to the current directory. sh-4.1$ your_command xfile2 xfile3 sh-4.1$ 5. Rename file xxx to xfile1 6. Verify that the renaming is successful. sh-4.1$ your_command xfile1 xfile2 xfile3 7. Create a sub-directory named lab8a under the current (home) directory 1

8. Verify the existence of directory lab8a by listing the directory itself. sh-4.1$ your_command drwx------ 2 your_name ugrad 4096 Jul 31 17:00 lab8a sh-4.1$ (Note, if the creation was unsuccessful, then you will get ls: cannot access lab8working: No such file or directory ) 9. Move all the 3 files (together) into lab8a (using one entry of utility) 10. Verify that the move is successful by listing files in directory lab8a (without going into that directory) total 12 -rwx------ 1 yourname yourgroup 140 July 31 16:58 xfile1 -rwx------ 1 yourname yourgroup 140 July 31 16:58 xfile2 -rwx------ 1 yourname yourgroup 86 July 31 16:58 xfile3 11. Copy directory lab8a to a new directory named lab8b (using one). 12. Verify that lab8b was created successfully total 8 drwx------ 2 yourname yourgroup 4096 July 31 17:00 lab8a drwx------ 2 yourname yourgroup 4096 July 31 17:02 lab8b sh-4.1$ 13. Verify that the two directories are identical lab8a: xfile1 xfile2 xfile3 lab8b: xfile1 xfile2 xfile3 14. Remove the whole directory lab8a using rmdir. What happened? 15. Remove the whole directory lab8a using a different (more effective) utility. 16. Verify that directory lab8a has been removed ls: cannot access lab8a: No such file or directory sh-4.1$ 17. Move xfile1 in lab8b to current (parent) directory, using relative directory. 18. Verify that the above move was successful. Lets do this by searching for the files sh-4.1$ find. name xfile*./lab8b/xfile2./lab8b/xfile3./xfile1 2

19. Change the name of directory lab8b to lab8working 20. Verify that the renaming was successful. ls: cannot access lab8b: No such file or directory 21. Navigate to directory lab8working 22. Verify that you are in lab8working /cs/home/your_account/lab8working 23. Move xfile1 (in the parent directory) to the current directory using relative path. 24. Verify that the move was successful by listing all the files currently in lab8working. total 12 -rwx------ 1 yourname yourgroup 140 Jul 31 16:58 xfile1 -rwx------ 1 yourname yourgroup 140 Jul 31 16:58 xfile2 -rwx------ 1 yourname yourgroup 86 Jul 31 16:58 xfile3 25. Display on stdout the contents of the three files with one command (Try more xfile1 xfile2 xfile3 or more xfile?) 26. Check how many lines xfile1 contains. You should get 5. 27. Display the first line of xfile1 28. Display the first two lines of xfile2 29. Display the last 3 lines of xfile2 30. Confirm that xfile1 and xfile2 are identical, using an utility, which should return silently (Hint: cmp or diff). 31. Confirm that xfile1 and xfile2 are identical, using another utility, which return silently (diff or cmp) 32. Show that xfile2 and xfile3 are not identical, using one utility, which will not be silent this time. Try to understand the message but don t spend too much time on it. 33. Show that xfile2 and xfile3 are not identical, using another utility, which will not be silent this time. Try to understand the message but don t spend too much time on it. Note: these two utilities are used by some professors to judge your labtest output with their solutions: gcc yourcode.c; a.out > youroutputfile; cmp youroutputfile professorsoutputfile; A student gets 0 if the last command does not return silently. 34. Concatenate the contents of the three files into a new file xfile123, in the order of xfile1,xfile2 and xfile3. Hint: use cat and redirection >. After that, show on stdout the content of xfile123. 3

sh-4.1$ more xfile123 35. Sort file xfile123 so the identical lines are adjacent now 36. Show on the stdout the count of identical lines (hint: use pipe involving sort and uniq -c). 3 3 3 2 2 37. Remove the identical lines and save the result into a file xfile123compact, without the counts (as show below). 38. Show on the stdout the content of xfile123compact 4

39. Issue chmod 775 xfile1, see the resulting permission mode of the file. Can you understand what we are doing here? 40. Issue chmod 777 xfile1, see the resulting permission mode of the file. Can you understand what we are doing here? 41. Change the permission of xfile123compact using octal numbers so that the permission becomes -rwxr--r-- 1 yourname yourgroup 140 Jul 31 17:23 xfile123compact 42. Change the permission of file123compact by giving group an execute permission. Then show the result: -rwxr-xr-- 1 yourname yourgroup 140 Jul 31 17:23 xfile123compact 43. change the permission of xfile123compact by giving group a write permission, and remove write permission of the owner of the file. You should do chmod only once. Then show the result: -r-xrwxr-- 1 yourname yourgroup 140 Jul 31 17:23 xfile123compact 44. Change the permission of xfile123compact by removing write permission from the group, and giving execution permission to the others. You should do chmod only once. Then show the result: r-xr-xr-x 1 yourname yourgroup 140 Jul 31 17:23 xfile123compact 45. Change the permission of xfile123compact by adding the write permission of user, group and other. You should do chmod only once. Then show the result: rwxrwxrwx 1 yourname yourgroup 140 Jul 31 17:23 xfile123compact 46. Modify xfile1 by adding a new line at the end of the file. This can be done by echo this is a xxx new line >> xfile1 cat >> xfile1 and then enter a new line followed by Ctrl-D. or 47. List the files in the current directory, sorted by the modification time. xfile1 should be the first file in the list and other files are also sorted according to the modification time. 48. List the files, sorted by the modification time, in reverse order. 49. List the files, sorted by the size of the files. xfile123 should be the first file in the list and other files are also sorted according to the sizes. 50. List the files, sorted by the size of the files, in reverse order. 51. Try to get the type of the file xfile123compact (Hint: use file utility) xfile123compact: ASCII text 52. Try to get the type of one of your c source files. hello.c: ASCII C program text 53. Try to get the type of one of your a.out files. a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped 5

54. Check that the link count of xfile123compact is 1, which means that entry xfile123compact is the only link to the physical file, which resides in the disk. -rwxrwxrwx 1 yourname yourgroup Jul 31 17:23 xfile123compact link count 55. Generate a hard link of xfile123compact called hardlink123 (Hint: use ln utility) Now both xfile123compact and hardlink123 'point' to the same physical file, which resides somewhere in the disk. 56. Verify that the link counts of both the two files are 2 now. This means that two pointers point to the same physical file. -rwxrw-r-- 2 yourname yourgroup 140 Jul 31 17:50 hardlink123 -rw------- 1 yourname yourgroup 140 Jul 31 17:48 xfile1 -rw------- 1 yourname yourgroup 366 Jul 31 17:10 xfile123 -rwxrwxrwx 2 yourname yourgroup 140 Jul 31 17:20 xfile123compact -rw------- 1 yourname yourgroup 140 Jul 31 16:58 xfile2 -rw------- 1 yourname yourgroup 86 Jul 31 16:58 xfile3 57. Remove file hardlink123. 58. Verify that hardlink123 is gone, but xfile123compact is still there, and its link count becomes 1. -rw------- 1 yourname yourgroup 140 Jul 31 17:48 xfile1 -rw------- 1 yourname yourgroup 366 Jul 31 17:10 xfile123 -rwxrwxrwx 1 yourname yourgroup 140 Jul 31 17:20 xfile123compact -rw------- 1 yourname yourgroup 140 Jul 31 16:58 xfile2 -rw------- 1 yourname yourgroup 86 Jul 31 16:58 xfile3 59. Since the newly added line of xfile1 is the only line that contains xxx in the file, we can remove the (last) line from xfile1 by issuing two utilities:, on a single line: sh-4.1$ egrep v xxx xfile1 > tmp sh-4.1$ mv tmp xfile1 Do you understand what I am doing here? Briefly explain here: 60. Sort xfile123compact according to the numerical value of the 3rd field 61. Sort xfile123compact according to the numerical value of the 3rd field, in reverse order 6

62. In exercise 59 and 60, John Smith and Lisa Sue, who have the same 3 rd field value, did not get further sorted according to the 4 th field. Sort xfile123compact according to the numerical value of the 3rd field, and then based on this, further sort according to the 4th field. You just need one command, don t sort twice. 63. Sort xfile123compact according to the year (the last field) 64. Sort xfile123compact according to the year (the last field), in reverse order. 65. Use cut utility to extract the first names in the file xfile123compact. That is, display the 1 st column/field of the file. Use man or look into the slides for the cut utility. Hint: cut fx -d" " file123compact, where x is an integer. This command means treat " " as column/field delimiter, and display column/field x. John John Larry Lisa Tony 66. Use cut utility to extract the month and year in the file xfile123compact. Hint: cut fx,y -d" " or cut fx-y -d" " Jan 1966 Apr 1956 Dec 1946 Jul 1980 Mar 1950 7

67. [Bonus]. Extract the first name, month and year in the file xfile123compact, and output the results sorted by month. John Jan 1966 Tony Mar 1950 John Apr 1956 Lisa Jul 1980 Larry Dec 1946 68. Suppose that we want to change all the lower case letters in xfile123compact to upper case letters. This can be accomplished by a utility called tr. Try tr [a-z] [A-Z] < xfile123compact and observe the following output. JOHN DUNCAN 1111 20 JAN 1966 JOHN SMITH 1222 26 APR 1956 LARRY JONES 1223 20 DEC 1946 LISA SUE 1222 15 JUL 1980 TONY JONES 1012 20 MAR 1950 69. Use the same utility to change every digit in the file into character +. John Duncan ++++ ++ Jan ++++ John Smith ++++ ++ Apr ++++ Larry Jones ++++ ++ Dec ++++ Lisa Sue ++++ ++ Jul ++++ Tony Jones ++++ ++ Mar ++++ The following exercises involve filtering files. Use egrep or grep E (which guarantee to accept the extended regular expression) and make sure you are in sh. 70. Display the records of people in file xfile123compact who were born in 1950s 71. Pipe egrep or grep E and another utility to get the number of peoples in xfile123compact who were born in 1950s. You should get 2. 72. The EECS department maintains the records of all the students, stuff and faculty members in a file /etc/passwd, one person per line. Issue a utility to find out how many students are in the list. You should get about 2312. 73. Retrieve your information in the file /etc/passwd, using your cse login account. 74. The (modified) class list of our class can be found at /cs/dept/course/2014-15/s/2031/2031cl. Each line of the file contains one student information. Get the number of students currently enrolled in the course. 8

You should get 44. You can use the file directly (by giving the path), or, copy the file to your current directory. If you are wondering how I modified the file, here is the single command: tr [0-9] * < original2031cl > 2031cl 75. Retrieve your record from the class list. 76. Look for the student(s) whose name contain Wang. 77. Pipe egrep and another utility to get the number of students whose name contain Wang. You should get 1 78. Look for the student(s) whose name contains Wang or Wong. 79. Pipe egrep and another utility to get the number of students whose name contain Wang or Wong. You should get 2. There is a file lyrics in directory /cs/dept/course/2014-15/s/2031. Use the file directly (by giving the path), or, copy the file to your current directory, and then use grep E or egrep utility to find the lines in lyrics that: 80. does not contain the Well you know it's your bedtime, Oh you sleepy young 1970 heads dream of wonderful things, 81. contains digits #So turn off the light, 1980 Oh you sleepy young 1970 heads dream of wonderful things, sea 1980 I got there by chance. 82. contains 1980 #So turn off the light, 1980 sea 1980 I got there by chance. 83. end with 1980 #So turn off the light, 1980 84. contains sea Beautiful mermaids will swim through the sea, sea 1980 I got there by chance. 85. begins with sea sea 1980 I got there by chance. 86. contains one (any) character followed by nd Say all your prayers and then, Oh you sleepy young 1970 heads dream of wonderful things, 87. contains one (any) character followed by nd, but as a whole world only (so wonderful does not match) Say all your prayers and then, 9

88. begins with one (any) character followed by nd 89. contains letter A or B or C or D Beautiful mermaids will swim through the sea, 90. begins with a capital letter Well you know it's your bedtime, Say all your prayers and then, Oh you sleepy young 1970 heads dream of wonderful things, Beautiful mermaids will swim through the sea, 91. begins with a non capital letter (lower case letter or any other characters) #So turn off the light, 1980 sea 1980 I got there by chance. 92. ends with a and one other character. Beautiful mermaids will swim through the sea, 93. contains a character that is either a or b or c, followed by nd Say all your prayers and then, 94. contains a character that is not a nor b nor c, followed by nd Oh you sleepy young 1970 heads dream of wonderful things, 95. Go back to your home directory, and issue the utility find. -name xfile* What do you get? 96. Issue find. -name xfile* -exec mv {} {}.2015 \; What we intend to do here? List directory lab8working and examine what happens to the files in lab8working? Submission Print out this pdf, write your solutions on this sheet. Hand in the sheet in next class. Or, write the question numbers and your answers in a file, and submit the file submit lab8 your_file_name End of lab8 10