Intro To Unix

From ITwiki

Jump to: navigation, search

Contents

Introduction

This document provides a short introduction to basic unix commands. Note that Unix is case-sensitive - most commands are all lowercase, as are usernames. Examples presented will use [username@timmermans username]$ as a command prompt. Your prompt may be different.

Accessing Unix machines on campus

On campus, students and faculty may access the UNIX machines in the Burchard Lab by either physically traveling to the lab, or accessing the system through PuTTY.

  1. By traveling to the lab, users may create an account with the person on duty
  2. Using PuTTY, refer to the Setting up PuTTY guide.

Logging in/Logging out

In order to log onto the UNIX system, in the username field, type in your Stevens Username. For the password field that comes up afterwards, type in your unique UNIX password, which is case sensitive and also not any word that may be in the dictionary. Note that your password can be a mixture of upper and lower case

Note

Depending on the system you are accessing, your credentials may differ.

Help

On Unix systems, help is available via the online manual pages by using the command man. To find out information on the command cat, for example, you would type man cat at the [username@timmermans username]$ prompt.

man - access manual pages.

Syntax: man [< section >] <command>
man -k <keyword>

man access a very large online help database and will probably be your main source of reference. The manual pages are divided into several sections (1 user commands,2 system calls, etc..). If you do not signify a section it will search through all sections starting at 1 until it find the command you want. man -k allows you to specify a keyword and it will search for that keyword and display a one line summary on all commands that have to do with that keyword.

Basic commands

pwd - The pwd command will tell you what directory you are currently in (pwd stands for "print working directory".)

ls - list directory contents

Syntax: ls [flags] [<director(y/ies)/file(s)>]

ls will list the contents of a directory. ls by itself will list the current directory. You can specify a list of a directories and/or files to list as well as several flags to control how the display appears. Wild cards can be used. Common wild cards are:

* - match anything 0 or more characters long
? - match one character
[x-y] - match any 1 character between x and y (i.e. [a-z]
[abc] - match abs (i.e. [agh]) (can be longed then 3 i.e. [abdteh])
[combination of above] - i.e. [adeA-Zhu-z]

Common Flags

-a - list all files including ., .. and all those beginning with a '.' (Files beginning with a . are normally not shown).
-s - show size of files
-l - show owner, permissions, size, group, etc.. of file
-C - show in columns
-F - append * to executables, / to directories, @ for links, etc.
-t - sort from most recent to oldest order by time.
-r - reverse the listing.

date

The date command prints the current time and date.

cp - copy files

Syntax: cp [-r] <original(s)> <new file or directory>
cp will copy one or more files either to a new file or into a directory. All command line elements not not beginning with a - except the last one are the files to be copied, and the last element is where or what to copy them to.

mv - move or rename files

Syntax: mv <original(s)> <new name or directory>
This is basically the same as cp only it does not leave a copy of the file, does not need the -r flag for directories and is a lot faster then cp. mv is also used to rename a file - think of it as moving a file from one name to another

cat - basic file display command

Syntax: cat <file(s)>
cat simply displays the files to the screen without pause.

more - Display file one page at a time.

Syntax: more <file(s)>
more is a program that displays a file onepage at a time. Pressing the space bar will move forward one page and return will move forward one line.

rm - Remove files.

Syntax: rm [<flags>] <file(s)>
rm main purpose is to remove files. It will also take flags that will allow it to remove directories. Wild cards are allowed as well.

Common Flags

-r - remove listed files and directories plus all files in those directories plus all sub-dirs and there files in listed directories, etc..
-f - don't ask if you want to remove a file.
-i - ask before removing a file.

hostname - prints the name of the host you are logged into. Useful if you have accounts in multiple machines.

w - Show who is on the system and what they are doing.

Syntax: w
This will show you everyone on the system, what tty they are on, where they logged on from, when they logged on, how long they have been idle, how much CPU they are using total, and how much CPU there foreground process is using (see fg, and bg).

who - Show who is on the system

Syntax: who
This will simply show who is logged on, what tty they are on, when they logged on and where they logged on from.

last - Shows a log of connections.

Syntax: last [<name>] [<tty>] [-<number>]
If you specifically a login name then the log will display connections by that user. If you specify a tty then it will show the connections for that tty (the tty is what port the user linked up to). If you specify a - and a number it will only show that many entries of the log.

whoami - Shows your login name.

Syntax: whoami
Simply displays your login name. Useful if you have multiple accounts.

finger - Find out information on about a user or a machine.

Syntax: finger -l <user>[@<host>]
finger @<host>
finger -l will show you information about a user. Last login, shell, etc.. finger @<host> will show you who is on that host or network.

The File System

In Unix, everything is a file. Memory, disks, and terminals are all treated as files under Unix. Disks, memory, and terminals are 'special' files, which are different from normal files of data or directories. In Unix, the file system is a hierarchical tree structure. The top of the tree is call the 'root' of the file system, and is referred to as '/'. The '/' character is also used to separate the components of a file's name. A file is referred to by its path and file name. The path is a list of all the subdirectories from the root of the file system to the file you want to access. For example, /usr/local/bin/gcc denotes the path /usr/local/bin, and the file gcc in that directory.

cd - change directory

Syntax: cd [<directory>]
cd will change your current directory to a directory specified in the command argument. <directory> can be absolute (cd /var), or relative (cd notes). <directory> can also use the '~' character either to change to your home directory (cd ~) or someone else home directory (cd ~<user>). cd with no arguments will change to your home directory and is identical in function to 'cd ~'.

mkdir - Make a directory.

Syntax: mkdir <director(ies)>
mkdir will make 1 or more directories.

rmdir - Removes a directory (if it is empty).

Syntax: rmdir <director(ies)>
When dealing with Unix files, there are three ways you can specify WHO can access them, and three ways you can specify HOW someone can access them.
All Unix files have a protection associated with them called permissions. Permissions are read, write, and execute, and can be specified for the user, group, and others.

The HOW

  • READ

If someone has read permission on a file, he can look at its contents. This naturally also means he can copy it.

Someone who has read permission on a directory can find out what files exist in that directory. He cannot find out detailed information about those files, or read their contents, unless he has execute access to the directory. The default gives no one but you read access to your files and directories.

  • WRITE

Someone who has write access to a file can change its contents ONLY. If someone has write permission to a directory, he can change its contents. This includes creating new files, renaming files, and removing files in that directory. The default gives no one but you write permission to your files and directories.

  • EXECUTE

Someone who has execute permission for a file can run it as a Unix program. That is, he can just type its name and it will be run, whether it is a shell script or compiled program. This is useful if you want other people to be able to run a program but not be able to copy it. If someone has execute access to a directory, then he can 'cd' to it and look at the contents of files in that directory, depending on their individual protections. He cannot find out WHAT files are in that directory, however, without read access. This is the default. It lets you easily give files to friends by telling them the names of the shared files, provided you give them read access to the files first. But they can't find out what OTHER files you have in your directory.

The WHO

  • OWNER

This is you, of course, for your own file or directory. Each file or directory has an OWNER.

  • GROUP

Every file or directory has a GROUP. Files and directories inherit their group from their 'parents', the directories above them, unless you specify otherwise.

  • WORLD

This is everybody else. To see all of these protections for a file or directory, use ls -lg:

$ ls -lg a.out
-rwxr--r-- 1 jfrosh u98 1237 Feb 1 22:30 a.out
^^^^^^^^
||||||||
USER | WORLD OWNER GROUP SIZE DATE NAME GROUP (in bytes)

The r indicates read permission, the w write, and the x execute. To change file permissions (also known as file mode), the chmod (change mode) command is used.

chmod [option] mode files Where mode consists of WhoOpcodePermission:

Who

  • u User
  • g Group
  • o Other
  • a All (default)

Opcode

  • + Add permission
  • - Remove permission

Permission

  • r Read
  • w Write
  • x Execute
For example: chmod a+r .plan would make the file .plan readable by everyone.

IMPORTANT: You may, for some reason, wish to make all the hidden files in some directory world-readable. DO NOT use the command "chmod a+r .*" to accomplish this. The directory above your current directory has the name ".." and your current directory is "." Therefore, the command "chmod a+r .*" will make both your current directory and the directory above your current directory world readable.

Editors

There are many editors used in Unix environments.

ed - ed is a line mode editor which is part of the standard unix distribution. It is not the most preferred editor, but knowing ed commands can help you in the vi editor. See the ed man page for more detail.

vi - Pronounced vee-eye, vi is the standard screen mode editor on most all unix systems. vi also has a line mode, where ed commands can be used. In addition to this page, there is additional information on vi, and a quick reference sheet.

emacs - emacs is a screen mode editor, and is included on the systems here in the Stevens UNIX lab. This runs almost like a regular word processor, and commands are very self explanatory by using the controls present on screen.

pico - simple text editor

Syntax: pico <file>
pico is a simple text editor that is fast loading and very easy to use. This is a good choice for quickly edited files, etc...

If you have questions or problems with these instructions, please contact the Information Technology Help Desk.

Personal tools