Advanced Linux Commands & Shell Scripting

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

Introduction: What is Unix?

Lab Working with Linux Command Line

Bioinformatics? Reads, assembly, annotation, comparative genomics and a bit of phylogeny.

The Unix Shell. Pipes and Filters

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

DATA 301 Introduction to Data Analytics Command Line. Dr. Ramon Lawrence University of British Columbia Okanagan

Why learn the Command Line? The command line is the text interface to the computer. DATA 301 Introduction to Data Analytics Command Line

Introduction To. Barry Grant

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

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

Recap From Last Time:

BGGN 213 Working with UNIX Barry Grant

Lab 2: Linux/Unix shell

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Working With Unix. Scott A. Handley* September 15, *Adapted from UNIX introduction material created by Dr. Julian Catchen

Chapter 4. Unix Tutorial. Unix Shell

UNIX Essentials Featuring Solaris 10 Op System

Linux Command Line Primer. By: Scott Marshall

Basic Linux (Bash) Commands

Filesystem and common commands

Lec 1 add-on: Linux Intro

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

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

Linux Bootcamp Fall 2015

Arkansas High Performance Computing Center at the University of Arkansas

Introduction to the shell Part II

Command Line Interface The basics

Recap From Last Time: Setup Checklist BGGN 213. Todays Menu. Introduction to UNIX.

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

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

Linux for Beginners. Windows users should download putty or bitvise:

CSCI 2132 Software Development. Lecture 4: Files and Directories

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

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

Useful Unix Commands Cheat Sheet

Introduction in Unix. Linus Torvalds Ken Thompson & Dennis Ritchie

Mills HPC Tutorial Series. Linux Basics I

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

EECS2301. Lab 1 Winter 2016

A Brief Introduction to the Linux Shell for Data Science

Linux Command Line Interface. December 27, 2017

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Introduction to UNIX. SURF Research Boot Camp April Jeroen Engelberts Consultant Supercomputing

Introduction of Linux

Files and Directories

Introduction to Linux

11/10/2011. Directory Structures (continued)

Cloud Computing and Unix: An Introduction. Dr. Sophie Shaw University of Aberdeen, UK

The Unix Shell & Shell Scripts

Cloud Computing and Unix: An Introduction. Dr. Sophie Shaw University of Aberdeen, UK

Basic UNIX commands. HORT Lab 2 Instructor: Kranthi Varala

Connecting to ICS Server, Shell, Vim CS238P Operating Systems fall 18

5/20/2007. Touring Essential Programs

Scripting Languages Course 1. Diana Trandabăț

Introduction to UNIX command-line

Introduction. File System. Note. Achtung!

EECS Software Tools. Lab 2 Tutorial: Introduction to UNIX/Linux. Tilemachos Pechlivanoglou

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

The Shell. EOAS Software Carpentry Workshop. September 20th, 2016

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

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

Short Read Sequencing Analysis Workshop

The Directory Structure

Introduction to Linux

Introduction to Linux

CSCI 2132 Software Development. Lecture 5: File Permissions

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

Introduction to Linux Part 1. Anita Orendt and Wim Cardoen Center for High Performance Computing 24 May 2017

Basics. proper programmers write commands

Chapter-3. Introduction to Unix: Fundamental Commands

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

UNIX. The Very 10 Short Howto for beginners. Soon-Hyung Yook. March 27, Soon-Hyung Yook UNIX March 27, / 29

COMS 6100 Class Notes 3

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Introduction to Linux. Roman Cheplyaka

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

CSC 2500: Unix Lab Fall 2016

INTRODUCTION TO BIOINFORMATICS

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Introduction to UNIX Command Line

Unix basics exercise MBV-INFX410

Introduction to Linux Workshop 1

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

Shells and Shell Programming

Introduction to Unix: Fundamental Commands

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

Exercise 1: Basic Tools

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.

Getting your department account

Wildcards and Regular Expressions

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 1

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

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

Review of Fundamentals

Introduction to UNIX command-line II

XSEDE Scholars Program Introduction to the Linux Environment. Tips and Tricks for Linux Users John Lockman III June 5 th, 2012

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

Introduction to the UNIX command line

Transcription:

Advanced Linux Commands & Shell Scripting Advanced Genomics & Bioinformatics Workshop James Oguya Nairobi, Kenya August, 2016

Man pages Most Linux commands are shipped with their reference manuals To view a command s manual, use man command & the command as an argument; e.g. $ man ls $ man cat Since man itself is a command, we can view also view it s manual $ man man man uses the same interface as less ; Use q to quit/close man

Listing Use ls command to list all files & directories in the current directory doesn t include hidden files & directories, by default Common pattern when using the command line is changing directories using cd & then immediately using ls to view the contents of the directory. ls has 2 output formats: Normal/short list format $ ls Long list format $ ls -l

Listing To view hidden files & directories, use -a option with less $ ls -a $ ls -la ls command can also be used to check if a file/directory exists $ ls file99.txt $ ls ~ $ ls ~/earth

Copying Use cp to copy a file or directory to the same or different directory. cp source destination Copying a file within the same directory $ cp file1.txt file2.txt Copying a file to a directory $ mkdir files $ cp file2.txt files/ Copying multiple files to another directory $ cp file1.txt file2.txt files/ Use -r option to copy a directory $ cp -r files/ new-files/

Renaming Use mv command to rename files & directories mv source destination To rename a file $ mv file1.txt file3.txt To move a file into a different directory $ mv file3.txt new-files/ To rename a directory $ mv new-files/ old-files/ To move a directory into another directory $ mv old-files/ files/ $ ls files/

Deleting Use rm command to delete files & directories rm file Take care when using rm command because there is NO Recycle Bin/Trash Use -r to recursively delete directories and their contents To delete a file $ rm file2.txt Delete a folder $ rm -rf files/old-files

Globbing Globbing(filename expansion) recognizes and expands wildcard characters( * and? ), character lists in square brackets & other special characters(e.g. ^ for negating the sense of a match) wildcard characters will not match filenames that start with a dot(. ) e.g..bashrc A few examples List all files beginning with file $ ls file* List all files ending with.txt $ ls *.txt

Redirecting and Appending Everything you type & read on the command line can be considered as character streams or just streams in general 3 types of streams: standard output(stdout): normal command output displayed on the screen standard error(stderr): displays errors from a command or program; similar to stdout Standard input(stdin): standard input stream for a command or program To redirect the stdout(output of a command) to a file $ echo Current date and time is > today.txt $ cat today.txt

Redirecting and Appending To append the stderr(output of a command) to a file $ date >> today.txt $ cat today.txt To use a file as stdin stream to a command $ cat < today.txt To write stderr output to a file $ ls files/ file99.txt 2> stderr To write stdout & stderr to different files $ ls files/ file99.txt 1> output 2> error $ cat output $ cat error

Redirecting and Appending To combine both stdout & stderr to one file $ ls files/ file99.txt &> streams $ cat streams

Piping A form of stream redirection whereby the output of a command is used as an input the other command The pipe operator( ) is placed in between the 2 commands command1 command2 command1 command2 command3 command4 A few examples: $ cat streams wc -l $ ls -l less $ cat streams grep file wc -l

Downloading from the internet GNU wget is a powerful non-interactive download manager in Linux non-interactive: can run in the background even when you're logged out Basic wget usage wget URL Downloading a file: $ wget http://hpc.ilri.cgiar.org/~joguya/gene-description.txt Download a file & save it with a different name $ wget http://hpc.ilri.cgiar.org/~joguya/gene-description.txt -O gene-desc.txt

Grepping Use grep command to search for a substring in a file grep substring file To ignore case distinctions in both substring & input file, use -i option A few examples: $ grep date today.txt $ grep protein_coding gene-description.txt wc -l

Making heads and tails of your files Two complementary commands for inspecting file contents head : shows the beginning(head) of a file tail : shows the end(tail) of a file You can also use stdout stream with head & tail commanda head commandb tail To view the first 5 lines of a file $ head -n5 gene-description.txt To view the last 5 lines of a file $ head -n5 gene-description.txt

Your first shell script A shell script is a text with a list of commands. Shell scripts are good for automating tasks you frequently do or for running batch jobs Using nano (text editor), we ll create a new file named script1.sh with the following contents: echo "Date and time is:" date echo "Your current directory is:" pwd

Your first shell script Run script1.sh shell script $ sh script1.sh It should output something similar this: Date and time is: Mon Aug 8 12:30:54 EAT 2016 Your current directory is: /home/user1

Your second shell script Using nano (text editor), we ll create another file named script2.sh with the following contents: DATE=$(date) PWD=$(pwd) echo "Date and time is: $DATE" echo "Your current directory is: $PWD"

Your second shell script Two new concepts: variables: a symbolic name for to hold data e.g. numbers, text, e.t.c. command substitution: starts a subshell to run the named command; It's recommended to use $(command) instead of `command`

Loops and sequences A loop is a block of code that iterates a list of commands as long as the loop control condition is true. Basic looping construct for arg in [list] do command(s)... done During each pass through the loop, arg takes on the value of each successive item in the list So, why do we need loops?

Loops and sequences Using nano (text editor), we ll create another file named script3.sh with the following contents: for num in 1 2 3 do echo we are on number: $num done Run script3.sh shell script $ sh script3.sh

Sequences Let s create another file named script4.sh with the following contents: for num in {1..3} do echo we are on number: $num done Run script4.sh shell script $ sh script4.sh

More sequences Same results but using a command substitution & seq command Create another file named script4.sh with the following contents: for num in $(seq 1 3) do echo we are on number: $num done Run script5.sh shell script $ sh script5.sh Several ways to achieve the same thing!!

Questions?