UNIX / LINUX NOTES       

 

Most LINUX commands mirror corresponding UNIX commands. Files below are assumed to have “cpp” or “java” suffixes. In the examples below, myfile and other similar names represent generic file names. In most cases, the actual name could include metacharacters ‘*’ and ‘?’.

 

0.   On most Windows systems, to signal EOF at the terminal, the command is ctrl/z. On UNIX/LINUX systems, ctrl/z stops the current process. The correct EOF command in UNIX/LINUX is system dependent, but the first thing to try is ctrl/d.

 

1.   Unix/Linux commands are case sensitive.

 

2.      Unix responds immediately to the erase character and the kill character. The keys shown below work on most systems.

 

erase - removes the last character in the current command ( backspace )

kill    - removes all the characters so far in the current command ( ctrl/u )

 

3.   Most UNIX commands have options. Options are selected by entering a dash followed by the list of options:

ls  -ac         -a list all files   -c  indicate last change

 

4.   Multiple commands can be placed on the same line if separated by semicolons(;)

date;  pwd; ls  -l

 

5.   A key command is man, short for "manual."  man followed by  a UNIX command will display the manual pages on that command. It is the equivalent to "help" on many systems. man man will give you additional information on how to use the manual pages. man  -k keyword will search the manual pages for references to the keyword given.  This is useful when you do not know the exact command to perform the desired task.

 

6.   Output redirection. The ">" symbol instructs the system to direct the output of the command to the given file. The previous contents of the file are discarded. The command below will send the long directory listing to the file "mylist".

ls  -l  >mylist

The use of  ">>" rather than ">" will cause the output to be appended to the end of the existing file.

ps  >>mylist

 

7.   Metacharacters:           *    Matches any character string

?    Matches any single character

 

8.   In the UNIX file system, "/" indicates a subdirectory. The command below will display myfile.java in subdirectory bin of the root directory. The root directory is indicated by "/"

cat  /bin/myfile.java

 

To print otherfile.java in the subdirectory java of the current directory, leave off the "/":                                cat  java/otherfile.java

 

Thus, any directory address beginning with a “/” is absolute. That is, it is relative to the root directory of the machine. Any directory address not beginning with a “/” is relative to the current directory. It is even possible to move both up and down in the same command. What do you think the following command will do?

                                                                              ls –l ../cpp/*.cpp

 

9.   Your home directory should automatically contain a file called:       .login

As files beginning with "." are not listed by "ls" (unless you use "ls  -a"), this file is present but not normally visible.  Like the autoexec.bat on a PC, .login is automatically executed each time you login.  Abbreviations that you find useful, and commands that set up the environment you like can be defined here.  For example, it is fairly common to get lost moving around the directory system and want to return to your home directory.  An abbreviation to perform this task can easily be included in your .login.  First perform a "pwd" to see the path to your home directory.  Then "vi .login" and move to the end of the file.  Insert a line in the file defining the following abbreviation:

alias  h='cd ThePathToYourDirectory'

Thereafter, the command "home" will change directory to your home directory. Another alias in my .login whose purpose should be obvious is : alias u='cd ..'

Your new .login will be activated the next time you login, but the command:

source  .login

will activate it immediately.

 

 


UNIX/LINUX COMMAND SUMMARY

 

cat  myfile.java           catenate. Most commonly used to display the contents of a file on the screen.

cat   -n  file.cpp >file.num

                  Create a new file that is like the old but line numbers have been added.

cat  > easyfilemade.txt // to easily create a text file under the given file name simply by typing

      from the keyboard without using an editor. Just keeping typing until pressing the CTRL-Z to finish.

 

cd                          change directory.

cd  ..                      move to parent directory

cd  java                 move to subdirectory java

cd  /usr/include     

                              move to the stated directory.  The beginning "/" indicates this is an absolute path, not relative.

 

chmod             change file modes.  Used to alter protection on file. See "man chmod" for more information.

            chmod  711  *.java

            chmod  u+rwx, go-rw *.java

            Above two commands should be equivalent (provided that group and others currently have “Read” permission) . They both set all files with the extension .java such that the user have all permissions (rwx) but the group and others have only the ‘Execute’ permission.

            File permisssions

 

Symbol

Meaning

File permission granted

Directory permission granted

-

None

None

None

r

Read

View contents

List contents

w 

Write

Edit, Delete

Modify, add or delete files

x

Execute

Execute a program

Enter or access a directory

 

 

 

clear                      clear the terminal screen

 

cp                          copy file. The original file still exits.

            cp  oldfile.cpp  newfile.cpp

            cp –r dirA  ../dirB       // to copy entire contents of (files and files in all

            subdirectories) of the directory (dirA) which is a subdirectory of PWD

            into dirB which is a sibling directory of the PWD

            cp  -r .  ../dirA       // to copy the entire contents of the PWD into directory

            (dirA) which is a sibling directory of the PWD

 

date                       display date and time

 

echo                       echos a string to the terminal (or current output device)

echo  hello

 

env  > globalVariables.lis

            create the named file to contain values of all global (environment) variables such as GROUP,

            HOME, LOGNAME, PATH, SHELL and USER

 

set  > localVariables.lis

            create the named file which is to contain values of all local (shell) variables such as BASH, PATH,

            LANG, LINES, COLUMNS, COLORS and MAIL

 

find   file.java  look for this file within and under the current directory.

                              Precede with  cd /”  to look within the entire file system.

 

finger  UserName gives information about this user if known to the system

 

grep                       Searches files in the input list for lines containing the pattern

grep  mplier  mycrypt.cpp

 

head  file.java Output the first 10 lines of a file or files

 

hostname         Display the name of the current host

kill                   Often follows ps.  Used to kill extraneous processes. ctrl / z often halts the current process without killing it. ctrl / c should kill the current process. kill -9  can be used as an override when kill does not work.

 

lo                           logout

 

ls                            list files in current directory

ls  /bin                    list files in directory /bin

ls  -l                       list files in current directory in long form

                              Long form includes:

1.      File type (regular, link or directory) and permissions

2.      Links

3.      Owner

4.      Group

5.      Size in bytes

6.      Last modification date

7.      Last modification time

8.      File name

            ls  -l  ../..    A long form listing of the parent directory of the parent of the PWD

            ls  -a           A long form listing of everything including hidden files, ., and ..

            ls  -A          Everything excluding . and ..

 

ls  m??.*                list files whose names begin with ‘m’ and are 3 letters long

 

man  <cmd>    display the manual pages on the given command

man  ls             display the manual pages on the ls command

man  -k <word>display the manual pages containing the word    

<spacebar>      causes the next page to be displayed

<q>                  exits the man pages

<cr>                 causes the next line to be displayed

 

 

mkdir              make directory

mkdir  sub       make directory "sub" under current directory

 

more    file.cpp     Displays a file one page at a time. <spacebar> for next page or <q>

to quit.

 

mv                   move file.  Move file from one place to another. File is removed from previous location.  Like a "rename".

mv  oldfile.java  newfile.java

 

nano    filename    A very simple point and shoot editor. Most changes are made by typing, backspace, and delete, but cut, paste, and other commands are listed at the bottom of the page and are activated by the correct control sequence. It works with any text file. If nano does not work in your shell, try pico.

 

pwd                       print working directory

 

pg                          page.  Allow you to browse thru a text file.  <spacebar> for next page or <q> to quit.

 

pg  Prog4.cpp would display the indicated file one page at a time

 

pico filename   very similar to nano as listed above. If pico does not work in your shell, try nano.

 

quota   -v         Display your quota

 

rename                  rename a file. Same as  mv.

rm                         remove file.     examples: rm oldfile.cpp              rm *.exe

 

rmdir                     remove directory. Before a directory can be removed, all of its

rmdir  sub             contents must be removed.

 

script filename      Begin a log of all that appears on the screen and save it in the designated file.  In order to generate a printout of a program run, type something like the following:

 

script  name_p1                      - begin script file called name_p1

cat   myClass.java                        - display source code

cat    main.java

cat   prog1dat.txt                    - display data file (if any)

javac  myClass.java               - compile each class file

javac main.java                      - compile the client

java main                                - execute the program

cat   prog1out.txt                    - display output file (if any)

exit                                                - exit the script

 

sort  file.txt           Write a sorted concatenation of the lines of the file or files to standard output (or a file using  >)

 

tail  file.cpp           Output the last 10 lines of a file or files

 

vi   myfile        enter the vi editor with the file specified. See the VI tutorial for more information.  Upon entering vi the first task is to determine the character on your terminal that terminates entry mode.  On most terminals and terminal emulators esc will work.

vim myfile       use the improved vi editor 

wc  file.java          Output the number of bytes, words, and lines in a file or files

who                       lists users currently on machine

 

VI EDITOR COMMAND SUMMARY

 

The VI editor (stands for VISUAL) is one of the earliest screen oriented editors ever written. While VI is not the only editor found on UNIX systems, it does seem to be found on all of them. There is a very good VI tutorial on SAL from which most of what follows is taken.  To use the tutorial, first type: "man learn" to see how to use the "learn" tutorial.  Then enter "learn vi". The system will remember where you stopped last and restart at that location if you take several sessions to complete the tutorial. The summary below uses the symbol (^) to indicate ctrl. The "learn vi" tutorial does this also and also shows upper case letters when lower case letters are actually typed.    

 

Note:      The carrot symbol (^) below indicates the control key (ctrl) is held down while the second key is pressed.

 

General:

esc                        = make vi listen (leave input mode and return to command mode)(^k on some systems)

                                   

       ^l or ^r                 = redraw the screen

 

Go to Command Mode by entering ( : )

 

Exiting and saving:

:q                         = quit without saving (used when file was not modified)

:wq  or  zz            = save then quit

:q!                        = quit without saving changes

:w                         = save current file

:w newfile            = save current file under name "newfile"

 

Movement:

-       <return>      = Move [ up / down ] one line to first non-blank character

<space>  <backspace>       = Move [ forward / backward ] one character

<arrow-keys>      = Move [ left / down / up / right ] one character

h     j      k     l      = Move [ left / down / up / right ] one character

0     ^     $            = Move to [ first / first non-blank / last ] character on line

       H    M    L            = Move to [ top / middle / bottom ] of screen (caps)

^d   ^u                 = Scroll [ down / up ] one half screen

^f    ^b                 = Scroll [ forward / backward ] a full screen

G                          = Goto end of file

57G                      = Goto line 57

 

Insert text:                 Enter input mode and:

i      a                    = Insert text before ('i') or after ('a') the current character

I      A                   = Insert text at beginning ('I') or end ('A') of current line

o     O                   = Open new blank line after ('o') or before ('O') current line

 

Delete text:

x     X                   = Delete current ('x') or previous ('X') character

dw                        = Delete the current word

dd                        = Delete the current line

D                          = Delete the rest of the line

5dd                      = Delete the next five lines

37Gdd                 = Delete line 37

J                           = Join lines

 

Replace text:

cc                          = Change the current line (del curr line, enter input mode)

r      R                   = Replace one ('r') or more ('R') characters by overwriting

s                           = Substitute the current character with new one(s)

3s                         = Substitute three characters with new one(s)

 

Undo previous change:

u                           = Undo most recent change to the file

 

Inline Searching:

fx                         = Find next        'x' on current line

Fx                         = Find previous 'x' on current line

tx                          = Like 'fx'  but stops one character short

Tx                        = Like 'Fx' but stops one character short

;                            = Repeat previous inline search

,                            = Repeat previous inline search but in opposite direction

 

Global Searching:

Any of these searches will wrap around to the other end of the file if necessary. For example: /sample<cr> would locate the next occurrence of the word "sample".

/text                             = Search forward for some <text>

?text                     = Search backward for some <text>

n                           = Repeat the previous search for the 'next' occurrence

N                          = Repeat the  previous search but in the opposite direction

        ' '  (Two single quotes)      = Go back to where you where previously

 

Global Search and Replace (s below is short for substitute):

 

:1,$s/*.cpp/*.cxx/g            

= From the beginning of the file(1) to the end($) change the suffix of each C++ filename to “cxx” ( g means global) .

 

:40,60s/old/new/

= From lines 40 through 60, change the first occurrence of ‘old’ on each line with ‘new’. (Notice the lack of a “g”.)

 

Many other variations are possible with the “s” command for those with sufficient bravery. Be advised however, that thoughtless and careless individuals should avoid this command.

 

Alteration Using Targets:

These are more general applications of the 'dd' and 'cc’ commands.  The 'd' and 'c' commands can be used in conjunction with all of the following:

 

-      return    space

^     $     0     H    L     M

w    b     e      W   B     E     (      )

f      F     t      T     ;      ,

/      ?     n     N

 

For example:

dl                  : delete the character to the right

dw                 : delete the next word

d$                 : delete to the end of the line

d)                  : delete to the end of the sentence

dL                 : delete to the bottom of the page

dG                : delete to the bottom of the file

dtx                : delete up to but not including the next 'x'

d/bye(return)       : delete up to the next occurrence of "bye"

c0                  : change from here to the beginning of the line

cb                  : change the previous word

c3w               : change the next three words

 

Moving Text (delete then put):

Any time text is deleted, a copy is saved (until the next delete) in a buffer.  Let us call it the paste buffer. The 'p' and 'P' commands stand for "put". These commands will insert the most recently deleted text back in the file.  As the delete text remains in the buffer, it can be 'put' back in as many times as necessary.

 

p     P            :      Insert the contents of the paste buffer [ after / before ] the current line/character.

 

Copying Text (yank then put):

The yank command (yy) memorizes text without deleting it. (Same buffer as delete buffer??)  If one wants to copy the next five lines to another place, first type '5yy' at the beginning of the text, then move to the destination and type 'p'.  Just  as with deleted text, as many copies can be made as desired. Furthermore, yank can be used in conjunction with any of the targets that 'd' and 'c' can use.