Unix and basic shell scripting

November 30th, 2007

Introduction

UNIX is a collection of software known as a computer operating system (OS) that is used to communicate with a computer's central processing unit (CPU) and memory. The OS software is also responsible for communications between the CPU/memory and the computer's other peripherals (disks, printers, networks and etc.). Unix however does not refer to a single well-defined collection of software. Instead Unix is a generic term for a number of similar collections of software that differ depending on the supplier of the OS software.

The Unix OS is made up of the kernel and shell. The kernel is the main control program that is responsible for machine level operations of the system and the connection to hardware. The shell is a command interpreter that can communicate with any part of the system except the hardware. The shell parses, checks, translates and etc., inputs that are then passed to the kernel for execution. An analogy for the OS is a walnut - the important part is the kernel inside; the shell is what the nut presents the the outside world.

Technically, utilities are not part of the OS. They are a collection of basic software tools that are often distributed with the Unix OS. They make the operating system more immediately useful to the user by simplifying the tasks of communications, programming, editing text and more.

History

Unix was originally developed in 1969 by a group of employees at AT&T Bell Laboratories. In 1973, Unix was rewritten in C, a computer programming language also developed at Bell Labs. This allowed Unix to be installed and run on different computers which had a C compiler. Since then there has been a large adoptions of Unix and Unix-like operating system such as Berkley Standard Distribution (BSD), Sun Microsystems, Mac OS X and Linux. These "flavors" of Unix are all very similar but all use a slightly different dialect, like slang speech for the same common tongue.

The Shell

In Unix, to get things done you need to type commands that a shell can interpret. A shell can read command lines from a terminal or from a file (called a shell script or program). Because the shell is a program, scripting is possible without using a programming language. Any command that can be typed after a prompt can be used in a shell.

Basic Commands

Unix is a command-driven OS. Below is a list of basic commands that can get you started using Linux and other UNIX variation. All these commands will work on the Mac OS X, BSD, Linux and on windows using Cygwin. If you are using Mac OS X, you can open the Terminal located at /Applications/Utilities/Terminal and execute the commands after the Bash shell prompt ($).

Info about paths and root
Unix separates file and directory paths with the forward slash "/". Also, a path that starts with the forward slash, means it is an absolute path from the "root" directory. If a path does not lead with the forward slash, then it is a relative path from the current directory.

Examples:

/         # "root" directory
/usr      # directory usr (sub-directory of / "root" directory)

Moving through the file system

pwd                 # shows the path of the "present working directory" or the current directory
cd                  # change the current directory to you HOME directory
cd /applications    # change the current directory to the "applications" sub-directory of "root"
cd ..               # changes the current directory to it's parent directory

List directory contents

ls         # lists the current directories contents
ls -l      # lists the current directories contents in long (detailed) format
ls -a      # lists the current directory contents including hidden files

Copying, renaning and moving files

cp file1 file2            # copies a file
mv file1 /applications    # move "file1" from the current directory to the applications directory
mv file1 newname          # rename "file1" in the current directory to "newname"
mkdir dir1                # make a new directory within the current directory
rm file1                  # remove 'file1' within the current directory
rmdir dir1                # removes the empty directory "dir1"
rm -r dir1                # recursively removes a directory and it's contents. BE CAREFUL!

Changing file permissions and attributes

chmod 775 file             # changes permissions of file to be read, write and execute (rwx) for the owner, and rx for everyone else.
chgrp user file            # makes the file belong to the group user
chown username file        # makes username the owner of file
chown -R username dir      # makes username the owner of dir and everything in its directory tree

Open chosen folder in iTerm

October 23rd, 2007

I wanted to find an easy way to open a selected folder from my finder window in a new iTerm session. I did a quick search and I came across this link. It works if you use plain old terminal but I had to modify it for iTerm. Here is the code I ended up with.

on run {input, parameters}
    tell application "iTerm"
        make new terminal
        tell the first terminal
            activate current session
            try
                -- launch a default shell in a new tab in the same terminal
                launch session "Default"
            on error
                display dialog "There was an error creating a new tab in iTerm." buttons {"OK"}
            end try
            tell the last session
                try
                    -- cd to the finder window
                    write text "cd \"" & (POSIX path of (input as string)) & "\""
                on error
                    display dialog "There was an error cding to the finder window." buttons {"OK"}
                end try
            end tell
        end tell
    end tell
    return input
end run