UNIX Chapter 09 Basic File Processing Mr. Mohammad Smirat.

Slides:



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

UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Jump to first page Unix Commands Monica Stoica Jump to first page Introduction to Unix n Unix was born in 1969 at Bell Laboratories, a research subdivision.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Working with Files How to create, view, copy, rename and print files.
Lab6 CPCS361 I.Mona Alshehri. Working with Files and Directories Creating files Create a file with the cat command type the command cat > name_of_file.
ATS Programming Short Course I INTRODUCTORY CONCEPTS Tuesday, Jan. 27 th, 2009 Essential Unix Commands.
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.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
CS 497C – Introduction to UNIX Lecture 12: - The File System Chin-Chih Chang
UNIX Utilities Software Tools. Slide 2 Getting Started on UNIX The machines in CS Lab2 are named csl2wk01 through csl2wk41. csl2wk01 means “CSLab2, workstation#1”
Chapter 7 Advanced Directory and File Management.
CSE 303 Lecture 2 Introduction to bash shell
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
UNIX Chapter 00 A “ Quick Start ” into UNIX Operating System Mr. Mohammad Smirat.
Guide To UNIX Using Linux Third Edition
Chapter 6 Basic Directory and File Management. Command Line Control Characters Control-s - Stops screen output - rarely used Control-q - Resumes screen.
UNIX Utilities Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn Input/Output.
9-2 How do you copy/move/rename/remove files? How do you create a directory ? What is redirection and piping? Readings: See CCSO’s Unix pages andCCSO’s.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Learning basic Unix command IT 325 operating system.
Chapter 4: UNIX File Processing Input and Output.
COMP1070/2002/lec4/H.Melikian COMP1070 Lecture #5  Files and directories in UNIX  Various types of files  File attributes  Notion of pathname  Commands.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter 9 Part II Linux Command Line Access to Linux Authenticated login using a Linux account is required to access a Linux system. The Linux prompt will.
CHAPTER 1 UNIX FOR NONPROGRAMMERS By U ğ ur Halıcı.
V Avon High School Tech Crew Agenda Old Business –Delete Files New Business –Week 9 Topics: Coming up: –Yearbook Picture: Feb 7 12:20PM.
Working with Files Chapter 5. Display a Calendar Display a calendar for a specific month – cal Display a calendar for a specific year – cal 2000.
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
CS 497C – Introduction to UNIX Lecture 7: General-Purpose Utilities Chin-Chih Chang
Jozef Goetz, expanded by Jozef Goetz, 2009 Credits: Parts of the slides are based on slides created by UNIX textbook authors, Syed M. Sarwar, Robert.
Managing Files. Module 5 Managing Files ♦ Introduction “On a Linux system, everything is a file; if something is not a file, it is a process.” ♦ Topics.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX Commands cal – will print a calendar.
BIF713 Basic Unix/Linux Commands Getting Help with Commands.
Chapter 9 Basic File Processing. Displaying File Contents cat, cat w/append tac nl pr more less head tail.
Welcome to CS323 Operating System lab 1 TA: Nouf Al-Harbi NoufNaief.net.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: whereis, which, whoami, finger, passwd, cal, date Working with Files: cat, more, less.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
Λειτουργικά Συστήματα – Lab2 Γιάννης Πετράκης. Directory Navigation and Control  The Unix file system is set up like a tree branching out from the root.
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: which, passwd, date, ps / kill Working with Files: file, touch, cat, more, less, grep,
Linux Lecture #02. File Related Commands cat --Concatenate and print (display) the content of files. --Also used to create a new file. Syntax cat [Options]
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
1 © 2001 John Urrutia. All rights reserved. Chapter 2 Getting Started.
Files and Directories in UNIX The first file in UNIX file system is “root” or “/”
Command Prompt Chapter 9 Pipes, Filters, and Redirection ©Richard Goldman 11/30/2000 Revised 10/16/2001.
CSCI 330 UNIX and Network Programming Unit II Basic UNIX Usage: File System.
Learning basic Unix command It 325 operating system.
CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT File Processing.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
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.
Some Linux Commands.
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands:
CSE 374 Programming Concepts & Tools
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Chapter Four UNIX File Processing.
Exploring Shell Commands, Streams, and Redirection
UNIX/LINUX Commands Using BASH Copyright © 2017 – Curt Hill.
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Module 6 Working with Files and Directories
Exploring Shell Commands, Streams, and Redirection
LPI Linux Certification
Presentation transcript:

UNIX Chapter 09 Basic File Processing Mr. Mohammad Smirat

Viewing Complete Files You can display the complete contents of one or more files on screen by using the cat command. cat[options][file-list] Concatenate/display the files in file-list on standard output. Options are: -e display $ at the end of each line -n put line numbers with the displayed lines.

CAT Examples $cat student_records [ contents of file displayed] $cat ~/courses/cis310/exam1 ~/courses/cis311/exam2 [ contents of exam1 and exam2] When cat command is executed without arguments, it takes input from the keyboard. $cat This is a test. This is a test.

Viewing Complete Files (cont … ) The UNIX utility nl allows you to display files having line numbers, so the nl student_records will display the lines in the student records file with line numbers. $ nl student_record 1 john Doe ECE Pam Meyer CIS Amy Nash CIS 3.25 The same task can be performed by: $cat -n student_record

Viewing Complete Files (cont … ) You can use the pr utility to display file contents of a file with time stamp and page number. $pr student_records Feb 10 11: student_records Page 1 John Doe ECE 3.54

Viewing Files One Page at a time more[option][file list] To concatenate/display the files in file-list on standard output a screenful at a time. Options: +/str start at the first line containing str. -nN display N lines per screen/page +N start displaying the contents of the file at line number N.

MORE Examples $ more sample [ contents of sample one page at a time] $ more sample letter memo [ contents of sample, letter, and memo] $ more -n20 sample [ contents of sample 20 lines per page] $more +/*.c [ contents of all.c files in the current directory]

Viewing the Head or Tail of a File The UNIX command for displaying the head or tail of a file are head and tail. head[options][file list] will display the beginning portion (head) of files in file list, the default head size is 10 lines. Options -N display first N lines. $head sample [first 10 lines of sample displayed] $head sample memo phones [ first 10 lines of sample, memo, and phone] $head -5 sample [first 5 lines of sample]

Viewing the Head or Tail of a File (CONT … ) Tail[options][file-list] display the last portion (tail) of the files in file_list, the default tail size is 10 lines. Options -f follow growth of the file after displaying the last line of a file, and display lines as they appended to the file +n/-n start n lines from the beginning of the file for +n, and n lines from the end of the file. -r display lines in reverse order (last line first)

Viewing the Head or Tail of a File (CONT … ) $tail sample … last 10 lines of sample … $ tail -5 sample … last 5 lines of sample … $ tail +8 sample … all the lines in sample starting with line 8 … $tail -5r sample …last 5 lines of sample displayed in reverse order.. $tail -f sample … last 10 lines of sample … more data as it is append to sample …

Copying Files The UNIX command for copying files is cp. cp[options] file1 file2 will copy file1 to file2 if file2 is a directory, make a copy of file1 in this directory. Options -i if destination exist, prompt before overwriting. -p preserve permissions and modifications times. -r recursively copy files and subdirectories.

COPY Examples $ls memo sample temp $cp temp temp.bak $ls memo sample temp temp.bak $cp ~/.profile ~/sys.backups/.profile.bak It will produce an error if.profile does not exist, or do not have permission to read it, or the other file does exist or you do not have a write permission to it. $cp -i lab* ~/courses/cis311 The above command copies all file in the current directory starting with the string lab to the directory ~/courses/cis311. The i option to prompt you for overwriting a file if it exist in the destination directory.

Moving Files The operation move in UNIX may result in simply renaming a file if it is on the same file system. If the source and destination are on different file system, the move operation results in a physical copy of the source file to the destination, followed by removal of the source file. mv [options] file1 file2 mv [options] file-list directory will move file 1 to file 2 or rename file1 as file2. Move all files in file_list to directory. Options: -f force the move regardless of the permission of the destination file. -i prompt the user before overwriting the destination.

MV Examples $mv temp temp.moved $mv temp backups/temp.old The first will move temp to temp.moved. The second will move temp to backups directory as temp.old. $mv -f temp temp.old The above command is a sure move one, it will force the move regardless of the permission for the target file. $mv dir/* dir2 will move all file and subdirectories of dir to dir2, after the move dir2 becomes an empty directory.

Removing/deleting Files The UNIX command for removing (deleting) files is rm. rm[options] file list To remove files in file list from the file structure. Options -f force remove regardless of the permission for file_list. -i prompt the user before removing the files in file_list. -r recursively remove the files in the directory which is passed as an argument. You want to be sure that you want to do so before using this option.

RM Examples $rm temp $rm temp backups/temp.old $rm -f phones grades ~/letters/letter.john $rm ~/dir/* The first removes file temp. The second removes the temp.old from the backups directory. The third removes the files phone, grades and ~/letter/letter.john regardless of their access permission. The fourth removes all the files from ~/dir directory.

RM Examples (cont … ) $rm [kK]*.prn will remove all files from current directory with name starting in letter k and with extension.prn $rm [a-kA-Z]*.prn all file with extension.prn and starting with lower case letter a-k or upper case letter. $rm -r ~ The above command you should never execute unless you know what you are doing??? You should always combine -i and -r options? Why? $ rm -ir ~/dir1 rm: examines files in direcrory /home.dir1(y/n)? rm: remove /home/dir1/file1 (y/n)? rm: remove /home/dir1/file2 (y/n)?

Determining File Size The two commands commonly used are the ls -l and wc. wc [options] file list will display the size of the files in the file list as number lines, words, and characters. Options -c display # of chars. -l display # of lines. -w display # of words.

WC Examples $wc sample sample lineswordsbytesname $wc letter sample test [ display each file info (lines,words,and bytes) then total them] $wc -lw letter 44250letter $wc /etc/passwd 000/etc/passwd $wc /usr/include/sys/* … output of the command …

Appending to Files cat[file_list] >> destination file $cat sample >> temp will append the contents of sample at the end of file temp. $cat memo1 memo2 memo3 >> memos.dat will append the contents of the three files at the end of memos.dat.

Appending to Files (cont … ) $cat business_lunch hi john, could we meet tomorrow at lunch $cat >> business_lunch we need to discuss some business please confirm by this afternoon, thanks $cat business_lunch hi john, could we meet tomorrow at lunch we need to discuss some business please confirm by this afternoon, thanks

Combining Files cat[file-list] > destination file The destination file is overwritten if it is already exists. If you do not have permission you will get an error. $cat memo1 memo2 memo3 > memos.file will put all three files into memos.file in the same order. The above command is the same as the following: $cat memo1 > memos.file $cat memo2 >> memos.file $cat memo3 >> memos.file

Removing repeated Lines You can use the unique command to remove all but one copy of successive repeated lines in a file. The command is designed to work on sorted files. Uniq [options][input file][output file] remove repetitions lines from the sorted file, and send unique lines to output file. Options -c precede each output line by the number of times it occurs. -d display the repeated lines -u display the lines that are not repeated.

Removing repeated Lines (cont...) $uniq -c sample display unique lines and precede each line by # of times it occurs. $uniq -d sample out find out the repeated lines and send output to out file.