30
07/03/22 Introduction to the Shell – Session 1 1 .0.0.3 – Introduction to the Shell 2.0.0.3. 1 Introduction to the Shell – Session 1 · Getting started at a shell prompt · Accessing a UNIX system · Commands in UNIX · Working with files and directories · Navigating through the file system · Moving yourself and your files around · Viewing and editing files · Common editors · Viewing files · A few useful file and command tools and tips

2.0.0.3.1 Introduction to the Shell – Session 1

  • Upload
    kristy

  • View
    23

  • Download
    0

Embed Size (px)

DESCRIPTION

2.0.0.3.1 Introduction to the Shell – Session 1. Getting started at a shell prompt Accessing a UNIX system Commands in UNIX Working with files and directories Navigating through the file system Moving yourself and your files around Viewing and editing files Common editors Viewing files - PowerPoint PPT Presentation

Citation preview

Page 1: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 1

2.0.0.3 – Introduction to the Shell

2.0.0.3.1Introduction to the Shell – Session 1

· Getting started at a shell prompt· Accessing a UNIX system· Commands in UNIX

· Working with files and directories· Navigating through the file

system· Moving yourself and your files

around· Viewing and editing files

· Common editors· Viewing files

· A few useful file and command tools and tips

Page 2: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 2

2.0.0.3 – Introduction to the Shell

What is a Shell?

· Shell prompt = command prompt = command line· Text interface that allows user to type commands which the OS then executes

· A more direct (and powerful) way to interact with your computer

Linux command line

DOS command line

Page 3: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 3

2.0.0.3 – Introduction to the Shell

Accessing a UNIX / Linux Shell Prompt

· If you are at the GSC, your user account allows you to log on to any Linux machine on our network

· Other ways to access UNIX/Linux:· Linux server set up by Sheldon: Access from other Linux systems or using eg. Putty from Windows

· UBC Roadmap to Computing (http://www.roadmap.ubc.ca/): free access to tutorials and UNIX shell

· Canadian Bioinformatics Resource (http://cbr-rbc.nrc-cnrc.gc.ca/): free academic access to bioinformatics tools and databases, web and UNIX command-line

· Cygwin (http://www.cygwin.com/): a free Linux emulator for computers running Windows

· Linux (http://www.linux.org/) : install Linux on your own computer – Linux itself is free to download and install; versions with full installation instructions and support are also available commercially

Linux and UNIXLinux is a UNIX-like operating system, which is open-source (free, code can be contributed by anyone). The command line and command line tools for UNIX systems and Linux are basically identical.

Page 4: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 4

2.0.0.3 – Introduction to the Shell

Logging In· To access a shell prompt you will have to log in to a UNIX system using a username/password

· How you do this will depend on what the system is· Once you are logged in to one computer, you can connect to a different computer on the same network using rlogin (remote login) or ssh (secure shell) – ssh is better as it encrypts any information you send

· Get back out using exit (if you opened another shell) or logout (if this is the shell you started with)

Page 5: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 5

2.0.0.3 – Introduction to the Shell

Basic UNIX Command Structure

· Command:· Any UNIX command or

program to run

· Options:· Modifiers of the

command, usually preceded by –

· Files or other things:· When appropriate, a file

name, string, etc. that the command will act on

command <options> <files or things to operate on>

ls –la README

CommandOptions

File

Spaces

My prompt just says >You can determine what the text at the prompt is. It can be the date, current directory, or your favorite saying. Details in session 2.0.0.3.2.

Page 6: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 6

2.0.0.3 – Introduction to the Shell

UNIX Help: man, info and apropos

· Look up usage of a command with

· Accesses the manual pages for the given command

· To exit man, type q (man uses less to display)

· Also try <command> --help

· To find a command you can try

but often the command you really want isn’t listed!

man

apropos

info

Page 7: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 7

2.0.0.3 – Introduction to the Shell

The UNIX File System

· When you log in, you will be in your home directory, in my case

· Files are arranged in directory trees:

/home/egarland

home

Parent directory: next directory up in tree (directory which contains current directory)

Child directory: all directories down in tree (directories which are contained by current directory)

Watch out for Caps lock!UNIX files are CASE SENSITIVE, so eg. there is no /Home/egarland, and you can have a file “myfile” AND a file “Myfile”. Many people just avoid using capitals altogether.

Page 8: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 8

2.0.0.3 – Introduction to the Shell

Navigating Through Directories: cd and pwd

· Check current directory with

· Change directories with

· Specify directory:

· cd only will go to your home directory

· cd – will go to previous directory

pwd

cd

cd <directory name>

What are all those dots?Current directory is called “.”Parent directory is called “..”The home directory is also called “~”

Page 9: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 9

2.0.0.3 – Introduction to the Shell

Absolute and Relative Pathnames

· You have to specify exactly where the shell should look for a directory

home

egarland

unix_intro

dir1 dir2

Absolute pathname: How to get to a directory or file starting in the root directory /

Relative pathname: How to get to a directory or file starting in the current directory

relative

absolute

Page 10: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 10

2.0.0.3 – Introduction to the Shell

Seeing What’s There: ls

· To list directory contents use

· Common ls options:

ls

-a list all files (including . and ..)

-l list “long” file info-t list in reverse

chronological order (newest first)

-h list file size in “human-readable” format (use with –l)

Options can be combined: ls –lt does both –l and –t (only if the options do not require any additional info)

Page 11: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 11

2.0.0.3 – Introduction to the Shell

Moving and Copying Files: mv and cp

· To rename or move a file, use

· To copy a file (while preserving the original), use

· Common cp options:

mv

cp

Naming filesFiles and directories can contain any characters except “/”. However, using spaces or other “special” characters (such as “*”) will cause you problems later. Avoid them! Instead of a space, use an underscore “_”.To refer to a file that has a space in it, use quotes: cp “my file” myfile

-I ask before copying over a file

-r copy recursively (directory and all its contents)

Page 12: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 12

2.0.0.3 – Introduction to the Shell

Creating Links to Files: ln

· If you want to create a “shortcut” or “pointer” to a file or directory, make a link using

· Editing the link will change the original file

· Common options: -s: “symbolic” link: removing the link does not remove the original file

ln

Page 13: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 13

2.0.0.3 – Introduction to the Shell

Creating and Copying Directories: mkdir and cp

· Create a directory with

· Copy whole directory structures using cp –r

mkdir

Wildcard characters* Matches any string of characters? Matches any single character

Page 14: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 14

2.0.0.3 – Introduction to the Shell

Getting Rid of It: rm and rmdir

· Remove a file using

· Remove a directory using

· Common rm options:

Be very wary of rm * ! This will remove all files in your directory! For safety, always use the –i option (or, even better, use an alias: see session 2.0.0.3.2).

rm

rmdir

-I ask before deleting a file-r remove recursively

(directory and all its contents)

Page 15: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 15

2.0.0.3 – Introduction to the Shell

Creating Files: pico, vi, and emacs Editors

· pico: simple text editor; intuitive· Just start typing· Commands listed at bottom

· ^X to exit (hold control and push X)· ^O to save· ^R to open

Page 16: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 16

2.0.0.3 – Introduction to the Shell

Creating Files: pico, vi, and emacs Editors

· Vi (or vim): not so simple· Two modes: command and insert

· Type a or i to start inserting· Esc to get back to command mode· Commands are in form :<letter>· :q to quit

Insert mode Command mode

Page 17: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 17

2.0.0.3 – Introduction to the Shell

Creating Files: pico, vi, and emacs Editors

· emacs: full-featured but more intuitive than vi

· Once a file is opened, just start typing

· Common commands:· Cntl-X F to open (hold control while typing X and then F)

· Cntl-X S to save· Cntl-X C to exit

· “Meta” key is either Alt or Esc· If started without a filename, you first have to “find” (open) a new file with cntl-X F

What are all these extra files in my directory? Emacs automatically creates autosave files (#filename#) when you don’t save before exiting and backup files (filename~) whenever you save changes

Page 18: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 18

2.0.0.3 – Introduction to the Shell

Viewing Files: more is Less and less is More

· To output the contents of a file to screen, use

· With long files, this scrolls past too fast to see

cat

Page 19: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 19

2.0.0.3 – Introduction to the Shell

Viewing Files: more is Less and less is More

· To view a file a bit at a time, use

· Navigating in more:· Go down 1 line: Enter· Go down 1 screen: Space

· Go back 1 screen: b· Exit: q or Cntl-c

more

Help! Let me out!Control-c is a general UNIX “exit” command. It will end whatever command you are running. For more commands like this, see “job control” in session 2.0.0.3.3.

Page 20: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 20

2.0.0.3 – Introduction to the Shell

Viewing Files: more is Less and less is More

· To view a file a bit at a time with full navigation and searching, use

· Navigating in less:· Go down 1 line: Enter· Go down 1 screen: Space or PgDown

· Go up 1 screen: PgUp· Go to beginning of file: Home

· Go to end of file: End· Search down: /· Search up: ?· Exit: q (Cntl-c does NOT work!)

less

Page 21: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 21

2.0.0.3 – Introduction to the Shell

Printing From the Command Line: lpr

· In an application, you can just hit “print”. What if you just want to quickly print a text file?

· Common options:

· Don’t print a non-text file with lpr! (You will print meaningless symbols, and lots of them)

lpr

-P[printername] print to specified printer

-K[# copies] print specified # copies

-V verbose: tells you everything it’s doing

Default printer (and other defaults)You can set the default printer (and all kinds of other cool and useful things) using environment variables – see session 2.0.0.3.2.

Page 22: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 22

2.0.0.3 – Introduction to the Shell

· Size· Owner· Permissions· Modification and access times

· To look at this info, use

· To update a file’s modification time, use

More About Your Files: stat (and touch)

stat

· UNIX stores more information about a file than just its contents

touch What’s the –rw-r--r--?These are the file’s permission settings, which determine who can read, write, or execute the file. More on permissions in session 2.0.0.3.2.

Page 23: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 23

2.0.0.3 – Introduction to the Shell

· Instead, use

· Possible types are specified in /usr/share/magic

More About Your Files: file

· UNIX does not require 3-character filetype specifications (eg. wordfile.doc), so just by looking at the name you can’t determine of what type a file is

file

Page 24: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 24

2.0.0.3 – Introduction to the Shell

· Result:· Not all Windows programs (eg. Notepad) will correctly display newlines

· UNIX programs (eg. xemacs) will display newlines with an extra ^M

· View these “hidden” characters with

· Convert Windows-type to UNIX-type with

A Text File is a Text File is a Text File? Using dos2unix

· Windows-created text files can be read in UNIX and vice versa, but they use different line break characters

· Linux uses only the linefeed (\n) character· Windows uses both the carriage return (\r) and linefeed (\n) characters

cat -A

dos2unix

Page 25: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 25

2.0.0.3 – Introduction to the Shell

Now Where Did I Put That? Searching for Files with find

· Search for files using

· Can search entire directory trees

· Find based on things like:· Filename (Where did I put that?)

· Last modification time (What was I just working on?)

· Size (What’s taking up so much space?)

· Owner (Is there anything of mine here?)

· Multiple search terms can be combined

find

Modified in last 40 min.

Matching name

Larger than 1000 KB

Check the man page!UNIX commands (especially powerful ones like find) have many options. You are never going to remember them all, so use man often!

Page 26: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 26

2.0.0.3 – Introduction to the Shell

A Few Tips to Make Your UNIX Life Easier: tab

· You don’t have to type out those long filenames

· Use tab autocompletion· When partway through typing a command, filename, etc., hit tab

· If there is only one name that starts that way, the shell will complete it for you

· If there is more than one name, it will list all the possibilities

Page 27: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 27

2.0.0.3 – Introduction to the Shell

A Few Tips to Make Your UNIX Life Easier: history· You don’t have to retype that complicated command

· You can scroll through previous commands by using the up and down arrows

· You can see all your previous commands using

· Some options:

history

Cntl-r “text” show last command containing “text”(navigate matching commands with

Cntl-r and Cntl-s)history 10 see only last 10 commands!500 repeat command #500

Page 28: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 28

2.0.0.3 – Introduction to the Shell

A Few Tips to Make Your UNIX Life Easier: Moving Around and Copy/Paste

· Moving the cursor on the command line:· Move to the beginning of a line with Cntl-a· Move to the end of a line with Cntl-e

· Copy text from one place to another:· Select the text in one place· Move the mouse place you want to put the text· Press the middle mouse button (or both left and right together, if you don’t have 3 buttons) to paste the text

Page 29: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 29

2.0.0.3 – Introduction to the Shell

2.0.0.3.1Introduction to the Shell – Session 1

· Now you know….· How to form UNIX commands· How to create, edit, view, delete,

move, copy, find, and learn about files and directories

· How to learn more with man· How UNIX users type so fast (they

don’t – they use tab!)

· Next you’ll learn….· Permissions· Customizing your UNIX environment· Pipes, redirection, and running

command line programs

Page 30: 2.0.0.3.1 Introduction to the Shell – Session 1

04/20/23 Introduction to the Shell – Session 1 30

2.0.0.3 – Introduction to the Shell

2.0.0.3.1Further Readings

· Introduction· Learning the UNIX Operating System Ch. 1

· Linux Cookbook Ch. 2· Files and directories

· Learning the UNIX Operating System Ch. 3.1, 3.2, 4

· Linux Cookbook Chapter 6· Viewing and editing text

· Learning the UNIX Operating System Ch. 4.1

· Linux Cookbook Chapter 10,11