| UNIX Tutorial |
 |
 |
MacOS X and various flavors of Linux are UNIX based operating systems. Because many of the programs used during the Workshop are run from the command line, one needs to know how to use some basic UNIX commands. Among these are how to create files, move files, copy files, delete files, and so on. This page gives an overview of UNIX basics for those who are unfamiliar with them. For an extensive list of other UNIX commands, try one of the numerous resources available on the internet.
Listing Directory Contents
To list all directories and files within a current directory, type:
ls
To list a detailed list of all directories and files sorted by creation date, type:
ls -lrt
Location of Files and Directories
To work with a file or directory, it is important to tell the system where you are. There are two ways to do that: the absolute and the relative path. The absolute path gives the whole address of the file, for example:
/home/mvaring/Desktop/file
The relative path is described with the signs "." and "/":
./ = current directory
../= parent directory of ./ (one directory up)
/ = root directory (top directory)
Where no location is specified, UNIX assumes we mean "./".
To find out which directory you currently are in, type:
pwd
This will show an output such as:
/home/mwaring/
Creating Directories
To create a new directory, type:
mkdir directory1
This will create a directory called directory1
Changing Directories
To go "down" one directory (in this example to go from mwaring to directory1), type:
cd directory1
To go "up" one directory (in this example, to go from directory1 to mwaring), type:
cd ..
Now go back to directory1, create directory2 in it and go to directory2. To go "straight back" to your home directory from directory2, type:
cd
Deleting Directories
To delete a directory, type:
rm -r directory1
This will remove the directory and all the files and directories in it so use this command carefully.
Viewing Files
There are many ways to view a text file. For viewing and editing, see the section on Unix Text Editors (in the menu on the left). One way for simple viewing is to type:
less filename
where "filename" is a file in your current directory. This command displays as much of the file as can fit onto the screen. To scroll up and down within the document, use the arrow keys. Hitting the space bar will bring a new screen-full of information.
To search forward in the file for a given pattern, enter:
/pattern
where "pattern" represents the character string you wish to find.
To exit, press:
q
Copying Files
To copy a file using a terminal, use:
cp
For example:
cp template.html course-schedule.html
This makes a new file that contains the same information as the first file with the name indicated. In the example above, cp takes the content of template.html and makes a file called course-schedule.html that contains all the information in template.html.
cp can also be used to copy a file from one directory to another. For example:
cp /home/tmp/mbl.color.logo.jpg /home/mwaring/images/MBL.jpg
This command will take mbl.color.logo.jpg in /home/tmp and copy it to /home/mwaring/images. It names the copy MBL.jpg. If a name is not specified for the file, a copy with the same name as the original is created. For example:
cp /home/tmp/mbl.color.logo.jpg /home/mwaring/images
Instead of using the whole path, you can also use a relative path in copying, for example, we can copy "fileX" from the current directory to its parent directory using, either:
cp /whole/path/from/root/directory/fileX ../
or:
cp ./fileX ../
or:
cp fileX ../
To copy an entire directory, use the -r flag:
cp -r dir-to-copy/ new-dir/
For example:
cp -r /home/tmp/images/ /home/mwaring/temp-images
copies the directory in /home/tmp called images into the temp-images directory in /home/mwaring. Note: on some computers cp -r does not copy dot-files (files that start with '.', for example .bashrc).
You may run into trouble copying files from or to directories which do not have permission to read or execute.
Moving Files
On a UNIX file system, moving a file is the same as renaming it. The command mv works like:
mv file1 file2
For example:
mv fileformats.html index.html
takes fileformats.html and renames it index.html.
Files can be moved from one location to another:
mv location/file1 location/file2
For example:
mv /home/mwaring/to-do-list /home/mwaring/Tasks
This command moves the file to-do-list from /home/mwaring/ to /home/mwaring/Tasks. To move a file and also rename it, specify the name after the second location. For example:
mv /home/mwaring/more-stuff /home/mwaring/Tasks/additional-tasks
Deleting Files
rm is the command to remove a file. There are flags (options) that can be used with rm. To delete a file, type:
rm filename
For example, to delete a file called coi.nex, type:
rm coi.nex
A prompt will ask you if you really want to delete it. If so, type:
y
To delete a file from a different directory, supply rm with the path. For example:
rm /home/mwaring/coi.nex
Getting Help
While the Workshop is in session, one of your best sources of help are the teaching assistants and support staff. This page will still be accessible after the course, but contains help on only a few basic UNIX commands. Some material may not apply to your home systems. On most systems, typing
man command
invokes information about commands.
For example, to learn how cp works, type:
man cp
To exit the man page, hit:
q
Note: man pages are often a bit obtuse, but are worth reading nevertheless
Connecting to a remote Machine
ssh is a program to connect to another computer. For Linux and MacOS X, there are two ways of using ssh from a terminal:
ssh user@computer
or
ssh computer -l user
For example:
ssh mwaring@gcg.workshop.priv
or
ssh gcg.workshop.priv -l mwaring
You will then be asked for your password.
If you are using a notebook computer that does not already have an ssh client program, you can get one for free. For MacOS 9, visit Nifty Telnet-SSH and download the client. For Windows, visit ssh.com and download the free educational ssh client.
Copying Files between Machines (File Transfer)
Some of you may be familiar with FTP (File Transfer Protocol), which we do not recommend because it is a relatively insecure method of transferring files between computers. To transfer files between machines, you should use:
scp
This command works very similarly to cp, except that the files you are copying reside on different machines.
To copy a file from a remote computer to the computer you are working at, type:
scp user@remote-computer:/remote/path/remote-file /local/path/file
You will then be prompted for the user's password on the remote computer. After you enter it, the file will be copied. For example:
scp molly@bigbox.university.edu:/home/molly/Info/info /home/mwaring/facts/
This command will copy the file called 'info' that lives on bigbox.university.edu at /home/molly/Info to /home/mwaring on my current machine and will rename it 'facts'. For those of you familiar with FTP, this is like 'get remote-file local-file'. Note: If you do not specify a local name, scp will name the file the same name as the remote file.
To copy a local file onto a remote computer, you type:
scp /local/path/local-file user@remote-computer:/remote/path/remote-file
For those of your familiar with FTP, this is like 'put local-file remote-file'. Note: As with cp, you need to have read/execute permissions for the two files. Also, you should only specify one computer name (either the local host or the remote host).
As with the cp command, you can specify the -r flag to recursively enter directories.
Note 1: This does not work with Windows machines. scp works only between UNIX based operating systems.
SSH File Transfer Protocol or SFTP is a network protocol that provides file transfer and manipulation functionality over any reliable data stream.
To copy files from a remote computer to a local computer, first navigate to the local directory where you want to copy or retrieve files, then type:
sftp user@remote-computer
You will then be prompted for the user's password on the remote computer. After you enter it, you will be logged into the user's home directory on the remote computer. Use UNIX commands to navigate to the remote directory where you want copy or retrieve files.
To copy a local file onto a remote computer, you type:
put local-file remote-file
To copy a remote file onto the local computer, you type:
get remote-file local-file
To copy multiple remote files onto the local computer, you type:
mget remote-file local-file
Text File Conversion
When text files are created in Windows, they are given line endings formatted for DOS. These files are unable to be utilized by programs run in UNIX, and must be converted.
There are many ways to acomplish this, however the simplelist is utilizing the following command, where you replace the dosfile.txt with the name of your file, and unixfile.txt with the new file name:
tr -d '\15\32' < dosfile.txt > unixfile.txt
Similarly, to convert from Mac line endings to UNIX, you type:
tr '\r' '\n' < macfile.txt > unixfile.txt
|