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
Locations of Files and Directories
To work with a file or directory, it is important to tell Linux where to find it. There are several ways to tell Linux where to look:
./ = the current directory
../ = the parent directory of ./ (one directory up)
/ = the root directory (top directory)
When no location is specified, Linux assumes we mean “./”. So 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 ../
Viewing Files
There are many ways to view a text file. For viewing and editing, see the section below on text editors. One way for simple viewing is to type:
less
This 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, press:
/pattern
To exit less, press:
q
Creating and Editing Files
There are several text editors that create plain text files. We have emacs and vi available from the terminals. Unless you are already familiar with vi, we strongly recommend that you use emacs. We strongly recommend you study the emacs tutorial, which you can easily start in any emacs session. To open and edit a file in emacs, simply type
emacs myfile
To create and edit a new file, type
emacs newfile
To edit a file in emacs and still be able to use the command prompt, type
emacs myfile &
If you forgot to type the “&” and you want to return to the command prompt mode, type
Control-Z
This command will suspend the emacs application, but will not kill it. Then type:
bg
to have emacs working in the background.
***User home directories are set at a maximum storage space of 3 gigabytes. Should you exceed your storage space, you will be warned that you are at your maximum storage space. Contact the Workshop computer staff before the grace period on this expires and anything of yours is possibly lost.***
Moving Files
Because of the way files are accessed in UNIX/Linux, 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 a name after the second location. For example:
mv /home/mwaring/more-stuff /home/mwaring/Tasks/additional-tasks
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 contents 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 made. For example:
cp /home/tmp/mbl.color.logo.jpg /home/mwaring/images
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 you do not have permission to read or execute.
Removing 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 if you really want to delete it. If so, type:
y
To delete a file from a different directory, supply rm with the pathname. For example:
rm /home/mwaring/coi.nex
Creating Directories
To create a new directory, type:
mkdir directory1
This will create a directory called directory1.
Changing Directories
To view the pathway of the current directory, type:
pwd
This will show output such as
/home/mwaring/directory1
To go “up” one directory (in this example, to go from directory1 to mwaring), type:
cd ..
To go “down” one directory (in this example, to go from mwaring to directory1), type:
cd directory1
To go “back” to your home directory, type:
cd
Deleting Directories
To delete a directory, type:
rm -r directory1
This will remove the directory and all the files in it so use this command carefully.
Viewing PDF files
PDF files can be accessed by either downloading and saving or reading files directly. To view files that have been downloaded, go to the terminal window and type:
acroread file1
This will open the pdf file in acrobat reader.
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.
ssh
ssh is a program to connect to another computer. For Linux and Mac OS 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 and it 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. Windows users can download Putty, a free ssh client.
Note: Your home directory (and all of /home) is available for you on every machine in the lab.
Copying Files between Machines (File Transfer)
Some of you may be familiar with ftp (File Transfer Protocol). We do not have ftp here because it is a relatively insecure method of transferring files between computers. To transfer files between machines, you need to 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, you type:
scp user@remote-computer:/remote/path/remote-file /local/path/local-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
If I am working on nixon (for example), this command will copy the file called info that lives on bigbox.university.edu at /home/molly/Info to /home/mwaring on nixon and will name 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 you familiar with ftp, this is like ‘put local-file remote-file’.Note: As with cp, you need to have read/execute permissions on 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 copy entire directories.
Note: This does not work with Windows machines. scp works only between UNIX/Linux machines.
Read More