COMMAND LINE UTILITIES

When Linus Torvald introduced Linux and For a long time thereafter, Linux did not have a graphical user interface: It run on character-based terminals only. All the tools ran from a command line.Today the Linux GUI is important, but many people-especially system Administrators-run many command line programs.

SPECIAL CHARACTERS

  •    -Escape character
  • /    -Directory separator
  • .    -Current director, Also use to “hide” files.
  • ..   -Parent directory
  • ~  -User’s Home directory
  • *  -Represent 0 or more characters in a filename or by itself all files in a directory

Example:

[root@www ~]# ls
anaconda-ks.cfg Documents install.log Music Public Videos
Desktop Downloads install.log.syslog Pictures Templates

[root@www ~]# ls inst*
install.log install.log.syslog
[root@www www]# ls
hello1.txt hello2.txt hello.txt hellow.txr

[root@www www]# ls hello*
hello1.txt hello2.txt hello.txt hellow.txr

  • ? -Represent a single character in a filename

Example:

[root@www www]# ls
hello1.txt hello2.txt hello.txt hellow.txr

[root@www www]# ls hello?txt
hello.txt

[root@www www]# ls hello1?txt
hello1.txt

[root@www www]# ls hello??txt
hello1.txt hello2.txt

  • [ ] -Can be used to represent a range of values i.e. [0-9], [A-Z] etc.

Example:

[root@www www]# ls
hello1.txt hello2.txt hello.txt hellow.txr

[root@www www]# ls hello[0-4].txt
hello1.txt hello2.txt

  • | -“Pipe”, redirect the output of one command into another command.

Example:

[root@www ~]# ls | more
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
Music
Pictures
Public
Templates
Videos
www

[root@www ~]# ls | less
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
Music
Pictures
Public
Templates
Videos
www
(END) –(press q to quit)

BASIC UTILITIES

One of the important advantages of Linux is that it comes with thousands of utlities that perform major functions.You will use Linux, whether you use them directly by name from the command line or indirectly from amenu or icon.

The term ‘directory’ is used extensively in the next sections. A directory is a resource that can hold fils. On other operating system, including Windows, Machintosh, a directory is referred to a folder. So directory is a folder that can hold files.

Note: When you log in on the system, you are currently in your home directory. In this chapter that is only directory you use: All the files you create in this chapter are in your home directory.

 ALL COMMANDS OR UTILITIES ARE CASE-SENSITIVE.

ls: LISTS THE NAMES OF FILES

[root@www ~]# ls

anaconda-ks.cfg Documents install.log
Desktop Downloads install.log.syslog

[root@www ~]# ls -l
total 96
-rw——-. 1 root root 1927 Dec 5 00:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jan 22 09:04 Desktop
drwxr-xr-x. 2 root root 4096 Dec 6 06:06 Documents
drwxr-xr-x. 4 root root 4096 Jan 19 13:31 Downloads
-rw-r–r–. 1 root root 43379 Dec 5 00:43 install.log
-rw-r–r–. 1 root root 11159 Dec 5 00:42 install.log.syslog
drwxr-xr-x. 2 root root 4096 Dec 5 00:47 Music
drwxr-xr-x. 2 root root 4096 Jan 12 16:12 Pictures
drwxr-xr-x. 2 root root 4096 Dec 5 00:47 Public
drwxr-xr-x. 2 root root 4096 Dec 5 00:47 Templates
drwxr-xr-x. 3 root root 4096 Dec 29 09:27 Videos

ls -a: LISTS THE NAMES OF HIDDEN FILES

[root@www ~]# ls -a
. Documents .icons Public
.. Downloads install.log .pulse
.adobe .elinks install.log.syslog .pulse-cookie
anaconda-ks.cfg .esd_auth .kde .recently-used.xbel
.bash_history .gconf .local .rnd
.bash_logout .gconfd .macromedia .tcshrc
.bash_profile .gimp-2.6 .mousetweaks.pid Templates
.bashrc .gnome2 .mozilla .themes
.cache .gnome2_private Music .thumbnails
.config .gstreamer-0.10 .mysql_history Videos
.cshrc .gtk-bookmarks .nautilus .viminfo
.dbus .gvfs Pictures .vmware
Desktop .ICEauthority .pki

touch: EMPTY FILE

[root@www www]# touch f1

[root@www www]# touch f2 f3

[root@www www]# ls
f1 f2 f3

cat: CATENATE – DISPLAY A TEXT FILE

The cat utlity displays the contents of a text file.The name of the command is drived from catenate, which means to join together, one after the other.

Some user’s also use cat command to make a new file as shown below :

[root@www ~]# cat > myfile
This is the contents of file.
filename is myfile.Press ctrl+d to save the file after press Enter.

[root@www ~]# cat myfile
This is the contents of file.
filename is myfile.Press ctrl+d to save the file after press Enter.

command line utilities

 rm: DELETES A FILE

The rm (remove) utility deletes a file.

Example :

[root@www www]# ls
hello1.txt hello2.txt hello.txt hellow.txr
[root@www www]# rm hello1.txt

[root@www www]# ls
hello2.txt hello.txt hellow.txr
[root@www www]# rm -i hello.txt
rm: remove regular empty file `hello.txt’? y

i stand for interactive form of rm to make sure that you delete only the file you intend to delete.

[root@www www]# ls
hello2.txt hellow.txr

[root@www www]# rm -rf hello2.txt

rf stand for recursively and forcefully.

[root@www www]# ls
hellow.txr

PAGERS

less ls more: DISPLAYING A TEXT FILE ONE SCREEN AT A TIME

When you want to view a file that is longer than one screen, you can use either the less utility or the more utility.
Because these utilities show one page at a time, they are called pagers.

Although they are very similar, they have subtle differences.

Different between less and more utility :

At the End of the file less displays and EOF (END OF FILE) message and waits for you to press q.

Example:

[root@www ~]# cd /etc

[root@www etc]# ls | less

abrt
acpi
adjtime
akonadi
aliases
cgconfig.conf
cgrules.conf
chkconfig.d
cluster
ConsoleKit
corosync
cron.d
: ————— press spacebar to goto to the next pager.

cron.daily
cron.deny
cron.hourly
DIR_COLORS
dnsmasq.d
dovecot
dracut.conf
dracut.conf.d
festival
filesystems
fonts
fprintd.conf
fstab
: ————— press spacebar to goto to the next pager.

gai.conf
gconf
gcrypt
gdm
inittab
inputrc
ipa
iproute2
issue
: ————— press spacebar to goto to the next pager.
issue.net
java
jvm
jvm-commmon
kde
lvm
lynx.cfg
lynx.lss
lynx-site.cfg
mail
mailcap
mail.rc
makedev.d
: ————— press spacebar to goto to the next pager.

ssh
ssl
sssd
statetab
statetab.d
sudoers
vmware-vix
vsftpd
warnquota.conf
wgetrc
wpa_supplicant
wvdial.conf
X11
xdg
xinetd.d
xml
yp.conf
yum
yum.conf
yum.repos.d
(END) —————- press q to quit.
[root@www ~]# cd /etc

[root@www etc]# ls | more

abrt
acpi
adjtime
akonadi
aliases
aliases.db
alsa
alternatives
anacrontab
asound.conf
at.deny
audisp
audit
autofs_ldap_auth.conf
auto.master
auto.misc
auto.net
auto.smb
avahi
bash_completion.d
bashrc
blkid
bluetooth
bonobo-activation
cas.conf
certmonger
cgconfig.conf
cgrules.conf
chkconfig.d
cluster
ConsoleKit
corosync
cron.d
–More– —- Press spacebar to goto to the next pager or End of file.
hostname: DISPLAY THE SYSTEM NAME

The hostname command displays the name of the system you are working on.
[root@www ~]# hostname
http://www.mc712.com
Temporary Changing the Hostname:

[root@www ~]# hostname Desktop.example.com

[root@www ~]# hostname
Desktop.example.com

WORKING WITH FILES:

cp: COPIES A FILE

The cp(copy) utlity makes a copy of a file.The utility can copy any file, including text and excecutable program (binar) files. You can use cp to make a backup copy of a file or a copy to experiment with.

Syntax: cp source-file destination-file

Example

[root@Desktop www]# ls
hellow.txr

[root@Desktop www]# cp hellow.txr hellow.txr
cp: `hellow.txr’ and `hellow.txr’ are the same file

[root@Desktop www]# cp hellow.txr myfile1

[root@Desktop www]# ls
hellow.txr myfile1

[root@Desktop www]# cp hellow.txr /root/Desktop/

[root@Desktop www]# cd /root/Desktop/

[root@Desktop Desktop]# ls
hellow.txr

[root@Desktop www]# ls
hellow.txr myfile1

[root@Desktop www]# cp -i hellow.txr myfile1
cp: overwrite `myfile1’? y

mv: CHANGES THE NAME OF A FILE

The mv(move) utlity can rename a file without making a copy of it.

Syntax: mv existing-filename new-filename

Example:

[root@Desktop www]# ls
hellow.txr myfile1

[root@Desktop www]# mv myfile1 file

[root@Desktop www]# ls
file hellow.txr

lpr: PRINTS A FILE

The lpr (line printer) utility places one or more files in a print queue for printing. Linux provies print queues so that only one job is printed on a given printer at a time. A queue allows several people or jobs to send output simultaneously to a single printer with the expected results. On machines with access to more than one printer, you can use the -P option to instruct lpr to place the file in the queue for specific printer, including one that is connected to another machine on the network.

The following command prints the file named ‘hello’. The below command does not specify the printer, the output goes to the default printer, whichis the printer when you have only one printer.

[root@www www]# lpr hello

The next command line prints the same file on the printer named ‘laser’:

[root@www www]# lpr -Plaser hello

You can see what jobs are in the print queue by using the lpq utility:

[root@www www]# lpq
lp is ready and printing
Rank Owner Job Files Total Size
active root 2(standard input) 950111 bytes

In this example, root has one job that is being printed; no other jobs are in the queue.You can use the Job number (2 in this case) with the ‘lprm utility to remove the job from the print queue and stop it from printing.

[root@www www]# lprm 2

You can send more than one file to the printer with a single command. The following command line prints three files on the printer named laser :

[root@www www]# ls
hello hellow.txr

[root@www www]# lpr -Plaser hello hello
hello hellow.txr
grep: GLOBAL REGULAR EXPRESSION PRINT – FINDS A STRING

The grep (GLOBAL REGULAR EXPRESSION PRINT) utility searches throgh one or more files to see whether any contain a specified string of characters. It does not change the file it searches but simply displays each line that contains the string.

[root@www www]# cat > introduction

My name is Manish Chauhan.
RHEL stand for Redhat Enterprise linux.
Latest version of redhat is 6.0
Linux is open source operating system
(ctrl+d)

[root@www www]# cat introduction

My name is Manish Chauhan.
RHEL stand for Redhat Enterprise Linux.
Latest version of redhat is 6.0
Linux is open source operating system

[root@www www]# grep ‘Linux’ introduction

RHEL stand for Redhat Enterprise Linux.
Linux is open source operating system
head: DISPLAYS THE BEGINNING OF A FILE

By default the head utility displays the first 10 lines of a file. You can use head to help you remember what a particular file contains.

[root@www www]# cat months
jan
feb
march
april
may
june
july
aug
sept
oct
nov
dec
[root@www www]# head months
jan
feb
march
april
may
june
july
aug
sept
oct
[root@www www]# head -3 months
jan
feb
march

tail: DISPLAYS THE END OF A FILE

The tail utility is similar to head but by default displays the last 10 lines of a file.

[root@www www]# tail months
march
april
may
june
july
aug
sept
oct
nov
dec

[root@www www]# tail -2 months
nov
dec

sort: Displays A FILE IN ORDER

The sort utility displays the contents of a file in order by lines but does not change the original file.

[root@www www]# sort months

april
aug
dec
feb
jan
july
june
march
may
nov
oct
sept

uniq: REMOVES DUPLICATE LINES FROM A FILE

The uniq(unique) utility displays a file, skipping adjacent duplicate lines, but does not change the original file. If a file contains a list of names and has two successive enteries for the same person, uniq skips the extra line.

[root@www www]# cat linux
server
server
yum
yum
dns
yum
server
dns
dns

[root@www www]# uniq linux
server
yum
dns
yum
server
dns
[root@www www]# sort -n linux
dns
dns
dns
server
server
server
yum
yum
yum

[root@www www]# sort -u linux
dns
dns
server
yum
yum

(Remove duplicate lines and sort the contents of file name ‘linux’)
diff: COMPARES TWO FILES

The diff (difference) utility compares two files and displays a list of the differences between them.This utility does not change either file, so it is useful when you want to compare two versions of a letter or a report or two versions of the source code for a program.

The diff utility with the -u (unified output format) option first displays two lines indicating which of the files you are comparing will be denoted by a plus(+) and which by a minus sign(-).A minus sign indicates the ‘file1′ ; a plus sign indicates the ‘file2′.

The diff -u command breaks long, multiline text into Hunks.
Each hunk is preceded by a line starting and ending with two at signs (@@).
The hunk identifier indicates the starting line number and the number of lines from each file for this hunk.

[root@www www]# cat file1
Hello
How

[root@www www]# cat file2
Hello
are
[root@www www]# diff -u file1 file2
— file1 2013-01-22 19:38:15.043744843 +0530
+++ file2 2013-01-22 19:38:32.550744879 +0530
@@ -1,2 +1,2 @@ (-1 is file1 , +1 is file2 and 2 words are present)
Hello (no sign means Hello is common word in both files)
-How (minus sign indicates the ‘file1′)

+are (plus sign indicates the ‘file2′.
file: TESTS THE CONTENTS OF A FILE

You can use the ‘file’ utility to learn about the contents of any file on a linux filesystem without having to open and examine the file yourself.

[root@www Desktop]# file image.png
image.png: PNG image data, 673 x 378, 8-bit/color RGB, non-interlaced

[root@www Desktop]# file hellow.txr
hellow.txr: empty

[root@www Desktop]# cat > hello
abc

[root@www Desktop]# file hello
hello: ASCII text

|(PIPE): COMMUNICATES BETWEEN PROCESSES

A process is the excecution of a command by Linux. Communication between processes is one of the hallmarks of UNIX/Linux.
A pipe (written as vertical bar, |, on the command line and appearing as a solid or broken vertical line on keyboards)provides the simplest form of this kind of communication.

[root@www www]# cat months | head -4
jan
feb
march
april
echo: DISPLAYS TEXT

The echo utility copies anything you put on the command line, after echo,to the screen

[root@www www]# ls
f1 f2 f3

[root@www www]# echo “HELLO WORLD”
HELLO WORLD

[root@www www]# echo ‘HELLO WORLD’
HELLO WORLD

[root@www www]# echo mc712 *
mc712 f1 f2 f3
DIFFERENCE BETWEEN “cat echo” and “echo cat” UTILITY :

[root@www www]# cat > echo
With the help of cat we create a new file echo.

[root@www www]# cat echo
With the help of cat we create a new file echo.

{ cat echo utility display the contents of file having name echo. }

[root@www www]# echo cat
cat

date: DISPLAYS THE TIME AND DATE

[root@www www]# cal
January 2013
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

[root@www www]# cal 2014

[root@www www]# date

Tue Jan 22 21:11:11 IST 2013

day month date Time YEAR
[root@www ~]# date –help

  • %A locale’s full weekday name (e.g., Sunday)
  • %B locale’s full month name (e.g., January)
  • %C century; like %Y, except omit last two digits (e.g., 20)
  • %D date; same as %m/%d/%y
  • %a locale’s abbreviated weekday name (e.g., Sun)
  • %b locale’s abbreviated month name (e.g., Jan)
  • %c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
  • %d day of month (e.g, 01)
  • %e day of month, space padded; same as %_d
  • %F full date; same as %Y-%m-%d
  • %g last two digits of year of ISO week number (see %G)
  • %G year of ISO week number (see %V); normally useful only with %V
  • %h same as %b
  • %H hour (00..23)
  • %I hour (01..12)
  • %j day of year (001..366)
  • %k hour ( 0..23)
  • %l hour ( 1..12)
  • %m month (01..12)
  • %M minute (00..59)
  • %n a newline
  • %N nanoseconds (000000000..999999999)
  • %p locale’s equivalent of either AM or PM; blank if not known
  • %P like %p, but lower case
  • %r locale’s 12-hour clock time (e.g., 11:11:04 PM)
  • %R 24-hour hour and minute; same as %H:%M
  • %s seconds since 1970-01-01 00:00:00 UTC
  • %S second (00..60)
  • %t a tab
  • %T time; same as %H:%M:%S
  • %u day of week (1..7); 1 is Monday
  • %U week number of year, with Sunday as first day of week (00..53)
  • %V ISO week number, with Monday as first day of week (01..53)
  • %w day of week (0..6); 0 is Sunday
  • %W week number of year, with Monday as first day of week (00..53)
  • %x locale’s date representation (e.g., 12/31/99)
  • %X locale’s time representation (e.g., 23:13:48)
  • %y last two digits of year (00..99)
  • %Y year
  • %z +hhmm numeric timezone (e.g., -0400)
  • %:z +hh:mm numeric timezone (e.g., -04:00)
  • %::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
  • %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
  • %Z alphabetic time zone abbreviation (e.g., EDT)

[root@www ~]# date +”%A %B %C %D”
Tuesday January 20 01/22/13

[root@www ~]# date +”%a %b %c %d”
Tue Jan Tue 22 Jan 2013 09:26:40 PM IST 22

script: Records a Linux Session

The script utility all or part of a login session, including your input and the system’s responses. This utility is useful only from character-based devices, such as a termnal or a terminal emulator. It does capture a session with vim; however because vim uses control characters to position the cursor and display different type-faces, such as bold, the output will be difficult to read and may not be useful.

[root@www ~]# script
Script started, file is typescript

[root@www ~]# echo mc712
mc712

[root@www ~]# cat > newfile
contents of file

[root@www ~]# cat newfile
contents of file

[root@www ~]# exit
exit
Script done, file is typescript

[root@www ~]# ls
typescript
[root@www ~]# cat typescript

Script started on Tue 22 Jan 2013 09:35:00 PM IST
[root@www ~]# echo mc712
mc712
[root@www ~]# cat > newfile
contents of file
[root@www ~]# cat newfile
contents of file
[root@www ~]# exit
exit

Script done on Tue 22 Jan 2013 09:36:01 PM IST

command line utilities1

unix2dos: CONVERTS LINUX FILES TO WINDOWS FORMAT

If you want to share a text file that you created on a Linux system with someone on a Window system, you need to convert the file before the person on the Windows system can read it easily.The unix2dos utility converts a Linux text file so that it can be read on a Windows system.

[root@www www]# unix2dos hello
unix2dos: converting file hello to DOS format …
dos2unix: CONVERTS WINDOWS FORMAT TO LINUX FILES

[root@www www]# dos2unix hello
dos2unix: converting file hello to UNIX format …

COMPRESSING AND ARCHIVING FILES

To reduce the amount of disk space you use without removing the file entirely, you can compress the file without losing any information it holds.Also you may frequently download compress files from the internet.The utilities described in this section is compress and decompress files.

bzip2: COMPRESSES A FILE

[root@www www]# ls
echo f2 file1 hello introduction months
f1 f3 file2 hellow.txr linux nths

[root@www www]# bzip2 hello

or

[root@www www]# bzip2 -v months
months: 0.648:1, 12.351 bits/byte, -54.39% saved, 57 in, 88 out.

[root@www www]# ls
echo f2 file1 hello.bz2 introduction months.bz2
f1 f3 file2 hellow.txr linux nths
[root@www www]# ls
echo f2 file1 hello

[root@www www]# bzip2 *

[root@www www]# ls
echo.bz2 f2.bz2 file1.bz2 hello.bz2

(.bz2 is filename extension after compressing the file)
bunzip2 and bzcat: DECOMPRESS A FILE

[root@www www]# ls
hello.bz2 months.bz2

[root@www www]# bunzip2 hello.bz2

[root@www www]# ls
hello

[root@www www]# bzcat months.bz2
jan
feb
march
april
may
june
july
aug
sept
oct
nov
dec

[root@www www]# ls
hello months.bz2

After bzcat is run, the contents of months.bz2 is unchanged; the file is still stored on the disk in compressed form

bzip2recover : The bzip2recover utility supports limited data recovery from media errors.Give the command bzip2recover followed by the name from which you can want to try to recover data.

gzip: COMPRESSES A FILE

The gzip (GNU zip) utility is older and less efficient than bzip2.

[root@www www]# ls
months

[root@www www]# gzip months

[root@www www]# ls
months.gz

(.gz is filename extension after compressing the file)

[root@www www]# zcat months.gz
jan
feb
march
april
may
june
july
aug
sept
oct
nov
dec

[root@www www]# ls
months.gz
gunzip: DECOMPRESSES A FILE

[root@www www]# ls
months.gz

[root@www www]# gunzip months.gz

[root@www www]# ls
months
DIFFERENT BETWEEN gzip and zip.

Do not confuse gzip and gunzip with the zip and unzip utilities. These last two are used to pack and unpack zip archives containing several files compressed into a single file that has been imported from or is being exported to windows.The zip utility constructs a zip archive, whereas unzip unpacks zip archives. The zip and unzip utilities are compatible with PKZIP, a windows compress and archive program.

zip utility

[root@www www]# ls
echo f2 file1 hello introduction months
f1 f3 file2 hellow.txr linux
nths
[root@www www]# zip folder file1
adding: file1 (stored 0%)

[root@www www]# ls
echo f2 file1 folder.zip hellow.txr linux nths
f1 f3 file2 hello introduction months

[root@www www]# zip folder2 file1 f2 linux months
adding: file1 (stored 0%)
adding: f2 (stored 0%)
adding: linux (deflated 40%)
adding: months (deflated 4%)

[root@www www]# ls
echo f2 file1 folder2.zip hello introduction months
f1 f3 file2 folder.zip hellow.txr linux nths
unzip utility

[root@www mc712]# ls
folder.zip

[root@www mc712]# unzip folder.zip
Archive: folder.zip
extracting: file1

[root@www mc712]# ls
file1 folder.zip

tar: PACKS AND UNPACKS FILES

The tar utility performs many functions. Its name is short for TAPE ARCHIVE, as it original function was to create and read archive and backup tapes. Today it is use to create a single file (called a tar file) from multiple files or directory hierarchies and to extract files from a tar file.

PACKS

[root@www www]# ls
echo f1 f2 f3 file1 file2 hello hellow.txr introduction linux months

[root@www www]# tar -cvf backup.tar f1 f2 f3
f1
f2
f3

[root@www www]# ls
backup.tar echo f1 f2 f3 file1 file2 hello hellow.txr introduction linux months
UNPACKS

[root@www mc712]# ls
backup.tar

[root@www mc712]# tar -xvf backup.tar
f1
f2
f3

[root@www mc712]# ls
backup.tar f1 f2 f3
POINTS TO REMEMBER:

  • tar xvzf file-1.0.tar.gz – to uncompress a gzip tar file (.tgz or .tar.gz)
  • tar xvjf file-1.0.tar.bz2 – to uncompress a bzip2 tar file (.tbz or .tar.bz2)
  • tar xvf file-1.0.tar – to uncompressed tar file (.tar)
  • * x = eXtract, this indicated an extraction c = create to create )
  • * v = verbose (optional) the files with relative locations will be displayed.
  • * z = gzip-ped; j = bzip2-zipped
  • * f = from/to file … (what is next after the f is the archive file)

LOCATING COMMANDS

The whereis and apropos utilities help you find a command whose name you have forgotten or whose location you do not know.
The slocate utility searches for files on the local system.

which AND whereis: LOCATE A UTILITY

When you give Linux command, the shell searches a list of directories for a program with the name and runs the first one it finds.This list of directories is called SEARCH PATH.

The which utility locates utilities (commands) by displaying the full pathname to the file for the utility.The local system may include several commands that have the same name. You can find out which copy of the program the shell will run by using which.

[root@www ~]# which tar
/bin/tar

[root@www ~]# which ls
/bin/ls

[root@www ~]# which cat
/bin/cat

[root@www ~]# which firefox
/usr/bin/firefox

The which utility can be helpful when a command seems to be working in unexpected ways.By running which utility, you may discover that you are running a nonstandard version of a tool or a different one than you expected.

For example, if tar is not working properly and you find that you are running /usr/local/bin/tar instead of /bin/tar, you might suspect that the version is broken.

The whereis utility seraches for files related to a utility by looking in standard locations instead of using your search path.

For example, you can find the locations for files related to tar:

[root@www ~]# whereis firefox
firefox: /usr/bin/firefox /usr/share/man/man1/firefox.1.gz

[root@www ~]# whereis tar
tar: /bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz

In this example whereis finds three references to tar:

  • -the tar utility file,
  • -a tar header file,
  • -the tar man page.

Under bash you can use the type builtin to whether a command is a builtin.

[root@www ~]# type cat
cat is /bin/cat

[root@www ~]# type firefox
firefox is /usr/bin/firefox

[root@www ~]# type echo
echo is a shell builtin

DIFFERENCE BETWEEN which AND whereis:

which utility:

The which utility looks through the directories in your SEARCH PATH, in order, and locates the program.
If more thean one program with the specified name in the search path , which displays the name of only the first one(the one you would run).

whereis utility:

The whereis utility looks through a list of standard directories and works independently of your search path.
The whereis utility is use to locate a binary(excecutable) file, any manual pages, and source code for a program you specify.
The whereis utility displays all the files it finds.

apropos: SEARCHES FOR A KEYBOARD

When you do not know the name of the command you need to carry out a particular task, you can use a keyboard and the apropos utility to search for it.

The whatis database has to be set up and regularly maintained with makewhatis for apropos to work.

[root@www ~]# whatis firefox
firefox (1) – a Web browser for X11 derived from the Mozilla browser

[root@www ~]# whatis ls
ls (1) – list directory contents
ls (1p) – list directory contents

[root@www ~]# whatis cat
cat (1) – concatenate files and print on the standard output
cat (1p) – concatenate and print files

[root@www ~]# apropos firefox
firefox (1) – a Web browser for X11 derived from the Mozilla browser

[root@www ~]# apropos sort

FcFontSetSort (3) – Add to a font set
FcFontSetSortDestroy (3) – DEPRECATED destroy a font set
FcFontSort (3) – Return list of matching fonts
alphasort [scandir] (3) – scan a directory for matching entries
bsearch (3) – binary search of a sorted array
bsearch (3p) – binary search a sorted table
bunzip2 [bzip2] (1) – a block-sorting file compressor, v1.0.4
bzip2 (1) – a block-sorting file compressor, v1.0.4
comm (1) – compare two sorted files line by line
ldap_parse_sort_control (3) – Decode the information returned from a search operation that used a server-side sort control
ldap_sort_entries (3) – LDAP sorting routines (deprecated)
ldap_sort_entries [ldap_sort] (3) – LDAP sorting routines (deprecated)
ldap_sort_entries [ldap_sort_strcasecmp] (3) – LDAP sorting routines (deprecated)
ldap_sort_entries [ldap_sort_values] (3) – LDAP sorting routines (deprecated)
ldap_sort_strcasecmp (3) – LDAP sorting routines (deprecated)
ldap_sort_strcasecmp [ldap_sort] (3) – LDAP sorting routines (deprecated)
ldap_sort_strcasecmp [ldap_sort_entries] (3) – LDAP sorting routines (deprecated)
ldap_sort_strcasecmp [ldap_sort_values] (3) – LDAP sorting routines (deprecated)
ldap_sort_values (3) – LDAP sorting routines (deprecated)
ldap_sort_values [ldap_sort] (3) – LDAP sorting routines (deprecated)
ldap_sort_values [ldap_sort_entries] (3) – LDAP sorting routines (deprecated)
ldap_sort_values [ldap_sort_strcasecmp] (3) – LDAP sorting routines (deprecated)
lsort (n) – Sort the elements of a list
qsort (3) – sorts an array
qsort (3p) – sort a table of data
sort (1) – sort lines of text files
sort (1p) – sort, merge, or sequence check text files
sort (3pm) – perl pragma to control sort() behaviour
tsort (1) – perform topological sort
tsort (1p) – topological sort
versionsort [scandir] (3) – scan a directory for matching entries
[root@www ~]# apropos who

at.allow [at] (5) – determine who can submit jobs via at or batch
at.deny [at] (5) – determine who can submit jobs via at or batch
doveadm-who (1) – Show who is logged in to the Dovecot server
w (1) – Show who is logged on and what they are doing
who (1) – show who is logged on
who (1p) – display who is on the system
whoami (1) – print effective userid

locate: SEARCHES FOR A FILE

[root@www ~]# locate hello

/opt/libreoffice3.5/program/python-core-2.6.1/lib/__phello__.foo.py
/opt/libreoffice3.5/share/Scripts/beanshell/HelloWorld/helloworld.bsh
/opt/libreoffice3.5/share/Scripts/javascript/HelloWorld/helloworld.js
/usr/lib/vmware-installer/2.0/python/lib/__phello__.foo.py
/usr/lib/vmware-installer/2.0/python/lib/__phello__.foo.pyc
/usr/lib64/python2.6/__phello__.foo.py
/usr/lib64/python2.6/__phello__.foo.pyc
/usr/lib64/python2.6/__phello__.foo.pyo
/usr/share/doc/cmake-2.6.4/Example/Hello/hello.cxx
/usr/share/doc/cmake-2.6.4/Example/Hello/hello.h
/usr/share/doc/gnome-python2-bonobo-2.28.0/bonoboui/hello.py
/usr/share/doc/numpy-1.3.0/docs-f2py/hello.f
/usr/share/doc/pygtk2-2.16.0/examples/simple/hello.py
/usr/share/doc/python-genshi-0.5.1/examples/webpy/hello.html
/usr/share/doc/python-genshi-0.5.1/examples/webpy/hello.py
/usr/share/doc/python-qpid-0.7.946106/examples/api/hello
/usr/share/doc/python-qpid-0.7.946106/examples/api/hello_xml
/usr/share/doc/python-twisted-web-8.2.0/examples/hello.rpy.py
/usr/share/dstat/dstat_helloworld.py
/usr/share/dstat/dstat_helloworld.pyc
/usr/share/tk8.5/demos/hello
/var/www/html/wp-content/plugins/hello.php

(hello is a filename)

OBTAINING USER AND SYSTEM INFORMATION

who: LISTS USERS ON THE SCREEN

The who utility displays a list of users who are logged in.

[root@www ~]# who

username terminal Date and Time

root tty1 2013-01-23 08:44 (:0)
root pts/0 2013-01-23 08:52 (:0.0)
[root@www ~]# who

username terminal Date and Time

root tty2 2013-01-23 09:30
root tty1 2013-01-23 08:44 (:0)
root pts/0 2013-01-23 08:52 (:0.0)
[root@www ~]# who

username terminal Date and Time

mc712 tty3 2013-01-23 09:30
root tty2 2013-01-23 09:30
root tty1 2013-01-23 08:44 (:0)
root pts/0 2013-01-23 08:52 (:0.0)
DIFFERENCE BETWEEN whoami and who am i :
who am i: If you find which terminal you are using or what time you logged in,you can use the command whoami.

[root@www ~]# who am i
root pts/0 2013-01-23 08:52 (:0.0)

[mc712@www ~]$ who am i
root pts/0 2013-01-23 08:52 (:0.0)

whoami: Display the name of current login user.

[mc712@www ~]$ whoami
mc712

[root@www ~]# whoami
root

finger: LISTS USERS ON THE SYSTEM

On systems whereis secuity is a concern, the system administrator may disable finger. This utility can give information that can help a malcious user break into the system.

[root@www ~]# finger

Login Name Tty Idle Login Time Office Office Phone
root root tty2 18 Jan 23 09:30
root root tty1 1:12 Jan 23 08:44 (:0)
root root pts/0 Jan 23 08:52 (:0.0)

[root@www ~]# pinky
Login Name TTY Idle When Where
root root tty2 00:21 2013-01-23 09:30
root root tty1 01:15 2013-01-23 08:44 :0
root root pts/0 2013-01-23 08:52 :0.0

lchfn: CHANGING FINGER INFORMATION.
[root@www ~]# lchfn

Changing finger information for root.
Full Name [root]: Manish Chauhan
Office: Jalandhar
Office Phone: 987654321
Home Phone: 0181-2222222
Finger information changed.
[root@www ~]# finger
Login Name Tty Idle Login Time Office Office Phone
root Manish Chauhan tty2 23 Jan 23 09:30 Jalandhar 987654321
root Manish Chauhan tty1 1:17 Jan 23 08:44 (:0)
root Manish Chauhan pts/0 Jan 23 08:52 (:0.0)

[root@www ~]# finger mc712
Login: mc712 Name: mc712
Directory: /home/mc712 Shell: /bin/bash
Last login Wed Jan 23 09:30 (IST) on tty3
Mail last read Tue Dec 18 11:24 2012 (IST)
No Plan.
w: LIST USERS ON THE SYSTEM

The w utility displays the list of uses who are logged in.
[root@www ~]# w

10:02:41 up 1:20, 3 users, load average: 0.03, 0.05, 0.11

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty2 – 09:30 26:04 0.01s 0.01s -bash
root tty1 :0 08:44 1:20m 1:17 1:17 /usr/bin/Xorg :0 -nr -verbose -auth
root pts/0 :0.0 08:52 0.00s 0.29s 0.11s w

The first line displays the time of the day, the period of time the computer has been running(in days, hours, and minutes), the number of users logged in, and the load average(how busy the system is).
The three load average numbers represent the number of jobs waiting to run, averaged over the past minutes.

Use the uptime utility to display just this line

[root@www ~]# uptime
10:08:20 up 1:26, 3 users, load average: 0.00, 0.02, 0.07

COMPARISON OF W, WHO, AND FINGER

Information Displayed                                               w  who  finger

  • 1. User login name                                                      X   X    X
  • 2. Terminal-line identification(tty)                                X   X    X
  • 3. Login day and time                                                  X         X
  • 4. Login Date and Time                                                    X
  • 5. Idls Time                                                                X         X
  • 6. What Program the user is excecuting                          X
  • 7. Where the user logged in from                                             X
  • 8. CPU time used X
  • 9. Full name (or other information from /etc/passwd)               X
  • 10.User-supplied vanity information                                         X
  • 11.System uptime and load average                               X

COMMUNICATION WITH OTHER USERS

write: SENDS A MESSAGE

The write utility sends a message to another user who is logged in.
When you and another user use write command to send messages to each other, you establish two-way communication.

[root@www ~]# write mc712
Hello mc712. How are you ? o

press (ctrl+d), o means over
Goto user terminal

[mc712@www ~]$
Message from root@www.mc712.com on pts/0 at 10:42 …
Hello mc712. How are you ? o
EOF

(EOF-END OF FILE)

mesg: DENIES OR ACCEPT MESSAGES

Give the following command when you do not wish to receive messages from another user:

[root@www ~]# mesg n

[root@www ~]# write mc712
write: you have write permission turned off.
Goto user terminal

[mc712@www ~]$ write mc712
write: root has message disable
You can allow messages again by entring mesg y.

[root@www ~]# mesg y

Hence mesg permits or denies messages sent by write.

wc: WORD COUNT

This command is use to count character, words and lines from the file.
[root@www www]# cat file1
Hello
How are you ?
I am fine

[root@www www]# wc file1

lines words characters
3 8 32 file1
tr: Translate or Transliterate

The tr utility copies the given input to produced the output with substitution or deletion of selected characters.
It takes as parameters two sets of characters, and replaces occurrences of the characters in the first set with the corresponding elements from the other set i.e. it is used to translate characters.

[root@www www]# tr ‘a-z’ ‘A-Z’
manish chauhan – input
MANISH CHAUHAN – output
mount: This command is use to mount the usb, cd or any other device.

[root@www ~]# mount /dev/cdrom /root/Desktop/mc712/

umount: This command is use to unmout the cd, Dvd or any other device.
[root@www ~]# umount/dev/cdrom