Monday, May 18, 2009

Tips & Tricks

Console-based calculator
To build a terminal-based calculator, add the following function to your ~/.bashrc file:
function calc
{
echo “${1}”|bc -l;
}
Reload Bash profile using the following command:
$source .bashrc
Now run calc from terminal as:
$ calc 4+7*9-10/5
You will get the following output:
65.00000000000000000000
Easy enough?
-----------------------------------------------------------

When did I execute a command?
Here is a tip that will help you understand when you did what. history will give the list of commands which your have executed earlier. If you want information on when you executed these commands, you need to do the following.
Open the /etc/bashrc file in a text editor and add
the following:
export HISTTIMEFORMAT=”%h/%d - %H:%M:%S “
After adding this line, re-login and execute the history command again. Now you will see the commands along with their time of execution.
-----------------------------------------------------------

Encrypt your file with Vim
Did you know Vim can help you encrypt your file so that no one can open it without knowing the encryption key? Well, all you need to do is to create a file
with the -x flag:
vim -x filename

The above command will prompt you for an encryption key. Provide the key and remember it, as you will need it to open the file from now on.
-----------------------------------------------------------

Using Vi’s .exrc file
As you know the ~/.exrc file is used for making permanent settings to your Vi editor. If you place the following commands in this file, these commands will
be available to all your subsequent Vi sessions:

1) :abb is for abbreviation
For example:
:abb etl Elitecore Technologies Limited, Ahmedabad

So whenever you type etl in your Vi editor and press ENTER, SPACE or TAB, etl will be replaced by
“Elitecore Technologies Limited, Ahmedabad”
I use it to create templates for C and C++ programs, like:
:abb CPP #include^M using namespace std;^M int
main()^M {^M return0;^M }

Here ^M is for a new line. Note that to make sure you do not type ^M, for a new line you have to:
a) ctrl+v
b) press ENTER
It will display ^M as above but it means a new line.
So, now every time I need a C++ template, I just type CPP and press ENTER, SPACE or TAB.

2) :map is for mapping some command to some
shortcut.
For example, the vi command :set number is used to display line numbers in a file for every new line. Let’s say we want to set a shortcut for it, say, F2. To map this command to shortcut key, the Vi setting will be:
:map #2 :set number^M

Same for :set nonumber; let’s set it to F3:
:map #3 :set nonumber^M

3) set tabstop=2 is for setting TAB. I want TAB to be equal to 2 spaces, so I set it to 2.
-----------------------------------------------------------

Read a CD volume label
To read the volume label of a CD-ROM from the terminal:
$ dd if=/dev/cdwriter bs=1 skip=32808 count=32
LFYCD_MARCH08 32+0 records in
32+0 records out
32 bytes (32 B) copied, 4.02731 seconds, 0.0 kB/s
Note: The value, /dev/cdwriter depends on your device file.
-----------------------------------------------------------

Disabling ‘shutdown’ with Ctrl+Alt+Del

If you get annoyed, when you accidentally reboot the system by pressing the three magic keys, comment out the following line in your /etc/inittab file:

ca::ctrlaltdel:/sbin/shutdown -t3 -r now

You may alternatively allow specific users with this capability by changing the line as below and adding the specific login ids in ‘/etc/shutdown.allow’,

ca::ctrlaltdel:/sbin/shutdown -a -t3 -r now
-----------------------------------------------------------

More passwd flags
You can change user account details using the passwd command—yes, it can do more than changing just the password. Open the new terminal and enter the
following commands:

passwd -d [user_name]

where -d deletes the user’s password.
Some other useful flags are:
  • -l locks the user account
  • -u unlocks the user’s account
  • -? is to get help
Ref: Linux For You May 2009
-----------------------------------------------------------

Extract the contents of an RPM
Sometimes we are required to extract the files inside an RPM file instead of installing the RPM. A good example is when we take binaries from one distribution and to use on another distribution, where RPM is not the default
package manager. The rpm2cpio command comes in handy under these circumstances. For example:
$rpm2cpio coreutils-6.9-2.fc7.i386.rpm |cpio -idv
./bin/basename
./bin/cat
./bin/chgrp
./bin/chmod
[...]
This command can be used for source RPMs also.
-----------------------------------------------------------

Checking memory and I/O
The vmstat utility provides interesting information about processes, memory, I/O and CPU activity. When you run this utility without any arguments, the output looks similar to the following:
procs memory swap io system cpu
rbw swpd free buff cache si so bi bo in cs us sy id
000 8 8412 45956 52820 0 0 0 0 104 11 66 0 33
Here, the ‘procs’ fields show the number of
processes:
  • Waiting for run time (r)
  • Blocked (b)
  • Swapped out (w)


The 'memory' fields show the KBs of:
  • Swap memory
  • Free memory
  • Buffered memory
  • Cached memory
The 'swap' fields show the KBps of memory:
  • Swapped in from disk (si)
  • Swapped out to disk (so)
The 'io' fields show the number of blocks per second:
  • Sent to block devices (bi)
  • Received from block devices (bo)
The 'system' field shows the number of:
  • Interrupts per second (in)
  • Context switches per second (cs)
The 'cpu' field shows the percentage of total CPU
time as:
  • User time (us)
  • System time (sy)
  • Idle (id) time
If you want vmstat to update informationautomatically, you can run it as vmstat nsec, where nsec is the number of seconds you want it to wait before
another update.
-----------------------------------------------------------

Displays information about ELF files.

$readelf --segments /bin/ls
$readelf --segments /lib/libc.so.6
-----------------------------------------------------------

Execute a program periodically

$watch -n1 "cat /proc/interrupts"
-----------------------------------------------------------

Find the memory used by a program / process using pmap command

You can find the memory used by a program / process by looking into /proc/[process pid]/maps.
You can use pamp command for same.

$pmap pid

-----------------------------------------------------------

Run your script at boot time

To run your script at boot time you need to first copy your script to the /etc/init.d directory

$ cp /etc/init.d

Now make it executable as follows.

$chmod +x

Create a link to script for all run levels that you want your script to run.

$ ln -s /etc/init.d/ /etc/rc.d/rc5.d/S85
$ ln -s /etc/init.d/ /etc/rc.d/rc5.d/K85

The name of each symbolic link begins with either a K or S. K links are process that are killed on that runlevel, while those beginning with S are started. The S85 tells the system to start the script after starting all scripts with a lower no. & so is the case while killing at shutdown.

Ref: Q & A section, Linux For You, July 2009.
-----------------------------------------------------------

In Shell Scripting, How to find the length of variable.

$ test="Hello World"
$ echo ${#test}
11................................................. o/p

Ref: Linux Journal, May 2009.

-----------------------------------------------------------

How To Enable Root User ( Super User ) in Ubuntu

How To Login Without Entering Username and Password
-----------------------------------------------------------


wet tips

-----------------------------------------------------------

Internet

Find the internet connection speed.

How to identify Public IP address of your internet connection and ISP details.

-----------------------------------------------------------


VIM

8 Essential Vim Editor Navigation Fundamentals


-----------------------------------------------------------


How to find and delete empty directories and files

Find empty directories in the current directory using find -empty
$ find . -type d -empty

Remove all empty directories under the current directory
$ find . -type d -empty -exec rmdir {} \;
 
Find empty files in the current directory using find -empty 

$ find . -type f -empty
 
How many empty files are located under the current directory
$ find . -type f -empty | wc -l
 
How many non-empty files are located under the current directory
$ find . -type f -not -empty | wc -l 
 
Find dead symbolic links
$ find . -type l | perl -lne 'print if ! -e'
-----------------------------------------------------------
 

HowTo: Linux / UNIX Create a Manpage


----------------------------------------------------------- 
 
View information about the motherboard and CPU 
$ dmidecode | more
demdecode's purpose is to report "information 
about your system's hardware as described in your 
system BIOS"
 
-----------------------------------------------------------

How to count how many processes each user is running in Linux

$ ps hax -o user | sort | uniq -c
ps will list the processes, h will remove the header,
-o user prints only the user column,
sort sorts in alphabetical order, so uniq can count each
occurrence and show the number of occurrences instead of all
the occurrences themselves.
-----------------------------------------------------------
 
How to find which process is eating RAM
 
$ ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 20
----------------------------------------------------------- 
 
 Sed tutorials
 
http://unstableme.blogspot.com/search/label/Sed 
 


-----------------------------------------------------------
 
cross compiling

http://www.gnu.org/s/hello/manual/libc/Configuring-and-compiling.html 
 
 
-----------------------------------------------------------
 
Using Vim as a hex editor

Open a file in vim as usual, hit escape and type:
: %!xxd to switch into hex mode
 
exit from hex mode
:%!xxd -r
 
 
 
-----------------------------------------------------------
 

How to see what is in an RPM without installing it?

 1. Create a temp directory

 

# mkdir x

 

 2. Change directory

 

# cd x

 3. Now extract the RPM

 

# rpm2cpio /path/of/ur/RPM/file.rpm | cpio -dmi
 
 
 Ref:  http://www.linuxquestions.org/questions/blog/hi2arun-520846/
how-to-see-what-is-in-an-rpm-without-installing-it-4062/
-----------------------------------------------------------
 
 
How to change the  sudo timeout length in Ubuntu
 
 https://help.ubuntu.com/community/RootSudoTimeout
 
-----------------------------------------------------------
 
 

Disable/Enable Auto-Mount in Ubuntu 10.04/10.10

 http://www.liberiangeek.net/2010/09/disableenable-auto-mount-ubuntu-10-0410-10-maverick-meerkat/

 
 

No comments: