The pulp of technology


Where my bash command history is stored

November 21, 2008 By: Neo Category: Bash

The bash shell stores all your command history in a file of your home directory. Typically it will be stored in ~/.bash_history. This is a global history of all bash terminals which are open. The bash shell appends all commands executed in a terminal just before closing the terminal.

The history command can be used to view the active history at bash shell prompt.

[neo@techpulp ~]# history
1  ls
2  vi test.c
3  gcc test.c -o test
4  ./test
5 pwd
6  history
[neo@techpulp ~]#

The number followed by NOT symbol (!) can be used to repeat a specific command. The following example shows how to repeat the command number 5 which is pwd.

[neo@techpulp ~]# !5
pwd
/home/neo
[neo@techpulp ~]#

Similarly the NOT symbol (!) followed by any string can be used to execute the recent command in history that starts with the given string. The given string can be partial and doesn’t have to be complete name of the command.

[neo@techpulp ~]# !pw
pwd
/home/neo
[neo@techpulp ~]#

At the bash shell prompt, one can use UP and DOWN arrow keys to traverse through the history of commands.

The command “history -c” can be used to clear the bash history.

[neo@techpulp ~]# history -c
[neo@techpulp ~]# history
[neo@techpulp ~]#

If you don’t want bash to add your command history after you logout, “unset HISTFILE” can be executed.

[neo@techpulp ~]# echo $HISTFILE
/home/neo/.bash_history
[neo@techpulp ~]# unset HISTFILE
[neo@techpulp ~]#

How to resolve “Device busy” error while unmounting a CD/DVD/Pen Drive

November 18, 2008 By: Neo Category: Unix/Linux

It is very annoying when Linux doesn’t allow you to unmount a CD or a USB flash disk saying “Device is busy”. In such cases, Linux doesn’t let you take out the CD by ejecting the drive. This typically happens when files in sub-directories or the directory in which the CD or the flash drive is mounted. For example, if your CD is mounted in “/mnt/cdrom” directory and you have some active applications that have opened files present in /mnt/cdrom or in any of its sub directories. Linux will not allow you to unmount the drive unless all those applications are closed. If you are not sure about the mount point of your device, run the ‘mount‘ command at the terminal.

The following can be some of the reasons.

  • You have opened a shell with one of the directories of mount point as current directory. In such case, you can use ‘pwd‘ command at shell to find out if current directory is falling under the mount point (/mnt/cdrom in this example). If so, just move out of mount point.
  • You have application like editor or konqueror using the files under the mount point. However this is sometimes not easy to identify.as application may have files open under mount point in the background. It depends on the implementation of the application and can not be generalized.

In any case, if you are unsure which application is currently using the files under mount point, you can use fuser command to find the list of processes.

The following example identifies all the processes which are attached to the directory /mnt/cdrom ot any of its sub directories. Here I have a bash terminal session which has /mnt/cdrom as current directory.

[root@techpulp ~]# umount /mnt/cdrom
umount: /mnt/cdrom: device is busy
umount: /mnt/cdrom: device is busy
[root@techpulp ~]# fuser -f /mnt/cdrom
/mnt/cdrom:                   3278c
[root@techpulp ~]# ps -e | grep 3278
3278 pts/5    00:00:00 bash
[root@techpulp ~]# kill -9 3278
[root@techpulp ~]# umount /mnt/cdrom
[root@techpulp ~]#

How to change my Bash shell prompt

November 16, 2008 By: Neo Category: Bash

The environment variable “PS1” determines how the shell prompt should appear in Bash. You can find the current setting of PS1 as shown below.

[neo@techpulp ~]# echo $PS1
[\u@\h \W]\$
[neo@techpulp ~]#

Bash supports some pre-defined automatic variable substitutions in the prompt string PS1. For example in the above PS1, bash will automatically substitute current user name ($USER) in the place of “\u”. Similarly host name for “\h” and base name of current directory name for “\W”. In the above example tilde (~) means I am currently in my home directory which is “/home/neo“.

Let us try few examples now.

Like Windows Shell

[neo@techpulp ~]# export PS1="\w> "
~> cd /usr/share/doc
/usr/share/doc>

Date in the prompt

/usr/share/doc> export PS1="(\d) [\W]$ "
(Sun Nov 16) [doc]$

Time in the prompt

(Sun Nov 16) [doc]$ export PS1="[\@ \W]# "
[03:32 AM doc]#

Here is the list automatic substitutions supported by Bash.

              \a     an ASCII bell character (07)
              \d     the date in "Weekday Month Date" format (e.g., "Tue May 26")
              \D{format}
                     the format is passed to strftime(3) and the result is inserted into the prompt string;  an
                     empty format results in a locale-specific time representation.  The braces are required
              \e     an ASCII escape character (033)
              \h     the hostname up to the first ‘.’
              \H     the hostname
              \j     the number of jobs currently managed by the shell
              \l     the basename of the shell’s terminal device name
              \n     newline
              \r     carriage return
              \s     the name of the shell, the basename of $0 (the portion following the final slash)
              \t     the current time in 24-hour HH:MM:SS format
              \T     the current time in 12-hour HH:MM:SS format
              \@     the current time in 12-hour am/pm format
              \A     the current time in 24-hour HH:MM format
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash, version + patch level (e.g., 2.00.0)
              \w     the current working directory, with $HOME abbreviated with a tilde
              \W     the basename of the current working directory, with $HOME abbreviated with a tilde
              \!     the history number of this command
              \#     the command number of this command
              \$     if the effective UID is 0, a #, otherwise a $
              \nnn   the character corresponding to the octal number nnn
              \\     a backslash
              \[     begin  a sequence of non-printing characters, which could be used to embed a terminal con-
                     trol sequence into the prompt
              \]     end a sequence of non-printing characters

How to find list of user groups I belong to in Linux

November 16, 2008 By: Neo Category: Command Line

The Linux command groups can be used to determine the list of groups a specific user belongs to. This should work in almost all flavors of Linux distributions. The following example shows how to use it.

[neo@techpulp ~]# groups
engineers devel
[neo@techpulp ~]# groups neo
engineers devel
[neo@techpulp ~]# groups liz
engineers qa
[neo@techpulp ~]#

How to find the available and free memory in my Linux system

November 16, 2008 By: Neo Category: Unix/Linux

The Linux system supports a special file system called PROC in which you can find various file describing the state of the current operating system. There is a file /proc/meminfo which contains information about the size of RAM currently installed on the system and how much of it is being currently used by the Linux kernel.

[neo@techpulp ~]# cat /proc/meminfo
MemTotal:      2009328 kB
MemFree:        385204 kB
Buffers:         58328 kB
Cached:        1251596 kB
SwapCached:          0 kB
Active:         778772 kB
Inactive:       763172 kB
HighTotal:     1113152 kB
HighFree:         1604 kB
LowTotal:       896176 kB
LowFree:        383600 kB
SwapTotal:      883532 kB
SwapFree:       883532 kB
Dirty:             136 kB
Writeback:           0 kB
AnonPages:      231992 kB
Mapped:         591780 kB
Slab:            34480 kB
SReclaimable:    21800 kB
SUnreclaim:      12680 kB
PageTables:       5352 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:   1888196 kB
Committed_AS:   715520 kB
VmallocTotal:   114680 kB
VmallocUsed:     49948 kB
VmallocChunk:    63476 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
Hugepagesize:     4096 kB
[neo@techpulp ~]#

The value of MemTotal is the size of RAM you have in the system and MemFree tells you how much memory is free. The value of SwapTotal is the collective size of all swap partitions used by Linux kernel.

Alternately you can use “free” command to get the same information.

[neo@techpulp ~]# free
total       used       free     shared    buffers     cached
Mem:       2009328    1625664     383664          0      58888    1252000
-/+ buffers/cache:     314776    1694552
Swap:       883532          0     883532
[neo@techpulp ~]#

Keyboard shortcuts/Hotkeys in Bash

November 16, 2008 By: Neo Category: Bash

Bash supports various hot keys to ease editing at bash shell prompt. The following are the commonly used shortcuts .

Ctrl+A Move to the start of the current lin
Ctrl+E Move to the end of the lin
Ctrl+F Move forward a characte
Ctrl+B Move back a characte
Alt+F Move forward to the end of the next word. Words are composed of alphanumeric characters (letters and digits
Alt+B Move back to the start of the current or previous word. Words are composed of alphanumeric characters (letters and digits)
Ctrl+L Clear the screen leaving the current line at the top of the scree
Ctrl+P Fetch the previous command from the history list, moving back in the lis
Ctrl+N Fetch the next command from the history list, moving forward in the lis
Alt+< Move to the first line in the histor
Alt+> Move to the end of the input history, i.e., the line currently being entere
Ctrl+R Search backward starting at the current line and moving ‘up’ through the history as necessary. This is an incremental searc
Ctrl+S Search forward starting at the current line and moving ‘down’ through the history as necessary. This is an incremental searc
Ctrl+K Delete the text from point to the end of the lin
Ctrl+U Delete backward from point to the beginning of the lin
Alt+D Delete from point to the end of the current word, or if between words, to the end of the next word
Ctrl+W Delete the word behind point, using white space as a word boundar

Effective usage of TAB button in Bash shell

November 16, 2008 By: Neo Category: Bash

The Bash shell supports good number of intuitive ways of using TAB button on your keyboard to do command or file/directory name completions. Here is the list of TAB key usage combinations bash supports.

Get lists all available commands (<TAB><TAB>)

[neo@techpulp ~]#<TAB><TAB>
Display all 4136 possibilities? (y or n)
:                                        ircat                                    pnmstitch
!                                        irdadump                                 pnmtile
./                                       irdaping                                 pnmtoddif
[neo@techpulp ~]#

Get all available commands starting with STRING (STRING<TAB><TAB>)

[neo@techpulp ~]# z<TAB><TAB>
zcat            zdump           zfgrep          zip             zipnote         znew
zcav            zegrep          zforce          zipcloak        zipsplit        zonetab2pot.py
zcmp            zeisstopnm      zgrep           zipgrep         zless           zsh
zdiff           zenity          zic             zipinfo         zmore           zsoelim
[neo@techpulp ~]# z

Get entire directory structure including hidden one (/<TAB><TAB>)

[neo@techpulp ~]# /<TAB><TAB>
audio/ dev/ lost+found/ opt/ selinux/ usr/
bin/ etc/ media/ mydata/ srv/ var/
boot/ home/ misc/ proc/ sys/ video/
drive_c/ server/ mnt/ root/ tmp/ drive_d/
audio/ lib/ net/ sbin/
[neo@techpulp ~]# /usr/<TAB><TAB>
bin/ games/ kerberos/ libexec/ sbin/ src/
etc/ include/ lib/ local/ share/ tmp/
[neo@techpulp ~]# /usr/

Get all local subdirectories without hidden ones (*<TAB><TAB>)

[neo@techpulp ~]# *<TAB><TAB>
Desktop/    examples/    source/    build/
music/ test/    demo/
[neo@techpulp ~]#

Get all users from “/etc/passwd” file (~<TAB><TAB>)

[neo@techpulp ~]# ~<TAB><TAB>
~adm         ~ftp/        ~lp/         ~ntp/        ~rpm/        ~tomcat/
~apache/     ~games/      ~mail/       ~openvpn/    ~shutdown/   ~torrent/
~avahi/      ~gdm/        ~mailnull/   ~operator/   ~smmsp/      ~uucp
~backuppc/   ~gopher      ~murali/     ~polkituser/ ~sobha/      ~vcsa/
~bin/        ~haldaemon/  ~news        ~pulse/      ~squid/      ~vdr/
~daemon/     ~halt/       ~nfsnobody/  ~root/       ~sshd/       ~vdradmin/
~dbus/       ~hsqldb/     ~nobody/     ~rpc/        ~sync/       ~webalizer/
~distcache/  ~jetty/      ~nscd/       ~rpcuser/    ~tcpdump/
[neo@techpulp ~]# ~

Get all system variables ($<TAB><TAB>)

[neo@techpulp ~]# $<TAB><TAB>
$_                         $KONSOLE_DCOP_SESSION
$BASH                      $LANG
$BASH_ARGC                 $LESSOPEN
$BASH_ARGV                 $LINENO
$BASH_COMMAND              $LINES
$BASH_LINENO               $LOGNAME
$BASH_SOURCE               $LS_COLORS
$BASH_SUBSHELL             $MACHTYPE
$BASH_VERSINFO             $MAIL
$BASH_VERSION              $MAILCHECK
$COLORS                    $OPTERR
$COLORTERM                 $OPTIND
$COLUMNS                   $OSTYPE
$COMP_WORDBREAKS           $PATH
$consoletype               $PIPESTATUS
$CVS_RSH                   $PPID
$DBUS_SESSION_BUS_ADDRESS  $PROMPT_COMMAND
$DESKTOP_SESSION           $PS1
$DIRSTACK                  $PS2
$DISPLAY                   $PS4
$EUID                      $PWD
$G_BROKEN_FILENAMES        $QTDIR
$GDM_LANG                  $QT_IM_MODULE
. . . .
[neo@techpulp ~]# $

Get all host names from “/etc/hosts” file (@<TAB><TAB>)

[neo@techpulp ~]# mail neo@<TAB><TAB>
@::1
@localhost
@localhost6
@localhost6.localdomain6
@localhost.localdomain
[neo@techpulp ~]# mail neo@

Get output like “ls” command (=<TAB><TAB>)

[neo@techpulp /]# =<TAB><TAB>
.bash_history                          examples/
.bash_logout                           me.png
.bash_profile                           msg.txt
.bashrc                                   source/
[neo@techpulp /]# =

Happy bashing!

My telnet session doesn’t respond - How to close it and get the shell back

November 16, 2008 By: Neo Category: Networking, Unix/Linux

Sometimes the telnet session doesn’t respond and almost locks up your shell. This can be because of any of the following reasons.

  • Your machine is disconnected from network
  • Telnet Server is disconnected from the network
  • Network problem between client and server

Your telnet client program uses Transmission Control Protocol (TCP) which takes lot of time (more than your patience) to realize that the connection to server is lost. If you are sure that the server is unreachable and would like to get out of the telnet session, you need to press “Ctrl+]” to get telnet client’s internal command prompt. There you can type ‘quit‘ command to close the telnet session.

[neo@techpulp ~]# telnet 203.25.132.64
Trying 203.25.132.64...
Connected to 203.25.132.64.
Escape character is '^]'.
Login: neo
Password:
[neo@tserver ~]#
^]
telnet> quit
Connection closed.
[neo@techpulp ~]#

How to give muliple lines of input to a command in Bash

November 16, 2008 By: Neo Category: Bash

There are two ways of sending multi-lined input to a command. One method is to save all the input in a file and redirect it to the command as input. The other method is useful if your input is small and can be typed at the shell itself.

This example shows how to redirect contents of a file as input to a command at bash shell.

[neo@tehpulp ~]# type <message.txt
Hi There
I am NeoX
[neo@tehpulp ~]#

The following example shows how to provide on the fly multi-lined input to a command at Bash shell. The “<<” operator with a word that determines end of input can be used as shown below.

[neo@techpulp ~]# cat <<EOF
> Hello There
> I am Neo
> EOF
Hello There
I am Neo
[neo@techpulp ~]#

Append redirected output of a command to a file in Bash

November 16, 2008 By: Neo Category: Bash

The bash command supports “>>” operator which can be used to append command output to a file instead of overwriting it. The following example appends output of two commands in a file.

[neo@techpulp ~]# echo Hi, There > msg.txt
[neo@techpulp ~]# echo I am Neo >> msg.txt
[neo@techpulp ~]# cat msg.txt
Hi, There
I am Neo
[neo@techpulp ~]#