Physics REU Unix Tutorial What is unix? Unix is an operating system. In simple terms, its the set of programs that makes a computer work. It can be broken down into three parts. (1) kernel: The component of the operating system working behind the scenes. (2) shell: This is where the user sends commands to the kernel, i.e. it is what allows the user and the kernel to talk to one another. (3) programs: These are the executables that do things for you in Unix, and generally this means they process or create files, or both. This tutorial should lead you through the basics of finding your way around Unix, but there are many online resources that will help you to do fancier things in the Unix operating system as you become more comfortable using it. Logging into your Unix account on faraday To log into faraday (the name of one of the physics computers) from the Windows machine, open the SSH Secure Shell Client. Hit enter and type faraday into the hostname field. Then type your username into the username field. Hit enter and then type in your password. You should now be logged into faraday. It will look something like... [username@faraday ~]$ This is the shell command line and is where we will give the operating system instructions for what we want it to do. Basic commands in Unix This set of basic commands will help you to maneuver at the simplest level in Unix. There are many more commands available, and here we'll just touch upon a few. Commands should be written at the shell command line as follows... [username@faraday ~]$ commandname Changing password For example, we'll start with the passwd command, which will let you change your current password. At the shell command line type passwd and then hit enter... [username@faraday ~]$ passwd Other command will be typed at the shell command line in the same way. Listing directory contents and finding where you are Notice that there is a ~ currently in the prompt, right after faraday. This ~ indicates you are in your home directory. A lot of Unix is learning how to move between directories, create directories, create files, and move files between directories. To see what your current directory path (a path is the absolute location of your directory within the Unix file-system), type pwd at the shell command line. The command pwd stands for print working directory. The results should look like... /usr3/username
This means that your home directory is called username and it is a subdirectory within the directory usr3 (as an example, it could be usr2, usr4, etc...) Now type ls.. at the shell command line. The result should be a large list of names. We'll break this command down. ls this is the command to list the contents of a directory.. this is a Unix notation that indicates the directory one level up from where you are at So, ls.. tells the operating system to list the contents of the directory one level up from your current location. Since your current location was /usr3/username, the ls.. command listed the contents of usr3/, which was a list of all the other users who have home directories in usr3/. Now type ls../.. at the shell command line. This will list the contents of the directory two levels up from your current location. To see the contents of your current location, just type ls at the shell command line. The result will be the files and/or directories in your current location. This should show you that we can think of the Unix filesystem as a tree, i.e. (root directory) ====================== usr1 usr2 usr3 ========= ======== ======== baker leone feynman einstein hawking ====== ====== ====== ===== ===== etc etc etc etc etc pwd (prints the path of the current working directory) ls (lists contents of a directory) Making directories and moving between them Sometimes we don't just want to list the contents of directories, but we want to be able to create directories and move between them. The next set of commands we are going to learn will enable us to do this. mkdir This command will make a directory within the current directory. Usage... [username@faraday ~]$ ls (see what the contents of the current directory are) [username@faraday ~]$ mkdir testdir1 (makes a new directory called testdir1) [username@faraday ~]$ ls (check to see that you've created the new directory) Now use the mkdir command to make a second directory called testdir2, then check with the ls
command that it exists. cd This command will allow you to move between directories, it stands for change directory. Usage... [username@faraday ~]$ pwd (print the current working directory is, the result should look like /usr3/username) [username@faraday ~]$ cd testdir1 (this will move you into testdir1) [username@faraday testdir1]$ pwd (see that you've moved into testdir1, the result should look like /usr3/username/testdir1. Note that the ~ in the command line has changed to say testdir1. The command line will tell you what current directory you are in like this.) Now list the contents of testdir1 with the ls command. It should be empty! We'll put stuff in it later. To go back to your home directory you have some options, so here are a few ways to do it... [username@faraday testdir1]$ cd.. (this will take you back to home, since home is only one directory up from where we are at. In general, cd.. means go up one level in the directory tree) [username@faraday ~]$ pwd (check to see you are back in home) [username@faraday ~]$ ls (check to see what directories and files are in home) [username@faraday ~]$ cd testdir1 (go back into testdir1 so we can try the other methods of getting back to home) [username@faraday testdir1]$ cd ~ (this will take you directly back to the home directory from ANYWHERE) [username@faraday ~]$ pwd (check to see you're in home) [username@faraday ~]$ ls (check directories and files) [username@faraday ~]$ cd testdir1 (go into testdir1 so we can try the final method) [username@faraday testdir1]$ cd /usr3/username (this will take you directly back to home from ANYWHERE also) [username@faraday ~]$ pwd (check the directory path) [username@faraday ~]$ ls (check the directory contents) mkdir (makes a directory within the current directory) cd (changes from one directory to another) Creating simple text files There are MANY tools in Unix for creating and editing simple text files (files without fancy formatting like font-types, boldface type, underlines, italics, etc.). We're going to use one called vi, which has extensive functionality, but only requires just a couple of key commands to use at the most basic level. You'll learn this basic level of vi operation, and can learn how to use it in a more advanced way at online resources like http://www.eng.hawaii.edu/tutor/vi.html as an example. To start the vi editor, just type vi at the shell command line followed by the name of the file (existant or non-existant) that you want to edit. Since we don't have any files yet, we will be creating and editing a non-existant file...
[username@faraday ~]$ vi myfile1.txt This will change your screen to the vi editor, where you'll see a cursor and lots of ~'s down the lefthand side of the screen. You won't be able to type in here yet. We have to go into insert mode. To enter insert mode in vi, just type the letter i. Now go ahead and write some stuff in the editor, whatever you want. Type two or three sentences. When you're done, hit the esc key on the keyboard. This will take you out of insert mode and back into command mode. Now, to save what you did to the file and quit, you should type :wq and hit enter. The w means write and the q means quit. You'll notice that you're now back at the shell command line. Note: If you are ever in vi and want to quit without saving, you can type :q! while in command mode. This will force quit without saving. If you now type ls you should see that the file, myfile1.txt, is found in your directory. Note: To edit it further, you would just use vi myfile1.txt, if you wanted. Moving, copying, and removing files and directories The last set of basic Unix skills we'll learn are how to manipulate files, and file locations once we've created them using something like vi. Type ls and you should see that myfile1.txt is in your directory (assuming you haven't changed directories since last section). We are going to start by making a copy of this file. cp This command will allow you to copy the contents of a file into a new file. If the file you are copying to already exists, it will be overwritten, so be careful! Usage... [username@faraday ~]$ cp myfile1.txt myfile1_copy.txt (this creates a copy of myfile1.txt called myfile1_copy.txt. Note that in Unix, you cannot have spaces in the names of files like in Windows, so good practice is to name files that have multiple words in them using underscores as separators). [username@faraday ~]$ ls (you should now see both files in your directory) To check that myfile1_copy.txt is REALLY a copy of myfile1.txt, let's use the cat command. cat Unix command to concatenate (combine together) and display files. Usage... [username@faraday ~]$ cat myfile1.txt (should display the contents of myfile1.txt to the screen) [username@faraday ~]$ cat myfile1_copy.txt (should display the contents of myfile1_copy.txt to the screen) Next we are going to move these files into the directories that we already have created, testdir1 and testdir2. mv This command allows you to move a file from one location to another. It also allows files to be renamed. We'll look at both usages. Usage...
[username@faraday ~]$ mv myfile1.txt testdir1/ (moves myfile1.txt into the directory testdir1) [username@faraday ~]$ mv myfile1_copy.txt myfile2.txt (renames myfile1_copy.txt as myfile2.txt) [username@faraday ~]$ mv myfile2.txt testdir2/ Note: Again, care must be taken with the mv command. If you move a file into another directory that already has a file of the same name, it will be overwritten. If you rename a file as the same name of another file, that file will be overwritten. Unix is unforgiving, and doesn't ask you are you sure like Windows before you make these decisions. Now if you type ls you'll see that the two text files are no longer in home. Change directory to testdir1. [username@faraday ~]$ cd testdir1 [username@faraday ~]$ ls You should see the contents of this directory are only the file myfile1.txt. Now we'll learn how to get rid of this file. rm This command will remove a file from existance. There is no turning back, so you have to be absolutely sure you know what you are removing. Never, ever type rm * unless you are sure of what you are doing. The * is a wildcard, and will match everything in the current directory, so this command would say to remove everything in the current directory. In other words, you could accidentally delete files that you don't mean to delete. Usage... [username@faraday ~]$ rm myfile1.txt (this will remove the file myfile1.txt) [username@faraday ~]$ ls (You should see there is nothing in the directory now) [username@faraday ~]$ cd ~ (change back to the home directory) Finally, we'll learn how to remove directories. Unix has one safeguard in place, in that it will only let you remove empty directories. We'll see how that works. rmdir This command will remove a directory, but only if it is empty. Usage... [username@faraday ~]$ rmdir testdir2 (You should notice that Unix complains that the directory is not empty and can't be removed) [username@faraday ~]$ rmdir testdir1 (This should work, since we got rid of myfile1.txt already) [username@faraday ~]$ ls (You should now see that only testdir2 remains, and testdir1 is gone) cp copy files from one location to another, make copies of files cat combine files together and display file content to screen mv move a file from one location to another, or rename a file rm delete a file from existance rmdir delete a directory from existance, only works on empty directories ------------------------------------------------------------------------------------------------------------------------- These are all of the most basic commands you'll need to use in Unix. For more info, just search Google for unix tutorials. A good link to start is http://www.ee.surrey.ac.uk/teaching/unix/ for more practice.