Guides:Shells/sh-bash/Basics

From CoderGuide

Jump to: navigation, search

Contents

Unix/Linux/Mac OS X Terminal Basics

This is NOT intended to be a comprehensive guide using the Unix shell, just a quick introduction.

The commands here are the same regardless of what Unix-Like environment you are in. This includes Solaris, IRIX (Silicon Graphics), BSD, FreeBSD, Darwin (Apple Mac OSX), Linux, MSYS, Cygwin, and many others.

First, you need to start the terminal (ion the Mac, you'll find this in the Finder under Utilities), or enter the shell. For *nix (Unix, and like systems), we'll leave it up to you to figure it out, but it normally starts when you log in, or can be found in the X Window manager menus (Look for something like Xterm, gterm, Terminal, etc.).

Depending on your shell, and your settings, your command prompt will look different. We will use '#' for the shell prompt in these examples. We will represent commands that you type in in bold.

All Unix systems are case sensitive. That means "File" and "file" are treated as two completely different names. There's sometimes an exception to this with Mac OS X, but Mac OS X is bastardized Unix, so we're just going to pretend it like every other Unix (and so should you, unless there is a circumstance in which you shouldn't).

File Management

Before we can discuss file management, lets set up the experimenting environment by typing in the following commands (don't type the '#'):

# cd ~
# mkdir l-env
# cd l-env 
# touch file1 file2 file3

The cd command changes the working directory, ~ is an alias to your home directory, mkdir creates directories (rmdir removes them), touch changes the time stamp on files and will create files if they don't already exist.

Listing Files and Directories

So, let's see what files we have in the current (or working) directory by using the ls command.

# ls
file1  file2  file3

ls can take a number of parameters. The GNU (ga-new) version, which is common on modern Unix distributions, can colorize the listing. You can get full information on your ls command by typing man ls (this probably won't work on MS Windows systems with MSYS or Cygwin). We will only cover the more commonly used parameters: l,a,F,d

On most Unix systems, -F and --color are set by default, the notable exception is when using installation or rescue disks, or when using Mac OS X (bastardized and neutered Unix).

Normally ls doesn't list files and directories that begin with a period, but you can tell it to by using -a. The output below assumes -F is not set by default.

# ls -a
.      ..     file1 file2 file3
# ls -aF
 ./     ../    file1 file2 file3

You will always find the '.' and '..' directories in every directory. '.' points to the directory it is in (the current directory in this case), and '..' points to the previous directory (also called the parent directory).

You can remove both of them, but that's kind of like cutting down a tree while sitting in it.

Now, we'll use the -l (dash ell, for the font challenged).

# ls -la
-rw-r--r--  2 userID group         0 Mar 31 2042 .
-rw-r--r--  5 userID group         0 Mar 31 2042 ..
-rw-r--r--  1 userID group         0 Mar 31 2042 file1
-rw-r--r--  1 userID group         0 Mar 31 2042 file2
-rw-r--r--  1 userID group         0 Mar 31 2042 file3

Your listing will look different from this. The first column lists the file permissions (in order of owner, group, and all users, and each can have read [r], write [w], and execute permissions [x], and a few other special permissions and others that identify the type of file), the second shows the number of hard links to the file or directory (each .. directory is a special hard link to the parent directory, so for directory entries, this will show the number of sub directories unless the root user forced the creation of another hard link to that directory--which is highly unlikely, not supported by many file systems, and is not recommended when it is supported).

In order to explain hard links, we need to discuss the Unix file systems. They have these funny things called "inodes." An inode is a number that references the data in an entry on a file system. An entry in a directory contains a file (or directory) name, information on who owns it, what type of file it is, three time stamps (atime, ctime, and mtime-- this one you see when you do ls -l), and the inode number of the data the entry represents.

Use man ls to learn more about ls, and man chmod to learn more about file permissions (and how to change them).

Hard Links and Symbolic links (symlinks)

A hard link means you have more than one directory entry which references that file. So, while that hard link may belong to someone else, have it's own name and set of file permissions, it still references exactly the same piece of data. In order to remove a file from a file system, you need to delete all hard links to that file. Normally, a file only has one hard link, so deleting (or unlinking) will remove it from the system (or, more accurately, mark the inode, and it's data, as being free). There is little reason to create hard link today.

A symlink, or symbolic link, is just a special file that allows you to link to a file without creating a hard link. They're a lot like short cuts in Windows (in fact, the idea of short cuts came from Unix symbolic links).

You create both with the ln command. Use man ln to learn more about it.


Deleting, Renaming, Copying and Removing files and directories

Moving and renaming a file or directory is accomplished by using the same command mv.

# mv file1 file-one
# ls'
file-one   file2    file3
# mkdir t
# mv file-one t
# ls
file2 file3 t/
# ls t
file-one

Removing a file can be done with the rm command. Just specify a list of files to be removed. rm won't normally delete directories unless it is given the -r parameter (this can be dangerous though, so be careful with it). There is (normally) no easy way to recover a file deleted using these terminal commands, so be careful regardless of using rm with -r or not.

Copying a file is similar to the mv command, except it's called cp and will create a duplicate of the file. Give it a list of files, and then a destination. If using more than one file, then the last item in the list must be a directory. To copy directories, use the -r argument. If you want to preserve the file dates/times and other attributes, then use -p. If, for instance, you're making a backup, then you'll want to use cp -rp files-or-directories... destination.

Job Control

To start a process running in the background, add an and sign & to the end of the statement. Alternatively, you can press CTRL-Z to suspend the program, and then type bg to make the program run in the background. fg tells the program to run in the foreground. You can get a list of currently running jobs by typing jobs.


# some_command &
# jobs
[1] + Status                 Command line

The [1] means that it's listing the job number, not the actual process ID. You can kill the job by typing kill %1 where 1 would be the job number. Use jobs -l or jobs -p to see the actual process ID (PID), or you can use the ps command.

You can also use job numbers with the bg and fg commands. If a background job is expecting input from the user, it's status will be changed to “Suspended” until it is made a foreground process with fg and then the user supplies the requested input.


You can terminate a program or script with CTRL-C (unless the signal has been trapped).

Redirecting input and output

You can redirect the output of one program into a file, or into the input of another program. Here are a few examples”

# echo Hello > File
# cat < File
Hello
# echo Hello2 >> File
# cat  File
Hello
Hello2
# echo Test | cat
Test
# cat << EOF > File
This is a test... 
Fee Fi Foe Fum
I smell a nice strawberry cheese cake
Yum yum yum!
EOF
# cat File
This is a test...
Fee Fi Foe Fum
I smell a nice strawberry cheese cake
Yum yum yum!
> Directs the output of a process (program) to a file, overwriting the existing file
>> Similar to >, but does not overwrite the file
< sends the contents of a file to the standard input (stdin) of a process
Continues to redirect text from the shell to the input of the command until a final line with just the statement TEXT is given.
Sends the output of one program into the output of another, or “pipes” the output of one process into another (the '&pipe;' character is called a pipe)

To redirect standard error (stderr) you'll need to use &> when using the Bourne/bash (Bourne Again Shell) shells.

Environment

To view the environment in Bourne shells (sh/bash), use export, or you can use setenv in csh style shells. Setting a environmental variable is easy. Here are two ways to do it in bourne shells:

Method 1:

#  var=contents
# export var

Method 2:

# export var=contents

PATH is a fairly important environment variable. It tells the shell where to look for executables. Normally the current directory is not part of the path (in MS-DOS/Windows the first place the shell looks is the current directory, this is not so with Unix, it looks only where it is told for security reasons). If you want to execute a program in the current directory, use ./command where . means the current directory, and / is the directory separator.

If you want to see the contents of a environmental variable, you can view it like any other.

# echo $PATH

It is important to note that in order to set an environmental variable, you need to export it. Just doing a PATH= won't.

Personal tools