Example file names:
HelloWorld.java, readme.txt, assigment1, tut_12, "my documents", myPhoto.jpg, .login
The 'Reference' corresponds to where Unix begins searching for a file. There are two possibilities:
Example: Directory tree (partial)
/
|
+----+----+--------+
| | | |
bin dev etc Users
| |
+ +----------------------------------+
| | |
passwd tjwhitfort wkvarcoe
| |
+-------+--------+--+----------+ +-------+
| | | | | |
.login ProgEnv geelong.txt public_html ProgEnv x.txt
| | |
+------+-----+ +----------+ +
| | | | |
Hello.java assign default.gif index.html Tut1.java
|
+------+-------+
| |
Assign1.class Assign1.java
/ /bin /etc /Users /Users/tjwhitfort /Users/tjwhitfort/ProgEnv /Users/tjwhitfort/ProgEnv/assign /Users/tjwhitfort/public_html /Users/wkvarcoe
/etc/passwd /Users/tjwhitfort/geelong.txt /Users/tjwhitfort/ProgEnv/assign/Assign1.java /Users/tjwhitfort/public_html/index.html /Users/wkvarcoe/x.txt
(Assume the present working directory is /Users/tjwhitfort)
ProgEnv (refers to /Users/tjwhitfort/ProgEnv) ProgEnv/assign (refers to /Users/tjwhitfort/ProgEnv/assign) public_html (refers to /Users/tjwhitfort/public_html) . (refers to /Users/tjwhitfort) .. (refers to /Users) ../wkvarcoe (refers to /Users/wkvarcoe) ../.. (refers to /) ../../etc (refers to /etc)
(Assume the present working directory is /Users/tjwhitfort)
geelong.txt (refers to /Users/tjwhitfort/geelong.txt) ProgEnv/Hello.java (refers to /Users/tjwhitfort/ProgEnv/Hello.java) ProgEnv/assign/Assign1.java (refers to /Users/tjwhitfort/ProgEnv/assign/Assign1.java) public_html/index.html (refers to /Users/tjwhitfort/public_html/index.html) ../wkvarcoe/x.txt (refers to /Users/wkvarcoe/x.txt) ../../etc/passwd (refers to /etc/passwd)
(Assume the present working directory is /Users/tjwhitfort/ProgEnv)
Hello.java (refers to /Users/tjwhitfort/ProgEnv/Hello.java) ./Hello.java (refers to /Users/tjwhitfort/ProgEnv/Hello.java) assign/Assign1.java (refers to /Users/tjwhitfort/ProgEnv/assign/Assign1.java) ../geelong.txt (refers to /Users/tjwhitfort/geelong.txt) ../public_html/index.html (refers to /Users/tjwhitfort/public_html/index.html)
(Assume the home directory is /Users/tjwhitfort; for these examples it doesn't matter what the present working directory is)
~ (refers to /Users/tjwhitfort) ~/ProgEnv (refers to /Users/tjwhitfort/ProgEnv) ~/ProgEnv/Hello.java (refers to /Users/tjwhitfort/ProgEnv/Hello.java) ~/.. (refers to /Users) ~/../.. (refers to /)
Determining relative addresses:
Example (home directory /Users/tjwhitfort)
cd /Users/tjwhitfort (pwd is /Users/tjwhitfort)
. (pwd is /Users/tjwhitfort/ substitute /Users/tjwhitfort/ for . =
/Users/tjwhitfort/)
.. (pwd is /Users/tjwhitfort/ remove last directory for the .. =
/Users/
Hello.java (pwd is /Users/tjwhitfort/ combined with Hello.java =
/Users/tjwhitfort/Hello.java)
ProgEnv/assign (pwd is /Users/tjwhitfort/ combined with ProgEnv/assign =
/Users/tjwhitfort/ProgEnv/assign)
../wkvarcoe/x.txt (pwd is /Users/tjwhitfort/, remove last directory for the .. gives
/Users/ combined with wkvarcoe/x.txt = /Users/wkvarcoe/x.txt)
../../etc/passwd (pwd is /Users/tjwhitfort/, remove last two directories for the ../..
gives / combined with etc/passwd = /etc/passwd)
cd /etc
~/public_html/index.html (substituting home directory for ~ gives:
/Users/tjwhitfort/ combined with public_html/index.html =
/Users/tjwhitfort/public_html/index.html
~/../wkvarcoe/x.txt (substituting home directory for ~ gives /Users/tjwhitfort/ and remove
single directory for .., gives /Users/ combined with wkvarcoe/x.txt =
/Users/wkvarcoe/x.txt)
ls is used to list the files in a directory. The files are generally listed in alphabetical order in columns, from top to bottom left to right. Uppercase (capitals) come before lowercase when sorting into alphabetical order.
Format (simplified)
ls [-adlFR] [names]
Note: [] indicates optional
ls
ProgEnv geelong.txt public_html
ls
Assignment2 ProgEnv dumpster nsmail tmp Desktop Test.class geelong.txt public_html Documents Test.java mac tim.outbox
ls /Users ProgEnv /Users: tjwhitfort wkvarcoe /ProgEnv: Hello.java assign
Lists all the files in the directory. This shows all files including hidden files. Hidden files have names starting with a "." e.g. .login
ls -a
.login Progenv geelong.txt public_html
Displays a listing of files using the long format.
ls -l
drwx------ 2 tjwhitfort itstaff 9 May 4 20:24 ProgEnv -rw-r--r-- 1 tjwhitfort itstaff 77 Apr 30 10:00 geelong.txt drwxr-xr-x 5 tjwhitfort 1234 4096 Apr 30 10:14 public_html
Explanation for the first line above
drwx------ The first column are the file protections: (more about these later) 2 The number of links to the file. tjwhitfort The username (or number) of the owner of the file. itstaff The group name (or number) the user belongs to. 9 The size of the file in bytes. May 4 20:24 The date the file was last modified. If the year isn't stated then it is the current year ProgEnv The name of the file.
Options can often be combined. Combining the -a (all) and -l (long format) option. ls -al is the same as ls -a -l
ls -al
-rwx------ 2 tjwhitfort itstaff 152 May 7 20:24 .login drwx------ 2 tjwhitfort itstaff 9 May 4 20:24 ProgEnv -rw-r--r-- 1 tjwhitfort itstaff 77 Apr 30 10:00 geelong.txt drwxr-xr-x 5 tjwhitfort 1234 4096 Apr 30 10:14 public_html
Display a listing of files in the current directory without showing the contents of subdirectories. This is useful when using wildcards (* or ?) as files in subdirectories are automatically shown when using wildcards.
ls -d
ProgEnv geelong.txt public_html
ls -d g*
geelong.txt
Display a listing of files showing identifying binary (executable) files with a trailing * and directories with a trailing /.
ls -F
ProgEnv/ geelong.txt public_html/
Display a listing of files in the specified directory and Recursively display the files in all of its subdirectories
ls -R
ProgEnv geelong.txt public_html ./ProgEnv: Hello.java assign ./ProgEnv/assign: Assign1.class Assign1.Java ./public_html: default.gif index.html
ls /User/tjwhitfort ls -l /Users/tjwhitfort/ProgEnv/assign ls -d /Users/t* ls -a /etc
ls (same as ls /Users/tjwhitfort) ls . (same as ls /Users/tjwhitfort) ls ~ (same as ls /Users/tjwhitfort) ls -a ProgEnv (same as ls -a /Users/tjwhitfort/ProgEnv) ls ProgEnv/assign (same as ls /Users/tjwhitfort/ProgEnv/assign) ls .. (same as ls /Users) ls -l ../wkvarcoe (same as ls -l /Users/wkvarcoe) ls -al ../.. (same as ls -al /) ls ~/ProgEnv/assign (same as ls /Users/tjwhitfort/ProgEnv/assign)
The shell also provides a pattern matching facility, (called globbing) where a pattern is typed instead of a file name. The shell then replaces that pattern with all the pathnames that match it before executing the command.
Two main wild card characters are:
Note: ? and * don't match a "." in the first character position.
ls a* (Displays all filenames with "a" as the first character) ls *.* (Displays all filenames containing a "." anywhere in the name) ls * (Displays all filenames (same as: ls)) ls ?h* (Display all files with a "h" character in the second position) ls ???? (Display all files with a 4 characters in the name) cp * public_html (Copy all of the files in the current directory to the subdirectory named public_html) mv a* .. (Move all files with names starting with an "a" to the parent directory) rm ProgEnv/*.class (Remove (delete) all files in ProgEnv that end in ".class" e.g. all class files) rm a?c* (Remove (delete) all files that have "a" as the first character, and "c" as the third character)
| Wildcard | Matches |
|---|---|
| [abc] | A single character that is one of those shown in the brackets (e.g. one of "a", "b" or "c") |
| [i-m] | A single character that is the range shown in brackets (e.g. one of "i", "j", "k", "l" or "m") |
| [!wxyz] | A single character that is NOT one of those shown in the brackets (e.g. not "w" and not "x", and not "y" and not "z") |
| [!b-e] | A single character that is not on of the letters in the rangle shown. (e.g. not "b" and not "c" and not "d" and not "e") |
ls [cv]*[abc] (Displays all filenames with "c" or a "v" as the first character and end in either "a", "b" or "c") ls [a-z]*.java (Displays all filenames that start with a lowercase letter and end in .java) ls g[!e]? (Displays all filenames that have 3 character names that start with a "g", and don't have an "e" as the second character) ls *[!d-f] (Displays all filenames that don't end in "d", "e" or "f")
Other wild cards are available.
Files can be displayed using cat, less or more. cat is generally used for viewing very small files. less and more display a text file a screen at a time. more can only more forwards through a file, whereas less can more forwards and backwards. Use cat where the file is small enough to fit on the screen. For larger files use less
cat (concatenate) can be used to display files or concatenate (join together) files.
cat file-name cat file-name file-name ... > file-name
cat .login (Display the contents of the text file .login) cat page1.txt page2.txt page3.txt > mystory.txt (create a new file called mystory.txt that has contains a copy of the files page1.txt, page2.txt and page3.txt)
less file-name
The less command can be used to view text files, and move backwards and forwards in the file. Below are a number of commands that can be used within less by pressing the appropriate keys or keys. Use the less command is in preference to more as it is far more flexible.
Note: in the following n represents a number.
| Command | Description |
|---|---|
| h or H | help - summary of commands (the commands to use in help are the same as for the less command) |
| q or Q | quit |
| f or space | forward one window |
| b | back one window |
| nf | forward n lines e.g. to go forward 5 lines, type 5f |
| nb | back n lines e.g. to go back 30 lines, type 30b |
| d | down (forward) one half window |
| u | up (back) one half window |
| down-arrow or Enter or Return |
scroll down 1 line |
| up-arrow | up 1 line |
| nEnter or nReturn |
scroll down n lines e.g. 20<Enter> scrolls down 20 lines |
| n% | reposition at the line n percent through file e.g. 20% scrolls to the line 20% through the file |
| g or < | go to the start of the file |
| G or > | Go to the end of the file |
| r | repaint screen |
less ProgEnv/Hello.java less ../wkvarcoe/ProgEnv/tut1.java less /etc/passwd
Head and tail can be used to get the first n lines from a file and the last n lines in a file respectively. By default 10 lines are displayed. Head and tail are often used in conjunction with another command.
head [-n n] file-name tail [-n n] file-name
head geelong.txt (Display the first 10 lines of the file geelong.txt) head -n 5 geelong.txt (Display the first 5 lines of the file geelong.txt) tail ProgEnv/Hello.java (Display the last 10 lines of the file Hello.java) tail -n 1 ProgEnv/Hello.java (Display the last line of the file Hello.java)
On some systems the command can be shortened by omitting the n. e.g.
tail -20 ProgEnv/Hello.java (Display the last 20 lines of the file Hello.java)
Text files consist of simple characters such as digits and letters that can be displayed on the screen. Text files can be displayed, created or modified using a text editor. Text files can also be displayed using the commands: cat, less or more. Some text editors available are: gedit, nedit, pico, ee, vi, emacs. Some of these editors will be discussed later.
Some files are binary files. Examples of binary files are executable programs and graphics files; these files contain specially encoded information that is not in a useful format for humans to read. For example a graphics file needs a special program to interpret and display the data in the file.
gedit is an editor that uses a graphical user interface (menus, mouse etc). It is easy to use and has some nice features for writing programs. This will be the recommended editor for creating Java programs (although any text editor will do).
gedit [filename]
gedit& Create a new file without specifying its name gedit Hello.java& Open a new or existing file called 'Hello.java'
gedit has many features in common with a Word Processor, but requires an <Enter> at the end of each line of text.
It can save a lot of time to learn the shortcut keys. Many of the shortcut keys are the same as for windows e.g. File Menu: New (Ctrl+N), Open (Ctrl+O), Save (Ctrl+S), Print (Ctrl+P); Edit Menu: Undo (Ctrl+Z), Cut (Ctrl+X), Copy (Ctrl+C), Paste (Ctrl+V)
gedit provides a number of preferences that are useful for programmers. Select Edit > Preferences to change preferences. Preferences has a number of tabbed pages: View, Editor, Font & Colors, Syntax Highlighting and Plugins. Suggested settings are outlined below:
Some useful preferences settings are:
Click the Close button to save the changes.
Ensure that the Toolbar and Statusbar are displayed: By ticking: View > √ Toolbar, View > √ Statusbar
Written by Tim Whitfort.