Title: ULSTER COUNTY COMMUNITY COLLEGE
1ULSTER COUNTY COMMUNITY COLLEGE
- CIS 116 Linux (Part 2)
- First Commands Karl Wick
2The 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.)
3Reserved Words
- Reserved words have special meaning to the Bash
shell - case do elif
- else esac fi
- for function if
- in select then
- until while
- time
- ! lt gt
4bash Command Line Syntax
5bash Rules of Syntax
6Naming 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.
7Man(ual) q exits man
- The most important Linux command is man pagename
- man opens the manual for the page (or command
specified). - Navigation
- ltspacegt moves forward
- ltbgt moves backward
- ltqgt exits the manual
- Type man man ltentergt
8Print man pages
- with man command col -b lpr
- Type !lpr filename ltentergt (text pg 85)
- Note This only works if a printer is attached
and configured.
9Swapping 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 shell
- su - log into root with its shell (better)
- su name log into name account
- su student
- su - root
10Where am I? pwd
- With Linuxs 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_at_localhost events - pwd shows /etc/sound/events
11Changing 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
12Absolute 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
13Some 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
14Options for the cd command
Command Function
cd return to login directory
cd return to login directory
cd / go to system top directory
cd /root go to root directory
cd /home go to users home directory
cd .. move up one level
cd otheruser go to his login dir if permitted
cd /dir1/subdir absolute path
cd ../../dir3/X11 up two levels, then down to X11
15An 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.
16List 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.
17ls 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
18ls 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 ..)
19Even 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
20Useful ls Options Chart
-a all list all files, including hidden
-l long detailed permissions, owner, etc.
-F file type - /dir, _at_link, executable
-r reverse Back to front
-R Recursive all dirs current and lower
-S Size sorts files by size
21Finding 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 ltstringgt
- Example locate finger ltentergt
22The 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.
23Updating slocate
- As weve already mentioned, cron updates the
slocate datebase nightly, but only if Linux is
up. - We can force an update by entering the command
updatedb ltentergt 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.
24Clearing 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
25Listing 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.
cat
more less
head tail
26cat
- 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.
27Standard Input Output
- The standard input device is the keyboard
- The standard output device is the monitor
- Type cat ltentergt
- The cursor moves to a blank line
- Type Stop at the music store. ltentergt
- cat echoed the string to the screen
- To quit cat, move the cursor to a blank line and
press ltCTRL-Dgt. The command prompt will reappear. - cat works a lot like the DOS ECHO command.
28Redirection
- Redirection means directing the shell to get
input or send output to/from somewhere other than
the standard devices. - The gt symbol redirects output
- The lt symbol redirects input
- Placing gt after any command that generated output
will send the output to that location (often a
file)
29cat and Redirection
- Type cat gt roadtrip.txt and then ltentergt
- now type these lines (all followed by ltentergt)
- Buy some CDs
- Then visit the coffee shop
- Buy some coffee
- Go home, drink coffee and listen to CDs
- At a blank line type ltCTRL-Dgt
- 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
30cat 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.
31cat with Redirection continued
- Create another file called part2.txt
- cat gt part2.txt ltentergt
- Make supper
- Watch the news
- Do homework
- ltCTRL-Dgt
- ls
- cat part2.txt
32Concatenation with cat
- Let us now join our two files into a third file
by using the cat command - cat roadtrip.txt part2.txt gt saturday.txt
- ls
- cat saturday.txt
- What do you see on the screen?
- cat has appended the second files contents to
the first file. - Format is cat file1 file2 filen gt destination
- cat can use any number of input files.
33Appending information
- The special character gt redirects information to
a file - The special character gtgt appends information to
the end of a file. - Type cat part2.txt gtgt roadtrip.txt
- cat roadtrip.txt
- roadtrip now contains its original text plus the
text of part2 at its end.
34Appending information
- CAUTION!
- Be careful to use the doubled gtgt when appending
information. If you accidentally use the single gt
you will replace your output file instead of
adding information to it!
35Make a larger file
- cat gt longfile.txt
- Type the numbers 1 through 50 one to a line.
- ltctrl-Dgt
- cat longfile.txt - The output scrolls off of
the screen. - How can we display this file and see all of it?
36more
- 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 ltentergt
- 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!
37less
- 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 ltentergt
- While more exits automatically at the end of a
file, you must exit less by pressing the q key.
38head 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 ltentergt
- tail longfile.txt ltentergt
- 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.
39Redirection with pipes
- Pipes connect the standard output of one command
to the standard input of another command. - Type ls al /etc ltentergt
- The information scrolls off of the screen
- Type ls al /etc less ltentergt
- Remember all of those messages during the boot
process? - Type dmesg less to see them.
40Finding 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
41Wildcards
- 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.
42wc - Word Count
- wc options filename
- The generic version shows
- number of lines
- number of words
- number of bytes
- wc longfile.txt
43diff - 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
44Command 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.
45Tab Completion
- You can type part of a simple command and then
lttabgt 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. - mlttabgtlttabgt
- molttabgtlttabgt
46Multiple commands on a single line
- separates commands
- cat roadtrip.txt cat saturday.txt will display
BOTH files on the screen.
47DOS/UNIX Cross Reference
DOS UNIX Function
DIR 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)
48Special Key Combinations
ltCTRL-sgt Stop screen output
ltCTRL-qgt Resume screen output
ltCTRL-cgt Stop current activity or process
ltCTRL-dgt Exit or End of File
ltCTRL-wgt Erase last word
ltCTRL-ugt Erase entire line
49Back to the regular book
- Now that we have a good grasp of the basic
operation of Linux let us explore our text book.