Showing posts with label TRICKS. Show all posts
Showing posts with label TRICKS. Show all posts

Tuesday, May 5, 2009

Unaliasing in BASH

Unaliasing in BASH
How can we unalias a command temporarily without using unalias command ?

just put the escape character before the command i.e. "\"e.g. :-

$\ls

It will show the without alias.

Wednesday, September 17, 2008

TRIMMING THE LOG

Various unix processes canproduce fast growing logs thatsometimes need to be trimmedinstead of deleted, for referenceor troubleshooting. And youlikely have no desire to edit thefiles. Here is a handy kshscript that will quickly trimthe log so it keeps recentinformation and lets you keep asmany lines as you think you mightneed.

I call it trimlog:

#! /bin/sh
# trimlog
filesize=`cat $1wc -l`
trim=`expr $filesize - $2`
if [ $trim -gt 0 ]
then
sed "1,$trim d" $1 > /tmp/$1
mv /tmp/$1 $1
echo $1 trimmed by $trim lines
else
echo "Invalid argument"
fi
Use it by feeding in the name of the log you want to trim followedby the number of lines you want to keep:

# trimlog oracle_listener.log 10000

Of course you can't use it in the /tmp directory.
There are probably newer and more efficient ways to do this, but this works.

Monday, September 15, 2008

CRYPT AN ASCII FILE

An ascii file can be easily encrypted and decrypted.
To encrypt simply pipe the STDOUT ofthe file to "crypt" and redirect it toa new file name. Enter a passowrd whenprompted with "Enter key".

$ cat foo crypt > foo.e
Enter key:

To unencrypt simply pipe the STDOUT ofthe encrypted file to "crpyt" andredirect it to a new file name. Entera passowrd when prompted with"Enter key".
$ cat foo.e crypt > foo.new
Enter key:

Friday, August 1, 2008

Tracking User's activity using "script -a"

Unix System provides a very good method allows us to capture of all commands issued by the user “XYZ”. For this, we can user "script" command. This will print all what appears on the screen (commands and their output) to the filename

But remember, Its not a User History.... It stores all commands entered by all users (.e. its the shell history .

Add the following line to your BASH profile

script -a ~/.termlog`date +%m%d%y`

This enable to capture all the terminal activities to a file ending with current date

Monday, July 28, 2008

Great Unix Tips & Tricks: Vol 1

Whether you are a newbie to Linux or you are a seasoned guru, you are bound to find some scripts or programs which just make your life on the computer exponentially easier. The following is a list of them and explains what they do.

1. tac

tac is a command similar to cat, except that it reverses the output of the specified file.

tac

2. Multiple X-Sessions

Using multiple X-Sessions allows a user to have open more than just one instance of XFree, so multiple desktop environments can be used at a time.

startx -- :

terminal name is simply which terminal X should start on. By default it starts on

0:0. If you want two open, you may want it open on 0:1, which can be run by:

startx -- :1

The resulting desktops will exist on F7 - F12.

3. links

links is a text based browser which has full support for tables

links

is simply the hostname or IP address of the computer to connect to.

Tuesday, July 8, 2008

Command based Calculator in BASH

Bash can perform simple arithmetic operations at the command prompt.

To use this feature, simply type in the arithmetic expression you wish to evaluate at the prompt within double parentheses, as illustrated below. Bash will attempt to perform the calculation and return the answer.


bash> echo $((16/2))
8


bash> echo $((16*2))
32

Wednesday, July 2, 2008

Limiting File Size in UNIX

C shell
limit filesize 2m

Korn/Bash shell

ulimit -f max-size
ex: Korn/Bash shell limit 2 MB.
ulimit
-f 2000

Removing non-consecutive duplicate lines from a file

The uniq command will "Discard all but one of successive identical lines" from a file or input stream.

In order to remove non-consecutive duplicate lines, use awk:

awk '!x[$0]++' FILE

The Easy PATH

To format one's PATH variable for easy viewing, try this function:
==============

path()
{
oldIFS=$IFS
IFS=:
printf "%s\n" $PATH
IFS=$oldIFS
}

==============

A typical run of the function:
$ path
/bin
/usr/bin
/usr/bin/X11
/usr/X11R6/bin
/usr/local/bin
/home/sanju/bin
/home/sanju/scripts

Linux: How to Encrypt and decrypt files with a password

To encrypt and decrypt files with a password, use gpg command. It is an encryption and signing tool for Linux/UNIX like operating system such as FreeBSD/Solaris.


GnuPG stands for GNU Privacy Guard and is GNU's tool for secure communication and data storage. It can be used to encrypt data and to create digital signatures. It includes an advanced key management facility.

Encrypting a file in linux
To encrypt single file, use command gpg as follows:

$ gpg -c filename


To encrypt myfinancial.info file, type the command:$ gpg -c myfinancial.info

Output:

Enter passphrase:
Repeat passphrase:


This will create a myfinancial.info.gpg file.

Option:
-c : Encrypt with symmetric cipher

Caution if you ever forgot your password aka passphrase, you cannot recover the data as it use very strong encryption.

Thursday, June 26, 2008

Local variable in BASH (Setting a variables for one command only)

If you want to set a variable for one command only, you can always write the variable setting first, immediately followed by the simplecommand (don't write any ; after the assignment). This will result in the variable being set for that command only, but keeping the old value (if it had one) after the command has been executed.

As an example, if you execute the code

MANPATH=/usr/man:/usr/local/man man test

will only look for the manual for test in directories /usr/man and /usr/local/man. This regardless of what value MANPATH had before the call.

Super Bash Tricks

I'll start off this blog with some good bash shell tricks. You'll be the envy of your friends with these up your sleeve.

For the uninitiated, bash is the default shell in many Linux distros, including Fedora, Ubuntu, Redhat etc. If you use a Linux based OS, then chances are that you are using bash. For this reason, I outline below a few common annoyances, and the simple ways to overcome them.

1. Lost bash history

If you have a terminal open and are typing commands, then open another one and use that for a while, the new terminal won’t remember any of the commands typed in the first one. In addition, closing the first terminal, and then the second will overwrite any of the commands typed in the first terminal. Doubly annoying! This happens because the bash history is only saved when you close the terminal, not after each command. To fix it:


Edit your .bashrc (for beginners, any file starting with a . is hidden - they contain user preferences.)

nano ~/.bashrc

No need for a sudo here Ubuntuers, this is your own file, not a system setting. I like nano, but it’s up to you, choose gedit, kate, mousepad, vi or emacs as you wish.add the lines

shopt -s histappend

PROMPT_COMMAND=history -a;

And save. (control - O to write out. ^ means control in nano and other software, so the bottom of the editor does actually make sense to beginners! ^X to exit.) This makes bash append history instead of overwriting it, and makes it so that each time the prompt is shown it writes out all the history.

2. Stupid spelling mistakes

Add


shopt -s cdspell


to your .bashrc again. This will make sure that spelling mistakes such as ect instead of etc are ignored.

3. Duplicate entries in bash history


I often type cd .. multiple times in a row, when I then press UP to go back to earlier commands I don’t want to be reminded of my earlier inelegant excursions around the file system.
Add


export HISTCONTROL="ignoredups"


to .bashrc again.
Even better, add


export HISTIGNORE="&:ls:[bf]g:exit”


This will ignore duplicates, as well as ls, bg, fg and exit as well, making for a cleaner bash history.

4. Multiple line commands split up in history


Add


shopt -s cmdhist


to .bashrc, this will change multiple line commands into single lines for easy editing.


5. A couple of neat extras suggested by commenters


Press control + R in bash, then start typing and you can search through your past commands much easier than just pressing UP 300 times…
Alternatively, use


history grep "foo"


to search through your history - “foo” is the thing you are searching for. (Thanks to Ally)


cd -


goes to the last directory you were in - useful if you want to go somewhere to change something, then need to quickly flip back again.

Conclusion So, there are a few tips to get your bash history more manageable - if you have any extra tips, add them to the comments and I’ll add them to the main article!