CS101 Lecture 9
Total Page:16
File Type:pdf, Size:1020Kb
How do you copy/move/rename/remove files? How do you create a directory ? What is redirection and piping? Readings: See CCSO’s Unix pages and 9-2 cp option file1 file2 First Version cp file1 file2 file3 … dirname Second Version This is one version of the cp command. file2 is created and the contents of file1 are copied into file2. If file2 already exits, it This version copies the files file1, file2, file3,… into the directory will be replaced with a new one. dirname. where option is -i Protects you from overwriting an existing file by asking you for a yes or no before it copies a file with an existing name. -r Can be used to copy directories and all their contents into a new directory 9-3 9-4 cs101 jsmith cs101 jsmith pwd data data mp1 pwd mp1 {FILES: mp1_data.m, mp1.m } {FILES: mp1_data.m, mp1.m } Copy the file named mp1_data.m from the cs101/data Copy the file named mp1_data.m from the cs101/data directory into the pwd. directory into the mp1 directory. > cp ~cs101/data/mp1_data.m . > cp ~cs101/data/mp1_data.m mp1 The (.) dot means “here”, that is, your pwd. 9-5 The (.) dot means “here”, that is, your pwd. 9-6 Example: To create a new directory named “temp” and to copy mv option file1 file2 First Version the contents of an existing directory named mp1 into temp, This is one version of the mv command. file1 is renamed file2. where option is -i Protects you from overwriting an existing file by asking you > cp -r mp1 temp for a yes or no before it copies a file with an existing name. This is useful to make a backup copy of your mp1 files. 9-7 9-8 mv file1 file2 file3 … dirname Third Version mv dirname1 dirname2 Second Version The files file1, file2, file3,… are moved into the directory dirname. In this version dirname1 is renamed dirname2 9-9 9-10 jsmith pwd rm option file1 file2 file3 … mp2 The rm command removes (deletes) each file in the list. temp source where option is -i For each file in the list, the user is asked whether or not to {FILES: mp2.c, mp2.h } {FILES: stat.c, input.dat } {FILES: edit.c } delete. -r Deletes a directory and every file or directory in it. Move all the files with .c suffix from the temp directory to the mp2 directory. Example: To delete a directory named temp and all its contents, > mv temp/*.c mp2 > rm -r temp Where temp is a subdirectory of the present working directory. Move the input.dat file into the home directory. Be very careful in using this command!!! > mv source/input.dat . 9-11 9-12 jsmith pwd mkdir dirname The mkdir command makes a subdirectory named dirname in mp2 workspace the present working directory. {FILES: mp2.c, main.c } Lab assignment: Create a directory named lab2 as a mp2 subdirectory of your home directory. Assuming that you are in your home directory, type Find all files named mp2.c . {FILES: mp2.c, main.c } >mkdir lab2 The mkdir creates a new directory but it does not cd for you. > find -name “mp2.c” -type f Find all directories named mp2 . > find -name “mp2” -type d 9-13 9-14 mp2 pwd Links commands to commands. Pipes are used to direct the output of one command to the input of the {FILES: mp2.c, input.c , stat.c, mp2.h, … } second. Unix uses the vertical bar “ | ” symbol to perform piping. General form > command1 | command2 You want to email your lab TA and send him/her all your files in the mp2 directory. > tar -czvf blah.tar.gz * creates a file named blah.tar.gz When your TA receives this file he/she can extract its contents by typing: > tar -xzvf blah.tar.gz 9-15 9-16 Example: when you use the ls command to list the contents of a directory the output may not fit on one full terminal screen. Since the screen scrolls you may not see the first part of the output. Use a pipe to solve this Links commands to a file. Redirection is used to take problem information that would normally be seen on the screen and > ls -la | more funnels it to a file or vice versa. Unix uses the “ > ” or “ < “ symbols to perform redirection. General form > command > filename output redirected to file filename > command < filename input from file filename not keyboard 9-18 Example: you can send the output from the ls command to a file” screen.txt ”. You can then use gedit to edit the file. passwd - change your password > ls -la > screen.txt clear - clears the terminal screen leaving the prompt > whoami - identity of the user man command - manual of information concerning command for example, type in “ man finger” to find all the options available for this command. 9-19 9-20 alias abbreviation command provides an alias for a command. alias abbreviation ‘ command1 command2 ’ provides an alias for compound commands For example, if you type, > alias dir ls -la For example, if you type, > alias dir ‘ls -la | more’ dir ls -la then is an alias for so if you type, > dir then dir is an alias for ls -la | more so if you type, > dir this is the same as typing in > ls -la this is the same as typing > ls -la | more To make this change effective each time you login, gedit the and this command displays the contents of the pwd one screen .cshrc file and type in the alias command. When you add a line at a time. at the end of the .cshrc file terminate the line by typing <return>. 9-21 9-22 alias abbreviation ‘ command1 \!* command2 ’ provides an How to: alias for compound commands and passes an argument. copy- cp/move - mv/rename - mv/remove-rm files. For example, if you type, Use the mkdir command to create a directory. > alias dir ‘ls -la \!* | more’ Redirection - uses the “<“ or “>” symbol. By default, then most commands expect input data from the keyboard and output data goes to the monitor. Using > dir lab1 redirection symbols, you can specify that the input comes from a file and output goes to a file. is the same as typing in > ls -la lab1 | more and this command displays the contents of the subdirectory of Piping - uses the “ | ” symbol. Output data from one the pwd named lab1, one screen at a time. command can be piped as into to another command. 9-23 9-24 .