728x90 AdSpace

Latest News

Thursday, 28 November 2013

55 Best Linux Tips, Tricks And Command Lines


55 Best Linux Tips, Tricks And Command Lines



55 Best Linux Tips, Tricks And Command Lines

Here we bring to you the 55 best tips, tricks and command lines of all time contributed by our readers. Try your hands and have fun.

If you are a Linux user and open to having some fun with your open source operating machine, then this is a must-read article for you. Here is a compilation of 55 tips, tricks and command lines of all time, enough to keep you equipped. Have a look:

1. Back-up and restore Thunderbird e-mails

In Linux, when you want to reinstall your system for any reason, you need to take a back-up of your data along with your e-mails in Thunderbird. Given below are a few simple steps that back-up e-mails manually.
Check for your Thunderbird e-mail and profile folder. If you have not changed it, it should be in /home//.thunderbird/.default_folder/

For example, the alphanumeric folder name on my system is vx3vg9j2.default
Copy the whole alphanumeric folder and place it in some other computer or storage device as an e-mail backup. Do remember to close Thunderbird before doing this.

After reinstalling your PC with any Linux-based OS, install the Thunderbird e-mail client and configure your e-mail credentials before closing the Thunderbird client.

Go to your Thunderbird folder in the new installation /home//.thunderbird/ folder. The alphanumeric named folder will be a different one now.

For example: /home/guest/.thunderbird/jx3gv9k2.default

Copy the contents of the old alphanumeric folder that is kept as a backup to the above location.

That is, you need to copy the contents of vx3vg9j2.default to jx3gv9k2.default
Now, open the Thunderbird client and you will find that all the e-mail credentials, e-mails, e-mail rules and address book will be available as they were before re-installation.

--Sobhanadri Agnihotram,
sobhanadri.a@gmail.com

2. Execute commands on a remote Linux machine

If you want to execute any command or script on a remote Linux machine, you can use ssh. Below are a few examples.

The syntax for running a command or script on a remote server is:
ssh [USER]@[IP] [command or script]

Let us look at how this can be done. Suppose you want to display the directory contents of /root of a remote host, you can run the following command:

[narendra@ubunu]$ ssh root@172.16.223.128 ls -l /root
root@172.16.223.128's password:
total 12

drwxr-xr-x. 2 root root 4096 Mar 12 00:05 device_drivers

drwxr-xr-x. 2 root root 4096 Mar 12 01:31 pthreads

drwxr-xr-x. 2 root root 4096 Mar 12 01:32 python

[narendra@ubunu]$

The same can be done to run any script on the remote computer.
--Narendra Kangralkar,
narendrakangralkar@gmail.com

3. Run a Linux command after every reboot

This tip allows you to run any Linux command or script just after system reboot. You can use the @reboot cron keyword.
If you have a script in your /home directory and it needs to be run on every boot, open the cron file in editable mode and add the following:

$crontab -e

@reboot /home/xyz/myscript.sh

Do remember to enable crond on boot.
Imran Sheikh,
imrannsheikh@gmail.com

4. Comment out hashes in large configuration files

Here is a small tip for system administrators, who need to tackle large configuration files, which include lots of commented lines (marked by #). With this tip you can remove all those hashes and provide only an uncommented configuration view for faster lookup into the file.
If you want to check the configuration file of the Squid proxy server, run the following command:

#cat squid.conf | egrep -v ^#

This will show only lines that do not start with a hash mark, thus giving the configuration parameter that is being used in the current set-up.
--Yogesh Upadhyay,
yogeshupadhya@gmail.com

5. Replacing '\n' with 'space' in each line of a file

You can use the awk statement given below to remove the '\n' from each line and replace it with a blank space:

awk '$1=$1' ORS=' ' /etc/passwd
--Rajeev N Sambhu,
rajnellaya@gmail.com

6. Know your shells

Here is a command that will let you know about the available shells on your Linux distribution:

#chsh -l

To change your login shell, use the following command:

# chsh
--Chandralekha Balachandran,
reachlekha@gmail.com
Advanced ls commands
The following commands are very useful to know your system better.
lspci – Lists all PCI devices. Use -v for verbose output.
lsusb – Lists all USB devices. Use -v for verbose output.
lsmod – Lists the status of modules in the Linux kernel.
lsattr – Lists file attributes on a second extended Linux file system.
lsof – Lists the file descriptors opened by all the processes. A very useful command when a process fails to close any file descriptors.
To know more details, you can view the manual pages of each command mentioned above.

--Prasanna Mohanasundaram,
prasanna.mohanasundaram@gmail.com


7. Checking for rootkits

Attackers install rootkits on a machine to gain root access, while its presence is hidden from the real administrator of the server. A tool that can help you to detect rootkits on your machine is chkrootkit. You can download this from ftp://ftp.pangeia.com.br/pub/seg/pac/chkrootkit.tar.gz
To install chkrootkit, you need to compile the code that you have just downloaded. Extract the downloaded tar file and change to the extracted directory, as shown below:

# tar -xvf chkrootkit.tar.gz

# cd chkrootkit-0.49/

Now compile the code by running the following command:

# make sense


After successfully compiling, the tool is ready to be used. To check for rootkits, simply run chkrootkit as the root user:

# ./chkrootkit

--Samual, samual45@gmail.com

8. Finding and replacing a test with sed

Let's look at how to find and replace a test using sed, a stream editor for filtering and transforming text. Let us first create a sample
text file with the following text:

$ cat > sample.txt

This is a first test of sample test file
This is a second test of sample test file

Press Ctrl+D after you finish entering the text.
Now run the command below to display the contents of the newly created text file:

$ cat sample.txt

The output should be as displayed below:

This is a first test of sample test file
This is a second test of sample test file

Now, substitute the first occurrence of the pattern 'test' in each line with 'log':

$ sed 's/test/log/' sample.txt

This is a first log of sample test file
This is a second log of sample test file

If you want to substitute the second occurrence of pattern 'test' in each line with 'log', use the following commands:




$ sed 's/test/log/2' sample.txt

This is a first test of sample log file
This is a second test of sample log file

To substitute every occurrence of the pattern 'test' in each line with 'log', use the code below:

$ sed 's/test/log/g' sample.txt

This is a first log of sample log file
This is a second log of sample log file

The syntax for the above options is:

sed 's/original_pattern/replacement/options'

Also, by default, sed will send the data to the stander output device,
and you can redirect it to any file by using the redirection operator ">".

$ sed 's/test/log/g' sample.txt > mod_sample.txt

--Jagan Teki, 402jagan@gmail.com

9. Backing up a MySQL database

There are several methods to back up a MySQL database, one of which is to a command line option. You need to have mysqldump installed for this method.
mysqldump is a command line utility that comes with the MySQL installation It can be used to archive one or all databases.
Given below is the command to back up a single database:

mysqldump --user [user_name] –password=[password_of_the user] [database name] > [dump_file]

The command for back up of all databases in existence is as follows:

mysqldump –u[user name] –p[password] –all-databases > [dump file]

To restore the back up taken by mysqldump, use the normal SQL command.

mysql --u [username] --password=[password] [database name] < [dump_file]
--Manish,
mt81@in.com

10. How to show the name of the current database in the MySQL prompt

If you need the name of the currently selected database in your MySQL prompt, use the following lines in your MySQL configuration file (my.cnf):

[mysql]

prompt='mysql(\d)> '

Now, when you connect, the MySQL prompt will look like what's shown below:

mysql((none))> use test;

Database changed

mysql(test)>

mysql(test)> use mysql;

Database changed

mysql(mysql)>

This makes it very easy to identify the name of the database that you are currently working on.

--Mohana Sundaram N,
mohan.linux@yahoo.com

11. Find your MySQL configuration file

We often have to administer a system that has been set up by someone else. In such a situation, it's difficult to find the correct configuration files for different applications. Here is a tip to find the correct configuration file for MySQL:

mysql -? | grep ".cnf"
--Remin Raphael,
remin@smartgeek.in

12. View the contents of tar and rpm files
Here are two simple commands to show you the contents of the tar and rpm files.
1. To view the content of a tar file, issue the following command:

#tar -tvf /path/to/file.tar

2. To view the content of an rpm file, use the command given below:

#rpm -qlp /path/to/file.rpm

--Giriraj G Rajasekharan,
girirajgr@gmail.com

13. Playing around with MP3 files

Here is a tip that helps you cut, split, join or merge MP3 files in Ubuntu, resulting in a better quality output.
To cut an MP3 file, you need to install poc-streamer, as follows:

$sudo apt-get install poc-streamer

The syntax for mp3cut is given below:

mp3cut [-o outputfile] [-T title] [-A artist] [-N album-name] [-t [hh:]mm:ss[+ms]-[hh:]mm:ss[+ms]] mp3 [-t ...] mp3 -o output: Output file, default mp3file.out.mp3

For example, if you want to cut a one-minute clip of the MP3 file named input.mp3 to a .wav file called output.wav, run the following command:

$mp3cut -o output.wav -t 00:00(+0)-01:00(+0) input.mp3

If you want to join two MP3 files, you need to install mp3wrap, as follows:

$sudo apt-get install mp3wrap

The syntax for mp3wrap is shown below:

$mp3wrap merged_filename.mp3 filename1.mp3 filename2.mp3

…where filename1.mp3 and filename2.mp3 are my input files that can be merged together.
Finally, you can split a single large MP3 file into small files by installing Mp3split using the following command:

$sudo apt-get install mp3splt

Now, to split the large file, run the following command:

$mp3splt filename.mp3 00.00 01.23 03.20

Filename.mp3 is my input file, which can be split into two MP3 files. One is from the start to the 1 min 23 sec point, and another one is from 1 min 23 sec to 3 min 20 sec. Mp3split can make smaller files without decoding even the file.

--Rajasekhar Chintalpudi,
rajasekhar.chintalapudi@gmail.com

14. GRUB 2 recovery

We often come across a condition in which the boot loader gets corrupt. Here are a few steps that will help you recover your GRUB 2 boot loader.
Boot from a live CD or DVD, which supports GRUB 2 (Ubuntu 9.10 CD or above. A DVD will take more time than a CD, so I suggest you boot from a CD).
Open the terminal and run fdisk -l to check the partition from which you want to recover GRUB 2.
Here I assume that you want to recover it from /dev/sda1.
Then run the following commands:

$sudo mkdir /media/sda1

$sudo mount /dev/sda1 /media/sda1

$sudo mount --bind /dev /media/sda1/dev

$sudo mount --bind /proc /media/sda1/proc

Now chroot into that partition by running the command given below:

$sudo chroot /media/sda1

Then re-install GRUB, as follows:

#grub-install /dev/sda

The output should be like what's shown below:

Installation finished. No error reported.

If you get an error, then try the following command:

#grub-install --recheck /dev/sda

After a successful installation, exit from chroot and unmount the file systems that were mounted to recover GRUB. Now reboot.

#exit

$sudo umount /media/sda1/proc

$sudo umount /media/sda1/dev

$sudo umount /media/sda1

$sudo reboot

You've successfully completed recovering your GRUB boot loader.

--Kousik Maiti,
kousikster@gmail.com

15. Record whatever you do in the terminal

Have you ever felt that you should record everything you do in the terminal in a file?
Then try out the following tip. There is a command named script, which can be used with option –a to append the output to a file.

Given below is an example that will show how it works:
Mandriva~:$ script -a lfy
Script started, file is lfy
Mandriva~:$ uname -a
Linux localhost.localdomain 2.6.33.5-desktop-2mnb #1 SMP Thu Jun 17 21:30:10 UTC 2010 i686 i686 i386 GNU/Linux
Mandriva~:$ uname
Linux
Mandriva~:$ exit
exit
Script done, file is lfy

Here, the name of the file is lfy. You can verify it later by using the code given below:

Mandriva~:$ cat lfy
Script started on Mon 16 May 2011 02:09:47 AM EDT
Mandriva~:$ uname -a
Linux localhost.localdomain 2.6.33.5-desktop-2mnb #1 SMP Thu Jun 17 21:30:10 UTC 2010 i686 i686 i386 GNU/Linux
Mandriva~:$ uname
Linux
Mandriva~:$ exit
exit

Script done on Mon 16 May 2011 02:10:32 AM EDT

--Sibi,
psibi2000@gmail.com

16. Wonders of VIM

VIM has a very useful command set. Here are a few commands that can be used to increase your productivity.

VIM as a file comparator:

Use '-d' switch to compare two files in VIM. This command splits the VIM screen vertically and shows the differences.

vim -d file1 file2

17.  load new files in separate windows:

If you have a file named 'first.txt' loaded already in VIM, then use ':split second.txt' to load another file named 'second.txt' in a separate window--IM will split the screen horizontally and load the second file. You can use ':vsplit' to split the screen vertically. 'Ctrl+w' can be used to switch between the windows.

18. VIM as a command:

Normally, we use VIM as an editor; however, it can be used as a command. It allows the execution of VIM commands with switch '-c', for example. Here is a command to replace all '>' characters to '>>' in a file FILE.TXT without opening VIM.

vim -c ":s/>/>>/g" -c ":wq" FILE.TXT

19. To open a file in read-only mode:

Use the '-R' switch to open a file in read-only mode; later on, '!' can be used to forcefully write to the file.

--Satya prakash,
satya.comnet@gmail.com

20. Check your processor and OS architecture

You might want to install a 64-bit OS on your machine, but the processor might just be 32-bit compatible. Sometimes it happens the other way too, i.e., you install a 32-bit OS on a machine that has a 64-bit processor. Here is how to find out whether the installed OS as well as the CPU are of 64-bit or 32-bit.

Given below is the command that will output the details of the OS installed:

$ uname -m

The result for a 64-bit OS installation (for x86_64 architecture):

x86_64

The result for a non-64-bit OS installation (for i686 architecture):

i686

To know about the processor, run the following command:

$ lshw -class processor | grep width

Shown below is the result for a 64-bit installation:

width: 64 bits

The result for a 32-bit installation:

width: 32 bits

Note: Please install lshw if it is not already installed on your system

--Srikanth Vittal,
vi.srikanth@gmail.com

21. Sudoing with Fedora

Ever felt tired of entering the super-user password after typing 'su –c' again and again? Type 'su -c visudo' just once and uncomment the following line:

# %wheel ALL=(ALL) ALL

Replace 'wheel' with your sudo username. So if the username is egghead, the line becomes…

%egghead ALL=(ALL) ALL

Save and quit. You're good to use egghead as the sudo user.

--A. Datta,
webmaster@aucklandwhich.org

22. Let your Linux system welcome you

Issue the following script and name it welcome.sh

echo "Hi zades you are welcome today is " | festival --tts
date| cut -d" " -f 1-3 | festival --tts

Now put the command sh welcome.sh at start-up. This will allow the script to run every time you log in to your system. Once done, restart your system to hear the message that is written in the Echo command.

The festival command is used to change the text to voice. You can use this command in many ways according to your creativity. Do remember to check that you have festival installed before trying this tip.

--Vinay Jhedu,
vinay.komal100@gmail.com

23. Ignoring the case during TAB-completion

By default, TAB-completion is not useful if the name of the file or directory starts with an uppercase. You can make your Shell totally ignore the case for the name by adding the following entry in /etc/inputrc:

set completion-ignore-case on

Then restart your Shell. From now onwards, TAB-completion will complete your file or directory name, and completely ignore the case.

Do remember to make changes to inputrc only as the root user.
You can read more about this in the manual pages of readline:

man readline

--Sachin P,
iclcoolster@gmail.com

24. Find your OS and distribution name

Here is a tip that will let you know the name of the OS, along with other details:

[root@vl-pun-blg-qa27]# lsb_release -a

LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description: CentOS release 5.5 (Final)
Release: 5.5
Codename: Final

--Narendra Kangralkar,
narendrakangralkar@gmail.com

25. Auto mounting a partition on Linux

The file that contains data regarding the devices to be mounted at start-up is in /etc/fstab. To automatically mount a partition, follow the steps given below.

First, create the directory in which your partition will be mounted. Create one directory per partition. I created the directory in /media. This directory is known as the 'mount point' for the partition.

To create the mount point, open up the terminal and type the following command:

sudo mkdir location_of_dir/name_of_dir

…or you can use Nautilus, the file manager, to create a folder.

If the directory is created in a location in which you need root privileges, use sudo. After creating the mount point, modify /etc/fstab as per your requirements. It is always advisable to create a backup of the /etc/fstab file before making any changes, because any error in that file can prevent your OS from booting.

Now, make changes in fstab to auto mount the HDD partition:

sudo gedit /etc/fstab

Open the /etc/fstab with a text editor of your choice with root privileges.
In this file, add the details in the same order as done for the existing partitions.

The order should be as follows: the device name, the default mount point, the file-system type, mount options, dump, and the fsck option.

The device name is the name of the HDD partition (such as /dev/sda5): the mount point is the full path of the directory where the partition is to be mounted. The file system type is the type of file system like ext4, fat, ntfs, etc. Mount options are normally given as defaults, while dump and fsck options are given as 0.

I had a partition /dev/sda5 and I created the directory /media/mydisk. My partition was of type ext4, so to my /etc/fstab, I added the following command:

/dev/sda5 /media/mydisk ext4 defaults 0 0

Save the file and in the command prompt, type the following:

sudo mount -a

Now, the partition will be automatically mounted on every reboot.
--Vineeth Kartha,
vineethkartha@ieee.org

26. Creating a virtual file system

Here is a simple tip that allows you to create a virtual file system and mount it with a loopback device.

STEP 1: First create a file of 10 MB using the following command:

$ dd if=/dev/zero of=/tmp/disk-image count=20480

By default, dd uses a block of 512 so the size will be 20480*512
STEP 2: Now create the file system as ext2 or ext3
Here, in the following example, let's use ext3 as a file system:

$ mkfs -t ext3 -q /tmp/disk-image

You can even use Reiser as a file system type, but you'll need to create a bigger disk image. Something like what's shown below:

$dd if=/dev/zero of=/tmp/disk-image count=50480

$mkfs -t reiserfs -q /tmp/disk-image

STEP 3: As the final step, create a mount point and mount the file system:

$ mkdir /virtual-fs

$ mount -o loop=/dev/loop0 /tmp/disk-image /virtual-fs

Note: If you want to mount multiple devices, you will have to increase the loop count as mentioned below:

loop=/dev/loop1, loop=/dev/loop2,... loop=/dev/loopn

After you complete the above steps, you can use it as a virtual file system. You can even add this to /etc/fstab to mount this virtual file system whenever you computer is rebooted.
Open your /etc/fstab in a text editor and add the following:

/tmp/disk-image /virtual-fs ext3 rw,loop=/dev/loop0 0 0

--Aarsh S Talati,
aarshstalati1989@gmail.com

27. Identify your current shell name

You can identify your current shell name by using the following commands:

[narendra@CentOS]$ echo $SHELL

/bin/bash

The "SHELL" environment variable stores the name of the current shell.
You can also use the command given below to get the shell name:

[narendra@CentOS]$ echo $0

bash

"$0" will print the name of the program; here the program name is 'current shell'.

--Narendra Kangralkar,
narendrakangralkar@gmail.com

28. Scan open ports

The command given below will scan all the open TCP ports on the loopback interface:

nmap -sS -O 127.0.0.1

In general, you can use the following:

nmap -sS -O

To scan open UDP ports in the system, use the command given below:

nmap -sU -O

--Prasanna,
prasanna.mohanasundaram@gmail.com

29. Rev up!

As *nix sysadmins, we need to do a whole bunch of text-based data processing, either in files or data streams.

Here is a shell command called rev that I came across and wanted to share with you geeks because I really liked it and found it useful.

The rev command utility reverses the order of characters in every line. In short, it creates a mirror image. The most common use of rev is to reverse the line, extract a particular string and then pipe through rev a second time to restore the original.

So, if I want to get the year mentioned at the end of a string, this is what I will do:

$cat fileinfo.txt

Last Changed Date: 2011-08-11 18:10:08 -0500 Thu, 11 Aug 2011

$cat fileinfo.txt | rev
1102 guA 11 ,uhT 0050- 80:01:81 11-80-1102 :etaD degnahC tsaL

$cat fileinfo.txt | rev | awk '{print $1}'
1102

$cat fileinfo.txt | rev | awk '{print $1}' | rev
2011

Voila! Got the year! This is just one workaround, out of the many ways of doing this same task. After all, it doesn't hurt to learn something new.
--Ram Iyer,
ramiyer1@gmail.com

30. Increment or decrement a number present in Vim editor

This tip will increment and decrement a number in Vim editor. To increment, use Ctrl+A and to decrement, use Ctlr+X.
The following example will explain it further.

Let's suppose a number, 5, is present in the file that is being edited in Vim editor. Now, if you need to increment or decrement the number by 1, place the cursor on the digit and press Ctrl+A to increment it (i.e., it becomes 6); if you press Ctrl+A again, 6 becomes 7, and so on. In the same way, if you press Ctrl+X, the number will be decremented by 1.
If you press 8 and then press Ctrl+X, the number will be decremented by 8. Similarly, pressing 12 and then Ctrl+A will increment the number by 12.
--Adithya Kiran Gangu,
adithya.kiran@gmail.com

31. Search and delete files from a folder

If you want to delete all the .lock files from a folder, use the following command:

find -name *.lock | xargs rm -rf

This will find all the files with the .lock extension and delete them. This can be done for any files that you need to delete.

--Mridhul,
mridhul@live.com

32. Get the right information easily

Newbies exploring GNU/Linux sometimes find it difficult to get the right information about a device that is not working. Yet, this information is required to make the device work. Here is a command that gives you the details of all PCI devices and the kernel driver that is associated with them.
Open the terminal and log in as the root user. Now run the following command:

lspci -k

lspci gives you the information about the PCI buses and also the devices connected to them, and the -k switch displays which kernel module is handling the device. So if it is missing on some device, you need to install the driver for that device.

--Pankaj Tanwar,
pankaj.tux@gmail.com

33. Number conversion in the Vim editor

Here is a tip that will let you convert hexadecimal numbers to decimal numbers and vice versa in the Vim editor.
To convert hexadecimal numbers to decimal numbers, you need to type the following in the Vim editor's command mode:

:echo 0x111

Press Enter and you will get the result:

273

You can even try the command given below to convert the number:

:echo printf ('%d',0x111)

273

Now to convert decimal numbers to hexadecimal numbers…

:echo printf ('%x', 273)
111

You can even perform simple arithmetic on Vim's command prompt as given in the example below:

:echo printf ('%x',273-173)

64

:echo 0x111-0x10

257
--Adithya Kiran Gangu,
adithya.kiran@gmail.com

34. Know the libraries used by a program

Here is a tip that will help you know what shared libraries are being used by a program.

For example, to figure out exactly which libraries are used by ls, run the following command:

ldd /bin/ls

The output will be a list of all shared libraries:

linux-gate.so.1 => (0xffffe000)
libselinux.so.1 => /lib/libselinux.so.1 (0xb786c000)
librt.so.1 => /lib/librt.so.1 (0xb7862000)
libcap.so.2 => /lib/libcap.so.2 (0xb785c000)
libacl.so.1 => /lib/libacl.so.1 (0xb7852000)
libc.so.6 => /lib/libc.so.6 (0xb76e4000)
libdl.so.2 => /lib/libdl.so.2 (0xb76df000)
/lib/ld-linux.so.2 (0xb78a4000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb76c4000)
libattr.so.1 => /lib/libattr.so.1 (0xb76be000)

--Aarsh S Talati,
aarshstalati1989@gmail.com

35. Use YUM to download a package

Often, you need to download rpm packages without installing them on the system that you are using to download it. Using the normal YUM command downloads the packages and also installs them on your computer. Here is an option that will only download the rpm package for you. It will download it in the folders specified in the '--downloaddir' option.

# yum update httpd -y --downloadonly --downloaddir=/opt

Now, you can install all rpm packages in this folder by running the following command:

# rpm -Uivh *.rpm

Do remember to run these commands as the root.

--Pratyay Modi,
pratyaymodi@gmail.com

36. Resolve FSCK failed error

Sometimes, while booting your Linux system, you may come across the error shown below:

FSCK failed. Please repair manually and reboot. The root file system is currently mounted read-only. To remount it read-write do:
bash# mount-n -o remount,rw /
Attention: Only CONTROL-D will reboot the system in this maintance mode. shudown or reboot will not work.
Give root password for login:_

Provide your root password and try the following command:

mount -n -o remount,rw /

If this does not work for you, reboot your system and do a manual file system check on your root partition as follows:

umount /dev/hddXXX

fsck -CV /dev/hddXXX

…where hddXX is the root partition.


--Abhishek Chib,
abhishek.chib@gmail.com

37. Undo your changes even after quitting the VIM editor

As all of us know, if you make changes in a file using VIM editor, the changes are permanent and you cannot get the old version back after you save and quit the editor.
But VIM v7.3 allows you to get the old version back even after quitting the editor.
Here is a tip that shows you how to configure VIM to remember changes.
To enable Undo, execute the following commands in VIM just before starting to edit the file.

:set undofile

:set undodir=/tmp

This is to be done every time you start editing a file. In case you need the configuration to be there for all files that you open in VIM, create a file called '.exrc' or '.vimrc' in $HOME directory. In my case, it is /myhome.
Open the just created file and add the following commands:

# vi /myhome/.exrc

set undofile

set undodir=/tmp

Save and close the file.

:wq

From now onwards, the Undo history is maintained in the background for all files that you edit with VIM.

--Adithya Kiran Gangu,
adithya.kiran@gmail.com

38. Using 'vi' commands on your terminal
Using 'vi' commands while working on the terminal is a good work enabler. To set your terminal to 'vi' mode, you need to use the following command:

set -o vi

Now you can use the command mode and the insert mode of 'vi' while working on the terminal.

--Dipjyoti Ghosh,
dipjyoti.ghosh@gmail.com

39. Get your IP address

Here is a one line command to fetch all the IP addresses (except localhost) of your computer:

# ifconfig | grep "inet addr:" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d:

Note: Use the above command as the root user.

--Balkaran Brar,
balkaran.brar@gmail.com

40. Make your system speak for you!

You can make your system speak for you by using the Speech Synthesizer command normally available in Ubuntu and many other distributions of Linux.
To do so, issue the following command:

# espeak "hello how are you"

You will hear a voice speaking for you.
To change the pitch of the voice, you can issue the following command in the format shown:

# espeak -p 80 "hello how are you"

…(default being 50)
Issuing the following form of command will control the speed of the speech, in terms of words per minute:

# espeak -s 80 "hello how are you"

There are more interesting options available in the man pages.
--Sanjay Goswami,
sanjaygoswamee@gmail.com

41. Measuring the network throughput between two Linux systems

Iperf is a tool that measures the bandwidth and the quality of a network link. It can be installed very easily on any Linux system. One host must be set as the client and the other one as the server. Make sure that iperf is installed on both systems. If it is not installed, then use your package manager to install it before trying this tip.

Now run iperf on one of the Linux systems as the server, as shown below:

linux-erv3:/home/test/Desktop # iperf -s

------------------------------------------------------------
Server listening on TCP port 5001

TCP window size: 85.3 KByte (default)
------------------------------------------------------------

Go to the second Linux system and run iperf -c as the client:

linux-6bg3:~ # iperf -c 192.168.1.100

------------------------------------------------------------
Client connecting to 192.168.1.100, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------

[ 3] local 192.168.1.109 port 39572 connected with 192.168.1.100 port 5001

^C[ ID] Interval Transfer Bandwidth

[ 3] 0.0- 6.3 sec 6.38 MBytes 8.51 Mbits/sec

By default, the iperf client connects to the iperf server on the TCP port 5001 and the bandwidth displayed by iperf is the bandwidth from the client to the server. In the above example, it is 8.51 Mbits/sec between two Linux test systems connected over a wireless network.

--Prasanna,
prasanna.mohanasundaram@gmail.com

42. Print a file with line numbers

If you want a file with line numbers (say for printing), you can use the 'nl' command in Linux:

$ nl file.c

This prints the file with line numbers to standard output or this can be even redirected to a

file as shown below:

$nl file.c > output.txt

Here, output.txt will have the codes of file.c with each line having a line number.

--Phaniram Vallury,

ramvvs@gmail.com

43. Cut specific logs

If you need to cut specific logs from the complete log of any application, here is a tip that will

be of help.

Open the log file in a vi editor and set the editor to display the line number:

vi server.log

:set nu

The above process will provide you the line numbers in the logs. You can then search for

the specific string and note down the line number (e.g., 550). Now, note down the last line

number by using Shift+G (e.g., 780)

sed -n 550,780p server.log > threaddump.log

So the threaddump only contains lines from 550 to 780.

--Venkatesh R,

venka.2k@gmail.com

44. Reset ifconfig counters

As you can see, ifconfig keeps a couple of counters (RX/TX packets, RX/TX bytes, errors,

dropped, overruns, frames and carrier collisions). You can quickly spot if there is a problem by

just looking at the ifconfig counters:

#ifconfig eth0

eth0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx

inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:4233700943 errors:2 dropped:0 overruns:3 frame:5

TX packets:1917219659 errors:1 dropped:0 overruns:0 carrier:348

collisions:5753 txqueuelen:3000

RX bytes:1467520026 (1.3 GiB) TX bytes:2299240337 (2.1 GiB)

You can now find out what driver is being used by the network interface, which you want to

reset by using ethtool.

#ethtool -i eth0

driver: e100

version: 3.4.14-k4-NAPI

firmware-version: N/A

bus-info: 0000:01:07.0

# modprobe -r e100; modprobe e100; ifup eth0

This will reset all counters.

--Vinod Rana,

rana3807@gmail.com

45. Power yourself with Netstat

Here are a few uses of the netstat command that can help you.
To display the kernel interface table:

netstat -i

To display the kernel routing table:

netstat -rn

To display all open network sockets:

netstat -uta

To display network statistics:

netstat -s

--Prasanna,
prasanna.mohanasundaram@gmail.com

46. Finding the full path of the shell command

There is a command named which that takes one or more arguments as input. It prints to standard output the full path of the shell command. It does this by searching for an executable or script in the directories listed in the environment variable PATH:

[aarsh@localhost ~]$ which poweroff

/usr/bin/poweroff

If the command is not found, it gives the output shown below:

[aarsh@localhost ~]$ which moodule

/usr/bin/which: no moodule in (/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/aarsh/bin)

--Aarsh S Talati,
aarshstalati1989@gmail.com

47. Securing files

Here is a simple tip to password protect your files:

vi -x test

This command will ask for an encryption key. You have to type the key twice. Then save and quit the opened file.
Now, whenever you open this file, it will ask for that password first.

--Sumit Chauhan,
sumit1203@gmail.com

48. Uninstalling a package

To completely uninstall a package, first check the exact name of the package to be uninstalled by using the following command:

sudo dpkg --get-selections | grep package_name

The output of the above command will display the name of the package.
Once you know the package name, you can remove it by using the command shown below:

sudo apt-get remove --purge package_name

--Neeraj Joshi,
neeraj88joshi@gmail.com


49. How to check the date and time the system was rebooted

Here is a simple command to check the system's reboot date and time:

#last reboot

reboot system boot 2.6.18-53.el5 Sat Aug 6 18:02 (8+04:45)
wtmp begins Sat Aug 6 18:02:07 2011

The command below will give you the date and time the system was booted:

#who -b

system boot 2011-08-24 09:43

--Sumit Chauhan,
sumit1203@gmail.com

50. Reloading XWindows System

Sometimes, while working on a Linux-based computer, XWindows System doesn't respond very well. Here are the steps to reload XWindows System in a non-responsive Ubuntu system.

1. First open the command mode by pressing: Ctrl + Alt + F2
…and then entering your username and password.

2. Then run the following command:

top

Search the process named 'Xorg' in the list.
If it does not appear, wait for a few seconds. Then find the PID of the 'Xorg' process, listed at the extreme left of the output on the top.
You can also get the PID using the command given below:

pgrep Xorg

3. Now run the following command to kill the Xorg process:

sudo kill "PID"

…where PID is the process ID of Xorg.
This will reload the XWindows System.

Note: You must have root access to use these commands

--Indermohan Singh,
indermohansinghk7@gmail.com

51. Handling log files

Developers require a lot of testing after coding the software and they frequently need to handle the log files to identify the errors in the code of an application program. Given below are the steps to handle various log files that are being generated on a Linux system.

1. Clearing a log/text file:

$ >filename

The above command will clear all contents of the file.
2. To view the log/text file:

$ tail -f filename

This command will display the file contents as and when the log is written. It also displays the flow of the log.
3. To use more sophisticated tools on log files:

$ less filename

This will display the log file contents. You can also use the commands below while using less.
a. To refresh the log automatically, press:

"f"

after issuing the less command.

b. To stop refreshing the logs, press:

CTRL + C

c. To scroll up, press:

w

d. To scroll down, press:

d

e. To exit the mode, press:

CTRL+C

and then press

q

--Pranavam Siddharthan,
pranavam.s@gmail.com

52. Changing file names from upper case to lower

To manually change the case (upper to lower or vice versa) of a large number of files can be tedious. So, here is a script that can make life easy:

#to change uppercase filenames to lowercase
#!/bin/sh
if [ $# -eq 0 ] ; then
echo Usage: $0 Files
exit 0
fi
for f in $* ; do
g=`echo $f | tr "[A-Z]" "[a-z]"`
echo mv -i $f $g
mv -i $f $g
done

If you want to change the case from lower to upper, replace

g=`echo $f | tr "[A-Z]" "[a-z]"`

with

g=`echo $f | tr "[a-z]" "[A-Z]"`

in the script.

--Anil Awasare,
anil.awasare@gmail.com

Counting the number of files in a directory
Here is a simple command that can count the number of files in a directory (not the hidden ones):

echo * | wc -w

--Anil Awasare,
anil.awasare@gmail.com

53. Find and move files

You can find and move files in two steps. Step 1 enables you to find all files with .mp3 as the file extension. In Step 2, you can move them.
Step 1:

[narendra@ubuntu]$ find DIR_NAME -type f -iname "*.mp3"

./dir2/f4.mp3
./dir2/f3.mp3
./dir1/f2.mp3

Note: Replace DIR_NAME with your actual directory name.

In the above example, you are only finding the files (that's why you used -type f) that have extension '.mp3'. Now you can move these files by using the '-exec' option of the 'find' command.
Step 2:

[narendra@ubuntu]$ find DIR_NAME -type f -iname "*.mp3" -exec mv {} /tmp/mp3/ \;

Here, '{}' matches each filename which is found by the 'find' command. And '\;' is used to indicate the end of the command.
After executing this command, all mp3 files are moved into the '/tmp/mp3' directory.

--Narendra Kangralkar,
narendrakangralkar@gmail.com


54. Burning a DVD using the command line

Do you know that burning content using the command line onto a DVD on your Linux-based computer can be easy and fun? Here are the steps and commands that allow you to do so.

Burning an ISO image
You can download your favourite Linux distribution and type the following single command:

$ growisofs -dvd-compat -Z /dev/dvd=your_linux_image.iso

Here /dev/dvd is your DVD burner device.

Burning a non-ISO image
For burning non-ISO images onto a DVD, first create an ISO image of the data and then burn the ISO image on the disk:

$ mkisofs -r -o /tmp/my_stuff.iso ~/Desktop/My_Stuff/

$ growisofs -dvd-compat -Z /dev/dvd=/tmp/my_stuff.iso

There are several options of mkisofs and growisofs that can be explored to suit your requirement.
You can read the man pages for more details of the commands and their options.

--Dibyendu Roy,
diby.roy@gmail.com

55. Find out the elapsed time of a running process

There are a lot of processes running on your Linux system. Here is a command that will let you know how long the process has been running:

#ps -eo "%p %c %t"|grep "sshd"

In response to the above command, you will get the following output:

2850 sshd 172-01:37:22
29532 sshd 125-09:07:10

In the above command %p is pid, %c is command and %t is elapsed time.

--Ravikumar R,
ravikumar.raam@gmail.com


no image
  • Title : 55 Best Linux Tips, Tricks And Command Lines
  • Posted by :
  • Date : 19:35
  • Labels :
  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Top