Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

Search:

CA249      CA318      CA425      CA651

w2mind.computing.dcu.ie      w2mind.org

Missing
DCU student

CASE3 student Paul Bunbury is missing since Thur 2 Feb 2012.
See appeals on crime.ie and garda.ie and facebook.

He is a great coder. See DCU page and boards.ie page.
He won major coding contests in 2010 and 2011.
He is author of the brilliant "FloodItWorld".
DCU can confirm that in Jan 2012 he passed all 6 modules comfortably.


Introduction to UNIX / Linux


The Command-line

Text-based interface.
Type commands with arguments at a "prompt":

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

The "prompt" may be anything, not necessarily "$".

Note I have changed my prompt so it looks different to yours. It doesn't matter what the prompt is.

Can launch GUI programs:

$ gedit file &


PATH

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





Absolute and relative paths

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 often matters on Web).

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

Advantages of case sensitivity:

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 on valid URLs (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 Program:
ErrorDocument 404  /cgi-bin/user/prog
The program it redirects to is a CGI script.
The CGI script looks at REDIRECT_URL and 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 
 . (note: at start of filename will make file hidden)
 - (note: bad at start of filename, looks like command-line switch)
 _





Some Commands to get started

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

man and more - space for next page, q to quit


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
 may return nothing if prog is an alias


type (prog)
 returns path of prog
 or else shows what prog is alias for


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 (on some systems)
lp -Pl128 (file)        Print on printer l128 (L128)

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
           (see this when you ssh student.computing.dcu.ie)
 
wget -q -O - URL        Download URL

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



Feeds      HumphrysFamilyTree.com

Bookmark and Share           On Internet since 1987.