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

Similar documents
Linux Shell Script. J. K. Mandal

LOG ON TO LINUX AND LOG OFF

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

Useful Unix Commands Cheat Sheet

Chapter 4. Unix Tutorial. Unix Shell

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

LING 408/508: Computational Techniques for Linguists. Lecture 5

Name: Tej. D. Shah Subject:CC-304 Linux Uni. Practical programme College :L.J. College Of Computer Application. Questions:

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 1

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

Week 2 Lecture 3. Unix

Shell Programming Overview

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Introduction to UNIX command-line II

Shells & Shell Programming (Part B)

Conditional Control Structures. Dr.T.Logeswari

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Part 1: Basic Commands/U3li3es

Basic Linux (Bash) Commands

Introduction to the UNIX command line

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

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

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

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

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

A Brief Introduction to the Linux Shell for Data Science

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

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

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Introduction to Linux Workshop 1

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

DAVE LIDDAMENT INTRODUCTION TO BASH

/smlcodes /smlcodes /smlcodes. Shell Scripting TUTORIAL. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation

Introduction to UNIX command-line

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

Files and Directories

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

Introduction: What is Unix?

COMS 6100 Class Notes 3

5/8/2012. Exploring Utilities Chapter 5

Shells and Shell Programming

CSE 15L Winter Midterm :) Review

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Shells and Shell Programming

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

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

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

Lecture 02 The Shell and Shell Scripting

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

9.2 Linux Essentials Exam Objectives

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.

Unix Introduction to UNIX

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

Linux shell programming for Raspberry Pi Users - 2

Shell Programming (bash)

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

Introduction of Linux

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

CSCI 2132 Software Development. Lecture 5: File Permissions

Files

Shell Programming (Part 2)

Assignment clarifications

Operating Systems. Copyleft 2005, Binnur Kurt

Linux environment. Graphical interface X-window + window manager. Text interface terminal + shell

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

Intro to Linux. this will open up a new terminal window for you is super convenient on the computers in the lab

The Unix Shell. Pipes and Filters

Common File System Commands

5/20/2007. Touring Essential Programs

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

CS Unix Tools & Scripting

COMP 4/6262: Programming UNIX

Linux Essentials Objectives Topics:

UNIX Essentials Featuring Solaris 10 Op System

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

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

Getting your department account

Shell programming. Introduction to Operating Systems

Computer Systems and Architecture

Bash scripting basics

Unix Handouts. Shantanu N Kulkarni

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Linux Command Line Primer. By: Scott Marshall

Command Line Interface The basics

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

Std: XI CHAPTER-3 LINUX

Basics. proper programmers write commands

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

National University of Computer and Emerging Sciences Operating System Lab - 02 Lab Manual

Bourne Shell Reference

Chapter-3. Introduction to Unix: Fundamental Commands

CSE Linux VM. For Microsoft Windows. Based on opensuse Leap 42.2

UNIX ASSIGNMENT 1 TYBCA (Sem:V)

Vi & Shell Scripting

The Unix Shell & Shell Scripts

A Brief Introduction to Unix

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

Transcription:

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

Table of contents Table of contents i 1 Unix commands 1 1.1 Unix commands cheatsheet........................................ 1 1.1.1 Basic file commands........................................ 2 1.1.2 Sort................................................. 2 1.1.3 Cut................................................. 3 1.1.4 Paste................................................ 3 1.1.5 Grep................................................. 3 1.1.6 Sed.................................................. 3 1.2 Multiple commands............................................. 3 1.2.1 Pipes................................................ 3 1.2.2 Run individual commands (;)................................... 4 1.3 Examples.................................................. 4 1.3.1 File examples............................................ 4 1.3.2 Sort example............................................ 5 1.3.3 Cut command............................................ 5 2 Shell scripting 6 2.1 Hello World................................................. 6 2.2 Execute commands from script...................................... 6 2.3 Shell Variables and input from user.................................... 7 2.3.1 Shell variable............................................ 7 2.3.2 Arithmetic............................................. 7 2.3.3 Single quote vs double quote................................... 7 2.3.4 User input.............................................. 8 2.4 Positional arguments............................................ 8 2.5 Mathematical operations.......................................... 9 2.6 if-else.................................................... 9 2.7 Case statement............................................... 10 2.8 For loop................................................... 10 2.9 While loop................................................. 11 i

Chapter 1 Unix commands Various useful unix-commands are listed in this chapter. Also, some of the examples of these commands are shown in the last section. Further, in next chapter, shell-scripting is discussed. 1.1 Unix commands cheatsheet Following are the list of various useful unix-commands, 1

1.1. Unix commands cheatsheet 1.1.1 Basic file commands Commands Details Basic opeations ls directory listing ls -a show hidden file ls -l show details for file ln file1 file2 create file2 (linked with file1). Changes will appear in both file ln -s file1 file2 create shortcut of file1 as file2 (soft link) mkdir dirname create directory dirname cd dirname go to dirname cd go to home directory pwd present working directory rm filename remove (delete) filename rm -r dirname remove directory dirname rm -f filename Force remove file filename rm -rf dirname Force remove directory dirname cp file1 file2 copy file1 to file2 cp -r dir1 dir2 copy dir1 to dir2 mv file1 file2 rename file1 to file2 touch filename create filename (if not exist) cat > filename add content to file (ctrl-c to exit) cat >> filename add content at the end of the filename cat filename show content of filename (all at once) more filename show content of filename (fit to screen with more option) head filename show first 10 lines of filename tail filename show last 10 lines of filename tail -f filename last 10 lines of the filename (useful for log files) command > filename write output of command in the filename (overwrite the fie command >> filname append the output at the end of file name wc filename (wc -l, -w, returns number of lines, word, character and filename. -c) chmod 777 filename read(4), write(2) and execute(1) permission to all [4+2+1=7] chmod [owner, group, others] filename 1.1.2 Sort Commands Details Sort sort filename sort the lines of the filename sort -u filename sort and eliminate duplicates sort -r filename reverse order sort sort file1 -o file2 sort data of file1 and save to file2 sort file1 > file2 sort -n filename sort data based on numeric value i.e. 12 > 4 sort -M fielname sort data with month name i.e. Jan, Feb, Mar etc. 2 PythonDSP

Chapter 1. Unix commands 1.1.3 Cut Commands Cut cut d"-" 1,4 filename cut -c3- filename cut -c-3-8 filename cut -c1-3,6-8,10- filename cut -d, -f3 filename cut -d, -f3,6 filename Details cut the 1st and 4th column of filename, where - is delimiter remove first 2 character from each line and printed the rest print character 3-8 from each line print character 1-3, skip 4, print 6-8, skip 9, print 10 to end from each line extact field-3 from each line with, as delimiter extact field-3-to-6 from each line with, as delimiter 1.1.4 Paste Commands Paste paste file1 file2 file3 paste -d, file1 file2 paste -s filename Details paste the lines of file2, then file3 beside the lines of file1 put comma at the end of each file content merge all lines of filename 1.1.5 Grep Commands grep grep pattern filenames grep [0-9] filenames grep -v pattern filenames grep -l pattern filenames grep -n pattern filenames Details print the lines with pattern in filenames print the lines from filenames which have numbers 0-9 (regular expression) print all lines which do not contain pattern print the filenames which have pattern print the line numbers as well 1.1.6 Sed Commands sed sed s/word1/word2/ filename sed s/word1/word2/ file1 > file2 mv file2 > file1 sed -n 2,4p filename sed -n /word1/p filename Details replace word1 with word2 in filename use two steps to make change permanent print line 2 to 4 of filename print lines which contain word1 in filename 1.2 Multiple commands 1.2.1 Pipes Pipes are used for sending output of one command to next command. # total number of files in the directory $ ls wc -l 1 3 Meher Krishna Patel

1.3. Examples # word counts for file testfile.txt $ cat testfile.txt wc 8 37 188 1.2.2 Run individual commands (;) Commands can be run separately from one line by separating them using semicolon, $ date; ls Thu Feb 23 15:24:09 NZDT 2017 testfile.txt 1.3 Examples 1.3.1 File examples $ ls $ touch testfile.txt # create testfile.txt $ ls testfile.txt $ cat testfile.txt $ cal > testfile.txt # write the calender to testfile.txt $ cat testfile.txt February 2017 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 $ date >> testfile.txt # append date at the end of testfile.txt $ cat testfile.txt February 2017 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Thu Feb 23 14:47:51 NZDT 2017 $ date > testfile.txt # replace all the content of testfile with date $ cat testfile.txt Thu Feb 23 14:48:09 NZDT 2017 $ cal > testfile.txt # write calender to file $ wc testfile.txt # count lines, word and character in testfile.txt 8 37 188 testfile.txt # other options for wc $ wc -l testfile.txt # lines 8 testfile.txt $ wc -w testfile.txt # words 4 PythonDSP

Chapter 1. Unix commands 37 testfile.txt $ wc -c testfile.txt # characters 188 testfile.txt 1.3.2 Sort example # create a file $ cat > test Tiger Lion Cat Eagle ^C # see the content $ cat test Tiger Lion Cat Eagle # sort the content $ sort test Cat Eagle Lion Tiger dhriti@meher ~/Desktop $ 1.3.3 Cut command $ cat > test first last age id Meher Patel 20 101 Krishna patel 30 102 Tom Jerry 43 103 ^C $ cut -d" " -f 1,3,4 test first age id Meher 20 101 Krishna 30 102 Tom 43 103 5 Meher Krishna Patel

Chapter 2 Shell scripting In this section, Unix-shell-scripting is discussed. 2.1 Hello World Files containing the shell scripts are usually saved as filename.sh. # helloworld.sh echo "Hello World" echo Hello World Again The bash or sh command can be used to execute the above file. Further, we can change the permission of the file using chmod command and execute it directly as shown below, $ bash helloworld.sh Hello World Hello World Again $ sh helloworld.sh Hello World Hello World Again $ chmod 777 helloworld.sh $./helloworld.sh Hello World Hello World Again 2.2 Execute commands from script We can store various commands in a file and then run all those commands by executing the file. This is quite useful for repetitive tasks, # run_commands.sh echo 'list of files' ls echo 'date' date 6

Chapter 2. Shell scripting $ sh run_commands.sh list of files helloworld.sh run_commands.sh testfile.txt date Mon Feb 27 07:10:33 NZDT 2017 2.3 Shell Variables and input from user 2.3.1 Shell variable In below code, n is the variable; and $ sign is used to display the value of the variable, # no spaces between variable and input n='tiger' echo $n Output is shown below, $ echo test.sh Tiger 2.3.2 Arithmetic For mathematical operation, $((expression)) is used as shown in following example, i=1 j=$((i+1)) echo $j Output of above code is shown below, $ bash test.sh 2 2.3.3 Single quote vs double quote Note the differences between the outputs with single and double quote, i=1 j=$((i+1)) echo '$j' echo "$j" Below are the outputs, 7 Meher Krishna Patel

2.4. Positional arguments $ bash test.sh $j 2 2.3.4 User input read is used to get input from user, # run_commands.sh echo enter your name read name echo Hello $name, How are you? $ bash run_commands.sh enter your name Meher Hello Meher, How are you? 2.4 Positional arguments We can read some arguments while executing the code as well. $1 and $2 are the positional arguments provided by the user, # $1 and $2 are the arguments provided while executing the file echo $1 $2 $ bash test.sh 12 12 $ bash test.sh 12 13 12 13 $ bash test.sh 12 13 14 12 13 set command can used to convert an string to positional parameter as shown below, $ set Meher Krishna Patel $ echo $1 Meher $ echo $2 Krishna $ echo $3 Patel $ echo $4 ( ) can be used to save the outputs of the command as positional parameter, $ cat > test How are you? ^C $ cat test How are you? $ set `cat test` $ echo $1 8 PythonDSP

Chapter 2. Shell scripting How $ echo $2 are 2.5 Mathematical operations expr, $, $((expression)) or bc commands can be used for mathematical operation as shown below. Note: There is no spaces between x=3. x=4 y=2 echo $x + $y echo `expr $x + $y` # ` ` is required with expr for maths operations i=$(expr $x + 1) echo $i z=`echo $x \* $y bc` # * has special meaning, hence \* echo $z d=$(($x / $y)) echo $d # using double bracket $((expression)) 2.6 if-else Note that [ ] are used for condition-check operations (not required for other cases). Also, look for the spaces after [ and before ]. echo enter a number read x if [ $x -gt 4 ] then echo 'greater than 4' elif [ $x -eq 4 ] then echo 'equal to 4' else echo 'less then 4' fi Following is the output of above code, $ sh test.sh enter a number 6 greater than 4 9 Meher Krishna Patel

2.7. Case statement Below code check whether the file exists in the directory or not. Similarly -d, -r and -w can be used for checking the directory, read and write permission respectively echo enter a filename read x if [ -f $x ] then echo 'yes, there is file' else echo 'no, there is no such file' fi Following is the output, $ sh test.sh enter a filename test.sh yes, there is file 2.7 Case statement Use double quote with case statement, # case '$1' # do not use single quote case "$1" in 0) echo zero;; 1) echo one;; *) echo 'neither 0 nor 1';; esac Following are the outputs, $ bash test.sh 1 one $ bash test.sh 0 zero $ bash test.sh 3 neither 0 nor 1 2.8 For loop Below code loop over all the files of the directory and print the result, for files in * do echo $files done Following is the output of above code, which listed all the files in the current directory, 10 PythonDSP

Chapter 2. Shell scripting $ bash test.sh helloworld.sh meher.txt month.txt m.txt run_commands.sh surname.txt temp.txt test.sh Following is the another example, for files in 1 2 3 do echo $files done # outputs # $ bash test.sh # 1 # 2 # 3 2.9 While loop Following are the example of while loop, i=1 while [ "$i" -lt 4 ] do echo $i i=$(($i+1)) done # outputs # $ bash test.sh # 1 # 2 # 3 11 Meher Krishna Patel