table of contents
- expected learning outcomes
- listing directory contents
- location of files and directories
- creating directories
- changing directories
- deleting directories
- viewing files
- copying files
- moving files
- deleting files
- getting help
- connecting to a remote machine
- copying files between machines
- text file conversion
expected learning outcomes
The objective of this activity is to promote understanding of basic UNIX commands, and it is structured as a command reference for better continued learning effectiveness.
MacOS X and various flavors of Linux are UNIX-based operating systems. Because many of the programs 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:
To see a detailed list of all directories and files sorted by creation date, type:
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:
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, use the Print Working Directory command:
This will show an output such as:
To create a new directory, type:
This will create a directory called directory1
To go "down" one directory (in this example to go from mwaring to directory1), type:
To go "up" one directory (in this example, to go from directory1 to mwaring), type:
Now go back to directory1, create directory2 in it and go to directory2. To go "straight back" to your home directory from directory2, type:
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:
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:
where "pattern" represents the character string you wish to find.
To exit, press:
To copy a file using a terminal, use:
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:
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:
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:
To copy an entire directory, use the -r flag:
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.
On a UNIX file system, moving a file is the same as renaming it. The command mv works like:
takes fileformats.html and renames it index.html.
Files can be moved from one location to another:
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:
rm is the command to remove a file. There are flags (options) that can be used with rm. To delete a file, type:
For example, to delete a file called coi.nex, type:
To delete a file from a different directory, supply rm with the path. For example:
To delete a directory, type:
This will remove the directory and all the files and directories in it so use this command carefully.
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
invokes information about commands.
For example, to learn how cp works, type:
To exit the man page, hit:
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:
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. If you are running MacOS X or Linux, simply open a terminal. If you are running 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:
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:
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:
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:
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:
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:
To copy a remote file onto the local computer, you type:
To copy multiple remote files onto the local computer, you type:
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:
Similarly, to convert from Mac line endings to UNIX, you type: