49
ULSTER COUNTY COMMUNITY COLLEGE CIS 116 Linux (Part 2) First Commands Karl Wick

ULSTER COUNTY COMMUNITY COLLEGE

  • Upload
    perdy

  • View
    23

  • Download
    0

Embed Size (px)

DESCRIPTION

ULSTER COUNTY COMMUNITY COLLEGE. CIS 116Linux (Part 2) First CommandsKarl Wick. The Command Interpreter - Bash. From man: Bash is (a) … command language interpreter that executes commands read from the standard input (the keyboard) or from a file. - PowerPoint PPT Presentation

Citation preview

Page 1: ULSTER COUNTY COMMUNITY COLLEGE

ULSTER COUNTY COMMUNITY COLLEGE

CIS 116 Linux (Part 2)

First Commands Karl Wick

Page 2: ULSTER COUNTY COMMUNITY COLLEGE

The Command Interpreter - Bash From man: Bash is (a) … command

language interpreter that executes commands read from the standard input (the keyboard) or from a file.

A blank character is used to separate words and redirections. The first word specifies the command to be executed. (The remaining words are treated as parameters.)

Page 3: ULSTER COUNTY COMMUNITY COLLEGE

Reserved WordsReserved words have special meaning to the

Bash shell; case do elif else esac fi for function if in select then until while {} time [[ ]] ! | < >

Page 4: ULSTER COUNTY COMMUNITY COLLEGE

bash – Command Line Syntax

Page 5: ULSTER COUNTY COMMUNITY COLLEGE

bash – Rules of Syntax

Page 6: ULSTER COUNTY COMMUNITY COLLEGE

Naming Comventions A file name may contain up to 256 characters. _, -, . are allowed. Other characters are also

allowed but not recommended because they may be OS command characters.

File name extensions are preceded by a dot (.) and may contain from 1 to 3 characters

Directory names may use extensions buy generally do not.

Page 7: ULSTER COUNTY COMMUNITY COLLEGE

Man(ual) q exits man The most important Linux command is man

pagename man opens the manual for the page (or

command specified). Navigation:

– <space> moves forward– <b> moves backward– <q> exits the manual

Type man man <enter>

Page 8: ULSTER COUNTY COMMUNITY COLLEGE

Print man pages with man command | col -b | lpr Type !lpr filename <enter> (text pg 85)

Note: This only works if a printer is attached and configured.

Page 9: ULSTER COUNTY COMMUNITY COLLEGE

Swapping user account Remember that the # prompt means that you are

logged in as root. The $ prompt means that we are logged in as user.

Usually it is dangerous to be logged on as root, and sometimes it is essential.

We can switch user levels with the su command.su log into root with your own shellsu - log into root with its shell (better)su – name log into name accountsu – studentsu - root

Page 10: ULSTER COUNTY COMMUNITY COLLEGE

Where am I? pwd With Linux’s extended tree hierarchy it is very

easy to get lost. The pwd command stands for “print working

directory” and will tell you the complete directory path that you are in.– cd /etc cd sounds cd events

The command prompt shows only the lowest level directory. [root@localhost events]#

pwd shows: /etc/sound/events

Page 11: ULSTER COUNTY COMMUNITY COLLEGE

Changing the Active Directory cd is the magic command

– cd (alone) returns you to your home directory– cd with a path name will (may) take you there– There are two ways of specifying the path

• Absolute• Relative

Page 12: ULSTER COUNTY COMMUNITY COLLEGE

Absolute vs Relative Pathnames A path is absolute if it specifies the entire

path name. An absolute path begins with / which is the name for the top directory.

A path is relative if it specifies where to go from the present directory

cd /etc/sounds/events is absolute if we are already in /etc we can type cd

sounds/events

Page 13: ULSTER COUNTY COMMUNITY COLLEGE

Some special paths . Stay in current directory .. Move up one level … Move up two levels / is the top directory /home is the normal user directory /root is the root directory

Page 14: ULSTER COUNTY COMMUNITY COLLEGE

Options for the cd commandCommand Functioncd return to login directorycd ~ return to login directorycd / go to system top directorycd /root go to root directorycd /home go to users home directorycd .. move up one levelcd ~otheruser go to his login dir if permittedcd /dir1/subdir absolute pathcd ../../dir3/X11 up two levels, then down to X11

Page 15: ULSTER COUNTY COMMUNITY COLLEGE

An experiment Make sure that you are logged in as student (if not then type su – student) and you will

be. Type cd /root

– You will get a message “Permission denied” Change to root (su – root, then type pw) Now you are in the /root directory. The root directory is protected.

Page 16: ULSTER COUNTY COMMUNITY COLLEGE

List the directory with ls Type ls from the root directory You will see a few entries

– Blue means a folder– Grey means a file– Green means special items

Type cd / Type ls again You now see many folders here including root and

home Just like with DOS, ls may not show everything in

the directory.

Page 17: ULSTER COUNTY COMMUNITY COLLEGE

ls options Type man ls When done type q

– There are many options listed– We will use a few

These many options allow us to sort the information displayed to best meet our needs.

With options we can– Specify HOW files are displayed– See file permissions– See other file attributes

Page 18: ULSTER COUNTY COMMUNITY COLLEGE

ls options practice Go back to the root directory cd Type ls again and note the output Now type ls –a and note the output again

– Are they the same? You should see more files or folders. These

inculde . and .. All of the new files may begin with dots (.) These are hidden files (“dot” files). Try ls –A (all but implied {. and ..})

Page 19: ULSTER COUNTY COMMUNITY COLLEGE

Even more information ls and ls –a give names only. We can also look at details. Use the l(ong)

option; ls –l Now we see a lot more information Many files are still missing. Try typing ls –al (this combines options)

– the output may scroll off of the screen– ls –al | more

Page 20: ULSTER COUNTY COMMUNITY COLLEGE

Useful ls Options Chart

-a all – list all files, including hidden

-l long – detailed: permissions, owner, etc.

-F file type - /=dir, @=link, *=executable

-r reverse – Back to front

-R Recursive – all dirs current and lower

-S Size – sorts files by size

Page 21: ULSTER COUNTY COMMUNITY COLLEGE

Finding files and directories Sometimes you know that a file or directory

exists but you cannot recall the exact location on the disk

The commands locate filename and slocate filename will find a file or directory.

slocate only lets you find files and folders that you have permission to access.

Format is locate [-options] <string> Example locate finger <enter>

Page 22: ULSTER COUNTY COMMUNITY COLLEGE

The cron daemon The locate command finds names from a

database created by a daemon called cron. (Daemons handle background tasks) The database is called slocate cron updates the database nightly if the

system is running. If you shutdown after using the system cron rarely has a chance to update slocate.

Page 23: ULSTER COUNTY COMMUNITY COLLEGE

Updating slocate As we’ve already mentioned, cron updates

the slocate datebase nightly, but only if Linux is up.

We can force an update by entering the command updatedb <enter> at the root prompt (only root can run this utility).

The update takes a minute or two depending on the number of files and speed of the system.

Page 24: ULSTER COUNTY COMMUNITY COLLEGE

Clearing the Window By now you must be getting tired of clutter

filling your screen and the cursor appearing only at the very bottom.

Just like DOS, Linux has a clear screen command.

In DOS the command was CLS, In Linux the command is clear

Page 25: ULSTER COUNTY COMMUNITY COLLEGE

Listing the Contents of a File Remember all files are text files unless

otherwise specified In Linux, there are several ways displaying the

contents of a text file. The commands are;

cat is the most versatile and also has other useful functions. Sometimes this can get us into trouble.

catmore lesshead tail

Page 26: ULSTER COUNTY COMMUNITY COLLEGE

cat cat is short for ‘concatenate’ – meaning ‘to

connect files together in order’ cat will display an existing file cat will create a new file if the name is not

already taken cat uses the standard input and output

devices unless you specify otherwise.

Page 27: ULSTER COUNTY COMMUNITY COLLEGE

Standard Input & Output The standard input device is the keyboard The standard output device is the monitor Type cat <enter>

– The cursor moves to a blank line– Type “Stop at the music store.” <enter>

cat echoed the string to the screen To quit cat, move the cursor to a blank line and

press <CTRL-D>. The command prompt will reappear.

cat works a lot like the DOS ECHO command.

Page 28: ULSTER COUNTY COMMUNITY COLLEGE

Redirection Redirection means directing the shell to get

input or send output to/from somewhere other than the standard devices.

The > symbol redirects output The < symbol redirects input Placing > after any command that generated

output will send the output to that location (often a file)

Page 29: ULSTER COUNTY COMMUNITY COLLEGE

cat and Redirection Type cat > roadtrip.txt and then <enter> now type these lines (all followed by <enter>)

– Buy some CDs– Then visit the coffee shop– Buy some coffee– Go home, drink coffee and listen to CDs

At a blank line type <CTRL-D> The command prompt should reappear. The four lines that we typed did NOT appear on the

screen. Instead, they were sent to a file called roadtrip.txt

Page 30: ULSTER COUNTY COMMUNITY COLLEGE

cat with Redirection – continued Type ls. you will see the new file listed. Read the file by typing cat roadtrip.txt

NOTE: Be careful using cat to create a new file because it will replace existing files with the same name without prompting.

Page 31: ULSTER COUNTY COMMUNITY COLLEGE

cat with Redirection – continued Create another file called part2.txt cat > part2.txt <enter>

– Make supper– Watch the news – Do homework

<CTRL-D> ls cat part2.txt

Page 32: ULSTER COUNTY COMMUNITY COLLEGE

Concatenation with cat Let us now join our two files into a third file by

using the cat command cat roadtrip.txt part2.txt > saturday.txt ls cat saturday.txt What do you see on the screen? cat has appended the second file’s contents to the

first file. Format is cat file1 file2 … filen > destination cat can use any number of input files.

Page 33: ULSTER COUNTY COMMUNITY COLLEGE

Appending information The special character > redirects

information to a file The special character >> appends

information to the end of a file. Type cat part2.txt >> roadtrip.txt cat roadtrip.txt roadtrip now contains its original text plus

the text of part2 at its end.

Page 34: ULSTER COUNTY COMMUNITY COLLEGE

Appending information

CAUTION!– Be careful to use the doubled >> when

appending information. If you accidentally use the single > you will replace your output file instead of adding information to it!

Page 35: ULSTER COUNTY COMMUNITY COLLEGE

Make a larger file cat > longfile.txt Type the numbers 1 through 50 one to a

line. <ctrl-D> cat longfile.txt - The output scrolls off of

the screen. How can we display this file and see all of

it?

Page 36: ULSTER COUNTY COMMUNITY COLLEGE

more The more command lets us display long files one

page at a time with a pause for a keypress between pages.

Type more longfile.txt <enter>– At the bottom of the screen you see --More--(35%)

Press the enter key – the text moves up one line Press the space bar – the text moves up one page More is similar to the DOS command MORE You CANNOT scroll back up!

Page 37: ULSTER COUNTY COMMUNITY COLLEGE

less less does what more does only more. less will display a file and will let you scroll

up and down with the arrow, page up, page down, b, enter and spacebar keys.

See man for more options. less longfile.txt <enter> While more exits automatically at the end

of a file, you must exit less by pressing the q key.

Page 38: ULSTER COUNTY COMMUNITY COLLEGE

head and tail head filename will display the first ten lines of a

file on the output device. tail filename will display the last ten lines of a file

on the output device. head longfile.txt <enter> tail longfile.txt <enter> These commands let us take a quick at the

beginning or ending of a file to see what kind of information it contains.

head –20 displays 20 lines, etc.

Page 39: ULSTER COUNTY COMMUNITY COLLEGE

Redirection with pipes Pipes connect the standard output of one

command to the standard input of another command.

Type ls –al /etc <enter> The information scrolls off of the screen Type ls –al /etc | less <enter> Remember all of those messages during the boot

process? Type dmesg | less to see them.

Page 40: ULSTER COUNTY COMMUNITY COLLEGE

Finding character strings The grep command will find specific

strings of characters within files grep searches are case sensitive the –i option makes the search case

insensitive. (grep –i string filename) grep coffee saturday.txt will show every

line containing the word coffee. grep can redirect its output into a file for

later review grep buy part2.txt | review

Page 41: ULSTER COUNTY COMMUNITY COLLEGE

Wildcards * matches any string including null string ? matches any single character \* matches the * character \? matches the ? character […] matches any one of the enclosed characters. A

pair of characters separated by a dash indicates a range of characters.

[A-z] means match all letters If the first character is ! or ^ then any character

NOT enclosed is matched.

Page 42: ULSTER COUNTY COMMUNITY COLLEGE

wc - Word Count wc [options] filename The generic version shows

– number of lines– number of words– number of bytes

wc longfile.txt

Page 43: ULSTER COUNTY COMMUNITY COLLEGE

diff - Comparing two files The diff command compares two text files

and finds the differences between them. diff [option] file1 file2

– The –l option treats all letters as lower case– The –c option gives a very detailed comparison

Page 44: ULSTER COUNTY COMMUNITY COLLEGE

Command History Linux stores a list of recently issued

commands in a file called .bash_history. By default bash stores up to 500 (1000) commands.

You can review these commands in reverse order by typing the up arrow.

You can view the file with more, less, et.al. Typing env will show the size of the file.

Page 45: ULSTER COUNTY COMMUNITY COLLEGE

Tab Completion You can type part of a simple command and

then <tab> and bash will complete the command or beep.

If it beeps just hit tab again for a list of all commands beginning with what you typed.

m<tab><tab> mo<tab><tab>

Page 46: ULSTER COUNTY COMMUNITY COLLEGE

Multiple commands on a single line ; separates commands cat roadtrip.txt; cat saturday.txt will

display BOTH files on the screen.

Page 47: ULSTER COUNTY COMMUNITY COLLEGE

DOS/UNIX Cross ReferenceDOS UNIX FunctionDIR ls list directory contents

CD pwd show working directory

CD cd change working directory

MD mkdir make new directory

RD rmdir remove directory

DEL rm Remove file(s)

COPY cp copy file

REN mv move/rename file

TYPE cat display file (cat does more)

SORT sort sort file

MORE more display file with pause

CLS clear clear screen

DATE date Disp date (and time)

EDIT vi/emacs Built in editors

HELP man help files

VER uname –a OS info (and more)

Page 48: ULSTER COUNTY COMMUNITY COLLEGE

Special Key Combinations<CTRL-s> Stop screen output

<CTRL-q> Resume screen output

<CTRL-c> Stop current activity or process

<CTRL-d> Exit or End of File

<CTRL-w> Erase last word

<CTRL-u> Erase entire line

Page 49: ULSTER COUNTY COMMUNITY COLLEGE

Back to the regular book

Now that we have a good grasp of the basic operation of Linux let us

explore our text book.