Richard's Linux Ch 1, 2, 3, 6

=================
Linux is case sensitive
=================

Ch 2. Getting Started

Logging In
Log in to a linux server (e.g. cscilinux3.apsu.edu) from a termial or terminal emulater (or whatever you what to call it, a phone? or that new green vegetable with a screen, I cannot predict the future)

The shell display a prompt

Which shell are you using?
$ ps

CONTROL-Z suspends a program (followed by jobs to display jobs, then kill %jobno)
CONTROL-C terminates a program
CONTROL-W delete a word
CONTROL-U delete a line
CONTROL-D logout

UP ARROW - precious command

root is a superuser

Where to help help?
$ help
$ cat -- help
$ ls --help | less --- use vi command to control
$ man ---- manual (try man man)
$ info ----- menu-based hytertext system (try info info), h to go through, ? to list info commands, SPACE to scroll, m followed by name of menu item to display, q to quit

$ exit --- exit the shell with status N
$ passwd -- change password

 

Ch 3. Command Line Utilities

Avoid special character (avoid using them): & ; | * ? ' " ' [ ] ( ) $ < > { } ^ # / \ % ! ~ +
Avoid RETURN, SPACE AND TAB

To quote (use as regular character) a special character use backslash ( \ ), or between single quote marks (' ').

Utilities:

$ ls --- list the names of files
(use vim editor, ch 6, to create a file)
$ cat filename --- display a text file "filename"
$ rm filename --- deletes a file "filename"
$ rm -i filename --- deletes a file "filename" with interaction

$ more filename --- like cat, but display by page, SPACEBAR to next page
$ less filename --- like cat, but display by page, SPACEBAR to next page, q to exit (h for help)

$ hostname --- display hostname
$ cp source-file destination-file --- copy file (overwrites same filename !!!)
$ cp -i source-file destination-file --- copy file (prompt you before overwriting)

$ mv existing-filename new-filename --- rename a file (can overwrite another file)
$ mv -i existing-filename new-filename --- rename a file (prompt you before doing it)

$ lpr report --- print report, send to default printer
$ lpr -Pmailroom report --- send report to printer called mailroom
$ lpr --- check printer status
$ lprm 86 --- remove job 86

$ cat memo --- see content first before grep below
$ grep 'credit' memo
--- (global regular exxpression print), see if (files) contain certain characters.
$ head -1 memo --- display the first x lines of a file (default is 10, here is 1)
$ tail -2 memo --- display the last x lines of a file (default is 10, here is 2)

$ sort numbers --- displays the content of a file in order by lines but does not change the original file
$ sort - n numbers --- put numbers in order
$ sort -u numbers --- generated a sorted list in which each line is unique

$ uniq numbers --- display a file, skipping adjacent duplicate lines, does not change the original file
$ diff numbers memo --- compare two files, list differences, options available

$ file memo numbers --- learn summary of the content of file, without opening it (can accept 2 or more files)

| Pipe: communication between processes
$ sort numbers | head -4
$ sort -n numbers | head -4
$ ls | wc -w --- word count -w word ohption
$ tail months | lpr --- send output of a program to a printer

echo: Display Text
$ echo 'My new file.' > myfile --- echo output characters on screen, here redirect to a file
$ cat myfile

date: Diplay date and time
$ date --- display current date and time
$ date +"%A %B %d" --- type info coreutils 'date invocation' for more information

script: Records a Shell Session
character-based devices only, captures in a file names typescript by default, to a different file? $ script filename, exit to stop script
$ script
$ whoami
$ ls -l /bin | head -5
$ exit
$ cat typescript

todos/unix2dos: Converts Linux and Mac OS X files to Windows Format
$ todos memo.txt
or
$ unix2dos memo.txt --- this works on our server (convert to DOS format)
$ unix2dos -n memo.txt newmomo.txt --- keeps old file

$ fromdos memo.txt
$ dos2unix memo.txt --- from DOS to Unix format

Something extra: The format of Windows and Unix text files differs slightly. In Windows, lines end with both the line feed and carriage return ASCII characters, but Unix uses only a line feed. As a consequence, some Windows applications will not show the line breaks in Unix-format files. Likewise, Unix programs may display the carriage returns in Windows text files with Ctrl-m ( ^M ) characters at the end of each line.

gzip: Compressing a File (bzip2 is newer, but may not be running on some machines)
$ ls -l letter_e --- list file with more information, e.g. letter_e is a big file
$ gzip -v letter_e --- -v cause it to report reduced size, try man -k gzip
$ ls -l letter_e.gz --- new file, old file is gone
$ gunzip letter_e.gz --- unzip file
$ ls -l letter_e
If you want to gzip multiple files, create a tar file then compress it using gzip. (gzip cannot zip multiple files directly)

tar: Packs and Unpacks Archives (used to be for tapes, but now it creates a single file)
$ ls -l letter_e practice practice2 --- list 3 files
$ tar -cvf all.tar letter_e practice practice2 --- -c (create) -v(verbose) -f(read or write from file)
$ ls -l all.tar
$ tar -tvf all.tar --- t (display)
--- remove old files
$ tar -xvf all.tar --- x (extract files from tar archive)
Note: Some tar archives contain lots of files. To list the files without unpacking, run with -t option first, and the -x option may overwrite files.

which: locate a utility (where is it? which directory?)
$ which tar --- which untility shell will run
$ whereis tar --- searches for files related to a utility
$ type echo --- determine if a command is a builtin

locate/slocate: Searches for a file
$ locate motd
$ locate tar

who: Lists users on the system
$ who am i
$ who

finger: List users, detailed info - detailed info (can be security risk)
$ finger
$ finger max

write: Send a message
$ write max --- send message to max (delete key to go back)
CONTROL - D --- to quite
mesg: Denies or accepts message
$ mesg y --- allow other users to send you message, by default mesg is y
$ mesg n --- disable message

Email: ok, today very few people use e-mail directly in linux shell . Skipping this one.


Ch 6. The vim editor

The vim editor is not a text formatting program. It is text editor meant to be used to write code. You can use fmt to do minimal formatting on a text file created with vim.
vi runs a minimal build of vim.

$ vimtutor -- run vim tutor

$ vim practice
:q! --- quite without saving work

:w --- save without exiting

vim has two modes: Command mode (normal mode) and Input mode (enter text)... actually more modes (like : put vim in Last Line Mode), let's just keep it simple here
:set number
--- turn on line numbers
:set nonumber --- turn off line numbers

i --- (or a key) put vim to Input mode (see -- INSERT --)
ESCAPE --- vim return to Command mode (after entering text)

:help [feature] --- to help help

ARROW keys will work or h, j, k, and l (left, down, up, and right)

CONTROL-H
delete a character? (same as back space)
CONTROL-W delete a word
CONTROL-U delete a line

in Command mode:
ZZ -- save tex to disk and end editing session.
dw
--- delete work
dd
--- delete line
u
--- undo
:redo
--- redo
CONTROL-R
--- redo
o
: opens a blank line
:!<shell cmd>
--- execute shell commands from vi mode

E.g. compiling a simple c++ file in vi
create a file test.cpp (extension is important)
type

----------------------------------------------------------
#include <iostream>
using namespace std;

int main () {
cout << "Programming is fun" << endl;
return 0;
}
----------------------------------------------------------

In vi you can do this (or you can do this outside, withouth the :! of course)
:! g++ test.cpp -o test

g++ creates a exe file call test (assuming you do not have any syntax error)
Now get out of vi, type
./test


CONTROL-L --- signal to printer to skip to next page

in Command mode:
CONTROL-L
--- redraw screen

$ view filename
--- use vim to look at a file but not to change it

Copy and paste
yy --- coy the current line into buffer
Nyy --- copy the next N lines, including the current line, into the buffer
p--- paste the line(s) in the buffer into the text


Moving the Cursor

fa
--- find character "a"
w
--- move cursor by word
)
--- move cursor to next sentence
} --- move cursor to next paragraph

CONTROL-D
scroll screen down
CONTROL-U scroll screen up

 

Searching for a String

fa
--- find character "a"
/searchtext --- find searchtext next occurance (can use regular expression)
?searchtext --- find searchtext last occurance
search and replace is also useful... maybe later...

Cut and Paste

  1. Position the cursor where you want to begin cutting.
  2. Press v (or upper case V if you want to cut whole lines).
  3. Move the cursor to the end of what you want to cut.
  4. Press d.
  5. Move to where you would like to paste.
  6. Press p to paste after the cursor, or P to paste before.

 

Show Line Numbers
:set nu ----------- show line number
:set nu! ----------- turn off line number