Thursday, October 21, 2010

Creating, Editing, and Viewing Text Files with Vim Editor


Vi / Vim Editor


Please note: This post is not completed yet.



 In this post I will show you how to:

  • create a text file or view an existing text file for editing
  • insert new lines / blank lines
  • navigate through the pages of a text file and move around it 
  • go to a specific line number 
  • search for a specific piece of text or information in a text file while it is opened
  • copy, cut, and paste text
  • set certain features on / off

Introduction

Vim is the improved or enhanced version of Vi Text Editor. It is available in most Unix and Linux Operating Systems by default. Its beauty lies in the fact that we can use it in both GUI (Graphical User Interface) and CLI (Command Line Interface) modes.

The Vim Editor can be used for a variety of purposes:
  • to create and edit plain text files
  • to write scripts or codes in a particular Computer Programming Language such as Perl and C
  • to write web pages in HTML  
Its code colorization or syntax highlighting also helps in recognizing keywords and constants (I will cover them in some other post) written in Shell Script or PHP or any other scripting language.

Normally vi would use vim. Please see this note:

$ whatis vim
vim                  (1)  - Vi IMproved, a programmers text editor
$ type vi
vi is aliased to `vim'
$ type vim
vim is /usr/bin/vim
 Vim Editor has these three modes:
  • Escape Mode 
    • This is the default mode in which Vim opens a named file. In the Escape Mode we can move around the file and perform deletion, copying, and pasting of specific text or lines.
  • Insert Mode 
    • This is the mode which is used for editing a text file. 
  • Command Mode
    • This mode is useful for saving an opened file, exiting from Vim, searching for a piece of text in the file, and for environment settings such as turning Line Numbering On or Off.
 How to create a text file or view an existing text file for editing?

Command: vi or vim

Usage:

$ whatis vim
vim                  (1)  - Vi IMproved, a programmers text editor

Examples: 
$ vi helloWorld
or

$ vim helloWorld
A window or view opens and at the bottom the following line is displayed:

"helloWorld" [New File]                                       0,0-1         All
Note:
If the named file existed then it would be opened or else a new file would be created as shown above.

To begin tpying in, hit i or Shift + i 

To save your work and exit from the Vim Editor, hit Esc and Shift + : and type wq and hit the Enter Key. 

To display the contents of the text file we have just created, we can use the cat command: 

$ cat helloWorld
Hello World!
How to insert a new / blank line?

Open a text file:

-bash-2.05b# vi hellWorld


|Vim is an enhanced version of Vi.
It is found on almost all Unix and Linux system.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"hellWorld" 2L, 83C                                           1,1           All

To insert a blank line below the current line, first make sure that we are in the Escape mode. Hit Esc. Now hit o to insert a new line as shown below:

Vim is an enhanced version of Vi.
|
It is found on almost all Unix and Linux system.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
-- INSERT --                                                  2,1           All

The cursor is placed on the second line and the INSERT mode is opened. We cab begin typing here.

To insert a blank line above the current line, hit Esc to close the INSERT mode and return to Escape mode. Now hit O (Shift + o) as shown below:

Vim is an enhanced version of Vi.
|
It can be used for editing a text file easily.
It is found on almost all Unix and Linux system.
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
-- INSERT --                                                  2,1           All
 Once again we are in the INSERT mode and the cursor is placed on the newly created / inserted blank line.

Save your work by hitting Shift + : and typing w and then hitting the Enter Key. To quit from the Vi editor, hit Shift + : and type q and then hit the Enter Key.


To save and quite in one go following this procedure: Shift + :wq




Tuesday, October 19, 2010

Creating & Removing Directories & Files

Unix / Linux - Getting Started


In this post I will show you how to

  • create a directory
  • change a directory or directory path
  • show the current / present working directory or path and switch between the current and the previous directories or paths
  • create a plain text  file
  • delete  a directory, a file, and a directory structure completely

Command: mkdir

Usage:

$ whatis mkdir
mkdir                (1)  - make directories
mkdir                (2)  - create a directory
$

Examples:

$ mkdir scripts
The above command creates an empty directory and name it scripts.


We can also define a sub-directory to be created beneath the top directory or in a specified directory path:

$ mkdir first/second/third
mkdir: cannot create directory `first/second/third': No such file or directory





As the specified directories first and second do not already exist or the path is invalid so we need to use the option "-p" to create the parent directories as well:


$ mkdir -p first/second/third



Let's check the contents of the parent directory first by recursively listing the directory contents:


$ ls -R first/
first/:
second

first/second:
third

first/second/third:
$


Command: cd

Usage:

$ whatis cd
cd [builtins]        (1)  - bash built-in commands, see bash(1)
$

Examples:


$ cd first
The above changes directory to first if it is found in the current directory / location or path. We can specify a path also:

$ cd /tmp/samples


Command: pwd

Usage:

$ whatis pwd
pwd                  (1)  - print name of current/working directory
pwd [builtins]       (1)  - bash built-in commands, see bash(1)
$


Examples:

$ pwd
/tmp/samples
$
The above command shows the complete path to our current working directory (CWD). We can check the previous directory name by using a special environmental or system variable "OLDPWD" as shown below:

 $ echo $OLDPWD
/root

Conversely we can use a variable that gives us the same information as produced by the command pwd:

$ echo $PWD
/tmp/samples
Let's change our current directory and go back in the previous one:

$ cd -
/root
$
Please note the hyphen "-" after "cd". We could have as well used:

$ cd /root

To directly go to the home directory we can use the option "~" or simply type "cd" without any option:

$ pwd
/tmp/samples
$ cd ~
$ pwd
/root

$ cd -
/tmp/samples
$ cd
$ pwd
/root

Command: rmdir

Usage:

$ whatis rmdir
rmdir                (1)  - remove empty directories
rmdir                (2)  - delete a directory
$

Examples:

 $ pwd
/root/first/second
$ ls -R
.:
third

./third:
$ rmdir third
$ ls -R
.:

The command "rmdir" deletes or removes only an empty directory.


$ rmdir first/
rmdir: `first/': Directory not empty
$
To recursively delete a directory structure we can pass the "-r" option to the command "rm" as shown below:

$ rm -r first/

Command: rm

Usage:

$ whatis rm
rm                   (1)  - remove files or directories
$

Examples:

$ rm hi
We can pass "-i" to interactively remove a file or directory:

$ rm -i hello
rm: remove regular empty file `hello'? y
We can pass "-f" to forcefully remove a file or directory, and "-r" to recursively delete a directory and all its sub-directories and files in one go.

Command: touch


Usage:

$ whatis touch
touch                (1)  - change file timestamps
$

Examples:

$ touch HelloWorld

The above command creates a plaint text file and name it "HelloWorld". The file contains nothing, i.e. it's empty.

$ touch HelloWorld
$ ls -l HelloWorld
-rw-r--r--  1 root root 0 Oct 20 21:38 HelloWorld

Had the file already been created, its time-stamp would have been updated:
$ touch HelloWorld
$ ls -l HelloWorld
-rw-r--r--  1 root root 0 Oct 20 21:39 HelloWorld
$ touch HelloWorld
$ ls -l HelloWorld
-rw-r--r--  1 root root 0 Oct 20 21:40 HelloWorld
For creating a text file that can contain some text or data we can use "cat" whose primary function is to concatenate and display contents of text files.


Command: cat


Usage:

$ whatis cat
cat                  (1)  - concatenate files and print on the standard output


Examples:

$ cat > HelloWorld
Hello, world!
This is a plain text file.

<Hit Ctrl + c to Cancel Entering Text or Ctrl + d to Terminate the Operation>
Let's display the contents of the file we created above:

$ cat HelloWorld
Hello, world!
This is a plain text file.
Note:

  • We do not use the greater then  ">" symbol after cat when we want to display the contents of the file. The angle bracket ">" acts as an Input Redirection Operator. The input is coming from the commonly used standard input device, i.e. the computer keyboard.
  • On some systems hitting Ctrl + d may terminate the log-in session as it is also used to Exit / Log off from an opened session. 
  • cat > fileName creates a new file if it doesn't already exist. If it exists then its contents are overwritten.

Let's create two text files and enter some data, and then concatenate them and display their contents:

$ cat > OS
Unix
Linux
Mac
<Hit Ctrl + c>

$ cat > Desktop
KDE
GNOME
CDE
<Hit Ctrl + c> 

$ cat OS Desktop
Unix
Linux
Mac
KDE
GNOME
CDE
For creating, editing, and viewing of text files and searching for a specific piece of information or text in them we use a Text Editor such as Vi or Vim and Emacs. I will cover them in some other post.

Directory Listing with the Command "ls"

Unix / Linux - Getting Started


Here I am going to show you some frequently used commands on a Unix / Linux running computer system.

In this post we will see how to do a directory listing of files and directories.

Command: ls [Options] [FileOrDirectoryName]
Usage: 

$ whatis ls
ls                   (1)  - list directory contents

Examples:


$ ls
bin  bin.tar.bzip2  co  contacts  contacts-new  hello  hi  Mail  man  mbox  mysql-init  test
$

Only directory and file names are listed. No extra or detailed information is displayed along with them.

We can also pass a file or directory name along with ls

$ ls bin
case      child.sh    contacts     hello     parent.sh    readLine.sh   variable.sh
case.man  compVar.sh  dumpHTML.sh  manifest  readFile.sh  scriptPid.sh
$
In the above example we have passed the directory name "bin" as the argument to the command ls so that its contents are displayed.

We can also pass a file name. If it exists then the file name itself is echoed otherwise we get an error:

$ ls contacts
contacts
$ ls salary
ls: salary: No such file or directory
$
For a detailed information we can use "-l" option:



$ ls -l
total 48
-rwxr-xr-x  1 root root 243 Aug 26 19:15 case
-rw-r--r--  1 root root 289 Aug 26 19:51 case.man
-rwxr-xr-x  1 root root  26 Aug 30 16:28 child.sh
-rw-r--r--  1 root root 402 Aug 31 16:20 compVar.sh
-rw-r--r--  1 root root 162 Aug 30 18:58 contacts
-rwxr-xr-x  1 root root 376 Sep  2 20:59 dumpHTML.sh
-rw-r--r--  1 root root   0 Sep  5 18:59 hello
-rwxr-xr-x  1 root root 156 Aug 27 14:22 manifest
$


To include hidden files (file and directory names that begin with a dot ".") we can pass "-a" option:


$ ls -a
.              .bashrc        contacts        .gstreamer-0.8  Mail            mysql-init  .Xauthority
..             bin            contacts-new    .gtkrc          man             .ssh
.bash_history  bin.tar.bzip2  .fonts.cache-1  hello           mbox            test
.bash_profile  co             .gnome          hi              .mysql_history  .viminfo
$

Sorting the directory list displaying largest to smallest files and directories:

$ ls -lS
total 64
-rw-r--r--  1 root root 10240 Oct  6 21:50 hello
-rw-r--r--  1 root root 10240 Oct  6 21:54 hi
drwxr-xr-x  2 root root  4096 Sep  5 18:59 bin
drwx------  2 root root  4096 Sep 26 15:34 Mail
drwxr-xr-x  3 root root  4096 Aug 26 20:59 man
drwxr-xr-x  3 root root  4096 Sep 17 14:22 test
-rw-r--r--  1 root root  1739 Oct  6 22:12 bin.tar.bzip2
-rw-------  1 root root   784 Sep 26 15:34 mbox
-rw-rw-rw-  1 root root   218 Oct 10 16:13 contacts
-rw-r--r--  1 root root   210 Oct 10 16:04 contacts-new
-rw-r--r--  1 root root   207 Oct  6 22:02 co
-rw-r--r--  1 root root    85 Sep 21 18:53 mysql-init
$
Sorting the directory list displaying newest to oldest files and directories:
 $ ls -lt
total 64
-rw-rw-rw-  1 root root   218 Oct 10 16:13 contacts
-rw-r--r--  1 root root   210 Oct 10 16:04 contacts-new
-rw-r--r--  1 root root  1739 Oct  6 22:12 bin.tar.bzip2
-rw-r--r--  1 root root   207 Oct  6 22:02 co
-rw-r--r--  1 root root 10240 Oct  6 21:54 hi
-rw-r--r--  1 root root 10240 Oct  6 21:50 hello
-rw-------  1 root root   784 Sep 26 15:34 mbox
drwx------  2 root root  4096 Sep 26 15:34 Mail
-rw-r--r--  1 root root    85 Sep 21 18:53 mysql-init
drwxr-xr-x  3 root root  4096 Sep 17 14:22 test
drwxr-xr-x  2 root root  4096 Sep  5 18:59 bin
drwxr-xr-x  3 root root  4096 Aug 26 20:59 man
To reverse the  sorting, i.e. to sort in descending order, we can use "-r" option. We can use any combination of options. For example to display all the files and directories, including the hidden ones, from oldest to newest we can issue the following command:


$ ls -alrt
total 132
drwx------   2 root root  4096 Jul 27  2004 .gnome
drwxr-xr-x   2 root root  4096 Jul 27  2004 .gstreamer-0.8
-rw-r--r--   1 root root    59 Sep 16  2004 .fonts.cache-1
-rw-r--r--   1 root root   120 Oct 25  2004 .gtkrc
drwx------   2 root root  4096 Mar 29  2005 .ssh
-rw-------   1 root root   259 Mar 29  2005 .Xauthority
-rw-r--r--   1 root root   207 Aug 26 20:58 .bash_profile
drwxr-xr-x   3 root root  4096 Aug 26 20:59 man
drwxr-xr-x   2 root root  4096 Sep  5 18:59 bin
drwxr-xr-x   3 root root  4096 Sep 17 14:22 test
-rw-r--r--   1 root root    85 Sep 21 18:53 mysql-init
drwx------   2 root root  4096 Sep 26 15:34 Mail
-rw-------   1 root root   784 Sep 26 15:34 mbox
-rw-r--r--   1 root root 10240 Oct  6 21:50 hello
-rw-r--r--   1 root root 10240 Oct  6 21:54 hi
-rw-r--r--   1 root root   207 Oct  6 22:02 co
-rw-r--r--   1 root root  1739 Oct  6 22:12 bin.tar.bzip2
-rwxr-xr-x   1 root root    50 Oct  8 15:44 .bashrc
-rw-r--r--   1 root root   210 Oct 10 16:04 contacts-new
-rw-rw-rw-   1 root root   218 Oct 10 16:13 contacts
-rw-------   1 root root  6215 Oct 10 16:13 .viminfo
drwxr-xr-x  19 root root  4096 Oct 19 19:13 ..
-rw-------   1 root root  3107 Oct 19 19:55 .mysql_history
-rw-------   1 root root 11437 Oct 19 19:55 .bash_history
drwxr-xr-x   2 root root  4096 Oct 19 20:29 .hidden
drwxr-xr-x  10 root root  4096 Oct 19 20:29 .
 By default only directory names are displayed, i.e. their contents are not displayed. To display sub-directories and their contents recursively we can use "-R" option as shown below:

$ ls -R
.:
bin  bin.tar.bzip2  co  contacts  contacts-new  hello  hi  Mail  man  mbox  mysql-init  test

./bin:
case      child.sh    contacts     hello     parent.sh    readLine.sh   variable.sh
case.man  compVar.sh  dumpHTML.sh  manifest  readFile.sh  scriptPid.sh

./Mail:

./man:
case.l.gz  cat1

./man/cat1:

./test:
tube

./test/tube:
go  goback  year
$
There are many other options available with the command "ls". We can check out the manual page by issuing the following command:

$ man ls