Wednesday, July 29, 2015

Basic capabilities of BASH History:



Basic capabilities of BASH History:1.To see all the commands what we executed previously
#history

2.To check the history size of your system
#echo $HISTSIZE

3.To check where is your history file, which stores all your previous commands
#echo $HISTFILE

4.To browse history.Just press up/down arrow to browse history
5.To see all the commands which have particular word#history  | grep string
Example:#history | grep cd
Medium capabilities of Bash history:6.Some times browsing history is very tedious job and some times we are executing some big big commands so there is a capability in Bash to over come this ie search-i-reverse. For doing this press ctrl+r and type a string in previous command which you want to execute. 

Lets see it with an exampleroot@krishna-laptop:~#(reverse-i-search)`se': service winbind restart
if you see above I just pressed ctrl+r and then started to type se, it is showingservice winbind restart command, so I no need to type entire command and I have to justent press enter 

root@krishna-laptop:~# service winbind restart 
* Stopping the Winbind daemon winbind [ OK ] 
* Starting the Winbind daemon winbind [ OK ]
root@krishna-laptop:~#
7.Changing the size of history. Most of the Linux machines by default it can store up to 500 previously executed commands. Some people likes to change it to some value, here i want to keep my previously executed 3000 commands
#HISTSIZE=3000


8.to execute previous command#!!
or
!-1

9.To execute 25 command in bash history
#!25 


10.To execute a recent command which start with a string 
#!string

11.To clear all the history#HISTSIZE=0
or 
#history –c
12.In Linux when we execute some command there will be no output of the command, for example useradd or mount -a commands will not give you output saying that command is executed successfully or not at that time we can used the below command to see whether the previous command is executed successfully or not#echo $?If the out put of the above command is “0”, that indicates previous command executed successfully, for any other values the command is not executed successfully(total there are 256 values, 0-255).

Tuesday, July 28, 2015

Linux find command AND Operator !!

Linux find command AND Operator

By default find command will use AND option between two options. No need of mentioning any option. For example see below examples.
Example: find all the files which are more than 100MB and less than 1GB in size.
find / -size +100M -size -1G
or
find / -size +100M -a -size -1G
Like above we can combine many options and your find command use AND operator by default no need of mentioning -a option.

Search for files and execute commands on Found files

find a file with passwd.txt in /var folder and long list this file for checking file properties.
find /var -iname passwd.txt -exec ls -l {} ;
Let me explain above command. Up to find /var -iname passwd.txt, this command you are aware. This command will search for passwd.txt file in /var folder and give the paths and file names where this file is located.
-exec: with this option we are saying to find command to execute a command(here its ls -l) followed by this option
{} -This is used as input to the command which we get as files/folders from find command output.
; –This indicates that find command is completed.
Actually ; is a command chaining capability and it’s a special character. In order to negate this special character we are using before;.
Example35: Find all the files with name test.txt in /mnt and change the ownership of the files from Surendra to Narendra
find /mnt -user surendra -name test.txt -exec chown narendra: {} ;
-exec command {} ; –for executing a command on find files -inum -For finding a file with inode number
Example36:Find all the files with name test.sh in /abc folder and then grep if for word is there in that file or not
find /abc -name test.sh -exec grep ‘for’ {} ;
chmod, grep, ls, rm, mv, cp,md5sum
Example37: Find all the files with name xyz.txt owned by Surendra in /var/ftp/pub and change the permissions to 775 to them.
find /var/ftp -user surendra -name xyz.txt -exec chmod 775 {} ;
Example 38:Find all the files with name temp.txt in /xyz folder and backup then compress them to send it for saving
Find /xyz -name xyz.txt -exec tar cvfz temp.tar.gz {} ;
Example39:Find files with name abc.txt in /home directory and take backup of each file before modifying it.
find /home -name abc.txt -exec cp {} {}.bkf ;
This above command will create files with .bkf extension whenever it finds abc.txt file.
Example40:Find files which are more than 1GB and not accessed for the past 6 months and delete them.
find / -size +1G -mtime +180 -exec rm -rf {} ;
Example41:Find all the files with executable permissions and display their checksum value
find / -perm /a=x -exec md5sum {} ;
Example42:find all the files with name abc.txt and owner as surendra then move them to /opt folder
find / -user surendra -name abc.txt -exec mv {} /opt/ ;
There are many other commands you can try your own. Some other commands which can work with -exec option is mv, md5sum etc.

Find command with multiple -exec option

Find command is capable of using multiple times using the same option(We seen it for finding files which are more than 200M and less then 1GB). In our previous examples we used -size to mention the size between two sizes. Similarly we can use -exec command multiple times.
Example43:Find files with abc.txt name in /opt directory change the owner permissions from Surendra to Narendra and change the permissions to 775
find /opt -user Surendra -name abc.txt -exec chown Narendra: {} ; -exec chmod 775 {} ;
Note: We can use this multiple -exec option more than two times.

Search for multiple files

Till this point if you observe we just search for single file. But sometimes there is a requirement to search for multiple files with single find command to save some valuable time. The following two examples we will see how to do that.Example44: Find all the commands which ends with .sh file extension in /opt folder

Linux Bash Shell short cuts !!



Frequently used shortcut keys

Ctrl+a – Bring back the courser to start of the bash prompt
Ctrl+c – Cancel the command before executing it
Ctrl+d – Logout from the Shell
Ctrl+e – Move the courser to end of the command
Ctrl+l – Clear the screen
Ctrl+r – Search the history reverse order
Ctrl+p – go to previous command in history
Ctrl+n – Go to next command in history
Ctrl+s – Suspend terminal output(Useful for long out commands )
Ctrl+z – suspend the command/send command running to background

 Less frequently used shortcut keys

Alt+. – To paste last used word from previous command
Ctrl + b – move backward one character
Alt + c – capitalize to end of word starting at cursor
Alt + d – delete to end of word starting at cursor
Ctrl + f – move forward one character
Ctrl + h – delete character before the cursor
Ctrl + k – delete all characters from cursor to the end of the command line
Alt + l – make lowercase from cursor to end of word
Ctrl + q – contunue the output after pressing ctrl+s
Ctrl + s – stops the output to the screen (for long running verbose command)
Alt + u – make uppercase from cursor to end of word
Ctrl + u – delete all characters from cursor to the start of the command line
Ctrl + w – delete from cursor to start of word, for each press this will delete one word at a time 
Ctrl + y – paste the word(s) which are cut by using ctrl+k or u or w
Ctrl + xx – move to and fro from cursor to start and vice-versa.

Auto completion

When we are at prompt we can press press TAB key to auto-complete commands, directories, removing files/directories etc. We will see these as examples below for better understanding.
Example1: Execute lsdiff command to see if any files are modified by using auto complete without executing entire command.
ls
Below commands are displayed to see which commands starts with ls
ls           lsb_release  lshw         lsof         lspgpot      
lsattr       lscpu        lsinitramfs  lspci        lss16toppm   
lsblk        lsdiff       lsmod        lspcmcia     lsusb   
Now if you type d then press  it will complete the lsdiff command as shown below because there is no other command which start with lsd name.
lsdiff
Example2: Directory structure autocompletion. Suppose I want to open smb.conf which is located in /etc/samba folder we can open this by just typing /et/sa/sm to open /etc/samba/smb.conf file.
vi /et
vi /etc/sa
vi /etc/samba/sm
vi /etc/samba/smb.conf

Wildcards


Example3: Bash support wildcards. Delete all the files which are .sh file extension by using *.
rm -rf *.sh
Example4: I want to list all the files which starts with file and ends with .txt and having one character in between them.
ls -l file?.txt
This command will list all the files such as file1.txt, filea.txt, filez.txt etc.
Example5: List all the files which ends with a or b or c and start with file.
ls -l file[abc]
This will list filea, fileb and filec
Example6: List all the files which end with a to z, how can I do that?
ls -l file[a-z]
lists the files from filea, fileb to filez.
Example7: Move all the files expect .sh file extention files from present directory to /opt.
mv !(*.sh) /opt
Please share your thoughts on this.

 History

!! – To execute previous executed command
!n – To execute nth previous executed command
!-n – To execute nth command from latest executed command
!char – To execute command which start with char

How to debug a Shell script in Linux !!!

source

Debugging a shell script is very essential to check your script for any errors before moving it into production. In this post we will see different debugging options useful for running your scripts without a flaw. When executing a shell script use sh command with below options to debug a shell script in effective way.  

Option -x: If you set this option, it will show you each line before it executes it. Make a note that comments will not be reported.
sh -x scriptname
Option -v: Echo’s each line as it is read. It’s a kind of verbose and even echo back commented lines as well.
sh -v scriptname
Note: A small difference between -x and -v is that -v echo’s the line as it is read (So it will even display comments too.), whereas -x flag causes each command to be echoed as it is executed.
sh -xv scriptname
Option -u: At times you use a variable without setting some value to it. If you use this flag it will give you the error saying "so and so" variable is not set before executing the script.
sh -u scriptname
Option -e: Exit the shell script if any error occurs. This option will stop the script to run further once the script encounters an error. Use full for debugging the first error itself when running big scripts…
sh -e scriptname
Option -n: Check for syntax errors. This option is verymuch usefull for new commers to check for if, for, while, case etc statements
sh -n scriptname
Note: This options will not give any error if the command you are running is not present in your machine.
Controlled debugging:
We can use set -x at start of your code in your shell script to enable debugging and use set +x to disable debugging.
 
Tips: 1) Always use vim editor to get the syntax error at the time of editing the script itself.
      2) Place brackets in a meaning full way using tabs.
      3) Try to read and understand the script before running a script. 4) Try to give as many comments as possible for better understanding of the script.
Debugging a shell script is very essential to check your script for any errors before moving it into production. In this post we will see different debugging options useful for running your scripts without a flaw. When executing a shell script use sh command with below options to debug a shell script in effective way.    
Option -x: If you set this option, it will show you each line before it executes it. Make a note that comments will not be reported.
sh -x scriptname
Option -v: Echo’s each line as it is read. It’s a kind of verbose and even echo back commented lines as well.
sh -v scriptname
Note: A small difference between -x and -v is that -v echo’s the line as it is read (So it will even display comments too.), whereas -x flag causes each command to be echoed as it is executed.
sh -xv scriptname
Option -u: At times you use a variable without setting some value to it. If you use this flag it will give you the error saying "so and so" variable is not set before executing the script.
sh -u scriptname
Option -e: Exit the shell script if any error occurs. This option will stop the script to run further once the script encounters an error. Use full for debugging the first error itself when running big scripts…
sh -e scriptname
Option -n: Check for syntax errors. This option is verymuch usefull for new commers to check for if, for, while, case etc statements
sh -n scriptname
Note: This options will not give any error if the command you are running is not present in your machine.
Controlled debugging:
We can use set -x at start of your code in your shell script to enable debugging and use set +x to disable debugging.
 
Tips: 1) Always use vim editor to get the syntax error at the time of editing the script itself.
      2) Place brackets in a meaning full way using tabs.
      3) Try to read and understand the script before running a script. 4) Try to give as many comments as possible for better understanding of the script.

All mount options !!!



Linux বিভিন্ন ধরনের Mount কমান্ড:
========================
1. Linux এর native পার্টিশন (ext3/ext4/xfs) মাউন্ট করার জন্য:
---------------------------------------------------------------------------
[root@serverX~ ]# mount /dev/sda1 /mnt
*** মাউন্ট করার আগে '[root@serverX~ ]# fdisk -l' কমান্ড দিয়া দেখে নিতে পারেন কোন পার্টিশন টা মাউন্ট করবেন।
2. Windows এর FAT-32 পার্টিশন মাউন্ট করার জন্য:
------------------------------------------------------------
[root@serverX~ ]# mount -t vfat /dev/sdb1 /mnt
3. DVD Drive মাউন্ট করার জন্য:
--------------------------------------
[root@serverX~ ]# mount /dev/sr0 /media
4. Pendrive মাউন্ট করার জন্য, প্রথমে দেখে নিতে হবে যে কি নাম পাইসে:
--------------------------------------------------------------------------------
[root@serverX~ ]# fdisk -l
[root@serverX~ ]# mount /dev/sdb1 /mnt
5. ISO File মাউন্ট করার জন্য:
---------------------------------
ISO File দুই ভাবে করা যায়। যদি কোনো DVD তে থাকে, তাহলে মাউন্ট করার জন্য:
[root@serverX~ ]# mount -t iso9660 -o loop /dev/sr0 /media
অথবা ISO যদি কোনো ফাইল হিসেবে (test.iso) থাকে:
[root@serverX~ ]# mount -t iso9660 -o loop test.iso /mnt
6. NFS Share মাউন্ট করার জন্য:
---------------------------------------
[root@serverX~ ]# mount -t nfs 192.168.11.X:/nfsdata /mnt
7. CIFS (samba) Share মাউন্ট করার জন্য:
----------------------------------------------------
[root@serverX~ ]# mount -t cifs 192.168.11.X:/samba /mnt
‪#‎Note‬ -1: এখানে মাউন্ট পয়েন্ট হিসেবে /mnt এবং /media করেছি। আপনি যেকোনো জায়গায় মাউন্ট করতে পারেন। যেমন, প্রথমে আমরা একটা মাউন্ট পয়েন্ট তৈরী করি তারপর আমরা মাউন্ট করব:
[root@serverX~ ]# mkdir /data
[root@serverX~ ]# mount /dev/sdb1 /data
#Note-2: আপনের কোন কোন পার্টিশন অথবা শেয়ার মাউন্ট করা আছে সেটা দেখার জন্য:
[root@serverX~ ]# mount


8. mount iso on windows :


>> mount -o loop /test/OracleLinux-R6-U1-Server-x86_64-dvd.iso /iso

Mount windows directory :

>> mount -t cifs -o username=administrator //192.168.1.58/Linux /image