22
Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Embed Size (px)

Citation preview

Page 1: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Basic Unix Commands

CGS 3460, Lecture 6Jan 23, 2006Zhen Yang

Page 2: 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

Page 3: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Overview

Files and Directories How to connect to GROVE Basic UNIX commands TAR Permissions UNIX editor pico Compiling C programs

Page 4: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Files and Directories

In computers, what is the difference between a file and a directory?A file is a program or a documentA directory contains files or other

directories

Page 5: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Directory Structure

Inside directories, you could find files and other directories

Inside those “other” directories, you might find more files, and more directories

Page 6: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

How do I connect to grove?

telnet grove.ufl.edu

Page 7: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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

Page 8: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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.)

Page 9: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

More UNIX Commands

pwd –let you know the absolute pathname of your current working directory (Where your are)

cd [dir] – change directory

cd .. –go back to parent directory. “..” is the

relative pathname to the parent directory.

Page 10: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

More UNIX Commands

pico [filename] : create a new fileText editor^X to quit and save the file

cpcp [file1 file2] – copy file1 to file2. If there’s

already a file2, the old one will be overwritten.

Page 11: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

More UNIX Commands

mv [sourcefile targetfile] – 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.

Page 12: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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.

Page 13: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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.

Page 14: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Permissions

There are three types of file access supported by UNIX.r – read, view the contents of a file w –write, edit file/directory contentsx –execute, run executable file

Page 15: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

- rwx r-x r-- 1 c3460abc cgs3460 858 Jan 23 8:28 f1

typeowner

groupsize Modification date/time File name

User permissions

Group permissions

Other Permissions

links

Page 16: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

Permissions

read=4;write= 2;execute=1 rwx r-x r--

4 + 2 + 1 4 + 0 + 1 4 + 0 + 0

7 5 4

Page 17: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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 .

Page 18: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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

Page 19: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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.

Page 20: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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.

Page 21: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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.

Page 22: Basic Unix Commands CGS 3460, Lecture 6 Jan 23, 2006 Zhen Yang

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