Brought to you by molecularsciences.org.
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
This publication may not be redistributed without this notice.

Linux

UNIX is a very powerful operating system which has been around for more than 30 years. Initially, it was primarily been used by industry and academia. However, since Linux, anyone can have a UNIX system installed on their computer. Having UNIX on your computer is like having an industry strength product. Much of the Internet still runs on UNIX.

Since UNIX has been in the academia for decades, much of its software is available for free. In addition, there is no shortage of blogs full of helpful comments.

UNIX is not a monolithic entity like Microsoft Windows. There are many types of UNIX. The most widely used and freely available version of UNIX is Linux. Linux itself has several versions. UNIX comes in many different flavors but it is essentially the same operating system and most of the commands are the same.

Main components of a computer

The physical components of a computer are referred to as the hardware and the code is referred to as the software. Computer hardware is made up of a number of different components, such as CPU (central processing unit), memory, and disks. An operating system enables all the different hardware components in a computer to interact with each other. There are many different operating systems such as UNIX, Linux, Microsoft Windows, etc. An operating system is a collection of programs and files which manage and instruct computer hardware.

Hardware

The four main components of a computer are:

CPU – central processing unit

CPU is the processor which executes instructions. CPUs are traditionally measured in clock cycles e.g. 6.0 GHz.

Run-time memory

Random access memory – RAM is an example of run-time memory. It is live when the computer is on. It loses its content as soon as we cut power. Since it is very fast, program stored in the long-term memory execute in run-time memory. Program and data being used by the computer are copied onto the RAM. This data remains the RAM as long as the program is running. When a program is done, it is removed from the RAM and this space is allocated to another program.

Long-term memory

Hard drives are the primary long term memory of the computer. All installed programs are stored on the hard drive. All your data is also stored on your hard drive.

I/O devices

Computers can read from input devices such as keyboard and mouse while output to devices such as the monitor and the printer.

Measuring Memory

Memory is measured in bytes. A byte is the number of slots used by a computer to represent one character.

1kb kilobyte 1024 bytes (for simplicity, 1000kb)
1mb megabyte 1000 kilobytes
1gb gigabyte 1000 megabytes
1tb terabyte 1000 gigabytes or approximately one trillion bytes

External memory

In addition to RAM and hard drive, there is a whole array of memory devices available, each with its own strong and weak points.

Floppy drive

Every now and then, you would like to move data from one computer to another. Floppy disks were invented with this need in mind. Due to its compact size and ease to transport, floppy disks became very popular in the 90s. Unfortunately the floppy disk technology was not upgrade. Several vendors such as iomega with their zip drives and sony with its superdisks tried to fill this gap but their technologies never became successful enough to replace the floppy disks.

Floppy disks have now become obsolete thank to CD, DVD and flash memory, and external hard drive technology. When a technology becomes very popular, it often requires several different technologies to push the technology out of the market.

CDs and DVDs

Before CD-ROMs, most software was sold in floppy drives. So if you bought PowerPoint in 1994, you would buy 8 floppy disks. As the size of the software became larger and larger, floppy disks had to be replaced by CD-ROMs. ROM stands for read only memory. Unlike a floppy disk, a CD-ROM is not rewriteable. CD-RWs are rewriteable CDs. This technology is good for erasing and rewriting the archives but not suitable for editing files on the CD.

DVD-R and DVD-RW are now replacing CDs since they are capable of storing 7 to 8 times more data on the same disk. DVDs are now consumers’ choice of saving data which does not require editing such as movies and music.

Flash Memory

We all have heard about USB. It is a revolution over older sockets. If you look at the back of your computer, you will find sockets of all shapes and sizes. Each of them with the exception of the serial port, is dedicated to a specific type of device. For example, you cannot attach a printer on a ps2 keyboard socket. USB technology has removed this restriction. It is a standard socket which accepts digital rather than an analog signal and you can plug in any device which support USB technology.

Flash memory (used in USB memory sticks) is a fast memory which can be connected to almost any computer and usually works painlessly. It is very compact and very easy to carry around. It is the technology which made floppy disks obsolete.

External hard drives

USB technology is very fast. This speed has finally made external hard drives possible. A typical external hard drive can now hold hundreds of megabytes of data, just like a normal hard drive. Mini HD is a technology which aims to miniaturize this technology. This new technology is expected to bring tough competition to the flash disks.

Tape drives

Tape drives have existed since the 1970s. The progress in this field has been steady and remarkable, yet it has gone unnoticed by the general public. A small collection of tape drives can backup terabytes of data. They were and still remain the technology of choice for industrial backups.

Internet

Internet can be used for data storage. In fact, many people use it for data storage. Services like xdrive.com have existed for years. They allow you to upload your data where it is guaranteed not to be lost since there is a team of experts watching out for viruses, doing backups, etc. In addition, you can access your data wherever you find access to the Internet. No need to worry about losing tiny flash memory sticks. With massive server space for gmail account, Google is slowly moving into the domain of data storage online.

Software

The code on your computer is the software.

Operating System

An operating system is the software responsible for the direct control and management of

The operating system interprets instructions from the user or an application and instructs the computer’s hardware. It handles the input, output, and keeps track of the data. It also enables different devices such as keyboard and CPU to communicate with each other. Examples of operating systems are UNIX, Linux, Windows XP, Windows 2000, and Mac OS.

Application Software

Application software is any software that runs on top of the operating system. Microsoft Word, Adobe PhotoShop, and Oracle are some examples. Whenever you install a software on your computer which already has an operating system installed, you are installing an application software.

UNIX operating system

The UNIX system contains 3 main components:

Kernel

The kernel is the master program which manages all the resources of the computer, including:

Shell

The shell is an interface between the user and the kernel. It is primarily a command interpreter. It accepts commands, interprets them, and then executes them. There are many different shells but Bash is the most widely used.

Common Shells

bash Bourne again shell
sh shell
csh C shell
tcsh Tenex C shell (not tab-completion-extended C shell)
tclsh Tcl shell
ksh korn shell
ash a shell
bsh bourne shell (/bin/sh)
zsh the Z shell
zsh the jail shell

Files and Processes

Everything in UNIX is either a file or a process. A file is a collection of data created by a user or process. A directory is also file. A process is an executing program. Every process is identified by a unique PID (process identifier).

Linux vs Linux Distributions

What is the difference between Linux and Linux distribution?

Linux is the core of the operating system i.e. the kernel. Kernel manages the physical resources of the computer and performs tasks such as starting and stopping programs, allocating memory, managing the processor, and managing network connections.

A Linux distribution contains the Linux kernel along with entire suite of Linux software such as GUI, editors, networking tools, administration tools, and pretty much everything you would see or use. The Linux distribution makes Linux useful and user-friendly. Different Linux distributions have different set of software and configurations. Most commonly used distributions of Linux are Red Hat Enterprise Linux, Fedora, SUSE, and Ubuntu.

What is the difference between different Linux Distributions? The valued-added tools that are packaged with each distribution.

Which distribution is better or the best? The answer depends on your needs. Do you need commercial support? Are you going to be using Linux for server or desktop? Which packaging system do you prefer? Which distribution is more suited for your line of work? It all depends on the answers to these and many other questions.

If you are learning Linux, start with Ubuntu or Fedora since they are commonly used and you would get plenty of support from the user-community.

Also see http://en.wikipedia.org/wiki/Comparison_of_Linux_distributions for a more comprehensive comparison of the differences.

UNIX command line syntax

UNIX command lines can be as simple, one-word entries like

pwd

or complex sentences like:

ls –R list.txt

UNIX commands may or may not contain arguments. An argument is an option or a filename. A UNIX command is written as:

command option(s) filename(s)

Options modify the way a command functions. For example, ls would simply print the directory listing. ls –al would give a listing with file details.

UNIX has a lot of commands. UNIX command line software such as vi editors also comes with its own set of commands. Attempting to memorize all the commands and their various options would be a waste of time. Instead, you should learn the commands and their options as you use them.

Every now and then, you would come across a frozen terminal. In such cases, try hitting the return key, ctrl-Q, ctrl-D or ctrl-Z.

Unix Directory Structure

An operating system is capable of storing data in an orderly fashion. A directory tree is used to organize and data. It organizes files hierarchically, with a root that branches into subdirectories. Thus the tree structure allows users to easily organize and find their data. Just like your paperwork, the better you classification, the easier it would be to find the documents when you need them. Each file and directory in an operating system has unique address thanks to the tree structure.


source: http://www.ee.surrey.ac.uk/Teaching/Unix/unixintro.html

In the diagram above, we see that the home directory of the undergraduate student "ee51vn" contains two sub-directories (docs and pics) and a file called report.doc. The full path to the file report.doc is "/home/its/ug1/ee51vn/report.doc"

Path

A path uniquely identifies a particular file or directory by specifying its exact location in the directory. The symbol slash ‘/’ separates names of files. In a UNIX operating system, everything including directories is considered files.

There are two types of paths. The UNIX directory structure starts from the root ‘/’. The absolute path specifies a file or a directory in relation to the entire UNIX system. The relative path describes the location of a directory or a file as it relates to the current directory.

Each directory contains two links, ‘./’ and ‘../’, which refer to the current directory and its parent directory. Relative addresses use ‘../’ to backtrack and forward addresses to move forward.

Suppose your current directory is

/home/usr/student

You wish to go to the images directory,

Using absolute path: /home/usr/student/documents/images
Using relative path: documents/images

To backtrack to /home/usr, type cd ../

Naming files and directories

Files can be created using alphanumeric characters, underscores, periods, and hyphens. It is wise to avoid all other characters even if the operating system would allow you to use them.

When naming a files or folders, it is very important to give meaning full names. This would greatly help you in find a file when it is needed. Suppose there are two users, Alice and Bob, both of whom using completely different naming schemes.

Alice’s file’s:

$ ls ~
work home photos
$ cd work
Accounts.dat faq.txt todo.list

Bob’s files

$ ls ~
Dir1 dir2 dr3
$ cd Dir1
1.txt 2.txt 3.txt

Right now we are only looking three files and 3 directories. It is already clear who would be capable of finding the “to do list” with much less ease and frustration. Bob has no choice but to open and read all his files until he reaches his to do list. Now imagine that Bob has to sift through hundreds of files. Most computer users have somewhere between hundreds to thousands of files.

It is wise to aim for uniformity rather than uniqueness in file naming since humans are better at remembering and following patterns. Following are some rule of thumbs:

Classify files by their function. Don’t put your vacation photos in the accounting directory.
All files of the same type must have the same extension
The filename should be indicative of the content of the file. The filename should be sufficient to remind you of what your file contains.
Try to keep the name as short as possible but not shorter. If this is not possible, add a readme.txt file next to the file.

UNIX is very powerful and allows to much more control over you own computer. More control means fewer forced restrictions. This in turn resulted in a number of conventions. For example, Microsoft Windows requires file extension to define the file type. UNIX has no such restriction but conventions define that you should use the extension to indicate the file type. It is often wise to following established conventions. Habits, both good and bad, are difficult to break. So why not start with good habits. Get in the habit of following well-established conventions.

Directory commands

To create a directory, we use the mkdir command:

$ mkdir new_directory

To see the directory you have just created, type

$ ls

Following is a list of command which would help you navigate the UNIX directory structure. UNIX also offers graphical environments to navigate the directory structure but very often you would have to come back to the command line. Therefore, it is a good idea to learn and use these simple commands.

Summary

cd:	change directory
pwd:	identifies the absolute path to the current directory
ls:	displays the contents of the directory
ls –a:	display all files including hidden files
ls –F:	display file types. 
	Directory	/
	Executable	*
	ASCII		none
	Symbolic link	@
ls –l	list files with their details
ls –R	recursive listing of subfolders and their files
ls –t	display sorted by time
ls –r	display in reverse order
Shell Metacharacters
Shell metacharacters are specific characters which have a particular meaning to the shell.

cd ~	go to the home directory, not supported by bourne shell
cd -	same as pwd. Korn, switch between two directories
ls m*	display all files beginning with f such as my, man, mime, etc.
ls m?	display all files beginning with an m followed by one character such as my.
ls [a-c]*	display all files beginning with an a, b, or c. 

The asterisk ‘*’ is a wildcard meaning on or more characters. The question mark ‘?’ matches any single character. The square brackets ‘[ ]’ match a set or range of characters for a single character position.

History Command

Unix systems store the commands you type. To get a chronological list of commands you typed, use the following command:

$ history

To repeat a previous command you can type:

$ !!

To repeat a previous command
$ !n (where n is a number from your history list)

or

$ !string (where string is a series of characters)

This would repeat the most recent command in your history whose first characters match string. If you do not want the command to be executed, you could use the following command.

$ history | grep 'scp'

would return all entries where scp command was used. None of these commands would be executed.

Examples
$ !12
$ !mys
$ history | grep 'make'

Copy, move, remove, rename, view

Copying Files

$ cp file1 file2

copies file1 in the current working directory into file2. It file2 exists, its content would be replaced. If it doesn't exist, it will be created.

Moving or renaming files

$ mv file1 file2

moves or renames file1 to file2

Removing files and directories

$ rm file1

removes file1

$ rmdir dir1

removes dir1 if it is empty

To remove a directory and all it contents use

$ rm -r dir1

Be sure that you want to remove a file before running the rm or rmdir command. The deleted files and directories cannot be recovered.

Displaying the contents of a file on the screen

$ clear

clears the screen

$ cat file1

displays file content on the screen. The content is not scrollable in the terminal but usually scrollable with the ternimal window.

$ less file1

displays file content, one page a time. Press the [space-bar] to see the next page, and type [q] to quit reading.

$ head file1

writes the first few lines of the file content on the screen.

$ head -25 file1

writes the first 25 lines of the file content on the screen.

$ tail -10 file1

writes the last 10 lines of the file content on the screen.

Command Summary

Command Meaning
cp file1 file2 copy file1 to file2
mv file1 file2 move or rename file1 to file2
rm file remove a file
rmdir directory remove a directory
cat file display a file
less file display file one page at a time
head file display the first few lines of file
tail file display the last few lines of a file

Symbolic Links

A symbolic link is a link which points to another file. In Microsoft windows, this is referred to as a shortcut and in Macintosh, an alias. To create a symbolic link:

$ ln -s source_file symbolic_link
$ ln -s readme.txt pointer-to-readme.txt

Now if you do:

$ cat readme.txt
$ cat pointer-to-readme.txt

Both commands would print the contents of the same file, readme.txt.

If you delete the symbolic link, the original file would remain intact. However, if you delete or move the original file, the symbolic link would not function properly.

To find out more about symbolic links, read the man pages for the ln command:

$ man ln 

Finding files and directories on unix and linux

Find is an extremely useful command for finding files and directories. It searches based a specified condition at a specified directory and descends into all subdirectories of the specified directory. You must always specify a directory and a condition.

You can specify the following:

* path (where to search)
* file type (-type: directories, data files, links)
* how to process the files (-exec: run a process against a selected file)
* file name (-name: partial or full name of file(s))
* logical operations on selections (-o and -a)

Find offers lots of very powerful options which allow you to precisely define your search criteria. Following are the most useful ones:

-exec command \;
Run unix command on each file matched by find, provided that it can execute successfully on that file.
-follow
Follow symbolic links and track visited directories.
-group gname
Find files belonging to group gname.
-mtime
Find files which were modifed +n or -n days ago.
-name pattern
Find files whose names match some pattern.
-print
Print results using absolute pathnames.
-type handle
Find files whose type is handle. Handle can be f for plain file, d for directory, l for symbolic link, etc.
-user user
Find files belonging to certain user.

Examples

Find files and printing their path

find . -name "httpd.conf" -print

This command will search in the current directory and all it sub directories for httpd.conf file. The -print option would print out the paths of matching results.

List all files named myfile in you home directory

find $HOME -name myfile -print

List all files beginning with ca in the project directory

find /project -type f -name ´ca*´ -print

-type f indicates that we are looking for a file

Seach the filesystem for manpage directories

find / -type d -name ´man´ -print

-type d indicates that we are looking for a directory

Remove all empty files

find / -size 0 -ok rm \;

-size indicates the file size.

Find all files modified within the last 90 days

find / -mtime -90 -print

Finding files and running commands on them

find . -name "httpd.conf" -exec chmod 755 '{}' \; 

This command would search for httpd.conf in the current directory and its sub directories. chmod 755 command would be applied to all matching files. The argument '{}' inserts each matching file into command line. The\; argument indicates end of exec command line.

Apply logical operations on selections

find /var/www/html/mysite -not \( -name "*.php" -o -name "*.inc" \) '{}' \; -print

This command will search the directory /var/www/html/mysite. All files ending in .php or .inc would be excluded.

-name "*.php" -o -name "*.inc"

This command means search for all files with name ending in .php and .inc

-not \( -name "*.php" -o -name "*.inc" \) 

-not negates the argument. Thus it searches for all files not ending in .php and inc. \( and \) define the boundary of -not. -o means a logical expression. Here -o refers to or.

Finding files containing a string
So far we had limited ourselves to searching by file name. We can also search for files containing certain strings of characters. This is accomplished using the grep command.

find . -exec grep "edit-this-directory" '{}' \; -print 

This command will search the current directory and its sub directories to return all files that contain the string "edit-this-directory".

Finding files containing a string but ignoring svn files
If you are using subversion, you would be annoyed that find is looking through svn-specfic files and returning results from it. To exclude svn files, use

find . -not \( -name *.svn-base \) -exec grep "edit-this-directory" '{}' \; -print 

This command will search the current directory and its sub directories to return all files not ending in svn-base which contain the string "edit-this-directory".

Finding executable files
To find executable files, you should use which or whereis commands. Some linux versions offer GNU locate to find files. All versions of unix and Linux fully support the find command.

File Security

A secure system is able to deny access to unauthorized users while maintaining access for authorized users. Both user and administrator are responsible for maintaining a secure system.

Linux provides two measures to protect a system from unauthorized access. First every user must login. The login and password are verified with /etc/passwd. Second, every file has access permissions associated with it. In this article, we would be discussing the second measure.

Viewing File and Directory Permissions

To view file and directory permission, type the following command:

$ ls -l

This should produce something like:

drwxr-xr-x ali staff accounts
-rwxr-xr-x ali staff test.txt
    (1)    (2)  (3)    (4)


This information could be divided into four different items separated by a space, as indicated by numbers:

  1. permissions
  2. owner
  3. group
  4. file/directory

Lets dissect the permissions a bit more.

d   rwx r-x r-x 
-   rwx r-x r-x 
(1) (2) (3) (4) 

  1. file type: d indicates a directory, - a file
  2. user: owner access permissions
  3. group: group access permissions. Groups are created and maintained by the system administrator. Group definitions are stored in /etc/group
  4. other: permissions for all other users

There are possible four permissions for each user, group, and other, defined by r, w, x, and -.

Permissions Summary
Permission File Directory
r file can be displayed or copied contents can be listed with the ls command
w file contents can be modified users with execute access can add or delete files
x file can be executed, if it is executable user can cd to the directory. users with read access can ls

creating user and groups

Note: To try out the examples in this document, you would need root access.

Users

To create a new user you use the adduser command. To add a user by called linuxjunky:

$ adduser linuxjunky

If you get a command not found error

$ /usr/sbin/adduser linuxjunky

To assign a password to your new user

$  passwd linuxuser

To grant ownership of a file to the new user

$ chown linuxjunky filename

To change grant ownership of a directory to the new user

$ chown -R linuxjunky myfolder/

To list all the user accounts on the computer:

$ ls /home

Groups

Suppose you have three users (john, jim, jake) each of whom require ownership of a directory. For example the web/ directory. The best way to do this would be to create a group. Make them members of this group and grant ownership to this group.

To create a group:

$ groupadd webmanagers

or

$ /usr/sbin/groupadd webmanagers

This would add a line such as the following to the end of your /etc/group

webmanagers:x:507:

Use a text editor such as vi to change this line the following

webmanagers:x:507:john,jim,jake

To grant ownership to a file

chgrp webmanagers index.html

To grant ownership to a directory

chgrp -Rv webmanagers web/

alias command

The alias command assigns a command or set of commands to a string. Aliases are generally used to simply typing a long command or to execute an option to a command by default.

$ alias aelog='tail -f /var/log/apache2/error.log'
$ alias rm='rm -i'

Now, when we type the command aelog on commandline, it would run tail -f /var/log/apache2/error.log. This is a time saving technique. rm deletes without asking for confirmation. Since we generally like to confirm before deleting anything, creating the alias on second line would now cause the -i option to run by default. The -i option requires the user to confirm the deletion of every single file and directory.

List aliases
To see a list of aliases in your system, type the alias command

$ alias

output

alias aelog='tail -f /var/log/apache2/error.log'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias rm='rm -i'

Making your aliases permanent
When you restart your computer, your aliases are lost. To solution to avoid this annoyance is to write your aliases in ~/.bashrc or the rc file of your shell if you are using another shell. Restart you command terminal and your aliases would work.

Removing aliases
To remove an alias, use the unalias command.

$ unalias aelog

If your aliases are written in your ~/.bashrc, an alias removed by unalias would reappear after reboot. To remove it permanently, delete the alias form ~/.bashrc file.

Comparing files using diff and cmp

The diff command gives details of differences between two files. The cmp command simply tells you whether two files are the same or different.

cmp command

The cmp command simply gives a yes|no answer. Ideal for comparing binary files. If the files are identical, no result is printed. If the files differ, the filename and the byte where the first difference is encountered is returned.

$ cmp one.txt two.txt

diff command

$ diff one.txt two.txt

The results would be something like the following:

14c14
< txt txt txt
---
> text text text

The first line indicates the line number(s) which are different between files. The letter c indicates that line 14 must be changed for them to match. The < symbol refers to the text in the first file. > symbol refers to the second file.

Suppose we the following two text files, a file a.txt

abc
bcd
wxy
xyz

and a file b.txt

abc
bcd
cde
def
efg
ghi
wxy
xyz

When I run a diff command

$ diff a.txt b.txt

I get the following result

2a3,6
> cde
> def
> efg
> ghi

2a3 indicates that new lines have been added between lines 2 and 3. The number 6 indicates that the addition ends on line 6. So now, line 3 in a.txt is equivalent to line 7 in b.txt.

To list files that are different in a directory tree:

$ diff -uwrq dir1 dir2

To ignore case:

$ diff -i f1 f2

To ignore repeat blank spaces and ignore case:

diff -ib f1 f2

copy all files with exception

Sometime we need to copy entire directory with the except of a few files or a specific directory. This could easily be accomplished with a regular expression as follows:

$ mkdir test; cd test
$ touch a.txt; touch b.txt
$ mkdir mod; cd mod
$ touch c.txt touch d.txt
$ mkdir ../views; cd ../views
$ touch e.txt touch f.txt
$ mkdir ../../test2
$ cd ..

Following is the directory structure created by this code.

$ ls -R
.:
a.txt  b.txt  mod  views

./mod:
c.txt  d.txt  touch

./views:
e.txt  f.txt  touch

Now we copy all files except for the mod directory

$ cp -a [^mod]* ../test2

This is what it looks like after copying. All files are copied with the exception of the mod directory and its contents.

$ ls -R ../test2
../test2:
a.txt  b.txt  views

../test2/views:
e.txt  f.txt  touch

Counting files in a directory

To count files in a directory

$ ls directory | wc -l

To count all files in the directory and its subdirectories

$ ls -R directory | wc -l

Creating empty files from command line

There are many ways to create an empty text file from command line. Following are the three easiest ways.
Using vi editor

$ vi newfile
press escape, then :wq

and an empty file would be created.

Using echo

$ echo > newfile.txt

Using touch

$ touch newfile.dat

Deleting files recursively

The rm command deletes files. To delete a directory which is not empty and its subdirectories, use

$ rm -rf directory

However, sometimes you would need to remove specific subdirectory rather than the entire directory and its contents. For example

|-- engine
|-- mod
     |-- .svn
     |-- anytext
     |    |-- .svn
     |    |-- actions
     |-- crontrigger
     |    |-- .svn
     |    |-- views

We want to delete all .svn directories and nothing else. We could manually do rm-r .svn for every .svn directory but there is an easier and more elegant way:

$ rm -rf `find . -type d -name .svn`

find will find all directories name .svn and rm would delete them and their contents.

Editing flv files on Linux

The best free flv editor installable on Linux that I could find is Adobe's RichFLV. Following are the steps to install RichFLV on Linux:

$ cd /home/me/Downloads
$ wget http://airdownload.adobe.com/air/lin/download/1.5/AdobeAIRInstaller.bin
$ wget http://www.richapps.de/files/richflv/RichFLV.air
$ chmod +x AdobeAIRInstaller.bin
$ ./AdobeAIRInstaller.bin

1. We download Adobe Air Installer and RichFLV
2. We installed Adobe Air Installer
3. We make Adobe Air Installer executable
4. We run Adobe Air Installer
5. Follow instructions and locate RichFLV.air when prompted by Adobe Air Installer.

Then start using RichFLV.

Finding IP address on Linux

To find IP address of your system on Linux, simply type:

/sbin/ifconfig

It would return IP address, inet6 address, mask, etc.

Gnome terminal keyboard shortcuts

Key Action
Shift+Ctrl+n new window
Shift+Ctrl+t new tab
Shift+Ctrl+w close tab
Ctrl+{PgUp,PgDown} change tabs
Ctrl+Insert copy
Shift+Ctrl+v paste

How can I find apache username on my server

The easiest way is to type the following command:

$ ps aux | grep 'apache'

You would get list of processes. I can see that the calling a /usr/sbin/apache2 is www-data. So the username of apache is www-data.

www-data  4126  0.0  0.3  32404 14296 ?        S    12:28   0:00 /usr/sbin/apache2 -k start
www-data  4132  0.0  0.1  26048  4520 ?        S    12:28   0:00 /usr/sbin/apache2 -k start
www-data  4134  0.0  0.3  32512 13964 ?        S    12:28   0:00 /usr/sbin/apache2 -k start
www-data  4135  0.0  0.1  26048  4516 ?        S    12:28   0:00 /usr/sbin/apache2 -k start
www-data  4145  0.0  0.3  31892 12964 ?        S    12:29   0:00 /usr/sbin/apache2 -k start

Installing Software on Linux

To install software on a Linux system, you can either use a package manager or install the software manually. Red Hat distributions (Fedora, CentOS, RHEL) use the Red Hat Package Manager (RPM) while Debian-based systems such as Ubuntu use Debian Package Management System (DPMS). the tool provided by Red Hat to use RPM is also called rpm. APT is a widely-used tool to work with DPMS.
RPM simplifies the task of installing, uninstalling, and managing installed software. Its primary functions are:

Like RPM, DPMS also simplifies the task of installing, uninstalling and managing software. DPMS packages have a .deb extension and these files can be easily manipulated with the dpkg application. APT is a wrapper tool which directly accesses dpkg and provides a powerful library of programming functions such as "apt-get" to its users. APT manages software via repositories in the sources.list configuration file.

Many GUI-based tools are also available if you do not prefer using CLI tools such as RPM or APT. Red Hat distributions provide RPM-based GUI, Debian-based systems provide synaptic, SuSe distributions use YAST (Yet Another Setup Tool) and there are many other tools available freely.

Advanced Packaging Tool - APT
APT allows users to install, uninstall, search and upgrade packages on Debian systems.

To search for a package:

apt-cache search package_name

To install a package

sudo apt-get install package_name

To uninstall a package

sudo apt-get remove package_name

To update your installed packages

sudo apt-get update

When you update, apt finds, downloads, and installs patches and updates for all your installed packages

To upgrade your system

sudo apt-get upgrade

To get help with apt commands:

apt-get help

Synaptic Package Manager
If you prefer a GUI tool instead of command line apt, there is the synaptic package manager, generally referred to as Synaptic. To start Synaptic, go to System > Administration > Synaptic Package Manager. You will be asked for the root password. Simply search for your package, choose and mark to select it and click on mark to install it. Click on unmark and apply to uninstall software.

Public key authentication with SSH

The following steps show how to set up public key authentication with SSH

1. Generate public/private key on your system

ssh-keygen -t rsa

2. Copy your public key to the remote server

scp ~/.ssh/id_rsa.pub xyz.com:~/.ssh/authorized_keys

Quick way to find which log file is logging errors

On development systems it is common practice to have different log file to log PHP error logs for different tools and different versions of the same tool. Generally it is a good idea to use meaningful names to make it easy to distinguish between the log files. If you have doubts, you can either look inside the code to see which error log file is being called or do the following:

$ cd ~
$ ls -al /var/log/httpd > before.diff

Run the tool such that it would write something to the log file. Then

$ ls -al /var/log/httpd > after.diff
$ diff before.diff after.diff

The diff file would show the file(s) which have changed. Read these files to see whether they are indeed the correct files.

Recusively counting lines in in files in a directory

To count lines of code in file:

wc -l filename

To count lines in all php files in a directory

wc -l *.php

This command counts lines in all php files in a directory but ignore its subdirectories. To include subdirectories, use the following command

find . -name '*.php' | xargs wc -l

Redirection

UNIX processes generally take input from standard input (keyboard), write output to standard out (screen) and may write to standard error if there are any error message which need to be displayed. Among the various powerful functions of Linux systems is the ability to redirect both input and output of commands.

The > symbol redirects output of a command. For example, cal command prints the calendar to screen. Using > we can save this calendar into a file.

cal > calendar.txt
cat calendar.txt

If the file calendar.txt already exists, its contents would be replaced. If you wish to append content to an existing file, use the >> symbol instead.

ls >> calendar.txt
cat calendar.txt

You can also use cat to concatenate files:

cal 2007 > 2007
cal 2008 > 2008
cat 2007 2008 > calendar.txt

Renaming multiple files

To rename multiple files using the command line, you can use the rename command or a shell script. Its syntax is as follows.

$ rename regular_expression files 

As you can see, the catch is that you need to know regular expressions. But for simpler tasks, regular expressions are really easy. For example,

$ rename *.htm *.html

This changes the extension of all files from .htm to .html.

$ rename "s/ *//g" *.jpg

This code removes spaces from filenames. Rename functions can be used for more powerful renaming features but the limitation is what you can do with a regular expression. For more powerful features, you need to write a shell script. The following shell script renames all photos in a folder to rockies-100.jpg, rockies-101.jpg, ...

i=100
for j in *.jpg; 
    do mv "$j" "rockies-$i.jpg" ;(( i++ )); 
done

A note about shell scripts. Do not add additional space i = 1 is not the same as i=1

rsync command

Rsync is a very useful alternative to rcp and scp. It tool lets you copy files and directories between a local host and a remote host. The main advantage of using rsync is that rsync can use SSH as a secure channel, send/receive only the bytes inside files that changed since the last replication, and remove files on the destination host if those files were deleted on the source host to keep both hosts in sync.

rsync -avz -e ssh user@host1.com:/~mydir ~/

This copies mydir and its contents from user1 to local

rsync -avz -e ssh user@host1.com:/~mydir/ ~/

This copies only contents of mydir from user1 to local. The only different between the two is a trailing slash.

Running a program at startup on Ubuntu

If you want a program to start when you boot your computer, you need to do the following:

1. Go to System > Preferences > Startup Applications
2. You will get a list of program that run at Startup
3. Click on Add
4. Choose a name and type the command you would type on the terminal to run this program
5. Click on Add and then Close.
6. Restart your computer to verify

Some quick APT commands

Advanced Packaging Tool (APT) is used by Debian system to manage package installations. Following are some quick commands:

Some common commands that can be used with APT:

Installing packages

$ sudo apt-get install package

Removing packages

$ sudo apt-get remove package

Getting a list of packages

$ sudo apt-get update

Upgrading packages

$ sudo apt-get upgrade

Sorting output of ls by date

A simple ls lists directory contents by name. To list by time, use

$ ls -t

To see time in the listing

$ ls -lt

To show the most recently modified files first

$ ls -ltr

SSH installation, usage, and troubleshooting

SSH or Secure Shell is used to access Linux machines remotely. It is among the most useful and powerful utilities of Linux.

Checking if SSH is already installed
Most likely SSH does not need to be installed on your system because it is already install. To check if it is already installed:

$ which ssh

or

$ whereis ssh

Installing SSH
If it is not installed, the easiest way to install SSH is through a package manager:

$ yum install openssh-clients.i386

on Red Hat/Fedora

$ sudo apt-get install openssh-client

on Ubuntu

If you prefer to install SSH client manually, follow these steps: can be downloaded from www.openssh.com. To install, do the following:

  1. download openssh from http://www.openssh.com
  2. $ tar xzvf openssh-x.x.tgz
  3. $ cd ssh
  4. $ make obj
  5. $ make cleandir
  6. $ make depend
  7. $ make
  8. $ make install
  9. $ cp /etc/ssh/ssh_config /etc/ssh/ssh_config_backup
  10. $ cp /etc/ssh/sshd_config /etc/ssh/sshd_config_backup
  11. $ cp ssh_config sshd_config /etc/ssh
  12. $ mkdir /var/empty

Using SSH
To use, do type the following:

$ ssh -l username remote_address
$ ssh -l user 123.123.123.123

If you are using a port specific port e.g. 7878

$ ssh -l user 123.123.123.123 -p 7878

Using SSH for remote X windows
SSH can be used to open windows applications remotely. For remote X windows, use the -X option.

$ ssh -X -l user 123.123.123.123
$ xeyes &

xeyes is a X windows application. If you do not have xeyes installed on your server, simply install it using "yum install xeyes" or its equivalent for with apt-get.

Error: Can't open display
If you get the error "error: can't open display", you need to check whether the "X11Forwarding" feature is turned on in the SSH server. To check this, log onto the remote server with regular SSH and do the following:

$ locate sshd_config
/etc/ssh/sshd_config
$ vi /etc/ssh/sshd_config

Search the file for "X11Forwarding" and set it to "yes" it is not already set to yes:

X11Forwarding yes

Save file restart sshd service under Debian Linux (such as Ubuntu)

/etc/init.d/ssh restart

On Fedora / Red Hat Linux restart sshd:

$ /etc/init.d/sshd restart

If you the problem is still not solved, log into SSH in verbose mode as follows:

$ ssh -v -X user@host

You will get something like:

debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 123.124.125.126 [123.124.125.126] port 7878.
debug1: Connection established.
...
debug1: Requesting X11 forwarding with authentication spoofing.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Remote: No xauth program; cannot forward with spoofing.

If you get the error message "Remote: No xauth program; cannot forward with spoofing", xauth is either not installed or not linked properly. To fix this problem, do the following on the server:

$ whereis xauth
$ which xauth
/usr/bin/which: no xauth in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

If xauth is not installed, install xauth:

$ yum install xauth
or
$ sudo apt-get xauth

Check the path of the installation

$ which xauth
/usr/bin/xauth

If xauth is installed somewhere else such as "/usr/X11R6/bin/xauth", create a symbolic link to "/usr/local/bin"

$ ln -s /usr/X11R6/bin/xauth /usr/bin/xauth

Try verbose SSH again

$ ssh -v -X user@host

This time the last few lines should look something like the following:

debug1: Requesting X11 forwarding with authentication spoofing.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
/usr/bin/xauth: creating new authority file /root/.Xauthority

Now type

$ xeyes &

and you should see a small window with two eyes following your mouse pointer.

When using ssh, if you get the following message, do not panic. Chances are nothing nasty has happened.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
e2:7c:58:33:c5:c7:ce:75:a9:55:1a:ef:ef:d7:4b:fe.
Please contact your system administrator.
Add correct host key in /home/someone/.ssh/known_hosts to get rid of this message.
Offending key in /home/someone/.ssh/known_hosts:1
RSA host key for www.somesite.com has changed and you have requested strict checking.
Host key verification failed.

Most probably this means that the public key of the host has changed and the key stored in your cache is no longer valid. Simply do the following;

$ vi .ssh/known_hosts

Inside this file, remove the line which refers to the host, e.g. somesite.com. Save the file and start ssh again. That's it!

Using ls to display a file tree

Recently, I found a really elegant solution to display a file tree using the ls command. I like is so much that I feel the need to share it.

It displays files as follows:

   .
   |-application
   |---configs
   |---controllers
   |---models
   |---views
   |-----helpers
   |-----scripts
   |-------error
   |-------index
   |-docs
   |-library
   |-public
   |-tests
   |---application
   |---library

The code is:

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/' 

I got this code from http://www.centerkey.com/tree and you will also find a ready to go shell script there.

If you want to add this your bashrc to create an alias, you would run into your problem since bash doesn't allow single quotes inside inside single quotes even if they are escaped with backslash. See this page to find out more about this. The solution is to use the following line instead.

alias lt='ls -R | grep ":$" | sed -e '"'"'s/:$//'"'"' -e '"'"'s/[^-][^\/]*\//--/g'"'"' -e '"'"'s/^/   /'"'"' -e '"'"'s/-/|    /'"'"''

Alternately, you can simple link to the shell script.

Thanks centerkey.com for this code.

wget error: failure in name resolution

If wget gives you the following message even though your browsers are able to access the Internet, it could be due to a problem with your /etc/resolv.conf

Resolving [some address]... failed: Temporary failure in name resolution

The solution is to edit /etc/resolv.conf and add IP addresses. On Debian systems, the file is /etc/nsswitch.conf

Which linux distribution, build, version am I running

$ cat /proc/version

Sample output:

Linux version 2.6.30.10-105.2.4.fc11.i686.PAE (mockbuild@x86-05.phx2.fedoraproject.org) (gcc version 4.4.1 20090725 (Red Hat 4.4.1-2) (GCC) ) #1 SMP Tue Jan 19 22:46:27 UTC 2010

Checking Disk Space

To check disk space, you can use one the following commands:

$ df

This command would list the amount of space used and available on each partition of the system.

$ du 

du commands lists disk space usage file by file. Read du man pages before using du.

$ vmstat

vmstat outputs virtual memory statistics

$ ls -l

Outputs file names and sizes.

Compressing and Archiving

Quick Reference

tar.gz     tar xzvf x.tar.gz
bz2        bzip2 -cd file.tar.bz2 | tar xvf -
Z          uncompress file.Z
tar.Z      zcat file.Z | tar xvf -

tar command

The tar command is used to collate collections of files into one larger file.

Creating archives

Suppose we have directory xdir containing many files, we use the following command to archive them into one file.

tar -cvf x.tar xdir


This command would archive the directory xdir and its contents into a file called x.tar. The extension '.tar' is used by convention to indicate tar files.

To create an archive and apply gzip to compress the archive,

tar -czvf x.tar.gz xdir

The only difference from last example is the -z option. By convention, we add '.tar.gz' for tar and gzip compressed files.

Extracting archives

To extract an archive, we use

tar -xvf x.tar

It would be extracted to the same directory by default.

To extract a zipped archive, we use

tar -xzvf x.tar.gz

To extract a zipped archive, we use

tar -xzvf x.tar.gz -C another_dir

tar would extract all files to a directory called another_dir

Common command options
c is for create
v is for verbose
f is to store in a file else it will go to stdin/stdout
x is for extract
z is for gzip, ungzip to unzip
j is for bzip2
p is to preserve permissions
C is to direct output to a user-specified location

gzip, gunzip, zcat

New unix users often get confused with gzip, gunzip, and zcat.

gzip
gzip compresses a single file. A tar archive is a single file, so it is common to use tar files in conjuction with gzip

gzip -c xdir.tar > xdir.tar.gz

uncompress
gzip -d xdir.tar.gz

or
gunzip xdir.tar.gz

uncompressing a .bz2 file

bzip2 -cd file.tar.bz2 | tar xvf -

or
bzcat file.tar.bz2 | tar xvf -

SCP - Secure Copy

SCP allows file to be copied to, from, or between different hosts using ssh for data transfer.

Copy files from remote computer to a local computer:

$ scp username@www.xzy.com:~/somefile.txt /home/local/directory

Copy files from local computer to remote computer:

$ scp /home/local/directory/somefile.txt username@www.xzy.com:~/

Copying a directory
Copy a directory 'dir' from local computer to remote computer

$ scp -r /home/local/dir username@www.xzy.com:~/

Using a specific cipher
SCP uses Triple-DES by default. To change cipher, use the following command:

$ scp -c blowfish username@www.xzy.com:~/somefile.txt /home/local/directory

Using a port other than 22
By default scp is configured to used port 22. If you a different port for secure shell, e.g. 7822, you would get the following error message:

ssh: connect to host some_site port 22: Connection refused
lost connection

To specify a different port, you need to use the -P option. To copy files from local computer to remote computer:

$ scp -P 7822 phyml_v3.0.tar.gz username@xyz.com:~/

To copy files from remote computer to local computer:

$ scp -P 7822 username@xyz.com:~/somefile.txt /home/local/directory

Ubuntu

Here you will find articles specific to Ubuntu Linux distribution

Adding entry to Ubuntu GNOME menu

Adding an item to a GNOME menu is easy.

1. right-click on the Applications menu
2. Choose Edit menus
3. A window called Main menu would open up. Choose a menu from the left pane
4. Click on New Item and fill in the Create Launcher form
5. Close the window and that's it.

Dual Monitors on Ubuntu

I recently hooked my laptop to two monitors. Ubuntu successfully detected both monitors but the second monitor was a mirror image of the first instead of an extension. I peeked around on Ubuntu Forums and Google. There was much talk about installing drivers such as Xinerama, Twin View, MergedFB, BigDesktop, etc. Numerous frustrated posts.

To my frustration, none of the drivers worked and I decided to do a clean install of Ubuntu 8.10. Then I simply went into System > Preferences > Monitors and unchecked the option "Same image on all monitors". You can also drag around the desktops to fit your needs, set different resolutions, etc.

This simple solution worked perfectly for me. My advice, try this before you attempt to change display drivers.

Graphically logging into a remote Linux desktop

I have been using SSH to log into remote servers for over a decade. I prefer commandline but every now and then I need to use the graphical interface. For this Ubuntu provides a simple and beautiful solution.

1. Click on Places > Connect to Server
2. Choose SSH or whatever applies for service type
3. Provide server address and port information
4. Click on connect. You will be asked for your username and password

Once authenticated, you a directory would open on your system. You can apply most of your software on these files. Use gedit, netbeans, GIMP, etc. Its is really fantastic.

If you were looking for SSH instructions, click here.

Installing DBDesigner4 on Ubuntu

DBDesigner 4 is a great free tool for database design, modeling, and visualization. Following are simple steps to install it on Ubuntu.

$ mkdir ~/src/dbdesigner
$ cd !!:1 # or $ cd ~/src/dbdesigner

Download the latest version of DBDesigner RPM (DBDesigner4-0.x.x.x.i586.rpm) from http://fabforce.net/downloads.php

wget ftp://195.220.108.108/linux/sourceforge/s/project/sk/skychart/OldFiles/l...

You cannot install RPM on Ubuntu since it uses debian package manager. You need to convert rpm packages to deb packages using alien first. If you do not have alien, install it as follows:

$ sudo alien *.rpm

Install the packages

$ sudo dpkg -i *.deb

Link to library

$ cd /usr/lib
$ sudo ln -s DBDesigner4/libsqlmy.so

Link to binary with an alias "dbd4" or any other name you like

$ cd /usr/lib
$ sudo ln -s /opt/DBDesigner4/DBDesigner4 dbd4
$ exit

The default settings should work fine but you can edit user settings if you wish to do so

cd ~/.DBDesigner4
vi DBConn_DefaultSettings.ini

Run DBDesigner4

$ dbd4

Installing pdf reader on Ubuntu

Ubuntu provides a free pdf reader which cannot handle fillable form. If you attempt to open such a document, you would get the following message:

If this message is not eventually replaced by proper contents of the document, you PDF viewer may not be able to display this type of document.

You can upgrade to the latest version of Adobe Reader for Windows, Mac, or Linux by visiting http://www.adobe.com/products/acrobat/readstep2.html.

For more assistance with Adobe Reader visit http://www.adobe.com/support/products/acrreader.html.

The simplest solution to this problem is to install natty's Adobe Reader.

$ sudo apt-get install acroread

Installing skype on Ubuntu

1. Go to Skype download page
2. Click on the suitable version to download
3. When you see the download prompt, choose to open with Ubuntu Software Centre
4. Follow instructions skype would be ready to use in a minute
5. To use, click on Applications > Internet > Skype

Installing Sun Java on Ubuntu 10+

As of Ubuntu 10, users are encouraged to use OpenJDK instead of Sun Java JKD/JRE packages. OpenJDK generally works very well but every now and then you would need to install Sun Java. For example, schemaSpy returns errors with OpenJDK. To install Sun Java, do the following:

sudo apt-get install sun-java6-jdk sun-java6-jre sun-java6-plugin

If you get an "unable to locate package message", then you need to add the canonical repository. To do so, do the following:

sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner"

and update to refresh the repository

sudo apt-get update

and the following to configure your packages to use Sun Java instead of OpenJDK

sudo update-alternatives --config java

Ubuntu: could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

Error: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

Solution:
1. Open httpd.conf for editing

sudo vi /etc/apache2/httpd.conf

You might be surprised to see that the file is empty
2. type the following:

ServerName localhost

3. Save file and restart apache server

service apache2 restart

Restarting X Server on Ubuntu

>/etc/init.d/gdm restart

* Stopping GNOME Display Manager... [ ok ]
* Starting GNOME Display Manager... [ ok ]