Unix/Linux Basics 1
Some basics to remember Everything is case sensitive Eg., you can have two different files of the same name but different case in the same folder Console-driven (same as terminal ) Each console runs a shell program, which is basically a process. Each shell has a prompt and you can provide commands to run on that prompt. Eg., $ ls would list the current folder contents (where $ is the prompt automatically displayed by shell.) shell is the equivalent of the explorer in Windows Use forward slash / to separate directory hierarchies Eg., the command $ ls ~/subfoldera/file1.c lists 2
Directory Structure In Unix, the directory structure is a tree, with a root (denoted by / ). The default folder that shows up when you open a new terminal is your home directory, which is also aliased by the symbol ~ cd ~ will take you to home directory from wherever you are currently. Note that if you just type in just cd without any arguments the effect is same as cd ~ cd.. will take the console to the parent directory cd. has no effect as. represents current folder You can specify a folder s path in two different ways: absolute and relative relative to the current folder: eg., cd A/B/C/ will be change the present working directory to a subfolder named A under., and then from it will jump two more subfolders to B and C similarly absolute path is the path relative to the root. Eg., cd /usr/bin will change to the usr/bin folder under the root pwd will always show the absolute path of the current folder Both relative and absolute paths can be interchangeably used, but absolute paths are generally preferred over relative within source codes so that one can run the program from anywhere 3
Basic commands ls man mkdir cd cd ~ cd. cd.. pwd Directory listing Manual for any system command Eg., man ls will show the help on the ls command and all its options Make a new directory Change directory to a target folder Eg., cd subf would change the shell to a folder by name subf under the current working folder Change to home directory Change to current directory (does nothing) Change to parent directory Displays full path of the current/present working directory 4
Basic commands echo hello there echo hello there > a cp a b mv a b Prints hello there to standard output (stdout) Prints hello there to a new text file called a in the current working folder Copies file a into b (overwrites b if it already exists!) Renames file a into b (overwrites b if it already exists!) clear cat a more a less a head lines=10 a tail lines=10 a Clear screen Prints out the contents of the text file by name a Prints out the contents of the file a but screen by screen Same as the more command except that you can scroll up too Prints the first 10 number of lines of file a Similar to head but prints the last 10 lines of the file 5
Basic commands grep hello a wc a cat a > b Find and report all lines containing the pattern hello in text file a Report word, character and line counts of file a Prints contents of file a and redirects the standard output to a file b. (Overwrite hazard) cat a >> b chmod ps top Same as cat a > b except that if b already exists before this command is run, then the contents of a are appended to b This is called pipe command. Eg., $ cat a grep hello would pass the standard output of the cat a command into the input of the grep command Access permission to files and directories (do a man chmod for more information) Lists all processes running on the current shell (do man ps to learn about more ps options) Lists all the currently running processes on the system with all their details such as memory, CPU usages, etc. Very useful command. 6
Basic commands script This will record all that is output on the console into a text file called typescript until you type exit command Eg., $ script $ ls a.txt b.txt c.txt $cp a.txt b.txt $exit The above sequence of commands records all the console stdouts onto the file typescript. So you can test that by $ cat typescript 7
Basic commands rm rm i rmdir rm r This is the command to remove files. Interactive option. You will be asked for a confirmation before the remove happens. This is the safest option that will prevent you from deleting a file by mistake. You can make this the default setting for rm so that you don t have to type -i option everytime. This can be done by typing the command export rm=rm i. After you run this command all rm will internally call rm i. But all this will hold good only for the current working terminal session. If you open a new terminal or logoff & login again, you will have to type the export command again. This can be avoided by directly including the export command line in the.profile or.bashrc files in your home directory. (see man pages on rm) This is deleting an empty folder. This is for deleting a folder and recursively all its contents 8
Lots of online tutorials.. http://www.ee.surrey.ac.uk/teaching/unix/ 9
Compiling & Running C++ Code A sample test code has been provided on this website: http://www.eecs.wsu.edu/~ananth/cpts223 /Codes/Test/ Follow the instructions in the readme file to successfully compile and test the C++ code 10
Editor options There are two popular editors for editing source code vim Emacs Links to a couple of online tutorials are on WebCT 11
More Exercises Open 2 new files using the editor of your choice (vim, emacs), and write your own TestPrinter.cc and Printer.cc code. The idea is to get to used to source-coding using these editors. Also experiment with the script command and redirect commands Peruse through man pages of each command listed in the previous slides Understand relative vs. absolute path Get familiar with as many commands and their options as possible 12