Introduction to UNIX / Linux


The Command-line

Text-based interface.
Type commands with arguments:

$ command (arg1) (arg2) ...
You can use this as an adjunct to (rather than replacement of) the GUI interface.

Can launch GUI programs:

$ gedit file &
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/staff/jsmith

cd                      Change directory
ls                      List files

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

.                       Current directory
/                       root directory
$HOME                   home directory
$HOME/public_html       public web space
/tmp                    system temporary files

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


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/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 before Command Directory after Notes
/users/group2/user cd /users/group1 /users/group1 Absolute path command
/users/group1 cd user/shell /users/group1/user/shell Relative path command
/users/group1 cd ../group2 /users/group2 Relative path command




Case sensitivity

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

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

Advantages:

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



Diversion - 404 redirection

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


My 404 handler

So this is what I do to implement an error-tolerant web server. I put a .htaccess file in:
$HOME/public_html/.htaccess
This .htaccess file has 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/*

not for mis-spellings higher up:

http://computing.dcu.ie/~humphreys
http://computing.dcu.ie/~nonexist


Override browser error message

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

Q. Would HTTP return code 3xx help?





Back to UNIX - Filenames and Special Characters

Long file names and multiple periods OK.
e.g. product.4652.suppliers.us.html


Avoid special chars

Because files are designed to be worked with on the command-line, rather than just point-and-click, it is best to avoid many special characters in filenames.

On many UNIX/Linux distributions (e.g. openSUSE) you can actually put these chars in filenames. But the file may then be hard to work with at the command-line, and scripts may crash.

Avoid these chars in filenames, because they may get confused with the instructions for command-line programs:

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

* wildcard
? wildcard
^ start of line
$ end of line / variable value
[ pattern matching
] pattern matching
\ "quoted" character
/ should be in pathname, not filename
' string delimiter
" string delimiter
! shell history

If you do actually just point-and-click your UNIX files (which is possible too) then you can allow many of these characters.
But if you're going to use the command-line, best to just use:

 0-9
 a-z
 A-Z 
 .
 -
 _




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.

firefox &              Launch Web browser from command-line
firefox "URL" &
                        






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


find                    Find files by name or date
find . -mtime -1        Files modified today



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

on DCU Solaris:
$ which grep
/usr/bin/grep
$ which ls
/usr/bin/ls

on DCU Linux:
$ which grep
/usr/bin/grep
$ which ls
(returns nothing - alias)



on DCU Solaris:
$ type grep
grep is a tracked alias for /usr/bin/grep
$ type ls
ls is a tracked alias for /usr/bin/ls

on DCU Linux:
$ type grep
grep is /usr/bin/grep
$ type ls
ls is aliased to `/bin/ls $LS_OPTIONS'



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


cal                     Calendar
cal 11 1818             Calendar for Nov 1818





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


df -h                   Show space on all disks
df -k                   exact kilobytes

du                      Space used by me

w                       Who is logged in
 
wget -q -O - URL        Download URL

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


Processes



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.




Interrupts

Usage seems to vary on different variants of UNIX and Linux. You may get something like:

Ctrl-S                  Pause
Ctrl-C                  Interrupt
Ctrl-D                  Kill, Logout

Ctrl-Z                  EOF
 
q		exit man, more



UNIX Tutorials