Helpful Tips for Labs CS140, Spring 2015
Linux/Unix Commands Creating, Entering, Changing Directories to Create a Directory (a Folder) on the command line type mkdir folder_name to Enter that Folder cd foldername (cd stands for change directory) to go back to the previous folder cd.. to move back 2 directories cd../.. or to move 2 directories in cd folder1/folder2. means current directory (or folder).. means parent (or previous) directory (folder) When you create a directory, you are creating a folder inside of another folder. The 1st directory is the parent directory.
Examples I want to create a folder for my cs140 labs. First, I open my terminal. Then: Looking at moving into and out from directories: I made the directory cs140, and then entered that directory. ls lists the files and folders within the directory I am currently in. You will notice that the 2nd time I typed ls nothing happened. This was because I have nothing in the cs140 folder. I move into lab1 folder from my home directory. Then I saw that I have no files or folders in the directory (and there shouldn t be since I have not created any). Next, I move back out to my home directory. Here I can list the cs140 directory.
Examples Now I want to create folders for labs 1 and 2: I created both folders and then listed them. I moved into the lab1 directory and saw I have no files in the new directory (and there shouldn t be since the folder was just created). I moved back into the parent directory (cs140) and listed the contents of that folder (showing only 2 folders, as it should). I then move into the lab2 directory and do the same as before. Now, I want to switch from the lab2 directory to the lab1 directory. Next I move back into the parent directory and list the contents again.
Linux/Unix Commands Dealing with Files to Create a file (.txt,.cpp,.c,.pl, etc) vi filename.txt or vim filename.cpp vi/vim is the text editor. In a few slides, I will talk about vi/vim commands. to Edit a file use the same command vi filename.cpp to List the Files in the Directory type on the command line ls to see all the possible ways of listing these files type man ls to see the manual page to Remove a File rm filename.cpp to Remove a Directory rmdir foldername (Note: the folder must be empty to be deleted)
Examples To create the file filename.txt: This is what the terminal looks like after I hit enter: to Write (or save) the file type :w to Quit (or exit) the file type :q to Write and Quit the file type :wq (shown below) to Quit without saving the file type :q!
Examples After I Write and Quit my text file filename.txt, I can now see the file I created when I use the command line argument ls : The cat command shows the line of text from my file. Then, I remove my text file from the directory. Next, I show an example script and output.
Linux/Unix Commands Copying and Moving Files to Copy a File cp file_to_be_copied.cpp new_file.cpp to Move (same as Cut and Paste) a File mv file_to_be_moved.cpp ~/directory/address/to/move/file/to/ if also want to change the name of the file being moved: mv file_to_be_moved.cpp ~/directory/address/to/move/file/to/new_name.cpp to Change the Name of a File mv old_filename.cpp new_filename.cpp
Examples I changed the name of the file; then I copied that file, naming the new file filename2.cpp: Here I am moving filename2.cpp from the lab1 folder into the parent folder.
Linux/Unix Commands Fun Tips pwd (print working directory) will show you the address of your current folder use man (for manual pages) to get more information about a command c or clear cleans the screen, moving the prompt to the top of the page (you can still scroll up to see the last so many lines printed to the screen) bye exits the terminal There s also the tutorial, help guides, and other useful resources at http://www.tutorialspoint.com/unix/
vi (or vim) i enters insert mode o enters insert mode and adds a newline r replaces one character R enters replace mode ESC exits these modes x deletes one character (5x deletes 5 characters) yy yanks (or copies) a line (5yy copies 5 lines) dd deletes (or cuts) a line (5dd cuts 5 lines) p pastes the line u to undo the last action :redo to redo the last action G moves to the last line in the file :1 moves to the first line in the file /word_to_be_searched_for will search for the next instance of this word (after the initial command you only have to type / to go to the next instance) :w to Write (or save) the file type :wq to Write and Quit the file :q to Quit (or exit) the file type :q! to Quit without saving the file type
Examples(vi) To create the file filename.txt: This is what the terminal looks like after I hit enter: To start typing, hit i on your keyboard. This puts the text editor into insert mode. To exit any mode, press the ESC key on your keyboard. To replace a single character (when not in any of the editing modes), using your arrow keys move the cursor over the character you wish to change, press the r key, then press the key of the character you wish to replace. If you wish to replace multiple characters, pressing shift + r (or R ) will put the editor in replace mode.
Commenting Your Code Taken from Dr. Parker s webpage: http://web.eecs.utk.edu/~leparker/courses/cs302-fall06 /Labs.html Here's an example of what I would consider a well documented program: #include < stdio.h > /* sumnsquared.c Author: Ima Smart Date: August 23, 1998 This program prompts for and reads a value n from standard input, and then calculates and prints the sum of squares from one to n. It uses a for loop instead of using the closed form expression. */ main(){ int n; /* The value */ int sum; /* The running sum of squares */ int i; /* An induction variable */ /* Prompt for and read in a value of n */ printf("enter a value n: "); fflush(stdout); if (scanf("%d", &n)!= 1) { exit(1); } /* Calculate sum for each value of i from one to n */ sum = 0; for (i = 1; i <= n; i++) sum += (i*i); /* Print the final summation */ printf("%d\n", sum); }
Remote Login & SCP For those who need to download Putty/WinSCP: http://www.eecs.utk.edu/resources/it/kb/remote-access To ssh into the Hydra computers: in your Terminal type ssh NetID@hydra#.eecs.utk.edu and when it prompts, type in your password (it is the same password as for your email) Use scp (secure copy) to download files to and from the Hydra computers. First move to the folder on your computer where you want the files or have the files. to Download onto your Computer: scp NetID@hydra#.eecs.utk.edu:cs140/lab1/filename.cpp. to Download onto Hydra: scp filename.cpp NetID@hydra#.eecs.utk.edu:cs140/lab1/ Reminder:. means current directory (or folder) and.. means parent (or previous) directory (folder)
Casey Miller Last update: 2015.01.07 Thanks and Good luck!