As a Unix user you may primarily work with files that other people have already created. However, you will still need to know how to create, name and edit your own files. In this workshop you will create, edit and share directories and files.
We will first take a look at how files should be named in Unix.
Naming Files in Unix
It is important to be aware of the file naming conventions in Unix, especially the use of case and meta-characters.
If you are used to other operating systems, you may not be accustomed to thinking about uppercase versus lower case letters. In Unix, case matters. Compare the following names:
memo.1 Memo.1 MEMO.1 meMO.1
In Unix, these are all different file names.
Using Spaces and Other Meta-Characters
Meta-characters are characters that are interpreted by Unix as special commands or instructions. These characters include spaces, semicolons, backslashes, dollar signs, question marks, and asterisks. It is best to avoid the use of these in filenames. In place of a space, people often substitute a "_".
Fortunately, Unix still gives you a lot of flexibility in naming files. File names in Unix can be up to 255 characters long!
Listing Your Files
Now that we understand how files are named, let's see what files and directories are currently in our account.
The ls command provides a listing of files and directories.
Step 1.To see the contents in your account, type:
On the mercury demonstration accounts, you see two directories listed: bin/ and www/.
Understanding Directory Structure & Paths
Before we start creating, moving, modifying, and deleting items in our account, we need to talk about where our account is in relationship to everything else in the server. When we typed "set", we found our home directory on the "PWD" line.
A generic account's home directory looks like this:
The path to a file or directory is merely all the directories from the top most level of the machine to the file, with slashes (/) placed between the directory names. The diagram below illustrates these concepts.
NOTE: Graphical operating systems like Windows and MacOS often call directories folders. Sometimes you'll hear them called folders in Unix too. They mean the same thing.
Viewing a Sample Directory Structure
This diagram represents a generic Unix directory structure:
One of the major advantages of Unix is its hierarchical file system. It is a tree of directories (folders) and files (text, html, data, scripts, etc). The directory at the top of the tree is called the root directory, which is denoted by a single /. Inside each of these directories can be subdirectories and files. In Unix, some common subdirectories under the root directory are /etc, /usr, and /dev.
Moving Through Directory Structures
In Unix, whenever you need to supply the name of a directory, you can use "." (a single period) as shorthand for my current directory and ".." (a double period) as shorthand for one directory up from my current directory.
Let's combine the lscommand with the.. shorthand to see the contents of the directory two levels above us (using ls ../..).
Step 1.To list the directory two levels above you, type:
ls ../.. Enter
NOTE: On mercury if you try to look at the directory directly above your home directory you will get an error message as you do not have permission to view this directory.
We would like to see what is contained in the etc folder in the directory listing that was returned.
Step 2.To see a list of the directories and files in etc, type:
ls ../../etc Enter
This last directory contains more directories than may be displayed on one screen. We can use the less command to control the output of other commands.
Redirecting Output with the less Command
The less command may be used to display information one screen at a time. Let's use it to easily read the contents of the etc directory that contains other directories.
The "|" (pipe) redirects the output of one command into another command. We will redirect the output of the lscommand into the less command.
NOTE: The "|" (pipe) character on most keyboards is located under the Backspace key.
Step 1.To list the directory that contains many other directories, and display that list one screen at a time, type:
ls ../../etc | less Enter
You may page down through this list using the spacebar.
Aborting a Command
Sometimes, and in this instance, we might want to end the output of a command.
Step 1.To end the output of this less command, type:
We are returned to the system prompt; our less command was stopped.
NOTE: Other important key combinations used in Unix are Esc keyor Control key+c, and will usually stop or get you out of any process you wish to quit.
Making Directories (Folders)
Your interface with the Unix computer is limited to 24 lines of text on your screen. After you have created more than 24 files, the list of the contents of your home directory will not fit on one screen. Directories help us to keep our files organized.
We will create a directory named "Shared" with the mkdir command, which stands for make directory.
Step 1.To create a directory, type:
mkdir Shared Enter
NOTE: Some Unix texts suggest using uppercase to begin directory names, and lowercase to begin filenames. Since Unix is case sensitive, and uppercase is given priority over lowercase, using uppercase to begin directory names will keep all your directories at the beginning of your listing.
Step 2.To list your account contents again, type:
You see the new Shared directory listed.
Understanding Your Working Directory
It is easy to get lost in Unix's directory structure. You can always tell where you are in the structure by using the pwd command, which tells Unix to print the working directory (i.e., the current directory).
Step 1.To see your working directory, type:
The host returns a path statement something like this:
This is your current working directory, which is always your home directory when you first log in. We want to put our files in the Shared directory we just created.
Changing Directories With cd
The cd command stands for change directory. The purpose of this command is to change from the directory you are currently in to the new directory you would like to see.
Using More Advanced Command Syntax
Some commands may be run explicitly on a file or directory.
The following syntax is used whenever you use a command that performs an action on a file or directory:
[command] [file or directory name]
To move into a directory we simply type cd and the name of the directory we want to move into.
Step 1.To change into the Shared directory , type:
cd Shared Enter
The Shared directory is now our present working directory.
There are several different ways to use this command. We will revisit the cd command a little later in the workshop.
Step 2.To verify your present working directory, type:
Notice that you are inside your Shared directory.
Using a Text Editor in Unix
To create files for use in workshop, you will use a menu-prompted text editor called Nano.
Step 1.To launch the Nano editor, type:
You see a blank writing space with a menu of commands available across the bottom. The contents of the writing space is called a buffer.
NOTE: In the menu, the "^" character represents pressing the Control key on the keyboard. The commands are entered as a simultaneous combination.
Step 2.To begin creating the document, type:
Your email AddressEnter
Step 3.To begin to save the document and exit the Nano buffer, press:
Nano asks if you want to save the buffer.
Step 4.To save what you have just typed, press:
Nano asks you to name your new document.
Step 5.To give your new document a name, type:
You have exited Nano, and the file has been saved on your account.
NOTE: Unix does not need file extensions like.txt, but it is a good idea to use them anyway. This is especially helpful for transferring files to a Windows platform, which requires file extensions.
Step 6.To view the contents of your current directory, type:
You see that the file private.txt has been added.
Modifying an Existing File
You can modify your existing private.txt file to create a new file meant for public use.
Step 1.To launch Nano and open the existing file, type:
nano private.txt Enter
Step 2.To move to the bottom of the file, press:
Step 3.To add a sentence, type:
A short sentence
Step 4.To save the file, type:
Nano asks you if you want to use the same name for this modified
Step 5.To erase the original name, press:
Backspace several times
Step 6.To give your modified file a new name, type:
NOTE: Since you saved the modified document with a new name, your old document is still intact.
Step 7.To confirm the new name, type:
Step 8.To exit Nano, press:
Step 9.To view the contents of your directory, type:
You see the additional file listed.
Using Command Options
With many commands there is a list of options that you can use to better define your intentions.
To modify a command with an option, use the following syntax:
Explicitly, you type the command, a space, a dash, and the option.
The ls command issued with the option -l will list the contents of a directory in long form, which contains information on the mode, owner, group, size, and date/time of the files and directories.
Step 1.To list the files long, type:
ls -l Enter
You see something like the following:
-rw-r--r-- 1 jump031 mypage 39 Sept 1 10:00 private.txt
-rw-r--r-- 1 jump031 mypage 56 Sept 1 10:00 public.txt
The three categories on the right are self-explanatory, and we will discuss the three categories on the left a little later in today's workshop.