Handling Ordinary Files

Similar documents
HANDLING ORDINARY FILES

Utilities. September 8, 2015

7. Archiving and compressing 7.1 Introduction

User Commands GZIP ( 1 )

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

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

Lecture 3. Essential skills for bioinformatics: Unix/Linux

Fall Lecture 5. Operating Systems: Configuration & Use CIS345. The Linux Utilities. Mostafa Z. Ali.

Chapter-3. Introduction to Unix: Fundamental Commands

Lab #1 Installing a System Due Friday, September 6, 2002

UNIX and Linux Essentials Student Guide

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

Files

acmteam/unix.pdf How to manage your account (user ID, password, shell); How to compile C, C++, and Java programs;

Unit: Making a move (using FTP)

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

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

Unix/Linux Primer. Taras V. Pogorelov and Mike Hallock School of Chemical Sciences, University of Illinois

SECTION -C. Getting Started with UNIX

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

Essential Skills for Bioinformatics: Unix/Linux

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

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

Basic Linux Commands. Srihari Kalgi M.Tech, CSE (KReSIT), IIT Bombay. May 5, 2009

IMPORTANT: Logging Off LOGGING IN

Introduction to Unix: Fundamental Commands

Introduction: What is Unix?

Basic Shell Commands

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

Lecture 5. Additional useful commands. COP 3353 Introduction to UNIX

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

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

Using Linux as a Virtual Machine

Std: XI CHAPTER-3 LINUX

commandname flags arguments

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Basic Survival UNIX.

Topic 2: More Shell Skills

Introduction to remote command line Linux. Research Computing Team University of Birmingham

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

CS Unix Tools. Lecture 2 Fall Hussam Abu-Libdeh based on slides by David Slater. September 10, 2010

Quick Start Guide. by Burak Himmetoglu. Supercomputing Consultant. Enterprise Technology Services & Center for Scientific Computing

Lab 1 Introduction to UNIX and C

Unix/Linux Operating System. Introduction to Computational Statistics STAT 598G, Fall 2011

CSC UNIX System, Spring 2015

Reading and manipulating files

Filesystem and common commands

Unzip command in unix

GNU CPIO September by Robert Carleton and Sergey Poznyakoff

Unix Introduction to UNIX

Unix Tools / Command Line

A Brief Introduction to the Linux Shell for Data Science

Introduction. File System. Note. Achtung!

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Introduction to UNIX Command Line

CHAPTER 1 UNIX FOR NONPROGRAMMERS

CS4350 Unix Programming. Outline

5/8/2012. Exploring Utilities Chapter 5

Computer Architecture Lab 1 (Starting with Linux)

UNIX Command Cheat Sheets

Outline. Structure of a UNIX command

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

Introduction to UNIX I: Command Line 1 / 21

UNIX Command Cheat Sheets

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

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

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

Chapter 4. Unix Tutorial. Unix Shell

Chapter 1 - Introduction. September 8, 2016

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

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

Linux Training. for New Users of Cluster. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Course 144 Supplementary Materials. UNIX Fundamentals

CSCE 212H, Spring 2008, Matthews Lab Assignment 1: Representation of Integers Assigned: January 17 Due: January 22

Advanced Linux Commands & Shell Scripting

Introduction to Linux Organizing Files

Linux 系统介绍 (I) 袁华

File: PLT File Format Libraries

Linux Tutorial #7. quota. df (disk free) du (disk usage)

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

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

Research. We make it happen. Unix Basics. User Support Group help-line: personal:

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

Part I. Introduction to Linux

Introduction to Linux

RH033 Red Hat Linux Essentials

Introduction to Linux

Common UNIX Utilities Alphabetical List

CS Fundamentals of Programming II Fall Very Basic UNIX

Mills HPC Tutorial Series. Linux Basics I

Linux Command Line Primer. By: Scott Marshall

BIOINFORMATICS POST-DIPLOMA PROGRAM SUBJECT OUTLINE Subject Title: OPERATING SYSTEMS AND PROJECT MANAGEMENT Subject Code: BIF713 Subject Description:

Lab 2: Linux/Unix shell

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits

Bash Programming for Geophysicists

Useful Unix Commands Cheat Sheet

Working with Basic Linux. Daniel Balagué

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

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

Transcription:

Handling Ordinary Files Unit 2 Sahaj Computer Solutions visit : projectsatsahaj.com 1

cat: Displaying and Creating Files cat is one of the most frequently used commands on Unix-like operating systems. It has three related functions with regard to text files: displaying them, combining copies of them and creating new ones. cat's general syntax is cat [options] [filenames] [-] [filenames] Sahaj Computer Solutions visit : projectsatsahaj.com 2

cat: Displaying and Creating Files Reading Files The most common use of cat is to read the contents of files, and cat is often the most convenient program for this purpose. All that is necessary to open a text file for viewing on the display monitor is to type the word cat followed by a space and the name of the file and then press the ENTER key. For example, the following will display the contents of a file named file1: cat file1 Sahaj Computer Solutions visit : projectsatsahaj.com 3

cat: Displaying and Creating Files The standard output stream for the cat command is the monitor. However this can be redirected to other stream like file. In the following example, the standard output of cat is redirected using the output redirection operator (which is represented by a rightward pointing angular bracket) to file2: cat file1 > file2 That is, the output from cat is written to file2 instead of being displayed on the monitor screen. Sahaj Computer Solutions visit : projectsatsahaj.com 4

cat: Displaying and Creating Files The standard output could instead be redirected using a pipe (represented by a vertical bar) to a filter (i.e., a program that transforms data in some meaningful way) for further processing. For example, if the file is too large for all of the text to fit on the monitor screen simultaneously, as is frequently the case, the text will scroll down the screen at high speed and be very difficult to read. Sahaj Computer Solutions visit : projectsatsahaj.com 5

cat: Displaying and Creating Files This problem is easily solved by piping the output to the filter less, i.e., cat file1 less This allows the user to advance the contents of the file one screenful at a time by pressing the space bar and to move backwards by pressing the b key. The user can exit from less by pressing the q key. Sahaj Computer Solutions visit : projectsatsahaj.com 6

cat: Options (-v and n) There are two cat options that you may find useful; Displaying Non Printing Characters Cat is normally used for displaying text files only. Executables, when seen with cat, simply display junk. If you have non printing characters in your input, you can use cat with the v option to display the characters. Sahaj Computer Solutions visit : projectsatsahaj.com 7

cat: Options (-v and n) Numbering Lines (-n) The n option is used to display numbered lines. It is necessary if you need to debug a program and locate an error on a line number. Sahaj Computer Solutions visit : projectsatsahaj.com 8

Using cat to create a file The second use for cat is file creation. For small files this is often easier than using vi, gedit or other text editors. It is accomplished by typing cat followed by the output redirection operator and the name of the file to be created, then pressing ENTER and finally simultaneously pressing the CONTROL and d keys. For example, a new file named file1 can be created by typing cat > file1 Sahaj Computer Solutions visit : projectsatsahaj.com 9

Using cat to create a file If a file named file1 already exists, it will be overwritten (i.e., all of its contents will be erased) by the new, empty file with the same name. Thus the cautious user might prefer to instead use the append operator (represented by two successive rightward pointing angular brackets) in order to prevent unintended erasure. That is, cat >> file1 then pressing the ENTER key and finally simultaneously pressing the CONTROL and d keys. Sahaj Computer Solutions visit : projectsatsahaj.com 10

Using cat to create a file That is, if an attempt is made to create a file by using cat and the append operator, and the new file has the same name as an existing file, the existing file is, in fact, preserved rather than overwritten, and any new text is added to the end of the existing file Sahaj Computer Solutions visit : projectsatsahaj.com 11

Concatenation of Files Sometimes you need to concatenate two files especially if you are following the modular coding techniques. You can easily concatenate two files by using the redirecting output parameter >. We can concatenate two files into the third file by following a syntax like cat file1 file2 >file3 It will first write down the file1 and then file2 into the file3. Sahaj Computer Solutions visit : projectsatsahaj.com 12

cp: Copying a file Use the cp command to create a copy of the contents of the file or directory specified by the SourceFile or SourceDirectory parameters into the file or directory specified by the TargetFile or TargetDirectory parameters. If the file specified as the TargetFile exists, the copy writes over the original contents of the file without warning. If you are copying more than one SourceFile, the target must be a directory. Sahaj Computer Solutions visit : projectsatsahaj.com 13

cp: Copying a File Synopsis: cp [OPTION]... SOURCE DESTINATION EXAMPLE: To copy a file in your current directory into another directory, type the following: cp jones /home/nick/clients This copies the jones file to /home/nick/clients/jones. Sahaj Computer Solutions visit : projectsatsahaj.com 14

cp:copying a File Example: To make a copy of a file in the current directory, type the following: cp prog.c prog.bak This copies prog.c to prog.bak. If the prog.bak file does not already exist, then the cp command creates it. If it does exist, then the cp command replaces it with a copy of the prog.c file. Sahaj Computer Solutions visit : projectsatsahaj.com 15

cp: Copying a File cp is often used with shorthand notation,.(dot), to signify the current directory as the destination For instance, to copy the file.profile from /home/sharma to your current directory you can use either of the two commands cp /home/sharma/.profile.profile cp /home/sharma/.profile. Sahaj Computer Solutions visit : projectsatsahaj.com 16

cp Options Interactive Copying (-i) The I (interactive) option wars the user before overwriting the destination file. If until exists, cp prompts for a response. Example: $ cp -I chap01 unit1 Sahaj Computer Solutions visit : projectsatsahaj.com 17

rm: Deleting Files The rm command deletes one or more files. It normally operates silently and should be used with caution. The following command deletes three files: rm chap01 chap02 chap03 You may need to delete all the files in a directory as a part of clean up operation. Sahaj Computer Solutions visit : projectsatsahaj.com 18

rm: Deleting Files The *, when used itself, represents all files, and you can use it with rm command. rm * Sahaj Computer Solutions visit : projectsatsahaj.com 19

rm Options Interactive Deletion(-i): Like in cp, the I (interactive) option makes the command ask the user for confirmation before removing each file. Example: rm I chap01 chap02 chap03 Sahaj Computer Solutions visit : projectsatsahaj.com 20

rm Options Force Removal (-f) rm prompts for removal if a file is writeprotected. The f option overrides this minr protection and forces removal. rm f chap01 If you combine this with r option it could be more risky thing to do. rm rf * will delete everything in the current directory and below. Sahaj Computer Solutions visit : projectsatsahaj.com 21

mv: Renaming Files The mv command returns (moves) files. It has two distinct functions: It renames a file (or directory) It moves a group of files to a different directory. mv doesn t create a copy of the file; it merely renames it. No additional space is consumed on disk during renaming Sahaj Computer Solutions visit : projectsatsahaj.com 22

mv: Renaming Files -f mv will move the file(s) without prompting even if it is writing over an existing target. Note that this is the default if the standard input is not a terminal. -i Prompts before overwriting another file. oldname newname filename mv [-f] [-i] oldname newname The oldname of the file renaming. The newname of the file renaming. The name of the file you want to move directory - The directory of were you want the file to go. Sahaj Computer Solutions visit : projectsatsahaj.com 23

more: Paging Output more is what we call a pager utility. Oftentimes the output of a particular command is too big to fit on one screen. The individual commands do not know how to fit their output to separate screens. They leave this job to the pager utility. The more command breaks the output into individual screens and waits for you to press the space bar before continuing on to the next screen. Sahaj Computer Solutions visit : projectsatsahaj.com 24

more: Paging Output Pressing the enter key will advance the output one line. Here is a good example: % cd /usr/bin % ls l That should scroll for a while. To break up the output screen by screen, just pipe it through more: % ls -l more The pipe is short for saying take the output of ls and feed it into more Sahaj Computer Solutions visit : projectsatsahaj.com 25

Navigation Irrespective of version, more uses the space bar to scroll forward a page at a time. To move forward one page, use f or the spacebar and to move back one page, use b Sahaj Computer Solutions visit : projectsatsahaj.com 26

Searching for a pattern You can perform search for a pattern with / command followed by the string. For instance, to look for the first while loop in your program, /while You can repeate this search for viewing the next while loop section by pressing n. Move back with b to arrive to the first page. Sahaj Computer Solutions visit : projectsatsahaj.com 27

The lp subsystem: Printing a File No user is allowed to direct access to the printer. Instead one has to spool(line up) a job along with others in a print queue. Spooling ensures the orderly printing the jobs and relieves the user from the necessity of administering the print resources. The spooling facility is provided by the lp(line printing) command. Sahaj Computer Solutions visit : projectsatsahaj.com 28

lp Options If printer has been defined the lp runs fine. If the printer is not defined or if more than one printers are defined, you need to use the -d with the printer name. lp dlaser chap01.ps The t option followed by the title string, prints the title on the first page. lp t First Chapter chap01.ps Sahaj Computer Solutions visit : projectsatsahaj.com 29

lp Options After the file has been printed, you can notify the user with the m (mail) option. You can also print multiple copies(-n) Example: lp n3 m chap01.ps Sahaj Computer Solutions visit : projectsatsahaj.com 30

Other commands in the lp subsystem The print queue is viewed with the lpstat command. By viewing this list, you can use the cancel command to cancel any jobs submitted to you. Cancel uses the request-id or printer name as argument. Example: cancel laser cancel pr1-320 Sahaj Computer Solutions visit : projectsatsahaj.com 31

file: Knowing the File Types Even if we know that there are 3 types of unix files we often want to know more about these files. For instance a regular file may contain plain text, a c file may contain executable code. Unix provides the file command to determine the type of file, especially of an ordinary file. Sahaj Computer Solutions visit : projectsatsahaj.com 32

file: Knowing the File Types Example: file archive.zip File correctly identifies the basic file types. Use * to signify all the files. Example: file * Sahaj Computer Solutions visit : projectsatsahaj.com 33

wc: Counting Lines, words and characters Unix features a universal word-counting program that also counts lines and characters. It takes one or more filenames as arguments and displays a four columnar output Synopsis: wc filename Sahaj Computer Solutions visit : projectsatsahaj.com 34

wc: Counting Lines, words and characters There are 3 Options with wc -l Counts the number of lines -w Counts the number of words -m or -c Counts the number of characters The -m option is available on Solaris and HP- UX. It is not available on Linux. On Linux systems, you need to use the -c option instead. Sahaj Computer Solutions visit : projectsatsahaj.com 35

wc: Counting Lines, words and Number of Lines characters To count the number of lines, use the -l ( l as in lines) option. For example, the command $ wc -l.profile produces the output 133.profile Sahaj Computer Solutions visit : projectsatsahaj.com 36

wc: Counting Lines, words and Number of Words characters To count the number of words in a file, use the -w ( w as in words) option. For example, the command $ wc -w.rhosts produces the output 14.rhosts which is what you expected. Sahaj Computer Solutions visit : projectsatsahaj.com 37

wc: Counting Lines, words and characters Number of Characters To count the number of characters, use either the -m option or the -c option. As mentioned, the -m option should be used on Solaris and HP-UX. The -c option should be used on Linux systems. For example, the command $ wc -c.profile produces the output 2908.profile Sahaj Computer Solutions visit : projectsatsahaj.com 38

od: Displaying Data in Octal od - dump files in octal and other formats octal dump displays contents as octal numbers. This can be useful when the output contains non-printable characters. For example, a filename may contain nonprintable characters. It can also be handy to view binary files. Sahaj Computer Solutions visit : projectsatsahaj.com 39

od: Displaying Data in Octal Synopsis od [-bc] filename The b option displays this value for each character seperately. Example: od b file1 The c command displays the associated character value. Example: od bc file1 Sahaj Computer Solutions visit : projectsatsahaj.com 40

cmp: Comparing two files You may often need to know whether two files are identical so one of them can be deleted. There are three commands in the UNIX system that can tell you this. The cmp command compares two files byte by byte and returns the location of the first mismatch on the screen. Sahaj Computer Solutions visit : projectsatsahaj.com 41

cmp: Comparing two files It takes two arguments: Synopsis: Output: cmp chap01 chap02 chap01 chap02 differ: char 9, line 1 If two file are identical, cmp display no message, but simply returns to the prompt. Sahaj Computer Solutions visit : projectsatsahaj.com 42

comm: What is Common? The comm command in the Unix family of computer operating systems is a utility that is used to compare two files for common and distinct lines. comm reads two files as input, regarded as lines of text. comm outputs one file, which contains three columns. The first two columns contain lines unique to the first and second file, respectively Sahaj Computer Solutions visit : projectsatsahaj.com 43

comm: What is Common?. The last column contains lines common to both. This functionally is similar to diff. Synopsis: comm file1 file2 Sahaj Computer Solutions visit : projectsatsahaj.com 44

Example File foo File bar Sahaj Computer Solutions visit : projectsatsahaj.com 45

diff: Converting one file to other In computing, diff is a file comparison utility that outputs the differences between two files. It is typically used to show the changes between one version of a file and a former version of the same file. diff displays the changes made per line for text files. Sahaj Computer Solutions visit : projectsatsahaj.com 46

diff: Converting one file to other It is invoked from the command line with the names of two files: diff original new. The output of the command represents the changes required to make the original file become the new file. If original and new are directories, then diff will be run on each file that exists in both directories. Sahaj Computer Solutions visit : projectsatsahaj.com 47

diff: Converting one file to other An option, -r, will recursively descend any matching subdirectories to compare files between directories. Synopsis: diff original_file new_file Sahaj Computer Solutions visit : projectsatsahaj.com 48

diff: Converting one file to other Usage: Suppose you have two sequences of items: a b c d f g h j q z a b c d e f g i j k r x y z From a longest common subsequence it's only a small step to get diff-like output: if an item is absent in the subsequence but present in the original, it must have been deleted. (The ' ' marks, below.) Sahaj Computer Solutions visit : projectsatsahaj.com 49

diff: Converting one file to other If it is absent in the subsequence but present in the second sequence, it must have been added in. (The '+' marks.) e h i q k r x y + - + - + + + + Sahaj Computer Solutions visit : projectsatsahaj.com 50

unix2dos and dos2unix: Converting between DOS and UNIX unix2dos (sometimes named todos or u2d) is a Unix tool to convert an ASCII text file from Unix format (line break) to DOS format (carriage return and line break) and vice versa. When invoked as unix2dos the program will convert a Unix text file to DOS format, when invoked as dos2unix it will convert a DOS text file to UNIX format. Sahaj Computer Solutions visit : projectsatsahaj.com 51

unix2dos and dos2unix: Converting between DOS and UNIX Syopsis unix2dos [-p] [file] p Access and modification time of the original file are preserved. file File to convert Sahaj Computer Solutions visit : projectsatsahaj.com 52

Compressing and Archiving Files To conserve disk space you need to compress large and infrequent used files. Unix comes with some compressing and decompressing utilitites gzip and gunzip(.gz) bzip2 and bunzip2 (.bz2) zip and unzip (.zip) Apart from compressing, you ll need to group a set of files into a single file, called archive, Sahaj Computer Solutions visit : projectsatsahaj.com 53

Compressing and Archiving Files The tar and zip commands can pack an entire directory structure into an archive. You can send this archive as a single file, either using ftp or as an email attachment to a remote machine. Sahaj Computer Solutions visit : projectsatsahaj.com 54

gzip and gunzip: Compressing and decompressing files Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension.gz, while keeping the same ownership modes, access and modification times. Gzip will only attempt to compress regular files. In particular, it will ignore symbolic links. Sahaj Computer Solutions visit : projectsatsahaj.com 55

gzip and gunzip: Compressing and decompressing files If the compressed file name is too long for its file system, gzip truncates it. Gzip attempts to truncate only the parts of the file name longer than 3 characters. Synopsis: gzip [ name... ] Example: gzip hello.html Sahaj Computer Solutions visit : projectsatsahaj.com 56

gzip and gunzip: Compressing and decompressing files To know the how much compression is applied on a file use l option. Example: gzip l hello.html.gz Sahaj Computer Solutions visit : projectsatsahaj.com 57

gzip and gunzip: Compressing and decompressing files gunzip can currently decompress files created by gzip, zip, compress, compress -H or pack. The detection of the input format is automatic. When using the first two formats, gunzip checks a 32 bit CRC. For pack, gunzip checks the uncompressed length. The standard compress format was not designed to allow consistency checks. However gunzip is sometimes able to detect a bad.gz file. Sahaj Computer Solutions visit : projectsatsahaj.com 58

gzip and gunzip: Compressing and decompressing files Synopsis: gunzip [ name...] Example: gunzip hello.html.gz or gzip d hello.html.gz Sahaj Computer Solutions visit : projectsatsahaj.com 59

tar: The archival program tar,is archiving program designed to store and extract files from an archive file known as a tarfile. A tarfile may be made on a tape drive, however, it is also common to write a tarfile to a normal file. The first argument to tar must be one of the options: Acdrtux, followed by any optional functions. The final arguments to tar are the names of the files or directories which should be archived. The use of a directory name always implies that the subdirectories below should be included in the archive. Sahaj Computer Solutions visit : projectsatsahaj.com 60

tar: The archival program A, --catenate, --concatenate append tar files to an archive -c, --create create a new archive -d, --diff, --compare find differences between archive and file system --delete delete from the archive (not for use on mag tapes!) Sahaj Computer Solutions visit : projectsatsahaj.com 61

tar: The archival program -r, --append append files to the end of an archive -t, --list list the contents of an archive -u, --update only append files that are newer than copy in archive -x, --extract, --get extract files from an archive Sahaj Computer Solutions visit : projectsatsahaj.com 62

zip and unzip Commands UNIX operating system provides the following two commands: zip - Command to package and compress (archive) files. unzip - Command to list, test and extract compressed files in a ZIP archive. Sahaj Computer Solutions visit : projectsatsahaj.com 63

zip and unzip Commands To create a zip file, enter: zip filename.zip input1.txt input2.txt resume.doc pic1.jpg To decompress a zip file in Unix, enter: unzip filename For example: unzip filename.zip Sahaj Computer Solutions visit : projectsatsahaj.com 64