Linux Basics Jump Start

22 11 2010

Use tab to complete a command. If the text entered is ambiguous then press tab again to see the options, or just complete more of the command or filename.

[root@vvx6 ~]# less install.log
install.log         install.log.syslog  

Find out where you are in the filesystem using the pwd. Move around the filesystem using the cd command.

[root@vvx6 ~]# pwd
/root
[root@vvx6 ~]# cd /tmp/
[root@vvx6 tmp]# pwd
/tmp
[root@vvx6 tmp]#

To move upwards a directory use cd and move to ../

[root@vvx6 tmp]# pwd
/tmp
[root@vvx6 tmp]# cd ../
[root@vvx6 /]# pwd
/
[root@vvx6 /]#

Copy or backup files using the cp command.

[root@vvx6 ~]# cp install.log copied.log

Rename or move a file using the mv command.

[root@vvx6 ~]# mv copied.log moved.log

Delete a file using the rm command.

[root@vvx6 ~]# rm moved.log 
rm: remove regular file `moved.log'? y

Help can be found for a command using the man package.

[root@vvx6 ~]# man ping

The asterisk character is expanded by the command shell before the command is run, so it’s useful when dealing with files of similar name.

Eg; List all the files in the /etc/sysconfig/network-scripts/ directory that begin with the string “ifcfg-“:

[root@vvx6 ~]# ls -l /etc/sysconfig/network-scripts/ifcfg-*
-rw-r--r--  1 root root 125 Nov 11 17:36 /etc/sysconfig/network-scripts/ifcfg-bond0
-rw-r--r--  1 root root 109 Nov 11 17:36 /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r--r--. 1 root root  73 Nov 11 17:19 /etc/sysconfig/network-scripts/ifcfg-eth1
-rw-r--r--. 1 root root 254 Sep 27 20:45 /etc/sysconfig/network-scripts/ifcfg-lo
[root@vvx6 ~]#

The grep package is a very powerful search tool that accepts regular expressions and can do very complex searches, however it’s useful as a general finder tool when searching for simple text.

Eg; Find all instances of the text string “kernel” in the install.log file:

[root@vvx6 ~]# grep kernel install.log
Installing kernel-devel-2.6.35.6-45.fc14.x86_64
Installing kernel-headers-2.6.35.6-45.fc14.x86_64
Installing kernel-2.6.35.6-45.fc14.x86_64
[root@vvx6 ~]#

Eg; Find all instances of the text string “IPADDR” in the /etc/sysconfig/network-scripts/ directory where the filenames begin “ifcfg-“:

[root@vvx6 ~]# grep IPADDR /etc/sysconfig/network-scripts/ifcfg-*
/etc/sysconfig/network-scripts/ifcfg-bond0:IPADDR="172.27.5.106"
/etc/sysconfig/network-scripts/ifcfg-lo:IPADDR=127.0.0.1
[root@vvx6 ~]# 

Eg; Same as above but list the line number where the text string is found:

[root@vvx6 ~]# grep -n IPADDR /etc/sysconfig/network-scripts/ifcfg-*
/etc/sysconfig/network-scripts/ifcfg-bond0:6:IPADDR="172.27.5.106"
/etc/sysconfig/network-scripts/ifcfg-lo:2:IPADDR=127.0.0.1
[root@vvx6 ~]#

Use the | (pipe character) to concatenate the commands together. This allows the output of the first command to be the input to the next command. eg

[root@vvx6 ~]# ls -l | grep iso
-rw-r--r--  1 root root 3520802816 Nov 11 17:20 Fedora-14-x86_64-DVD.iso
[root@vvx6 ~]#

Use the commands head and tail to look at the top and bottom of a file (the default is to show 10 lines). eg;

[root@vvx6 ~]# head install.log
Installing libgcc-4.5.1-4.fc14.x86_64
warning: libgcc-4.5.1-4.fc14.x86_64: Header V3 RSA/SHA256 Signature, key ID 97a1071f: NOKEY
Installing setup-2.8.23-1.fc14.noarch
Installing filesystem-2.4.35-1.fc14.x86_64
Installing ca-certificates-2010.63-3.fc14.noarch
Installing basesystem-10.0-3.noarch
Installing poppler-data-0.4.3-1.fc14.noarch
Installing mailcap-2.1.34-1.fc14.noarch
Installing xkeyboard-config-1.9-6.fc14.noarch
Installing ncurses-base-5.7-8.20100703.fc14.x86_64
[root@vvx6 ~]# tail install.log
Installing dracut-006-3.fc14.noarch
Installing kernel-2.6.35.6-45.fc14.x86_64
Installing system-setup-keyboard-0.8.6-2.fc14.1.x86_64
Installing xorg-x11-server-Xorg-1.9.0-15.fc14.x86_64
Installing systemtap-runtime-1.3-2.fc14.x86_64
Installing systemtap-1.3-2.fc14.x86_64
Installing freenx-server-0.7.3-18.fc13.x86_64
Installing autofs-5.0.5-28.fc13.x86_64
Installing sysprof-1.1.6-1.fc14.x86_64
*** FINISHED INSTALLING PACKAGES ***
[root@vvx6 ~]#

Use the less command to read a whole file (use the q key to exit out of the file):

[root@vvx6 ~]# less anaconda.log

# Kickstart file automatically generated by anaconda.

#version=DEVEL
install
cdrom
# System services
services --disabled="autofs,avahi-daemon,cups,mdmonitor,netfs,portreserve,sendmail" --enabled="network"

lang en_US.UTF-8
keyboard uk
network --onboot no --device eth0 --noipv4 --noipv6
network --onboot no --device eth1 --noipv4 --noipv6
timezone Europe/Dublin
rootpw  --iscrypted $6$8k/y9KnwZPr3.r88$R5vbCD4SIadfadfaeee452gfsy44
selinux --disabled
authconfig --enableshadow --passalgo=sha512 --enablefingerprint
firewall --port=68:udp --port=161:udp --port=162:udp --port=199:tcp --port=2812:tcp --service=ssh
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
#clearpart --all --drives=sda
#ignoredisk --only-use=sda
anaconda-ks.cfg 

It’s also possible to print out a complete file to the screen, this is useful if the file is only a few lines long. The cat command will print out multiple files but works just as well with a single file (it’s also really useful to cat and file and use the output in another command using the | character):

[root@vvx5 network-scripts]# cat ifcfg-eth0
# Intel Corporation 82575EB Gigabit Network Connection
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:14:4F:CA:84:CC
ONBOOT=yes
IPADDR=172.27.5.105
NETMASK=255.255.255.0
[root@vvx5 network-scripts]#

Use the history to view previously entered command (including incorrect ones!). It’s useful to use the grep tool to search for a particular command. It’s also possible to rerun a particular command using the number beside the command.

Eg; Show the last 10 commands entered:

[root@vvx6 ~]# history | tail
  718  service monit stop
  719  service monit start
  720  vi /etc/monit.conf 
  721  service monit reload
  722  monit status
  723  date
  724  exit
  725  ls
  726  ls -l | grep iso
  727  history | tail
[root@vvx6 ~]#

Eg; Show the commands that include the string “install”:

[root@vvx6 ~]# history | grep install
  115  yum install genisoimage
  127  cd installdir/
  132  cd installdir/
  133  ./emsinstall.sh 
  135  mvn clean install
  137  mvn install clean
  144  cd installdir/
  147  ./emsinstall.sh 
  173  mvn install -Pproduction
  175  mvn clean install -Pproduction   
  732  history | grep install
[root@vvx6 ~]#

Eg; Show the commands that include the string “filesystems” and rerun the command at position 713 in the command history:

[root@vvx6 monit.d]# history | grep filesystems
  673  scp filesystems.inc crond.inc net-snmp.inc sshd.inc padmin@cache
  674  scp filesystems.inc crond.inc net-snmp.inc sshd.inc padmin@cache:/tftpboot/
  713  cat filesystems.inc 
  739  history | grep filesystems
  742  history | grep filesystems
[root@vvx6 monit.d]# !713
cat filesystems.inc 
check filesystem rootfs with path /
if space usage > 75% for 5 times within 10 cycles then alert
group filesystems

check filesystem backupfs with path /backups
if space usage > 75% for 5 times within 10 cycles then alert
group filesystems

check filesystem logfs with path /var/log
if space usage > 75% for 5 times within 10 cycles then alert
group filesystems
[root@vvx6 monit.d]#

Use the watch command to rerun a command periodically (the default is every 2 seconds):

Eg; Get the size of the Fedora-14.iso file every 2s:

[root@vvx6 ~]# watch du -h Fedora-14.iso

Every 2.0s: du -h Fedora-14.iso                                                                 Mon Nov 22 16:09:05 2010

1.2G    Fedora-14.iso

Eg; As above only changing the

Cancel a running command using “Ctl+c”.

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso
^C
[root@vvx6 ~]#

Put a job in the background using the & character (the job PID number is shown along with the background job number)

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[1] 5240
[root@vvx6 ~]#

Alternatively you can pause the current job using “Ctl+z” and then put in the background using the bg command (Note if a command finishes in the background, it will automatically report that is the case when the next command is entered):

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso
^Z
[1]+  Stopped                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
[root@vvx6 ~]# bg
[1]+ cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[root@vvx6 ~]#

You can also list running background jobs using the jobs command:

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[2] 5407
[root@vvx6 ~]# jobs
[1]+  Stopped                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
[2]-  Running                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[root@vvx6 ~]#

You can also bring a background job back into the foreground by running the fg command (this is useful if you want to stop a background job). Note that the fg command also can take a job number:

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[1] 5363
[root@vvx6 ~]# fg
cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
^C
[root@vvx6 ~]#

[root@vvx6 ~]# cp Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[2] 5407
[root@vvx6 ~]# jobs
[1]+  Stopped                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
[2]-  Running                 cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso &
[root@vvx6 ~]# fg 1
cp -i Fedora-14-x86_64-DVD.iso Fedora-14.iso
^C
[root@vvx6 ~]#

The package vi is used to modify text files. It has 2 main modes of operation, insert mode (in which normal changes can be made) and command mode (where more complex searches and edits can be achieved). It starts in command mode by default, to move to insert mode press i. To leave insert mode and enter command mode press ESC. Open a file using vi:

[root@vvx6 ~]# vi test.file

To enter or modify text press i and enter insert mode.

testing one two
~                                                                                                                       
~                                                                                                                       
~ 
-- INSERT --

To save the file, move to command mode using ESC then type : followed by a w and then a q (this is write and quit) and press the enter key:

testing one two
~                                                                                                                       
~                                                                                                                       
:wq

To close a file without saving the changes enter the command :q!

testing one two
~                                                                                                                       
~                                                                                                                       
:q!

To search within a text file in vi using the / character.

testing one two                                                                                                               
~                                                                                                                
~                                                                                                                
/one

Useful keys to use in vi are (all are input in command mode but without the : character, case is also important):

'I' - insert at the start of the line in insert mode
'A' - append at the end of line in insert mode
'Ctl+f' - page forward
'Ctl+b' - page backwards
'w' - move forwards a word
'b' - move backwards a word
'cw' - change the word under the cursor
'dw' - delete the word under the cursor
'dd' - delete the line under the cursor
'.' - repeat the last text altering insert
'yy' - copy the line under the cursor to the clipboard
'p' - paste the contents of the clipboard on the line below the cursor
'P' - paste the contents of the clipboard on the line above the cursor
'G' - go to the last line in the file
'1G' - go to the first line (number in front of command repeats the command)
'10G' - go to line number 10
'/walrus' - search forwards for the word 'walrus'
'?walrus' - search backwards for the word 'walrus'
'n' - go to next instance of search text
'N' - go back to the last instance of search text

Install a new software package:

[root@vvx5 ~]# yum install yum-versionlock 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirrors.dedipower.com
 * base: mirrors.dedipower.com
 * extras: mirrors.dedipower.com
 * rpmforge: apt.sw.be
 * updates: mirrors.dedipower.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package yum-versionlock.noarch 0:1.1.16-14.el5.centos.1 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================
 Package                        Arch                  Version                                 Repository           Size
========================================================================================================================
Installing:
 yum-versionlock                noarch                1.1.16-14.el5.centos.1                  base                 15 k

Transaction Summary
========================================================================================================================
Install      1 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)         

Total download size: 15 k
Is this ok [y/N]: y
Downloading Packages:
yum-versionlock-1.1.16-14.el5.centos.1.noarch.rpm                                                |  15 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : yum-versionlock                                                                                  1/1 

Installed:
  yum-versionlock.noarch 0:1.1.16-14.el5.centos.1                                                                       

Complete!
[root@vvx5 ~]# 

Search for the name of a new software package:

[root@vvx5 ~]# yum search yum-versionlock
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirrors.dedipower.com
 * base: mirrors.dedipower.com
 * extras: mirrors.dedipower.com
 * rpmforge: apt.sw.be
 * updates: mirrors.dedipower.com
=============================================== Matched: yum-versionlock ===============================================
yum-versionlock.noarch : Yum plugin to lock specified packages from being updated
[root@vvx5 ~]#

Better not forget how to reboot the server:

[root@vvx5 ~]# reboot

Or how to power the server off completely:

[root@vvx5 ~]# poweroff
Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: