CIS 105 Using Linux

CIS 105

Using Linux

Please experiment with these commands as much as you can on your own. The more you work with the terminal prompt, the more comfortable you will be. Be certain to read the output you receive on the screen if it is not what you expected.

Basic Utilities

Log on to the terminal using the PuTTy program, your username and password. Read the lines on the left, and then issue the commands to the right at the PuTTy terminal. Assume there is a request for the enter key after the lines at the right.

Issue the command When you press enter, the command line you typed in sent to the shell, which interprets your instructions and executes whatever programs you specify. In this case, the whoami program runs. Its output which is your login name or username, is displayed on your screen

whoami

Notice the error message that is displayed. Remember ? Linux and Unix are case sensitive, so the same letters in different case do not yield the same results.

WHOAMI

Issue the command

ls

The list utility (ls) will list files in the current directory (DOS

command dir)

Check the options you can use with the ls command View all files (including hidden, which begin with a period .) View long format (use the lowercase letter L)

This will list the all information about the file, the permissions, size, date, etc View all files and output in columns

ls ?-help ls ?a ls ?l

ls ?Ca

Enter the command The cat command is similar to the type command in DOS. It will display the contents of the file practice TIP: if you type cat p and then hit the tab key on your keyboard, the terminal prompt will attempt to auto-fill any file in the current directory that begins with the letter p. If there are several options, hit the tab key again, and the files will be listed for you to choose from.

cat practice

The cat command will display the contents of a text file. The command interprets each argument as the name of a file to

open, read and write to the output (defaults to the screen unless otherwise noted)

We will need some additional files to play with. We will use additional features of the cat command to accomplish this. Enter the command The curser returns to the beginning of the next line. The shell does not display a new prompt. This is similar to issuing the DOS command copy con. The cat utility is waiting for input from the keyboard to redirect to the output file firstfile. The > symbol is used as it is in DOS, to redirect output of a command as input to the file named after it.

cat > firstfile

Type in the following lines

CTRL-D is an end-of-file (EOF) character, and informs the cat utility to terminate. You should now see the shell prompt display

This is a line of text in the firstfile This is another CTRL-D

Issue the command

ls

The file named firstfile and practice should be listed

Create a second file with the cat utility Add some text of your choice (be nice ? I can see this). End the input to the file by pressing CTRL-D

cat > secondfile

Issue the command

ls

You should now see three files, firstfile, secondfile and practice

Deleting a file There was no "are you sure", nothing. The file should be gone. This is like the del command in DOS

rm practice

Safe Delete Using the optional parameter ?i, you receive a confirmation that you wish to delete the file. Do not delete it.

rm ?I secondfile n

Issue the command

ls

The file practice is gone

Copying a file This creates a copy of the file named secondfile and names the new copy newfile

cp secondfile newfile

Check your results

ls

CIS105

Page 2 of 9

You should now see the files firstfile, secondfile and newfile

Changing the filename As ren is to DOS, mv will change the name of the file newfile to oldfile. No news is good news and the command worked

Searching a file grep outputs the line that it found the word text in Lets look for something fun grep has found the word root in the password file. Take a look at the size of the file Ignore the case while searching Look at all the fun you can have with gre Type q to get out of the man pages

mv newfile oldfile

grep `text' firstfile grep root /etc/passwd cat /etc/passwd grep ?I TEXT firstfile man grep q

Display the beginning of a file This will display the first 10 lines of your .bash_history file

Display the end of a file This will display the last 8 lines of your .bash_history file (since you logged out last)

head -10 ~/.bash_history tail -8 ~/.bash_history

Gosh, we need more files to play with! Use cat to create a file called test_sor with the following lines:

abc 1234 mary 75 About town (blank line) +abc 9 92 18 _Abc abc +777 ZZ #ZZ (blank line) zz my files 7453 ?mary abc 96

cat > test_sor

CIS105

Page 3 of 9

Mary ^Mary CTRL-D

Display a file in order We are "piping" the output of the sort command through the more command. The "|" pipe symbol is usually located above the enter key on your keyboard. This will show us one screenful at a time. The sort command has put the blank lines at the front, then sort the rest of the output. Notice how Linux sees the characters it is sorting. Lets see if we can get Linux to put all the numbers at the end of the list We can also sort in reverse order

sort test_sor | more

sort ?n test_sor | more sort ?r test_sor | more sort ?rn test_sor | more

Create another file to play with Input the following:

000 Dyllis B. Harvey nurturer 001 Lyle C. Strand mentor 002 James V. Miller dean 003 Marjorie M. Conrad teacher 004 Orin C. Braucher farmer 005 Dvid A. Wass professor 006 Peter M. Kindfield friend 007 Marge M. Boercher writer ctrl-d

cat > namelist

Other options with sort This will sort by the fourth character of the file namelist

This will sort by the middle initial, or the data between the second and third spaces.

We are passing 5 arguments to the sort command. +2 Counts two field separators (spaces) and starts sorting

each record. -3 Stop sorting each record at the third field separator, so the

third field is sorted +1 -2 If the data in the third field results in two or more

records with identical data, breaks the tie by examining the second field of those records.

sort -k 4 namelist sort +2 -3 namelist

sort +2 -3 +1 -2 namelist

Put the data resulting from sort into a file The > symbol redirects the output of the sort command and puts it into the file called mi_names Verify success Look at the other options you can play with using the sort command

sort +2 -3 namelist > mi_nam

more mi_names man sort

CIS105

Page 4 of 9

Type the letter q to end

Identifying and/or removing duplicate lines with the uniq utility Yes, we need more files. Create a file called test-u with the following lines:

aa bbb ccc aaa aaa aaa bbb bbb eee fff aaa

Issue the following command Look at the output of the file. It should look like this:

aa bbb ccc aaa bbb eee fff aaa Uniq reads a line, then reads the next. If they are duplicates, it then discards the second line. All duplicate lines that are not next to each other will be displayed. This is the same as running the command Sort with the ?u which looks for unique lines, but scans more than just the adjacent line.

To remove all duplicate lines with the uniq command, those

lines must be next to each other. We can accomplish this by

first sorting the lines, then using the uniq command to remove

those lines.

sort test?u > sor-test-u

Creates a new sorted file called sor-test-u (remember the >

redirects the output of a command as the input of a file)

This works, but it is a two step process, and now we have

another file to deal with. We can instead "pipe" the information

from the sort command to the uniq command by issuing the

following command sort test-u | uniq

This accomplishes the same task on one line. The pipe utility

takes the output of the first command and uses is as the input

to the second command. Pretty cool stuff!

q

uniq test-u sort ?u test-u uniq sor-test-u

CIS105

Page 5 of 9

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download