ulster county community college n.
Skip this Video
Loading SlideShow in 5 Seconds..
Download Presentation


100 Views Download Presentation
Download Presentation


- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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

  2. 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.)

  3. Reserved 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 [[ ]] • ! | < >

  4. bash – Command Line Syntax

  5. bash – Rules of Syntax

  6. 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.

  7. 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>

  8. 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.

  9. 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 shell • su - log into root with its shell (better) • su – name log into name account • su – student • su - root

  10. 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

  11. 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

  12. 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

  13. 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

  14. Options for the cd command

  15. 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.

  16. 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.

  17. 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

  18. 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 ..})

  19. 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

  20. Useful ls Options Chart

  21. 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 locatefilename and slocatefilename 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>

  22. 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.

  23. 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.

  24. 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

  25. 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.

  26. 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.

  27. 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.

  28. 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)

  29. 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

  30. 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.

  31. 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

  32. 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.

  33. 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.

  34. 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!

  35. 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?

  36. 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!

  37. 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.

  38. 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.

  39. 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.

  40. 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

  41. 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.

  42. wc - Word Count • wc [options] filename • The generic version shows • number of lines • number of words • number of bytes • wc longfile.txt

  43. 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

  44. 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, • Typing env will show the size of the file.

  45. 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>

  46. Multiple commands on a single line • ; separates commands • cat roadtrip.txt; cat saturday.txt will display BOTH files on the screen.

  47. DOS/UNIX Cross Reference

  48. Special Key Combinations

  49. Back to the regular book Now that we have a good grasp of the basic operation of Linux let us explore our text book.