Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

UNIX BASICS

INTRODUCTION

CAMERON UNIVERSITY

UNIX was developed in the early 1970's at AT&T Bell Labs. A number of companies have licensed UNIX and modified it for their systems. When the companies license it they also give their UNIX a new name. IBM calls it AIX. HP is HP-UX. Sun called it SUN-OS and now Solaris. Digital called it Ultrix, then OSF/1, then Digital UNIX and now it is Tru64 UNIX. NCR calls theirs NCR UNIX SVR4 MP-RAS. The Linux operating system is not UNIX. Linux was written from the ground up and is not based on any UNIX code. The different variants or flavours of UNIX are based either on System V UNIX or BSD. System V UNIX came out of the original UNIX at AT&T Bell Labs. BSD Unix was developed at the University of California Berkeley. UC Berkeley had purchased the UNIX source code and had developed their own version from 1979 to 1994.

ACCESSING THE SYSTEM If a terminal emulator (a program that acts like a dumb terminal) is not on your system then Microsoft Windows comes with a telnet program. From the Windows Start/Run line type telnet. Select remote and enter the host name of the system that is being connected to. Once connected a login prompt will appear. A login name and password are required to access the remote UNIX system. login: stu01 <Enter> password: xxxxxxxx <Enter> If the login name and password are correct the system prompt will appear as a dollar sign. UNIX commands can now be entered at the system prompt. Once logged into the system the current directory is the accounts home directory. This is a area set up just for this account to store information. To change the accounts password use the command passwd at the system prompt: $ passwd A prompt will appear asking for the current password. If the password is correct a prompt will appear asking for the new password. The system prompts twice for the new password to be sure that it was entered correctly. 1

UNIX BASICS
To leave the UNX system use the exit command: $ exit This will terminate your connection to the UNIX system.

CAMERON UNIVERSITY

DIRECTORY STRUCTURE The UNIX system is set up as a tree hierarchy. At the top of the tree is the root. The root is represented by the slash character. Off of the root are branches of the tree. The branches are directories. Files or directories can be off the tree.

DIRECTORY COMMANDS After logging into the system, the current directory is your home directory. So for the account stu01 the current directory would be /home/students/stu01 . To view what the current directory is use the pwd command:

UNIX BASICS
$ pwd

CAMERON UNIVERSITY

To create a new directory off of the home directory use the command mkdir. $ mkdir newdir To view a listing of the contents of the current directory use the command ls. $ ls For a directory listing that gives more information use the command: $ ls -l To view hidden files that dont normally show up with an ls use the command: $ ls -la Change the current directory to the new directory that was just created use the change directory command cd. $ cd newdir The newdir directory is down one level in the tree from the home directory for stu01. Check to see what directory is current: $ pwd In this directory files could be stored or additional sub directories could be created. To move back up one directory use the command: $ cd .. The dot dot represents the current directory. To rename a directory use the move command mv. $ mv newdir newname

UNIX BASICS

CAMERON UNIVERSITY

The remove directory command rmdir will erase a directory. All the contents of the directory must be removed before using the rmdir command. $ rmdir newname FILE COMMANDS A file is used to store characters or bytes of information in a directory. Besides the ls command there are other commands used to work with files. To create an empty file use the following command: $ > filename Do an ls -l to see that the file has been created and that nothing exists in the file. An empty file doesnt do us much good. To put some text into the file use the following command: $ echo hello world > filename To view the contents of the file use the cat command. $ cat filename The contents are displayed on the screen. If the file was longer than one screen the contents would scroll off the screen. Only the last page would be remaining to view. Use the more command to view a file that is too long to view on one screen. $ more filename To create a duplicate copy of a file use the cp command. $ cp filename dupfilename Files can also be renamed. Use the move command mv. $ mv dupfilename newfilename To purge a file from the system use the command rm. $ rm filename

UNIX BASICS

CAMERON UNIVERSITY

The wildcard characters in UNIX are the asterisk and the question mark. The question mark represents a single character position. The asterisk represents multiple positions. To list all the files starting with the letter n type: $ ls n*

ACCESS RIGHTS A file or directory has certain rights associated with it. To view the rights use the ls -l command.

Rights drwx--x--x -rw-r--r--

Links Owner Group 5 stu01 1 stu01

Size

Date

Name

student 398 student 12

Jun 23 09:00 public_html Jun 26 09:49 newfilename

$ ls -l d in the first position of the output indicates a directory. The next three positions are the rights of the owner of the file. The next three positions are the rights for the group that owns the file. The last three positions are for all other users that are not the owner or are in the group. The account stu01 has rwx access to the directory public_html. Read is r. Write is w. Executable is x. The group student has read access to the file newfilename. All other users that are not the account stu01 and not in the group student have executable access to the directory public_html.

VI EDITOR The vi (pronounced vee-eye) editor is a program that allows for the creation of documents. An editor is not the same as a word processor. A word processor will put soft returns at the end of lines. An editor does not. To advance to a new line the Enter key must be pressed. 5

UNIX BASICS
To create a new file follow the vi command by a filename. $ vi filename

CAMERON UNIVERSITY

Tilde marks will appear on the screen. This indicates nothing in the file. Once in vi there are several operating modes. After first entering vi the mode is command mode. Pressing the Esc key also returns to command mode. In command mode almost all the keys on the keyboard do some special action. The action is also different for upper or lower case. Another mode is known as last line mode. From command mode type a colon to access the last line mode. Press enter at the end of the command to execute it. Press the Esc key to abort the last line mode. In command mode press the i key to enter insert mode. Common commands in Command Line Mode Moving around Move back a screen page Move forward a screen page Inserting Appending to end of line Modifiying a single position Modifiying multiple positions Deleting a character Deleting a line Search forward from cursor Search backward from cursor Saving Quit Save and Quit Quit and dont save Shell command j, k, l, m, <arrow keys> ctrl-b ctrl-f I A r R x dd /string ?string :w :q :wq :q! :!ls

UNIX BASICS
REDIRECTION

CAMERON UNIVERSITY

To run any command on UNIX enter the command at the system prompt and press Enter. If the command to run was called xyz. $ xyz <press Enter> If the program generated output it would be displayed on the screen. To redirect the output to a file use the greater than character to send it to a file. $ xyz > outfile Be careful in the use of the greater than. overwritten without prompting. If the outfile was already present it will be

If the program required input from the user a file could be created. The file would contain the required input. It would be directed into the program with the less than character. $ xyz < infile To direct input into a program and direct output to a file use a combination of the less than and greater than characters. $ xyz < infile > outfile

APPENDING TO A FILE To append the contents of one file to end of another file use the >> characters. $ cat file1 >> file2 Now file2 would have its original contents plus the contents of file1. The command: $ cat file1 > file2 would wipe out the contents of file2 and place in it the contents of file1.

UNIX BASICS

CAMERON UNIVERSITY

SORTING To sort the contents of a file use the sort command. $ sort datafile The above command would display to the screen the sorted contents of datafile. To redirect the sort to a file use the following: $ sort datafile > newdatafile

DISPLAYING FILES Besides the cat and more command to display files, other commands can be used. The head command will display the top lines of a file. $ head datafile The above command will show the first ten lines in the file datafile. To display the first twenty lines use the command head followed by a dash twenty. $ head -20 datafile To view the end of a file use the tail command. $ tail datafile $ tail -20 datafile

SEARCHING FILES To search for a file use the find command. $ find . -name filename The dot after the find tells the command in which directory to start the search. To search from the root of the tree type:

UNIX BASICS
$ find / -name filename To search for a file that starts with xyz use the command: $ find . -name xyz* To search a file for a certain string use the grep command. $ grep abc filename To search all the files in a directory for a string: $ grep abc *

CAMERON UNIVERSITY

ON-LINE HELP UNIX systems have on-line manuals. Use the man command to find the syntax and an explanation of the options associated with a command. $ man vi Use the keyword search to search the description field of each command. $ man -k editor The above command will return a list of all the commands that contain some form of editor.

PROCESSES Every time a command is executed at the system prompt that process is given a number. This number is known as the process id or pid. To view the current accounts pids use the ps command. $ ps PID TTY 6884 tty00 6888 tty00 TIME 0:01 0:02 9 COMMAND -sh ps

UNIX BASICS

CAMERON UNIVERSITY

The output will show the process id, the terminal associated with the command, CPU time, and the command itself.

To stop a job from running use the kill command. $ kill -9 7343 Where the number after the -9 is the number of the process to kill.

JOBS A process can run in the foreground or background mode. The default is foreground mode. When an ls command is executed it is run in the foreground. $ sort largedatafile The above command sort a large data file. Nothing can be done until the command finishes. To stop a foreground process before it has finished use ctrl-c to break out of the job. $ sort largedatafile & $ The ampersand means to run the command in background mode. The system prompt returns immediately while the sort command runs in the background. To view processes that are running in the background use the jobs command. $ jobs [1]+ Running sleep 5 &

To bring a job to the foreground use the fg command followed by the job number. $ fg %1 Jobs can also be killed using the kill command. $ kill %1 10

You might also like