Logo     "Graphical user interfaces make easy tasks easier, while command line interfaces make difficult tasks possible."

* Some of the material is taken from this online course.

A shell is a program that runs operating system commands. In general, there are two key differences between shell scripts and more complex programs. First, shells are designed to handle short, simple tasks.They lack the rigid structure and semantic checking of a high-level language. Shell scripts can be written quickly because they assume the programmer knows what he or she is doing, and for short scripts, this is usually true. Second, shells always execute their scripts slowly. Although most shells perform some kind of pre-processing to speed up execution, they still interpret and carry out one line at time. High-level languages are faster because they almost always translate a program into machine language to gain the best performance. When tackling any programming problem, it’s essential to choose the right tool for the job. Developing large projects as shell scripts will mean your project will run slowly and be difficult to maintain. Developing scripts in a high-level language will lead to a longer and more costly development time. (source)

Useful Shell Commands

Navigation

Finding files

Aliases

I/O

$ do_something < input-file
$ do_something > output-file (because stderr is not the same as stdout, error messages will still be seen on the terminal windows)
$ do_something >> append-to-output-file

When commands are executed, by default there are three standard file streams (or descriptors) always open for use: stdin (descriptor 0), stdout (descriptor 1) and stderr (descriptor 2). If other files are opened in addition to these three, which are opened by default, they will start at file descriptor 3.

- To redirect stderr to a separate file: $ do_something 2> error-file
- Shorthand notation to put anything written to stderr in the same place as stdout:
$ do_something > all-output-file 2>&1 (bash-specific: $ do_something >& all-output-file)

$ cat > myfile << EOF   writes everything typed into myfile until EOF is typed (can use anything instead of EOF).

/dev/null is a special file that discards all data that gets written to it.
To ignore the standard output stream but printing any errors on the console:
$ find / > /dev/null

<<< (in newer versions of bash) can be used to redirect content of variables:
$ TEST="John"
$ wc -m <<< $TEST
5

Information about the system

Editting

Getting Help

Links

ln can be used to create hard links and (with the -s option) soft links (aka symbolic links or symlinks).
Suppose that file1 already exists. A hard link, called file2, is created with the command: $ ln file1 file2
Note that two files now appear to exist. However this is not quite true. If we run $ ls -li file1 file2 (the -i option to ls prints out in the first column the inode number, which is a unique quantity for each file object) we see that inode field is the same for both of these files; so it is only one file but it has more than one name associated with it, as is indicated by the 2 that appears in the ls output.
If you remove either file1 or file2, the inode object (and the remaining file name) will remain. If you edit one of the files, exactly what happens depends on your editor; most editors including vi and gedit will retain the link by default but it is possible that modifying one of the names may break the link and result in the creation of two objects.

Symbolic (or Soft) links are created with the -s option: $ ln -s file1 file4
Notice file4 no longer is a regular file, it points to file1 and has a different inode number. Symbolic links take no extra space on the filesystem (unless their names are very long). They are extremely convenient as they can easily be modified to point to different places.
Unlike hard links, soft links can point to objects even on different filesystems (or partitions) which may or may not be currently available or even exist. In the case where the link does not point to a currently available or existing object, you obtain a dangling link.

Compression

In addition the tar utility is often used to group files in an archive and then compress the whole archive at once (using the above compression tools).

File Permissions and Ownership

Files have three kinds of permissions: read (r), write (w), execute (x). These permissions affect three groups of owners: user/owner (u), group (g), and others/world (o).
chmod is used for changing permissions. There are two ways to di it.
$ chmod uo+x,g-w test adds the execute permission to user and other, and takes away write permission from group.
Another method is using a single digit to represent permissions. This digit is the sum of:
4 if read permission is desired.
2 if write permission is desired.
1 if execute permission is desired.
Thus 7 means read/write/execute, 6 means read/write, and 5 means read/execute.

chown is used to change user ownership of a file or directory and chgrp is used to change group ownership.
For example: $ sudo chown root test changes the ownership of test to root.

Processes

Misc