Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

Online coding site: Ancient Brain

coders   JavaScript worlds

Search:

CA114      CA170

CA686      Projects

Free AI coding exercises


Introduction to UNIX / Linux (More)



Some Commands to get started

Explore commands and options by typing "man (command)"
e.g. "man ps"

clear                   Clear screen
  


Files


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
                        enter for new line, space for next page, q to quit


Manipulating files

Human can use GUI file manager. Programs can do anything a human can do, with these commands. Human sometimes types these commands too.

cp                      Copy files
mv                      Move / Rename files
rm                      Remove files
mkdir                   Make directory
rmdir                   Remove directory
  
See tutorials on manipulating files with cp, mv, rm, etc.


Detach program (&)


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

           
eog file.jpg &		Launch image viewer Eye of GNOME on file
    



Web browsers


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

konqueror "URL" & 




sort


sort 		Sort alphabetically (pipe some stream into sort)
sort -n		Sort numerically
sort -r		Reverse sort 

sort by 5th column:
sort -k 5

old syntax to sort 5th column:
sort +4     

  

grep


grep      Search for a string in a file or files

grep (string) (file)
grep (string) *html 

grep -i (string) (file)  Ignore case
grep -v (string) (file)  Return lines that do NOT match 
  

find

find      Find files by type/date/name, in this dir or below 

find -type d       Find all dirs 
find -mtime -1     Find files modified today



du


du                      Space used by me

du | sort -n		# why -n?

default is k

listing in M  
du -BM

listing in G  
du -BG

top-level only
--max-depth=1 

  

Printing


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
  

cal


cal                     Calendar

cal 8 1752              Calendar for Aug 1752
cal 9 1752              Calendar for Sept 1752
cal 10 1752             Calendar for Oct 1752

  

which


which (prog)            
 what runs if "prog" is typed
 may return nothing if prog is an alias

which ls


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

type h
type history


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




Misc


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


w                       Who is logged in
           (see this when you ssh student.computing.dcu.ie)
 
(command) ; (command)   Multiple commands on same line





wget

Command-line HTTP client.


wget -q -O - URL      					  	Download URL, quiet, output to command-line
 
wget -q -O - URL > file.htm 					Output to file

wget -q -O file.htm  URL  					Output to file

wget -q -O - http://site/file.jpg  > file.jpg 		Output JPEG to file (output JPEG to command-line won't work)

wget -q -O file.jpg  http://site/file.jpg   		Output JPEG to file 


 see more on wget





wget appears in this hacking scene from the movie The Social Network (2010).
Perhaps the most realistic hacking scene in the history of movies.





Absolute and relative paths


Relative path of a file

index.html
What file that refers to depends on what directory you are in now.
It looks for index.html in the current directory.
../index.html
is also relative path. It looks for index.html in the parent of the current directory.


Directory before Command Directory after
/users/gdf cd users/ec2 /users/gdf/users/ec2
/users/gdf cd ../ec2 /users/ec2
/users/gdf cd ec2 /users/gdf/ec2



Absolute path of a file

/dir/dir/dir/public_html/index.html
Gives the full "path" from the root down to the file.
Refers to the same file no matter what directory you are in.


Directory before Command Directory after
/users/gdf cd /users/ec2 /users/ec2




Case sensitivity

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

Question: Is case sensitivity a good thing? Or is it a flaw in UNIX?

Advantages of case sensitivity:

  1. More readable code. You know what to expect.
  2. More variables. num and NUM and Num
  3. Set up conventions, so that NUM probably refers to a compile-time-coded constant, num is a real-time-changing variable, etc.
  4. Quicker/simpler searches on file lists, since can just search for the literal string.
  5. Better to be case-sensitive for passwords. - Larger space to pick from. Harder to guess. Good to be "unforgiving" for security.
Not much return for such huge disadvantages:
  1. Millions of programmer and user hours lost on case not right.
  2. Millions of failed "404 Not Found" website hits because of wrong case in the URL.

  
Solutions to "404 Not Found" because of case:
  1. Set up program to handle 404. See My "404 Not Found" Handler
  2. Detach website URLs from underlying (case sensitive) file system.
    e.g. Content Management System.



Filenames and Special Characters

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


Limits


Avoid these chars

If the command-line is used to address files, it is best to avoid many special characters in filenames.

Avoid these chars in filenames, because they have meaning to the Unix command-line and utilities:

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



Stick to these chars

If you're going to use the command-line, best to just use these chars in filenames:

 0-9
 a-z
 A-Z 

Use these inside filename only, not at start or end:
 .  
 -  
 _





ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.