240 likes | 465 Views
Basic Unix Commands. CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang. For this class you need to work from your grove account to finish your homework Knowing basic UNIX commands is essential to finish your homework. Overview. Files and Directories How to connect to GROVE Basic UNIX commands
E N D
Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang
For this class you need to work from your grove account to finish your homework • Knowing basic UNIX commands is essential to finish your homework
Overview • Files and Directories • How to connect to GROVE • Basic UNIX commands • TAR • Permissions • UNIX editor pico • Compiling C programs
Files and Directories • In computers, what is the difference between a file and a directory? • A file is a program or a document • A directory contains files or other directories
Directory Structure • Inside directories, you could find files and other directories • Inside those “other” directories, you might find more files, and more directories
How do I connect to grove? telnet grove.ufl.edu
Telnet Window • For login, enter the username on your grove account card • For Password, enter the password on the grove account card • Grove will ask you to change your password • Just follow the on-screen instructions • http://grove.ufl.edu/ for more information
UNIX Commands • mkdir [directory]– create a new directory • ls–list files or directories in current directory. • ls –l list in long format (including details like permissions, owner, size, etc.)
More UNIX Commands • pwd–let you know the absolute pathname of your current working directory (Where your are) • cd [dir]– changedirectory • cd ..–go back to parent directory. “..” is the relative pathname to the parent directory.
More UNIX Commands • pico [filename]: createa new file • Text editor • ^X to quit and save the file • cp • cp [file1 file2]– copy file1 to file2. If there’s already a file2, the old one will be overwritten.
More UNIX Commands • mv [sourcefiletargetfile]–basically mv renames sourcefile to targetfile. If there’s a file with the same name as targetfile, it will be overwritten. • rm file(s)– delete file(s). • rmdir directories–delete one or more empty directories.
TAR • Create tape archives and add or extract files. • Creating a tar file: tar -cvf file.tar myfile.txt In the above example the system would create a tar named file.tar in the directory you currently are in. tar -cvf file.tar *.txt would compress all txt files in the current directory. tar -cvf home.tar home/ In the above example command the system would create a tar file named home.tar in the directory you currently are in of the home directory.
TAR • Extracting the files from a tar file: tar -xvf myfile.tar In the above example command the system would uncompress (untar) the myfile.tar file in the current directory. tar -xvzf myfile.tar.gz In the above example command the system would uncompress (untar) the myfile.tar.gz file in the current directory. Note: There is no "untar" unix command.
Permissions • There are three types of file access supported by UNIX. • r– read, view the contents of a file • w–write, edit file/directory contents • x–execute, run executable file
- rwxr-xr-- 1 c3460abccgs3460 858 Jan 23 8:28 f1 owner links type size Modification date/time File name group User permissions Group permissions Other Permissions
Permissions • read=4;write= 2;execute=1 • rwx r-x r-- 4 + 2 + 1 4 + 0 + 1 4 + 0 + 0 7 5 4
Permissions • chmod mode file(or directory) –change the permission of the file or directory. • Examples: • Change the permission of file2 to: • rwx r-x --x • chmod 751 file2 .
Unix Editor pico • Simple and very easy to use text editor on UNIX • Open a file using pico pico filename For example: pico hello.c The above example would open the editor with the file hello.c if present. • Basic pico command (^ hold ctrl key) ^W where is ^Y previous page ^V next page
Compiling C Programs gcc is the "GNU" C Compiler. It is a free compiler that is available on grove.ufl.edu. Below are several examples that show how to use gcc to compile C programs.
Compiling a simple program • Consider the following example: Let "hello.c" be a file that contains the following C code. #include <stdio.h> main() { (void) printf("Hello…\n"); return (0); } • A standard way to compile this program is with the command gcc hello.c -o hello This command compiles hello.c into an executable program named "hello" that you run by typing './hello' (or possibly just 'hello') at the command line. It does nothing more than print "Hello..." on the screen.
Compiling a simple program • Alternatively, the above program could be compiled using the following two commands. gcc -c hello.c gcc hello.o -o hello The end result is the same, but this two-step method first compiles hello.c into a machine code file named "hello.o" and then links hello.o with some system libraries to produce the final program "hello". In fact the first method also does this two-stage process of compiling and linking, but the stages are done transparently, and the intermediate file "hello.o" is deleted in the process.
Compiling a program with multiple source files • If the source code is in several files, say "file1.c" and "file2.c", then they can be compiled into an executable program named "myprog" using the following command: gcc file1.c file2.c -o myprog The same result can be achieved using the following three commands: gcc -c file1.c gcc -c file2.c gcc file1.o file2.o -o myprog