Basic Survival UNIX.

Similar documents
Working with Basic Linux. Daniel Balagué

Introduction to Unix: Fundamental Commands

Introduction to Linux

Chapter-3. Introduction to Unix: Fundamental Commands

commandname flags arguments

UNIX File Hierarchy: Structure and Commands

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

Unix Tutorial Haverford Astronomy 2014/2015

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

CSC209. Software Tools and Systems Programming.

Unix Filesystem. January 26 th, 2004 Class Meeting 2

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

Introduction to the Linux Command Line

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

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

Essential Linux Shell Commands

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]

INSE Lab 1 Introduction to UNIX Fall 2017

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

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

Overview of the UNIX File System

Overview of the UNIX File System. Navigating and Viewing Directories

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

Oxford University Computing Services. Getting Started with Unix

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

Using the Zoo Workstations

A Brief Introduction to the Linux Shell for Data Science

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

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

Unix background. COMP9021, Session 2, Using the Terminal application, open an x-term window. You type your commands in an x-term window.

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

Introduction. SSH Secure Shell Client 1

Linux File System and Basic Commands

Linux Command Line Primer. By: Scott Marshall

Hitchhiker s Guide to VLSI Design with Cadence & Synopsys

Scripting Languages Course 1. Diana Trandabăț

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

CSC209. Software Tools and Systems Programming.

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

Common UNIX Commands. Unix. User Interfaces. Unix Commands Winter COMP 1270 Computer Usage II 9-1. Using UNIX. Unix has a command line interface

The Directory Structure

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

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

CS Fundamentals of Programming II Fall Very Basic 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.

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.

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

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

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

SECTION -C. Getting Started with UNIX

Chapter 4. Unix Tutorial. Unix Shell

You will automatically be in your user (home) directory when you login.

Week 2 Lecture 3. Unix

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.

Exploring UNIX: Session 3

Practical Session 0 Introduction to Linux

Lab Working with Linux Command Line

Introduction of Linux

CS246 Spring14 Programming Paradigm Notes on Linux

CHE3935. Lecture 1. Introduction to Linux

h/w m/c Kernel shell Application s/w user

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

Overview LEARN. History of Linux Linux Architecture Linux File System Linux Access Linux Commands File Permission Editors Conclusion and Questions

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

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

Session 1: Accessing MUGrid and Command Line Basics

UNIX Quick Reference

CS/CIS 249 SP18 - Intro to Information Security

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

CS197U: A Hands on Introduction to Unix

Essential Unix and Linux! Perl for Bioinformatics, ! F. Pineda

Mills HPC Tutorial Series. Linux Basics I

Operating System Interaction via bash

1 Installation (briefly)

Computer Architecture Lab 1 (Starting with Linux)

Filesystem and common commands

Linux Command Line Interface. December 27, 2017

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Practical 4. Linux Commands: Working with Directories

Introduction to Linux Workshop 1

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

Introduction: What is Unix?

Std: XI CHAPTER-3 LINUX

Unix Introduction to UNIX

GNU/Linux Course Lesson 1. Puria Nafisi

Outline. Structure of a UNIX command

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering

Introduction. File System. Note. Achtung!

UTA Tech Orientation Spring 2019

Introduction to Unix - Lab Exercise 0

Physics REU Unix Tutorial

Parts of this tutorial has been adapted from M. Stonebank s UNIX Tutorial for Beginners (

Basic UNIX commands. HORT Lab 2 Instructor: Kranthi Varala

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

UNIX Essentials Featuring Solaris 10 Op System

CS CS Tutorial 2 2 Winter 2018

Linux/Cygwin Practice Computer Architecture

Introduction to Linux. Fundamentals of Computer Science

Some Linux (Unix) Commands that might help you in ENSC351

Transcription:

Basic Survival UNIX Many Unix based operating systems make available a Graphical User Interface for the sake of providing an easy way for less experienced users to work with the system. Some examples are Mac OS X, Gnome and KDE for Linux, and CDE for Solaris. (There are many other window managers available for Linux and Solaris.) However, the real power and flexibility of Unix comes from the command shell and the many programs that operate from it. This document describes the Unix file system and a set of useful commands that will get you started with Unix. Finding out about Unix commands For more information about Unix commands, see section 1 of the Unix man pages. ( Man is short for manual. ) There are two ways to view this information. The sure-fire way, accessible under any circumstances where you have access to a Unix command prompt, is to type man command-name at the prompt, where command-name is the name of the command you are interested in. A good starting point in your education on Unix commands is typing man man to find out how the man command works. If you just have a general idea of the job you want to do, type apropos keyword at the prompt. This will give you a list of command names relevant to the keyword and the section of the manual where they are described. (Admittedly, this list is often quite long.) You can also see a general discussion of user commands, along with a brief synopsis of all the available commands, by typing man -s 1 Intro at the prompt. (The capital I is significant here.) There is a web interface to the Unix man pages available locally, which includes a search function that is a bit more useful than apropos. You can access this version of the man pages at URL http://cramer.richmond.edu:8888/ab2/coll.40.6/@ab2collview? As before, section 1 of the manual contains the information about application programs and user utilities. UNIX Files UNIX uses the familiar tree-like organization for its file system. There is a directory called / at the base of the tree, known as the root directory. It contains a number of special files. A directory is considered a file. In the root directory, /, one finds the special directories bin, etc, dev, and usr. Their full path names are /bin, /etc, /dev, and /usr respectively. /bin contains many of the files that are executed when a command is typed. /etc contains a number of database or table files for use by the system. /dev contains the device independent interface files used for I/O. /usr contains the portion of the tree that has the directories of utilities and library files. In the directory /usr one finds the directory lib whose full path name is /usr/lib. Basic Survival UNIX 1

Suppose a user with login id csu2t has a home directory /home/mcs-stu/csu2t. Logging in to a Unix system will result in this home directory being the current working directory, which is the starting place for commands that work with files or directories. If the user csu2t then creates a file unix.doc, the file s full name will be /home/ MCS-Stu/csu2t/unix.doc. The next section will discuss a number of commands that allow you to create new directories and navigate among directories that belong to you. At the, Mathematics and Computer Science student users home directories will be found in a directory with a name like /home/mcs-stu2. The system administrator is responsible for determining the location of home directories. UNIX has file names that are case sensitive: data and DATA and Data are different names. File names can be arbitrarily long and include letters, digits, and characters like underscore, hyphen, period. More recent versions of the Unix file system have allowed spaces in file names, but note that there are some circumstances where this poses difficulties for some commands that operate on lists of file names. It is safest to substitute an underscore or dash where you would normally put a space in a Unix file name. Please note that when a user transfers UNIX files to MSDOS diskettes, long filenames can be truncated. So this_is_a_long.name will become this~1.nam (or something similar.) In the Unix file system, every directory contains two special files with unusual names:. and.. (often pronounced dot and dot dot ). The file. is an alias for the current directory, so referring to./foo.txt is the same thing as referring to foo.txt. The file.. is an alias for the parent of the current directory, and can be used to refer to files in directories above the current one in the tree structure. So,../foo.txt refers to a file one level above the current directory. UNIX Commands All computer systems allow the user to name, print, and view files. In multi-user systems like UNIX, files are assigned permissions. The owner typically has all permissions by default, but other users may be allows to read the file or execute it. One of the commands described below is used to alter permissions on files you own. The commands below can be given in a terminal window in the graphical user interface or used when the user connects to the system remotely using ssh or telnet and needs to perform basic tasks. When connecting remotely to one of the computers, no graphical interface is provided. Many commands take some number of flags that alter their behavior in various ways. These flags are usually preceded by a - and appear after the command name. You will often see commands referred to with a few characters in parentheses after the command name. This indicates the section of the Unix manual where the man page for the command or file in question will be found. Many files operate on a file or list of files. In these cases, you can save a lot of typing by taking advantage of file name completion. Type the first few characters of the filename (enough to distinguish the file you want from similarly named files) then type the Esc key twice. The shell will fill in the rest of the filename (or as much of the name as it can unambiguously figure out) for you. Other shells, such as the bash shell commonly found on Basic Survival UNIX 2

Linux systems, use different key combinations, mainly using the tab key to request completion. If a command (or a program you have written) is taking longer than you think it should, you can terminate the program s execution by typing control-c. Some useful commands To view a text file named dumb.file one page at a time (Note - don t try this on a binary file!) more dumb.file To dump the complete contents of a file without pausing on each page cat dumb.file Cat is an abbreviation of concatenate, the idea being that this command concatenates the characters from the file onto the end of standard output. To print a text file named project1.java (Once again, be sure not to try this on a binary file!) genscript project1.java If you don t find genscript, try enscript instead. To print the file above but save paper by having two pages per sheet genscript -2r project1.java Rename the file dumb to the file old_dumb ( mv is an abbreviation for move ) mv dumb old_dumb Copy the file dumb to dumb.bak cp dumb dumb.bak Make a new subdirectory stuff mkdir stuff Change to the subdirectory stuff cd stuff Move up to the parent directory cd.. Move to your home directory cd Basic Survival UNIX 3

Print the path of the current working directory pwd As you attempt more complex programming tasks, you should get into the habit of creating a new directory for each project you work on. This way, all the files related to a project such as program specifications, makefiles (described elsewhere) or documentation are stored in the folder with the program source files they refer to. List the files and their permissions and sizes in the current directory ls -l (That is an ell, not a one. Without -l only the names of the files are listed.) Permissions and their meaning: A typical set of permissions for a non-executable file are -rw-r--r-- which means the file is an ordinary file (leftmost symbol is -), not a directory or special file. The next three symbols from the left (rw-) indicate that the owner can read (r), write (w), but not execute(-) the file. Only program files are executable! The next three characters and the rightmost three are both r--, indicating that others can read the file but not make any changes to it. To check the size of a file joe.gif ls -l joe.gif You will get something like -rw-r--r-- 1 kent other 21093 Dec 6 1994 joe.gif where the 21093 says the file takes up 21,093 bytes or about 21 kilobytes. (Note that this doesn t work for directories.) An alternative is to type du -a joe.gif which returns the number of disk blocks used. How much disk space a disk block takes up depends on the operating system. At this writing, our version of Solaris has a disk block size of 1024 bytes, so du reports the size in kilobytes. To check the space used by an entire directory and subdirectory, du -s directory_name This returns the number of disk block used, each of which is.5 kilobytes in size. To delete a file dumb rm dumb Basic Survival UNIX 4

To delete a file without a confirmation (be careful) /bin/rm dumb To delete all files in the current directory with confirmation rm * Be extremely careful using this command, otherwise, you can end up deleting many files that you didn t really mean to. To delete a directory stuff and all its files and subdirectories without confirmation (-r means recursive ) /bin/rm -r stuff This can also be extremely dangerous, so be sure that you really want to delete everything below the specified directory in your directory tree before issuing this command. Permissions: The following are examples of changing the permissions or access rights to your files and directories using the chmod command. By default, your top level directory is readable by all users, but any files and directories you create there are not accessible to anyone but you. You will need to use chmod if you ever want to allow other users access to your files. To make a directory public_html readable by others but not changeable chmod 755 public_html To make a file home.html readable and writable by the owner, but only readable by others chmod 644 home.html To make all files that end in.html readable, chmod 644 *.html To make a file local.html unreadable by anyone except the owner chmod 600 local.html Permissions are a bit tricky, but they are also important. There are symbolic ways of altering permissions with chmod that you may find a bit more intuitive than the octal bitflags we have demonstrated here. You can, of course, find the details on the chmod(1) man page. Find strings within files grep string [file-list] Basic Survival UNIX 5

The command name grep stands for something like grab regular expression. The purpose is to search the contents of a list of files for strings that match a pattern given by string and to print the line containing the string for each time the string is found. For example, imagine you have a large Java project and you want to know which of your classes makes use of the StringBuffer class. You would type grep StringBuffer *.java Remember that grep works only on files in the current directory, and it works only on plain text files. Also, if no file list is provided, grep processes input on standard input, so it can be used as part of a pipeline as described below. Find files with certain attributes As your directory structure grows, things can become lost. Suppose you are working on a new program that needs a hash table, and you know you ve written hash.java at some point, but you don t remember where the file is. The find command can help. The general syntax is find pathname expression where expression is composed of various operations and logical connectives. The format of these expressions is given on the man page for find(1). To get back to our example, in your home directory, type find. -name hash.java -print The. says to start looking in the current directory. Find operates recursively on subdirectories, so this will search your entire account if you are in your home directory when you start. The expression -name hash.java will be true when a file with the name hash.java is found, and -print says to print out the pathname of the file when found. The find command is very powerful and useful. Other attributes that you can search on include modification time, file size, file type, permissions, ownership, etc. You can also cause arbitrary commands to be executed when the expression is matched as well. The find(1) man page gives several examples of these capabilities. I/O Redirection and Pipelines One of the design strategies of the unix user commands is to isolate useful behaviors in commands that do one small job, and then provide ways to link the commands together to tackle more complex situations. To this end, all jobs run from the command line will take input from something called standard input and will produce output to something called standard output. In the case of one command run by itself from the command line, any required input will be expected from the keyboard and output will go to the terminal window where the command was run. The shell also provides ways to redirect the input and Basic Survival UNIX 6

output for commands to other sources or destinations. For example, suppose you had a directory containing over a thousand files, and you wanted to create a new file that had a list of all the existing files. The ls command, discussed earlier produces the list you want, and to get the list into a file, you can redirect standard output like so: ls > files.txt When this command completes, the output of the ls command will be in the newly created file called files.txt. (If you want to append the output of a program to an existing file, use >> instead of > for the redirection.) On the other hand, suppose you re writing an interactive program that has fairly complex user input. During testing, you will wind up typing in various input sequences over and over again to test various aspects of the program. This gets tedious, so it makes sense to put the input sequence for each test case into a file and then somehow get the program to take its input from these files rather than the keyboard. Suppose your program is called grumble and your first input data file is called test1.dat. Typing grumble < test1.dat does exactly what we described. Back to our first example. Suppose instead that you just wanted to look at the list of files, but with so many, they zip past on the screen before you can get a look at them. Here, our old friend the more command can help. Remember that more files.txt will display the contents of files.txt on the screen one page at a time. You hit the space bar to advance to the next page. In addition to accepting a filename as a command line argument as we have shown here, more is written to behave as a filter, reading from standard input and sending its output to standard output. In this case, we can substitute the output of one command for the input from the file as follows: ls more ( is the vertical bar character, usually found on the key with the \ character.) Now, instead of appearing directly on the screen, the output of the ls command is piped into the more command, and more does its job of showing one page at a time on this input stream rather than on the contents of the file. Output redirection can be used as a quick and dirty way to type some text into a file. To do this, type the command cat > quick.txt then just type whatever text you want in the file (you ll notice that you don t get the Unix prompt back after this command). When you re done, type control-d, and your text is in quck.txt. How does that work? Recall that most commands take input from the keyboard (standard input) if no filename argument is supplied on the command line. Further, recall that cat s job is to take its input and pump it to standard output, which in this case has been redirected to the file quick.txt. So, you re just putting whatever you type on the keyboard straight into the file. The control-d is how you type an end-of-file mark on the keyboard. Basic Survival UNIX 7

Command aliases You may find that you use certain combinations of command flags quite often, and would like a way of reducing the amount of typing you do in those cases. For example, using the ls -l version of the ls command to do the detailed listing of file information is very handy. If you type alias ll= ls -l then for the remainder of the session, the shell will check each command you type and substitute ls -l whenever it finds ll at the beginning of a command line. The rest of the line will be unaffected. While this is useful, you don t want to have to type in your aliases every time you start a new shell. Fortunately, you don t have to. The shell (in our case, ksh, otherwise know as the Korn shell) always executes the commands from a startup file before it gives you the first prompt. One of the things this startup file can do is set up aliases. Have a look at the file.kshrc in your home directory to see the set of aliases we have set up initially for you. You can add your own aliases to this file and have access to them in any shell session you start up. If you are used to some other command line environment, such as the Windows command prompt, you can use this facility to make yourself feel more at home. The alias command is not really a command, but instead is a shell built-in. The difference is that a command is really a separate program with its own executable, while shell bulletins are essentially functions that are built in to the shell program you are running. As an upshot of this, you won t find a man page for alias. Instead, you ll find the description of alias (and many other shell built-ins) in the ksh(1) man page. Summary of Commands alias cat cd chmod cp du find genscript grep ksh ls mkdir more mv (p. 8) create a command alias (p. 3) view the entire contents of a text file (p. 3) change directory - make another directory the working directory (p. 5) change mode - change the protection mode of a file or set of files (p. 3) copy - make a copy of a file (p. 4) disk usage - display the disk space used by a file or set of files (p. 6) find a file with specified characteristics (p. 3) print a text file (p. 5) get regular expression - find instances of a string in text files (p. 8) The Korn shell command processor (p. 4) display a list of the files in a directory (p. 3) create a new directory (p. 3) view a text file one page at a time (p. 3) move - rename a file Basic Survival UNIX 8

pwd (p. 4) print working directory - show the path of the current working directory rm (p. 4) remove - delete a file or set of files from the file system > (p. 7) redirect standard output to a file, replacing any existing file with the specified name >> (p. 7) redirect standard output to a file, appending to existing contents < (p. 7) redirect standard input from a file (p. 7) pipe the output from one command to the input of another Basic Survival UNIX 9