Introduction to UNIX

The Command-line

You type commands, UNIX runs them:

$ command (arg1) (arg2) ...
You can use this as an adjunct to (rather than replacement of) the menu-driven utilities.

The "prompt" may be anything, not necessarily "$".
Most commands are not in-built, but are programs, found somewhere in the "path" variable:

echo $path
This makes the command-line "shell" a reasonably simple program:
See if there exists in the directories listed in the variable $path an executable file with the same name as the 1st "argument". If so, execute this file, passing it the other arguments. Else print error message.

Note: The complete list of executable files in the path is normally read once and cached in memory for fast access every time a command is typed.




Hierarchical Directory (Folder) structure

pwd                     Print working directory 
                        e.g. /users/ca2/loginid.ca2

cd                      Change directory
ls                      List files

cd ..                   Go to parent directory 
                        e.g /users/ca2/ 

.                       Current directory
/                       root directory
$home                   home directory
$home/.cshrc            personal initialisation file
/var/mail/$user         incoming email
/tmp                    system temporary files

Hierarchical file system - /directory/sub-directory/file
Forward slash (this is why Web is forward slash).

See also the name of slashdot.org


The "path" environment variable   v.   The "path" of a file:
Confusingly, when we refer to the absolute location of a file in the file system:   /dir/dir/dir/sub-dir/file,   we call this the "path" of the file, even though we are already using the word "path" to refer to one of the major environment variables.





Directory         Command          Directory 
before                             after


---- Absolute path commands: ---------------------------

/users/cae2/user   cd /users/ca2   /users/ca2


---- Relative path commands: ---------------------------

/users/ca2         cd user/shell   /users/ca2/user/shell
/users/ca2         cd ../cae2      /users/cae2




Case sensitivity

Case matters in filenames in UNIX (this is why case matters on Web).

Question - Is case sensitivity ever a good thing? Or is it just a flaw in UNIX (and C/C++)?

Advantages:

Not much return for such huge disadvantages: Can you think of any more?




404 redirection

Note the apache web server allows 404 to remap to a script instead of just a standard error page. The script could then do a case-insensitive search (perhaps pre-build a list of all files, and then   grep -i   on the input string).

In fact, I do this now. I use a .htaccess file with an ErrorDocument line to redirect 404's to a CGI script. The CGI script does a case insensitive and partial-line match on a pre-built list of all URLs. e.g. Try something hopelessly wrong like:

http://computing.dcu.ie/~humphrys/NOTES//Unix///INTRO/nonexist.html
or:
http://computing.dcu.ie/~humphrys///Somewhere-In-Notes///intro.html
This works for my sub-site only.
i.e. only for:

http://computing.dcu.ie/~humphrys/*

Useful tip: If IE 5 receives a return code of 404, it may override the server error handling with its own useless error message. You can turn this off at Tools-Options-Advanced- "Show friendly HTTP error messages". But obviously you can't get every client to do that. So to get my script to work, you have to tell IE 5 that it is not an error. i.e. The first 2 lines output by the script are:

Status: 200
Content-type: text/html
Returning 200 does have problems, though, because then spiders do not realise this link is broken. Everything seemed to work just fine. So, for example, all error URLs will be archived in the Internet Archive as well as all real URLs, since the archive cannot tell they are just error screens.



Filenames and Special Characters

Long file names and multiple periods OK, e.g. "product.4652.suppliers.us.html", typically 32 chars allowed (Windows has caught up with this now. Some people on Web learnt with old Windows - seen in their awful web filenames, "p4652sus.htm").

But spaces in filenames not ok. Also, because files are designed to be worked with on the command-line, rather than just point-and-click, some non-alphanumeric chars in filenames may get confused with the instructions for the command-line programs:

# comment
< redirection
> redirection
` result of a program
| pipe
& detach process
; separate multiple commands on the same line

* wildcard
^ start of line
$ end of line
[ pattern matching
] pattern matching
\ "quoted" character
/ should be in pathname, not filename
' string delimiter
" string delimiter
If you do actually just point-and-click your UNIX files (which is possible too) then you can probably allow most (perhaps all) of these characters, and spaces too.

But if you're going to use the command-line, best to just use alphanumeric and '.' and '-' and '_'.




Some Commands to get started

Explore all these by typing "man (command)", e.g. "man ps"


ls                      List files
ls -a                   Show "hidden" files (begin with ".")
ls -l                   Detailed
ls -alR                 Recursive

cat (file)              Type file out in command-line window
more (file)             Type file, pause for each screenful

cp                      Copy files
mv                      Move / Rename files
rm                      Remove files
mkdir                   Make directory
rmdir                   Remove directory
clear                   Clear screen




(prog) &                Launch a process detached 
                         from command-line (e.g. windowed)
(prog)                  Command-line frozen until prog exits.

netscape &              Launch Netscape from command-line
                        (can read email with Netscape Mail
                         and can read usenet newsgroups
                         with Netscape News)





xedit (file) &          Edit file
textedit (file) &       Edit file

To edit in command-line window (e.g. when logged in through telnet):
 vi, emacs


grep                    Search for a string in a file or files
grep (string) (file)

find                    Find files by name or date


which (prog)            What runs if "prog" is typed
which ls
which xcalc

whereis (prog)		Where the binary, source, manual pages are for this prog
whereis perl
 





tar                     Bundle a lot of files or directories
                         into an archive file
gzip (file)             Compress a file (e.g. an archive file)
gzip -d (file.gz)       Uncompress file


ghostview (file.ps) &   View a PostScript file

To view Windows-type files see StarOffice.


xcalc &                 Calculator

cal                     Calendar
cal 11 1818             Calendar for Nov 1818





lp (file)               Print
lpr (file)              Print
lpq                     See print queue
lprm                    Remove job from queue

printers

ps                      See what processes are running
kill (process id)       Terminate some of your processes
kill -1                 All my processes
kill -KILL (pid)        Definite kill
xkill &                 Kill the next thing I click on      

nice			Run something at low priority deliberately
time			Time a run of some program    
In the setup at DCU, you each have your own CPU and memory, sharing a central filesystem. So "ps" will show that the only processes running on the client machine are yours and the Operating System's.
Ctrl-Z                  EOF
Ctrl-S                  Pause
Ctrl-C                  Interrupt
Ctrl-D                  Kill, Logout

df -k                   Show space on all disks
du                      Space used by me

w                       Who is logged in
finger (user)           See if someone is logged in
finger (user@host)
talk (user@host)        Chat to someone

(command) ; (command)   Multiple commands on same line

finger was what people used for self-expression before the Web and personal home pages. You would put your "home page" in your ".plan" file, which finger would display when called.
For security reasons, finger is now often disabled for remote users.





UNIX Tutorials