Monday, September 1, 2008

Linux Commands - Basics

Linux has comparatively many commands than any other operating system in the world. It is difficult to remember all the commands. To overcome this linux has some of the easy ways to identify the commands. When you are in the linux shell prompt, just by typing starting few of the alphabets you can get the full command in the prompt. For this you need to use the key. If many commands starting by that alphabets exists, linux will list all the commands starting with alphabets given.

Example1:
$what
$whatis

Example2:
$a
Display all 127 possibilities? (y or n)
a2p aleph ar atq
a2ping alias arch atrm
a2ps allcm arecord atrun
ab allec arecordmidi attr
ac allneeded ark audiofile-config
accept alsacard arp audit2allow
....
....

$a

Example3:
$wh
whatis which whiptail whoami
whereis while who whois
$wh

Sometime you may not know the command usage. Every command has its own help page and manual.

To get the command help

$--help

For example to get the 'ls' command help page.

$ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print octal escapes for nongraphic characters
--block-size=SIZE use SIZE-byte blocks
....
....

To get the command manual.


$man ls #This command will get you to the manual page of the 'ls'

LS(1) User Commands LS(1)

NAME
ls - list directory contents

SYNOPSIS
ls [OPTION]... [FILE]...

DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuSUX nor --sort.

Mandatory arguments to long options are mandatory for short options
too.
....

....

:q

To come out of the manual page we have to use ':q', just like vi editor command.

If you want to know what for that command is used in a single line. You can use this command

$whatis ls
ls (1) - list directory contents
ls (1p) - list directory contents

$whatis grep
grep (1) - print lines matching a pattern
grep (1p) - search a file for a pattern
grep (rpm) - The GNU versions of grep pattern matching utilities.
If you want to know where does that command exists. I mean the directory under this command lies and its orgin. You can use the following command.

To find out the directory and the origin of the command.

$whereis ls
ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz

$whereis apachectl
apachectl: /usr/sbin/apachectl /opt/CollabNet_Subversion/bin/apachectl /usr/share/man/man8/apachectl.8.gz

If you want all the relevant files (binary,manuals etc) pertaining to the command. Use the following command

$locate ls #This command is works only redhat based linux OS
/bin/alsacard
/bin/alsaunmute
/bin/false
/bin/ls
/boot/grub/menu.lst
/etc/alsa
/etc/lsb-release.d
/etc/mtools.conf
/etc/protocols
/etc/redhat-lsb
/etc/shells
/etc/alsa/ainit.conf
/etc/alsa/alsa.conf
/etc/alsa/cards
/etc/alsa/pcm
/etc/alsa/sndo-mixer.alisp
/etc/alsa/cards/AACI.conf
....
....

whenever you install new tool or package or utility. For that the above command will not work. For example if apache is newly installed in linux. The following command will not return anything.

$locate apachectl

Under this circumstances, you have to use the command updatedb. This command can be executed only by the superuser. This command updates the indexes in the db where the all the commmand directory details stored. After this you will be able to get where exactly new installed binaries lies.

$su #acuquires super user
#updatedb
^d #logoff
$locate apachectl
/etc/opt/CollabNet_Subversion/default-site/htdocs/manual/programs/apachectl.html
/etc/opt/CollabNet_Subversion/default-site/htdocs/manual/programs/apachectl.html.en
/etc/opt/CollabNet_Subversion/default-site/htdocs/manual/programs/apachectl.html.ko.euc-kr
/home/anand/httpd-2.2.6/docs/man/apachectl.8
/home/anand/httpd-2.2.6/docs/manual/programs/apachectl.html
/home/anand/httpd-2.2.6/docs/manual/programs/apachectl.html.en
/home/anand/httpd-2.2.6/docs/manual/programs/apachectl.html.ko.euc-kr
/home/anand/httpd-2.2.6/support/apachectl
/home/anand/httpd-2.2.6/support/apachectl.in
...

To get the all the aliases and the directories where and all the executable exist, the following command can be used.

$which ls
alias ls='ls --color=tty'
/bin/ls

--
Anand

No comments: