Pages

Tuesday, February 14, 2012

VI editor (move, delete, copy, paste)


Moving between characters:

If we are writing something and missed a word, pressing backspace will erase the characters that we typed. Instead we can use the following in command mode.

The h key,moves the cursor one character left.
The j key, moves the cursor one character down.
The k key, moves the cursor one character up.
The l key, moves the cursor one character right.
(+& key, moves backward by sentence.
)+& key, moves forward by sentence.


Delete:
To delete something we can do the follwing in command mode.

x key, deletes one character. To delete 10 character once, type: 10x
dw, deletes one word. To delete 10 words once, type: 10dw
dd, deletes one line. To delete 10 lines once, type: 10dd


Copy & Paste:

Press yy to copy / yank a line in command mode and use p to paste it in desired postition.

Linux VI editor


The most powerful and most used editor in linux/unix is vi. In a Unix like operating system you can start with vi <filename>
example: vi test
It will create a file, named test. 

To save the file:
Press esc key and type :q
It wil create a empty file in linux. 

Keep in mind that vi editor has two modes: command mode and insert mode.

To edit a file:
vi test (enters command mode)
press i or insert button (Insert Mode) and start typing.

After editing save with the changes:
Press esc and then 
:wq (write and quit)   or
:x      or
:wq!     or
:x!       or
:ZZ

Now check the file in current directory "ls test". You can check the contents through "cat test" or "more test".

Linux Beginner Part-7 (Piping and Redirection)

Piping:

The pipe character, “|”, is used to chain two or more commands together. The output of the first command is “piped” into the next program, and if there is a second pipe, the output is sent to the third program, etc.  For example:

ls -la /usr/bin | less

In this example, we run the command “ls -la /usr/bin”, which gives us a long listing of all of the files in /usr/bin. Because the output of this command is typically very long, we pipe the output to a program called “less”, which displays the output for us one screen at a time.


Redirection:

Redirection used for different purpose. Sometimes we want to input something in file without editing it with an editor. Example:

echo "this is a test" > test

It will enter the text "this is a test" in the file named test.

Sometimes we need to save the output of a command to a file without showing it in the display. Example:
grep "status=sent" /var/log/mail.log > /tmp/log
It will save the output of the command in the file, named log.

Monday, February 13, 2012

Linux Beginner Part-6 (Informational Commands)


Linux Informational Commands

ps                  Lists currently running process (programs).

w                  Show who is logged on and what they are doing.

df                  Report filesystem disk space usage (“Disk Free” is how I remember it)
Example: df -h

du                  Disk Usage in a particular directory. “du -s” provides a summary for the current directory. Example: du -m

top                  Displays CPU processes in a full-screen GUI. A great way to see the activity on your computer in real-time.  Type “Q” to quit.

free         Displays amount of free and used memory in the system. Example: free -m

cat /proc/cpuinfo Displays information about your CPU.

cat /proc/meminfo Display lots of information about current memory usage.

uname -a         Prints system information to the screen (kernel version, machine type,
etc.)

Linux Beginner Part-5 (Finding Tools)


Finding things in Linux:

which Shows the full path of shell commands found in your path. For example, if you want to know exactly where the “grep” command is located on the file system, you can type “which grep”. The output should be something like: /bin/grep


whereis        Locates the program, source code, and manual page for a command (if all information is available). For example, to find out where “ls” and its man page are, type: “whereis ls”  The output will look something like:

ls: /bin/ls /usr/share/man/man1/ls.1.gz


locate A quick way to search for files anywhere on the filesystem. For example, you can find all files and directories that contain the name “mozilla” by typing:

locate mozilla
If you find "command no found" message in debian, you have to install locate by following process. "apt-get install locate" then run "updatedb".

find        A very powerful command, but sometimes tricky to use. It can be used to search for files matching certain patterns, as well as many other types of searches.  A simple example is:

find . -name \*mp3

This example starts searching in the current directory “.” and all sub-directories, looking for files with “mp3” at the end of their names.

Sunday, February 12, 2012

Linux Beginner Part-4 (Basic Commands)


Basic Linux Commands Description

pwd   “Print Working Directory”. Shows the current location in the directory tree.

cd           “Change Directory”. When typed all by itself, it returns you to your home directory.
Example: cd /home/

cd ~ “~” is an alias for your home directory. It can be used as a shortcut to your “home”, or other directories relative to your home.

cd .. Move up one directory. For example, if you are in /home/vic and you type “cd ..”, you will end up in /home.
Example: If you are in /etc/init.d/, after giving the following command "cd .." you will be in "/etc"

cd - Return to previous directory. An easy way to get back to your previous location!

ls         List all files in the current directory, in column format.

ls -l   List files in “long” format, one file per line. This also shows you additional info about the file, such as ownership, permissions, date, and size.

ls -a   List all files, including “hidden” files. Hidden files are those files that begin with a “.”, e.g. The .bash_history file in your home directory. You can use more options like ls -lah, ls -ld for different types of output.

cat          Display the contents of a text file on the screen. For example: cat <file-name> would display the file we created in the previous section.

head        Display the first few lines of a text file. Example: head /etc/services

tail        Display the last few lines of a text file. Example:  tail /etc/services

tail -f        Display the last few lines of a text file, and then output appended data as the file grows (very useful for following log files!). Example:  tail -f /var/log/messages

cp         Copies a file from one location to another. Example:  cp <file-name> /tmp (copies the file-name file to the /tmp directory)

mv       Moves a file to a new location, or renames it. For example:  mv <file-name> /tmp (copy the file to /tmp, and delete it from the original
location)

touch    Used to create empty files in Linux. Example: touch file1 file2 It will create two empty files named file1 & file2.

rm       Delete a file.  Example:  rm /tmp/<file-name>

mkdir       Make Directory.  Example: mkdir /tmp/myfiles/

rmdir       Remove Directory.  Example: rmdir /tmp/myfiles/

To be continue..... 

Linux Beginner Part-3 (Directory Layout)


The Linux Directory Layout

/            The nameless base of the filesystem. All other directories, files, drives, and devices are attached to this root. Commonly (but incorrectly) referred to as the “slash” or “/” directory. The “/” is just a directory separator, not a directory itself.

/bin Essential command binaries (programs) are stored here (bash, ls, mount, tar, etc.)

/boot Static files of the boot loader.

/dev Device files. In Linux, hardware devices are acceessd just like other files, and they are kept under this directory.

/etc Host-specific system configuration files.

/home Location of users' personal home directories (e.g. /home/susan).

/lib Essential shared libraries and kernel modules.

/proc Process information pseudo-filesystem.  An interface to kernel data structures.

/root The root (superuser) home directory.

/sbin Essential system binaries (fdisk, fsck, init, etc).

/tmp Temporary files.  All users have permission to place temporary files here.

/usr The base directory for most shareable, read-only data (programs, libraries, documentation, and much more).

/usr/bin Most user programs are kept here (cc, find, du, etc.).

/usr/lib Libraries for most binary programs.

/usr/local “Locally” installed files. This directory only really matters in environments where files are stored on the network. Locally-installed files go in /usr/local/bin, /usr/local/lib, etc.). Also often used for software packages installed from source, or software not officially shipped with the distribution.

/usr/sbin Non-vital system binaries (lpd, useradd, etc.)

/usr/share Architecture-independent data (icons, backgrounds, documentation, terminfo, man pages, etc.).

/usr/src Program source code.  E.g. The Linux Kernel, source RPMs, etc.

/var Variable data: mail and printer spools, log files, lock files, etc.

Linux Beginner Part-2 (Special Characters)


Special Characters
Before we continue to learn about Linux shell commands, it is important to know that there are
many symbols and characters that the shell interprets in special ways.


Character Description

/     Directory separator, used to separate a string of directory names.
Example: /usr/src/linux

.    Current directory.  Can also “hide” files when it is the first character in a filename.

..   Parent directory

~   User's home directory

*   Represents 0 or more characters in a filename, or by itself, all files in a directory.
Example: pic*2002 can represent the files pic2002, picJanuary2002, 
picFeb292002, etc.

?   Represents a single character in a filename.
Example: hello?.txt can represent hello1.txt, helloz.txt, but not 
hello22.txt 

|   “Pipe”.  Redirect the output of one command into another command.
Example: ls | more 

>   Redirect output of a command into a new file.  If the file already exists, over-write it.
Example: ls > myfiles.txt 

>>  Redirect the output of a command onto the end of an existing file.
Example: echo “Mary 555-1234” >> phonenumbers.txt 

<   Redirect a file as input to a program.
Example: more < phonenumbers.txt 

;   Command separator.  Allows you to execute multiple commands on a single line.
Example: cd /var/log ; less messages

&&  Command separator as above, but only runs the second command if the first one
finished without errors.
Example: cd /var/logs && less messages 


&   Execute a command in the background, and immediately get your shell back.
Example: find / -name core > /tmp/corefiles.txt & 

Linux Beginner Part -1 (Shell)


What is a command shell?
Answer:
A program that interprets commands
Allows a user to execute commands by typing them manually at a terminal, or automatically in programs called shell scripts.




What is BASH?
Answer: 
Bourne Again SHell
A shell written as a free replacement to the standard Bourne Shell (/bin/sh).
Originally written by Steve Bourne for UNIX systems.
All of the features of the original Bourne Shell, plus additions that make it easier to program with and use from the command line.
Since it is Free Software, it has been adopted as the default shell on most Linux systems.