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 [-adlR] [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
-a Option
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
-l Option
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.
ls -l filename
Displays a long listing about a directory or file
ls -l ProgEnv
drwx------ 2 tim itstaff 9 May 4 20:24 ProgEnv
-al Options
Combines 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
-R Option
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
-d Option
Display a listing of files in the current directory without showing the contents of subdirectories. This is useful when use wildcards (* or ?) as files in subdirectories are automatically shown when using wildcards.
ls -d g*
geelong.txt
Using absolute references
ls /User/tjwhitfort ls -l /Users/tjwhitfort/ProgEnv/assign ls -d /Users/t* ls -a /etc
Using relative references (home and present working directory is /Users/tjwhitfort)
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 (tcsh) also provides a pattern matching facility, (called globbing) where a pattern is typed in instead of a file name. The shell then replaces that pattern with all the pathnames that match it before executing the command.
Two main wildcharacters are:
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)
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. Some commands in less are q to quit, space or f for forward one screen, b for back one screen..
cat file-name
less file-name
more file-name
cat geelong.txt more geelong.txt less ProgEnv/Hello.java less ../wkvarcoe/ProgEnv/tut1.java less /etc/passwd
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: 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.
nedit 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).
nedit [filename]
nedit& Create a new file without specifying its name nedit Hello.java& Open a new or existing file called 'Hello.java'
nedit 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. Cut (Ctrl+X), Copy (Ctrl+C), Paste (Ctrl+V), Undo (Ctrl+Z), Save (Ctrl+S).
nedit provides are number of preferences that are useful for programmers. The preferences can be changed temporarily or permanently. To change preferences until you exit nedit select options from the Preferences menu (e.g. Preferences > Statistics Line). To save your preferences permanently use Preferences > Default Settings >(set the desired preferences), then Preferences > Save Defaults.
Some useful preferences settings are:
If you would like to use a different font size, set the following
To save the above settings you must:
To activate the preferences exit nedit then run it again.
Special directories are:
Display the path name of the present (current) working directory
pwd
pwd
/Users/tjwhitfort
change the present working directory. This affects commands that use relative addresses, as the addresses are relative to the current working directory.
cd [directory]
cd / (Changes directory to the root directory) cd /etc (Changes to the etc directory /etc) cd /Users (Changes to the Users directory /Users) cd /Users/tjwhitfort (Changes to the directory /Users/tjwhitfort) cd /Users/tjwhitfort/ProgEnv (Changes to the directory /Users/tjwhitfort/ProgEnv) cd /Users/wkvarcoe (Changes to the directory /Users/wkvarcoe)
cd ProgEnv (down one directory level to /Users/tjwhitfort/ProgEnv) cd ProgEnv/assign (down two directory levels to /Users/tjwhitfort/ProgEnv/assign) cd ./ProgEnv (the same as cd ProgEnv above) cd .. (up one directory level to /Users) cd ../wkvarcoe (up one then down one directory level to /Users/wkvarcoe) cd ../../etc (up two directory levels then down to etc to /etc)
cd (Changes to the users home directory e.g. /Users/tjwhitfort) cd ~ (Change directory to /Users/tjwhitfort) cd ~/ProgEnv (Change directory to /Users/tjwhitfort/ProgEnv) cd ~/ProgEnv/assign (Change directory to /Users/tjwhitfort/ProgEnv/assign) cd ~/.. (Change directory to /Users)
makes (creates) a new directory.
mkdir directory
mkdir ITFun (create subdirectory called ITFun: /Users/tjwhitfort/ITFun) mkdir ProgEnv/tutes (create subdirectory called tutes: /Users/tjwhitfort/ProgEnv/tutes) mkdir /Users/tjwhitfort/photos (create subdirectory called photos: /Users/tjwhitfort/photos)
mkdir test (create subdirectory called test: /Users/tjwhitfort/public_html/test)
removes (deletes) a directory. If the directory is not empty then the command fails. Also see rm.
rmdir directory
rmdir ITFun (remove subdirectory called ITFun: /Users/tjwhitfort/ITFun) rmdir ProgEnv/tutes (remove subdirectory called tutes: /Users/tjwhitfort/ProgEnv/tutes) rmdir /Users/tjwhitfort/photos (remove subdirectory called photos: /Users/tjwhitfort/photos)
Commonly used file maintenance commands are cp (copy), mv (move/rename) and rm (remove/delete).
copy a file or files.
cp [-r] file1 target-file
Warning: if the target file is an existing file it will be overwritten.
cp Test.java Test2.txt (copies /Users/tjwhitfortTest.java to /Users/tjwhitfortTest2.java)
cp Test.java ProgEnv (copies /Users/tjwhitfort/Test.java to
/Users/tjwhitfort/ProgEnv/Test.java)
cp public_html/index.html . (copies /Users/tjwhitfort/public_html/index.html to
/Users/tjwhitfort/index.html)
cp assign/* Backup (copies the files in /Users/tjwhitfort/assign into the directory
/Users/tjwhitfort/Backup)
cp -r assign/* Backup (copies the files and subdirectories in /Users/tjwhitfort/assign
into the directory /Users/tjwhitfort/Backup)
cp -r assign Backup (copies the directory /Users/tjwhitfort/assign and all its files
and subdirectories into the directory /Users/tjwhitfort/Backup/assign
moves or renames a files. If the file is being moved from another directory to the current working directory then use "." as the new-file-name.
mv old-file-name new-file-name
Warning: if the new-file-name is an existing file it will be overwritten.
mv Test.java Test3.java (renames Test.java as Test3.java)
mv Test.java ProgEnv (moves Test.java to /Users/tjwhitfort/ProgEnv/Test.java)
mv Test.java ProgEnv/assign (moves /Users/tjwhitfort/Test.java to
/Users/tjwhitfort/ProgEnv/assign/Test.java)
mv public_html/index.html . (moves /Users/tjwhitfort/public_html/index.html to
/Users/tjwhitfort/index.html)
mv ProgEnv/* PE (moves the files and subdirectories in the directory
/Users/tjwhitfort/ProgEnv to /Users/tjwhitfort/PE)
mv ProgEnv PE (renames the directory /Users/tjwhitfort/ProgEnv to
/Users/tjwhitfort/PE)
removes (deletes) files or directories. The -r or -R switch removes all matching files in subdirectories as well. Use wildcards (*, ?) and -r with extreme care. Do an ls first to see what files would be affected.
rm file rm [-r] directory
Warning: use with extreme care.
rm Test.java (removes Test.java)
rm *.java (removes all files that end in ".java" from the current directory)
ls -a (List the files to check what would be deleted by the following command) rm * (Removes all files in the current directory) ls -aR ProgEnv (List the files to check what would be deleted by the following command) rm -r ProgEnv (Removes the directory ProgEnv including all files and subdirectories in it) ls -aR (List the files to check what would be deleted by the following command) rm -r * (Removes all files and subdirectories in the current directory)