Thursday, 16 August 2012

7 Linux read command examples for Shell scripting

                              Read Command Examples

read command is useful in scripts when reading an input from user. This read command is used when the script want to interact with user for his inputs.

read command syntax
read VARIABLE



Example1:Read a value from user input.
read VAR1

To display this value we have to use echo command.
echo $VAR1

Example2: Reading two words/variable/values at a time.
read VAR1 VAR2

Example3: Reading multiple values at a time.
read VAR1 VAR2 VAR3 VAR4

Example4: Read values in to an Array
read VAR1
ARR1=(VAR1)

to display first value in array use below command
echo ${ARR1[0]}

Example5: Read values from a command
read VAR1 VAR2 VAR3 << ( echo surendra kumar anne )
echo "Enter values are $VAR1 $VAR2 $VAR3"

Example6: Read user input and give some info to user what he have to give. For this use -p option to display some info when reading value.
read -p "Please enter 1 to 10 numbers: " VAL1

Note: You no need to echo command to display information to user, you can achieve that one using read -p option.

Example7: Read have inbuilt variable called REPLY. this is system variable which stores read value in to $REPLY.
read -p "Please enter a value"
echo "Enter value is $REPLY"



Linux/Unix sort command examples

This is a small tutorial on how to use sort command to soft a file. Sorting is very much useful when dealing with DB files, CSV, xls, log files in fact a text file to. By default sort command will sort according to alpha-bates. First sort tries to sort according to single character, if it finds the first character same in two lines, then it move on to sort second character. Suppose I have a following word list in a file

cat filename.txt

abc
cde
hij
klm
kle
ble

This will be sorted first with first char. When it finds both the char same in this example klm and kle start with same characters, so it tries to sort with third character which is different in them. The output of sort as below
sort filename.txt

abc

ble
cde
hij
kle
klm

Sort command syntax
sort filename.txt

Example1: Sort a given file according to alpha-bates
sort filename.txt

Example2: I have a file with host names in third column, how can i sort them according to this column?. Use -k for sorting according to column
sort -k3 filename.txt

The above command will sort according to third column.

Example3:I want to sort /etc/passwd file according to home directories but my sort is not working how can i sort them?. by default sort will take space/tabs as field separators. But in /etc/passwd file the field separator is : so we have to mention this one when sorting a file. This can be done with -t option
sort -t: -k6 /etc/passwd

Example4: I want to sort according to number, suppose i want to sort /etc/passwd file according to UID, use -n option to do that.
sort -n -t: -k3 /etc/passwd

Note: For example with out -n option sort will put 10 before 3 when it find this values, by default it will sort only first numerical char.

Example5: Sort the file and reverse the order
sort -r filename.txt

Example6: Some times its required to sort the file and display only uniq values.
sort -u filename

Note: though the values on other field are different this will not consider by -u option.

Example7: I want to sort a file according to my requirement and save it to a different file. Use -o option to save the sorted output to a file.
sort -o temp.txt filename.txt

You can now mix above options to get your sorting work done.


Linux Bash Shell auto completion and Wild cards


Bash is one of the best shell available in Linuix and Unix OS. This is due to its capabilities such as

history
shortcuts

Auto completion and file wild cards

command chaining

We already discussed about history and shortcuts in some older posts. Today we will see how we can use BASH Shell to do auto complete and file wild-cards.

Auto completion

When we are at prompt we can press press TAB to autocomplete commands, chang 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<press tab>

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 <tab> 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<tab>/sa<tab>/sm<tab> to open /etc/samba/smb.conf file.

vi /et<tab>

vi /etc/sa<tab>

vi /etc/samba/sm<tab>

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


Learn Linux/Unix Find command with 60+ Practical examples Part-II


Advanced finds command usages

Playing with basics find command options

For basic Linux Find command usage please click here Example31:find all the files which are with more than size 100MB and less than 1GB and the owner of the file is xyz and the file name is Adda.txt in /red folder
find /red –size +100M –size -1G –user xyz –iname adda.txt

Example32:find all the files with SGID for the group sales and with size exactly 100MB with file name as pass.txt under /opt
find /opt –size 100M –group sales –perm g+s –name pass.txt


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.

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


Caution: Be careful when using -exec option which you are going to learn below. This is very dangerous option which can change/remove anything if don’t use wisely. There are some instances when you want to execute commands on the found files with find command. -exec is the option used in find command to execute shell commands directly on found files. Let’s discuss this with a basic example.

Example34: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 xvfz 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
find /opt –name *.sh

Note: Sometimes the above command will not work properly because your shell will try to parse the * before find command is executed. So you have made * as not a special character or wild character. In order to understand more about this type of characters you have to learn RegExp. Please find our other posts on Basic RegExp and Advanced RegExp.

Example45:
find /opt –name \*.sh

Or
find /opt –name “*.sh”

Note: These two will work, because you negated your shell parsing * wild character.

Example46:Search for all the files which start with abc and ends with different extension in /opt folder
find /opt –name abc.\*

Example47:Search for files which start with red and ends with many names such as redhat, redtop, redsoap etc.
find / -name red\*

Example 48:How about search for files which always end with dump.
find / -name \*dump


Search for files in multiple locations


Search multiple locations using single find command We can accomplish searching multiple folders with single command without any options. Lets see below command.

Example49: Find abc.txt file in /opt and /var folder at a time
find /opt /var –name abc.txt

The above command will search in only two locations i.e. in /opt and /var Search multiple locations but not in particular location. Example50:Search in entire system expect /proc folder
find / -path /proc -prune -name cpuinfo

Let me explain above command. The -path variable to define the path of a location. And -prune combined with -path will say not to descend in to the mention path /proc

Example51:Search for abc.txt in /opt and /var expect in /var/tmp folder
find /opt /var -path /var/tmp -prune -name abc.txt

Find command OR –o operator


Find have OR operator to do multiple file searches at a time.

Example52:I want to search for abc.txt and hash.c file at a time. This can be achieved by using -o operator
find / -name abc.txt -o -name hash.c

Here when ever find command sees -o it just or the options on its left and right hand side.

Example53:How about i want to find two directories say opt and var how can i find them?
find / -type d - name opt -o -type d -name var

This above command may trow error. Try below syntax
find / -type d \( -name opt -o -name var \)

let me explain above command. () are used to combine two or more options in to one. So \( -name opt -o -name var \) are combined in to single option and are treated as directories as we given -type is d. We will see more about this operator in our coming post on find command advanced usage.


Linux find command ! Negation operator


Example54: Negation operator is useful for negating a search team. for example we want to find all the files with name abc.txt which don’t have 755 permissions
find . -type f ! -perm 755 -name abc.txt
In above command -perm 755 is negated so that all the files with name abc.txt is displayed expect file abc.txt with permissions 755.
Stay tuned our next tutorial on find command to do following things
1)Dont search in .cvs folder
2)Search for files with out owner
3)Search for zero size files
4)Search for files and don’t show me permission denied error
5)Searching in hidden folders
6)Creating alias for frequently used find commands
7)Search for files with spaces
8)Search for files and grep for multiple pattern in one find command.
9) Miscellaneous examples




Learn Linux/Unix Find command with 60+ Practical examples Part-I

60+ Practical find commands examples with explanation

find command is very much powerful command which can do good work when its needed to find files with conditions. Find command is useful when finding files with complex requirement such as size, permissions etc. Suppose we want to find a file which is a regular file and its size is more than 1GB and its accessed more than 90 days back and its owner is none and then delete it. This entire requirement is done with single command without even writing a shell script. Let’s see how we can use find command from basics to advanced in this post.

find command can find files according to
1) File names

2) File types

3) Permissions

4) Owners

5) Modified date and time

6) Size

Advanced finds command usages
1) Mix of all the above things
2) AND operator
3) Search for files and execute commands on them
a) chown, chmod, grep, ls, rm, mv, cp,md5sum

4) Multiple execute commands

5) Search for multiple files
a) With different files with same extension
b) Same file With different extensions
6) Search in multiple locations
a) Exclude one location
b) Search in multiple locations

7) OR( –o ) operator
8) ! Negation operator
9) Linux find command with Regular Expressions
10) Linux find commnd practical examples


Basics of file/folders search using find command
find files with name

Syntax:

find path options filename

Example1: find all the files in /home with name test.txt. Here –name is used to specify the filename.
find /home –name test.txt

Example2: find the files whose name is test.txt and in present working directory
find . –name test.txt

Or
find –name test.txt

Example3: find all the files whose name contains both capital letters and small letters in it.
find /home –iname test.txt

-iname option is used to mention ignore the case sensitivity of a file.

Search for files depending on their File types:

Example4: Search for only directories whose name is var in / directory
find / -type d –name var

Example5: Search for an mp3 files whose name is temp.mp3
find / -type f –name temp.mp3

Below are the file types supported by find command, to know more about file types in Linux/Unix please have a look at our other post on File types.Sl.no Symbol Type
1 f Regular file
2 d Directory file
3 b Block file
4 c Character file
5 p Pipe file
6 l Symbolic link file
7 s Socket file.

Search for files depending on their Permissions

Example6:Search for a file name test.txt and its permissions are 775 in a given box
find / -perm 775 –name test.txt

Example7: How about searcing files with SUID bit set and file permissions are 755?
find / -perm 4755

Example8:How can i find SGID bit set files with 644 permissions?
find / -perm 2644

Example9: How can i find Sticky bit set files in my system with permissions 551?
find / -perm 1551

Example10:Search for all the files whose SUID bit is set
find / -perm /u=s

Example11: Search for all the files whose SGID bit is set
find / -perm /g+s

Note: We can use = or + interchangeably to check if a permissions is set or not as shown in above two examples.

Example12: Search for all the files whose StickyBit is set
find / -perm /o=t

Example13: Search for all the files whose owener permissions is read only.
find / -perm /u=r

Example14:Search for all the files which have user, group and others with executable permissions
find / -perm /a=x

To know about more on the permissions you have look at our other posts on chmod command.

Search according to Owners and group owners.

Example15: Search for all the files with name test.txt and the owner of this file is Surendra
find / -user Surendra –name test.txt

Example16: find all the files whos name is test.txt and owned by a group called redcluster
find / -group redcluster –name test.txt

to know more about owners and groups you have to look at our previous post on chown command.

Search according to Modified date and time.


Below is the matrix which give you brief idea on how to search according to modified date, accessed date etc.Sl. No -ctime -mtime -atime
+90 File status changed more then 90 days back Modified more than 90 days back Accessed more than 90 days back
90 File status changed exactly 90 days back Modified exactly 90 days back Accessed exactly 90 days back
-90 File status changed less than 90 days Modified less than 90 days Accessed less than 90 days back


Example17: Search for a file: test.txt whose file status is changed more than 90 days back
find / -ctime +90 –name test.txt

Example18: Search for all the files which are modified exactly 90 days back
find / -mtime 90

Example19: Search for all the files with name test.txt which is accessed less than 90 days
find / -atime -90

Example20: find all the files which are modified more than 90 days back and less than 180 days
find / -mtime +90 –mtime -180

Below is the matrix which gives you brief idea on how to search according to modified time, accessed time in minutes etc.Sl. No -cmin -mmin -amin
+30 File status changed more then 30 mins back Modified more than 30 mins back Accessed more than 30 mins back
30 File status changed exactly 30 mins back Modified exactly 30 days back Accessed exactly 30 mins back
-30 File status changed less than 30 mins Modified less than 30 mins Accessed less than 30 mins




Example21: find all the files changed less than 30mins
find / -cmin -30

Example22: find all the files modified exactly 30 mins back
find / -mmin 30

Example23: find all the files accessed more than 30 mins back
find / -amin +30

Example24: find all the files which are modified more than 5mins back and less than 25mins
find / -mmin +5 –mmin -25

Example25: I have new file called test.txt which is just created, now I want to get all the files which are created later this file creation.
find / -newer test.txt


Search for files/folders depending on the size with –size option


Sl.no +10 10 -10
c for bytes(8 bits) Search for files more than 10c size Search for files exactly 10b size Search for files less than 10b size
k for kilobytes Search for files more than 10k size Search for files exactly 10k size Search for files less than 10k size
M for Megabytes Search for files more than 10M size Search for files exactly 10M size Search for files less than 10M size
G for Gigabytes Search for files more than 10G size Search for files exactly 10G size Search for files less than 10G size




Example26: Search for files whose size is more than 10bytes
find / -size +10c

Example27: Search for files which are exactly 10kb in /opt folder
find /opt –size 10k

Example28: Search for files which are less than 10MB in /var folder
find /var –size -10M

Example29: Search for files which are more than 1GB size in /usr folder
find /usr –size +1G

Example30: find all the empty files in my system
find / -size 0k

In next post on find command we will see more depth in to this find command. Please click here for more advanced Linux Find command usage



BASH_History_Capabalities


Though this is a basic topic known to many of you, But I want to share so that some one will get new things.
BASH(Broune Again Shell) is the default shell in Linux, which will act as a communicator between Kernel and user. Its having so many capabilities such as

a.Short cuts
b.Command chaining
c.History

As I mention we will see all about BASH shell history capabilities here. And I have divided this BASH capabilities in to three parts like basics, medium and advanced.

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 example
root@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 showing service 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).

Advanced capabilities of Bash history:
History Modifiersreferences:
http://linux.about.com/od/commands/l/blcmdl3_history.htm
http://www.linuxtopia.org/online_books/redhat_linux_debugging_with_gdb/using-history-interactively.html
http://docstore.mik.ua/orelly/linux/lnut/ch08_06.htm
http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/


RHEL 4 vs RHEL 5


Major differences between RHEL4 & RHEL5
1.Virtualization added in RHEL5
2.Package installation method changed to YUM(Yellow-dog Updater Modifier) in RHEL5
3.Redhat Clustring added in RHEL5
4.RHEL5 now supports unlimited RAM and Hard disk
5.Now varients in RHEL5 are
* Red Hat Enterprise Linux Advanced Platform (former AS)
* Red Hat Enterprise Linux (former ES) (limited to 2 CPU-s)
* Red Hat Enterprise Linux Desktop with Workstation and Multi-OS option
* Red Hat Enterprise Linux Desktop with Workstation option (former WS)
* Red Hat Enterprise Linux Desktop with Multi-OS option
* Red Hat Enterprise Linux Desktop (former Desktop)

Where as in RHEL4
* Red Hat Enterprise Linux AS for mission-critical/enterprise computer systems
* Red Hat Enterprise Linux ES for supported network servers
* Red Hat Enterprise Linux WS for technical power-user desktops or high- performance computing
* Red Hat Desktop ΓÇô for multiple deployments of single-user desktops

Minor differences
1.Newer kernel(2.6.18)
2.Newer softwares packages
3.Office removed from Server edition
4.More Hardware support
5.By default netconfig command is not there, we have to install it manually
6.SELinux improved a lot in RHEL5
7.We have to provide key at the time of installation so that we can choose to install most of the packages such as Clustring/Virtualization.
8.Some of the commands changed like in LVM

References
https://www.redhat.com/docs/manuals/enterprise/RHEL-5-manual/release-notes/package-changes.html
http://en.wikipedia.org/wiki/Red_Hat_Enterprise_Linux