0

LFCS command line part 1

Welcome to post 17 of my 100 day challenge. Checkout my introduction for some background.

This is post two of my LFCS series. In this post I will discuss the intricacies of the Linux command line interface. You can go back to the overview post for a brief introduction or take a look at post one from yesterday for instructions on setting up the exam practice system which I will be using throughout this series.

This is the first of a two part article. I have updated the overview post appropriately. I ran out of time writing the article so had to split it up into two parts. The second part will be tomorrow.

LFCS command line part 1

What is the Linux command line?

The Linux command line more correctly known as the shell is a textual display mode within which we input commands to interact with the computer. The default shell or command line interpreter on Linux and OSX is the Bourne Again SHell commonly known as the BASH shell.

On Linux distributions running a graphical user interface the BASH shell runs inside a terminal emulator examples of this are konsole for KDE and gnome-terminal for Gnome. It will more than likely be referred to as the ‘terminal’ on most distributions.

Although there are many different flavours of terminal emulator they all do the same thing which is to give us access to the command shell. My own personal favourite is terminator which is a terminal multiplexor for the Gnome desktop environment. A multiplexor enables me to have multiple terminal sessions open within the same window or multiple tabs.

Editing text files on the command line

There are many ways to edit text files on the command line. Here I will discuss the main ones that are available on CentOS but this most likely apply to you if you are using a different Linux distribution. My editor of choice is vi/vim. This is available on all POSIX derived Linux Standard Base compliant Linux distributions by default so it is a good choice for editing text files.

The vim minimal package is installed by default on CentOS 6.4 which is less feature rich. You can check this out for yourself by issuing the following command as the root user:

# yum provides $(which vim)

Which should give you an output similar to:

[root@centospractice ~]# yum provides $(which vi)
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.ukhost4u.com
 * extras: mirror.ukhost4u.com
 * updates: www.mirrorservice.org
2:vim-minimal-7.2.411-1.8.el6.x86_64 : A minimal version of the VIM editor
Repo : base
Matched from:
Filename : /bin/vi
2:vim-minimal-7.2.411-1.8.el6.x86_64 : A minimal version of the VIM editor
Repo : installed
Matched from:
Other : Provides-match: /bin/vi

You can see we have vim-minimal installed. Install the full version of vim with the following commands:

[root@centospractice ~]# yum -y install vim-enhanced
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.melbourne.co.uk
 * extras: centos.serverspace.co.uk
 * updates: mirror.bytemark.co.uk
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 2:7.2.411-1.8.el6 will be installed
--> Processing Dependency: vim-common = 2:7.2.411-1.8.el6 for package: 2:vim-enhanced-7.2.411-1.8.el6.x86_64
--> Processing Dependency: perl(:MODULE_COMPAT_5.10.1) for package: 2:vim-enhanced-7.2.411-1.8.el6.x86_64
--> Processing Dependency: libperl.so()(64bit) for package: 2:vim-enhanced-7.2.411-1.8.el6.x86_64
--> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.2.411-1.8.el6.x86_64
--> Running transaction check
---> Package gpm-libs.x86_64 0:1.20.6-12.el6 will be installed
---> Package perl.x86_64 4:5.10.1-136.el6_6.1 will be installed
--> Processing Dependency: perl(version) for package: 4:perl-5.10.1-136.el6_6.1.x86_64
--> Processing Dependency: perl(Pod::Simple) for package: 4:perl-5.10.1-136.el6_6.1.x86_64
--> Processing Dependency: perl(Module::Pluggable) for package: 4:perl-5.10.1-136.el6_6.1.x86_64
---> Package perl-libs.x86_64 4:5.10.1-136.el6_6.1 will be installed
---> Package vim-common.x86_64 2:7.2.411-1.8.el6 will be installed
--> Running transaction check
---> Package perl-Module-Pluggable.x86_64 1:3.90-136.el6_6.1 will be installed
---> Package perl-Pod-Simple.x86_64 1:3.13-136.el6_6.1 will be installed
--> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.13-136.el6_6.1.x86_64
---> Package perl-version.x86_64 3:0.77-136.el6_6.1 will be installed
--> Running transaction check
---> Package perl-Pod-Escapes.x86_64 1:1.04-136.el6_6.1 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================
 Package Arch Version Repository Size
====================================================================================================
Installing:
 vim-enhanced x86_64 2:7.2.411-1.8.el6 base 892 k
Installing for dependencies:
 gpm-libs x86_64 1.20.6-12.el6 base 28 k
 perl x86_64 4:5.10.1-136.el6_6.1 updates 10 M
 perl-Module-Pluggable x86_64 1:3.90-136.el6_6.1 updates 40 k
 perl-Pod-Escapes x86_64 1:1.04-136.el6_6.1 updates 32 k
 perl-Pod-Simple x86_64 1:3.13-136.el6_6.1 updates 212 k
 perl-libs x86_64 4:5.10.1-136.el6_6.1 updates 578 k
 perl-version x86_64 3:0.77-136.el6_6.1 updates 51 k
 vim-common x86_64 2:7.2.411-1.8.el6 base 6.0 M
Transaction Summary
====================================================================================================
Install 9 Package(s)
Total download size: 18 M
Installed size: 54 M
Downloading Packages:
(1/9): gpm-libs-1.20.6-12.el6.x86_64.rpm | 28 kB 00:00
(2/9): perl-5.10.1-136.el6_6.1.x86_64.rpm | 10 MB 00:18
(3/9): perl-Module-Pluggable-3.90-136.el6_6.1.x86_64.rpm | 40 kB 00:00
(4/9): perl-Pod-Escapes-1.04-136.el6_6.1.x86_64.rpm | 32 kB 00:00
(5/9): perl-Pod-Simple-3.13-136.el6_6.1.x86_64.rpm | 212 kB 00:00
(6/9): perl-libs-5.10.1-136.el6_6.1.x86_64.rpm | 578 kB 00:00
(7/9): perl-version-0.77-136.el6_6.1.x86_64.rpm | 51 kB 00:00
(8/9): vim-common-7.2.411-1.8.el6.x86_64.rpm | 6.0 MB 00:11
(9/9): vim-enhanced-7.2.411-1.8.el6.x86_64.rpm | 892 kB 00:00
----------------------------------------------------------------------------------------------------
Total 423 kB/s | 18 MB 00:43
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
 Package: centos-release-6-4.el6.centos.10.x86_64 (@anaconda-CentOS-201303020151.x86_64/6.4)
 From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
 Installing : 1:perl-Pod-Escapes-1.04-136.el6_6.1.x86_64 1/9
 Installing : 1:perl-Module-Pluggable-3.90-136.el6_6.1.x86_64 2/9
 Installing : 4:perl-libs-5.10.1-136.el6_6.1.x86_64 3/9
 Installing : 1:perl-Pod-Simple-3.13-136.el6_6.1.x86_64 4/9
 Installing : 3:perl-version-0.77-136.el6_6.1.x86_64 5/9
 Installing : 4:perl-5.10.1-136.el6_6.1.x86_64 6/9
 Installing : 2:vim-common-7.2.411-1.8.el6.x86_64 7/9
 Installing : gpm-libs-1.20.6-12.el6.x86_64 8/9
 Installing : 2:vim-enhanced-7.2.411-1.8.el6.x86_64 9/9
 Verifying : 2:vim-enhanced-7.2.411-1.8.el6.x86_64 1/9
 Verifying : 3:perl-version-0.77-136.el6_6.1.x86_64 2/9
 Verifying : gpm-libs-1.20.6-12.el6.x86_64 3/9
 Verifying : 1:perl-Pod-Simple-3.13-136.el6_6.1.x86_64 4/9
 Verifying : 1:perl-Module-Pluggable-3.90-136.el6_6.1.x86_64 5/9
 Verifying : 2:vim-common-7.2.411-1.8.el6.x86_64 6/9
 Verifying : 4:perl-5.10.1-136.el6_6.1.x86_64 7/9
 Verifying : 4:perl-libs-5.10.1-136.el6_6.1.x86_64 8/9
 Verifying : 1:perl-Pod-Escapes-1.04-136.el6_6.1.x86_64 9/9

Installed:
 vim-enhanced.x86_64 2:7.2.411-1.8.el6

Dependency Installed:
 gpm-libs.x86_64 0:1.20.6-12.el6 perl.x86_64 4:5.10.1-136.el6_6.1
 perl-Module-Pluggable.x86_64 1:3.90-136.el6_6.1 perl-Pod-Escapes.x86_64 1:1.04-136.el6_6.1
 perl-Pod-Simple.x86_64 1:3.13-136.el6_6.1 perl-libs.x86_64 4:5.10.1-136.el6_6.1
 perl-version.x86_64 3:0.77-136.el6_6.1 vim-common.x86_64 2:7.2.411-1.8.el6

Complete!

Here is a quick vim cheat sheet:

:q Exits vim if no changes have been made.
:q! Exit without saving changes.
:wq! Write current file and exit.
a Append text after cursor location.
A Append text at the end of the line.
i Insert text before the current cursor location.
o Begin a new line in insert mode below the cursor.
O Begin a new line in insert mode above the cursor.
[esc] Exit insert mode.
u Undo changes.
CTRL-R Redo changes which were just undone.
U Undo all latest changes made on one line.
x Delete characters after the cursor.
X Delete characters before the cursor.
dd Delete the line.
/<pattern> Search forward in the file for a pattern.
?<pattern> Search backward in the file for a pattern.
n Repeat the search started with / or ?.
N Repeat the search started with / or ? in the opposite direction.
h or left arrow Cursor key go one character left.
l or right arrow Cursor key go one character right.
k or up arrow Cursor key go one line up.
j or down arrow Cursor key go one line down.

Create or edit files using vim

Perform the following exercise to get used to creating and editing files using vim.

  • Create a file in the root home directory /root called testfile1 you can get to the root directory by typeing in cd and pressing <Enter>:
    # vim testfile1

    press <i> to enter insert mode and type in the following text:

    Three blind mice, see how they run.

Press the <Esc> key to exit insert mode, followed by :wq to save the file.

  • Use cat to output the file to stdout:
# cat testfile1
Three blind mice, see how they run.
  • Open testfile1 again in vim. Press o create a new line below the current line and enter insert mode. Add the following text so the file looks like the example below. Again save the file using <Esc> followed by :wq:
Three blind mice, see how they run.
They all ran after the farmer's wife.
  • Once again use cat to output the file and confirm the changes that you have just saved:
# cat testfile1
Three blind mice, see how they run.
They all ran after the farmer's wife.

Great you have successfully created a file with vim.

Tune in tomorrow for part 2 of LFCS command line where we will discuss how to manipulate text files from the command line.

Subscribe to my feed either by E-mail or by RSS to receive updates as they happen.

Can you improve on any of the tips I’ve discussed here? If you can let me know in the comments.

Jason Edwards