Everything Shell Script

  • Home
  • Home

Thursday, 29 June 2017

How to install Docker and run Docker containers on Linux Mint 18/18.1

 Niraj Bhagchandani     05:31:00     Docker in Mint 18.1, Docker Linux Mint, Install Docker, Install Docker in Mint 18, Install Docker Linux Mint     No comments   


https://c1.staticflickr.com/8/7336/14098888813_1047e39f08.jpg    https://c1.staticflickr.com/3/2543/3844521796_c8945ea740_z.jpg?zz=1

How to install Docker and run Docker containers on Linux Mint 18/18.1

So, now before going through all the steps update and upgrade the required repositories and applications.


Step A.


sudo apt-get update # Enter the Password if asked
sudo apt-get upgrade # Enter the Password if asked


Step 1.


# Let us import the GPG key.


sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 \
  --recv-keys 58118E89F3A912897C070ADBF76221572C52609D


Step 2.


# Add repository to the package manager, thus point to official
# Docker Repository


sudo apt-add-repository 'deb https://apt.dockerproject.org/repo ubuntu-xenial main'


Step 3.


# Now, Let us update the Repository...


sudo apt update


Step 4.


# Installing all the unmet dependencies and fix certain issues if any
# before installation


sudo apt install linux-image-generic linux-image-extra-virtual


Step 5.


# As we have updated the linux image and other extra stuffs it is
#recommended to reboot the system to take effect in kernel.


sudo reboot


Step 6.


# Finally, we come to the point… Let’s Install Docker


sudo apt install docker-engine


Step 7.


# Voilla, its installed now let us Run a Docker container
# This container is just a test container, and it will run and exit


sudo docker run hello-world

For more queries and answer you can post your comments in my blog or youtube video.
  1. Blog: http://eshellscript.blogspot.com/2017/06/how-to-install-docker-and-run-docker.html
  2. Youtube Video: Comming Soon.
  3. Google Docs: https://docs.google.com/document/d/1BdOGIhxOeXuhJkeTPPnQBsZqa27JDYuLOILq4zmuT-c/edit?usp=sharing
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Sunday, 6 November 2016

Powerline – Adds Powerful Statuslines and Prompts to Vim Editor and Bash Terminal

 Niraj Bhagchandani     23:15:00     tecmint     No comments   

Powerline is a great statusline plugin for Vim editor, which is developed in Python and provides statuslines and prompts for many other applications such as bash, zsh, tmux and many more.


Features

  1. It is written in Python, which makes it extensible and feature rich.
  2. Stable and testable code base, which works well with Python 2.6+ and Python 3.
  3. It also supports prompts and statuslines in several Linux utilities and tools.
  4. It has configurations and decorator colors developed using JSON.
  5. Fast and lightweight, with daemon support, which provides even more better performance.

Powerline Screenshots

 

In this article, I will show you how to install Powerline and Powerline fonts and how to use with Bash and Vim under RedHat and Debian based systems.

Step 1: Installing Generic Requirements for Powerline

Due to a naming conflict with some other unrelated projects, powerline program is available on PyPI (Python Package Index) under the package name as powerline-status.
To install packages from PyPI, we need a ‘pip‘ (package management tool for installing Python packages). So, let’s first install pip tool under our Linux systems.

Install Pip on Debian, Ubuntu and Linux Mint

# apt-get install python-pip
 
Sample Output 

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Recommended packages:
python-dev-all python-wheel
The following NEW packages will be installed:
python-pip
0 upgraded, 1 newly installed, 0 to remove and 533 not upgraded.
Need to get 97.2 kB of archives.
After this operation, 477 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty-updates/universe python-pip all 1.5.4-1ubuntu3 [97.2 kB]
Fetched 97.2 kB in 1s (73.0 kB/s)     
Selecting previously unselected package python-pip.
(Reading database ... 216258 files and directories currently installed.)
Preparing to unpack .../python-pip_1.5.4-1ubuntu3_all.deb ...
Unpacking python-pip (1.5.4-1ubuntu3) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up python-pip (1.5.4-1ubuntu3) ...

Install Pip on CentOS, RHEL and Fedora

Under Fedora-based systems, you need to first enable epel-repository and then install pip package as shown.

# yum install python-pip          
# dnf install python-pip                     [On Fedora 22+ versions] 
 
Sample Output 

Installing:
python-pip          noarch          7.1.0-1.el7             epel          1.5 M
Transaction Summary
=================================================================================
Install  1 Package
Total download size: 1.5 M
Installed size: 6.6 M
Is this ok [y/d/N]: y
Downloading packages:
python-pip-7.1.0-1.el7.noarch.rpm                         | 1.5 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : python-pip-7.1.0-1.el7.noarch                                 1/1 
Verifying  : python-pip-7.1.0-1.el7.noarch                                 1/1 
Installed:
python-pip.noarch 0:7.1.0-1.el7                                                
Complete!

Step 2: Installing Powerline Tool in Linux

Now it’s’ time to install Powerline latest development version from the Git repository. For this, your system must have git package installed in order to fetch the packages from Git.
# apt-get install git
# yum install git
# dnf install git
Next you can install Powerline with the help of pip command as shown.
# pip install git+git://github.com/Lokaltog/powerline
 
Sample Output 

Cloning git://github.com/Lokaltog/powerline to /tmp/pip-WAlznH-build
Running setup.py (path:/tmp/pip-WAlznH-build/setup.py) egg_info for package from git+git://github.com/Lokaltog/powerline
warning: no previously-included files matching '*.pyc' found under directory 'powerline/bindings'
warning: no previously-included files matching '*.pyo' found under directory 'powerline/bindings'
Installing collected packages: powerline-status
Found existing installation: powerline-status 2.2
Uninstalling powerline-status:
Successfully uninstalled powerline-status
Running setup.py install for powerline-status
warning: no previously-included files matching '*.pyc' found under directory 'powerline/bindings'
warning: no previously-included files matching '*.pyo' found under directory 'powerline/bindings'
changing mode of build/scripts-2.7/powerline-lint from 644 to 755
changing mode of build/scripts-2.7/powerline-daemon from 644 to 755
changing mode of build/scripts-2.7/powerline-render from 644 to 755
changing mode of build/scripts-2.7/powerline-config from 644 to 755
changing mode of /usr/local/bin/powerline-config to 755
changing mode of /usr/local/bin/powerline-lint to 755
changing mode of /usr/local/bin/powerline-render to 755
changing mode of /usr/local/bin/powerline-daemon to 755
Successfully installed powerline-status
Cleaning up...

Step 3: Installing Powerline Fonts in Linux

Powerline uses special glyphs to show special arrow effect and symbols for developers. For this, you must have a symbol font or a patched font installed on your systems.

Download the most recent version of the symbol font and fontconfig configuration file using following wget command.

# wget https://github.com/powerline/powerline/raw/develop/font/PowerlineSymbols.otf
# wget https://github.com/powerline/powerline/raw/develop/font/10-powerline-symbols.conf
 
Then you need to move the font to your fonts directory, /usr/share/fonts/ or /usr/local/share/fonts as follows or you can get the valid font paths by using command xset q.

# mv PowerlineSymbols.otf /usr/share/fonts/ 
 
Next, you need to update your system’s font cache as follows.

# fc-cache -vf /usr/share/fonts/ 
 
Now install the fontconfig file.

# mv 10-powerline-symbols.conf /etc/fonts/conf.d/ 
 
Note: If custom symbols doesn’t appear, then try to close all terminal sessions and restart X window for the changes to take effect.

Step 4: Setting Powerline for Bash Shell and Vim Statuslines

In this section we shall look at configuring Powerline for bash shell and vim editor. First make your terminal to support 256color by adding the following line to ~/.bashrc file as follows.

export TERM=”screen-256color” 

Enable Powerline on Bash Shell

To enable Powerline in bash shell by default, you need to add the following snippet to your ~/.bashrc file.
First get the location of installed powerline using following command.

# pip show powerline-status 
Name: powerline-status
Version: 2.2.dev9999-git.aa33599e3fb363ab7f2744ce95b7c6465eef7f08
Location: /usr/local/lib/python2.7/dist-packages
Requires:  
 
Once you know the actual location of powerline, make sure to replace the location in the below line as per your system suggested.

powerline-daemon -q
POWERLINE_BASH_CONTINUATION=1
POWERLINE_BASH_SELECT=1
. /usr/local/lib/python2.7/dist-packages/powerline/bindings/bash/powerline.sh
 
Now try to logout and login back again, you will see powerline statuesline as shown below.

 

Try changing or switching to different directories and keep a eye on “breadcrumb” prompt changes to show your current location.
You will also be able to watch pending background jobs and if powerline is installed on a remote Linux machine, you can notice that the prompt adds the hostname when you connect via SSH.

Enable Powerline for Vim

If vim is your favorite editor, luckily there is a powerful plugin for vim, too. To enable this plugin, add these lines to ~/.vimrc file.

set  rtp+=/usr/local/lib/python2.7/dist-packages/powerline/bindings/vim/
set laststatus=2
set t_Co=256
 
Now you can launch vim and see a spiffy new status line:



Summary

Powerline helps to set colorful and beautiful statuslines and prompts in several applications, good for coding environments. I hope you find this guide helpful and remember to post a comment if you need any help or have additional ideas.

Source: http://www.tecmint.com/powerline-adds-powerful-statuslines-and-prompts-to-vim-and-bash/

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Wednesday, 26 October 2016

Until Loop in Shell Script.

 Niraj Bhagchandani     10:43:00     No comments   

To run these programs online I request you to go to following link: https://goo.gl/xKheU3
  1. The until loop is similar to while loop in shell scripting. But the major difference in two of them is
  2. Until loop executes the statement executes its code-block while its conditional expression is false,
  3. While loop statement executes its code block while its conditional expression is true.
The syntax for until expression is:

syntax
until expression
do
    command1
    command2
    command3
    .
    .
    .
    commandN
done


The above syntax indicates
until do and done are keywords
expression is any conditional expression (termed here to be false)

filename: main.sh
Let us review one example for until loop.

until [[ $var -le 6 ]]
do
    echo "Variable: $var"
    (( var++ ))
done


Thank you for watching this article.

Regards
Niraj Bhagchandani.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

While Loop in Shell Script

 Niraj Bhagchandani     02:23:00     No comments   

To run the given example live online please visit the following link: https://goo.gl/mD69Gj

There are basic loops in shell script.
  1. For Loop
  2. While Loop
  3. Until Loop
But, In this tutorial we are going to cover While loop.

BASH WHILE LOOP

The Syntax for while loop is as follows:

Syntax
while expression
do
           command
done


In the above syntax I am trying to explain three major components of while loop

  1. keywords - while, do and done
  2. Expression is any expression which returns scalar values
  3. The statement will execute all the commands between do and done until the condition or expression is true.
filename: while1.sh

#!/bin/sh
a=0
while [ $a -lt 10 ]              #Spaces are very important here so keep that in mind.
do
         echo $a
         a=`expr $a + 1`
done


The another syntax for while loop can be termed in this following sequence.

while IFS= read -r line
do
     command 1;
     command 2;
     .
     .
     .
     command n;
done  < "path/to/dir/filename with or without space"


You may also write the following syntax.

while IFS= read -r field1 field2 field3 .... fieldN
do
         command1 on any one of the field
         command2 on any of the other fields.
         .
         .
         command3 on any of the fields
done < "path/to/dir/filename with or without space"


Here, IFS is used to set field separtor (by default the field seperator is white space). The -r option is to read command disables backslashes escaping such as \n,\t etc. This is failsafe while read loop for reading text files.

Here, IFS means Internal Field Separator. If time permits I will write a post for IFS too in future.

You may also use expression format in while Loop as.

filename: while2.sh
#!/bin/sh
a=0
while (( $a -lt 10 ))              #Spaces are very important here so keep that in mind.
do
         echo $a
         a=`expr $a + 1`
done


So, as we now know about the above loops let us take care of one more syntax which I have described above.
For that we need a file to read. Just look at the simplicity on how we can read the file in most efficient manner in Linux compared to C LANGUAGE.

Reading a text file using "space" as default field separator.

filename: while3.sh

#!/bin/sh
file="namefile"
while read -r f1 f2
do
      echo "Field 1 is: $f1 and Field 2 is: $f2"
done< "$file"



Reading a text file using separator field.

filename: while4.sh

#!/bin/sh
file="commafile"
while IFS="," read -r line
do
      echo $line
done < "$file"


Here, Internal Field Separator is set as "," with every comma it finds it will treat as a line and print its value.

Even though we have "," as a separator, the entire lines are printed because everytime it takes the entire line from the file and stores in in "line" variable.

Let us view one more example for IFS using ":" separator file. As we all know the information of all the users are stored in /etc/passwd file in linux.
So, Let us view that file using while loop

filename: while5.sh

#/bin/sh
file="/etc/passwd"
while IFS=: read -r user enpass uid gid desc home shell
do
    echo "User $user ($uid) it's home directory is $home with shell as $shell";
done < "$file"


With all lot of experience shared above, I can also term that different shell in linux have different while loop syntax.
Such as.

While Loop Syntax in ksh

while [[ condition ]]; do
    command-1
    command-2
    .
    .
    .
    command-n
done


While loop syntax in csh is as:

while ( condition )
    command
end;


Now, let us view the simple examples of the above syntax one by one.

KSH While Loop Example
filename: while6.sh 

#!/bin/ksh
index=1
while [[ $index -le 5 ]]; do
    echo "Welcome to e-shellscript: $index times";
    ((index++ ))
done


CSH While Loop Example

filename: while7.sh

#!/bin/csh
index=1
while ( $index <=5 )
    echo "Welcome to e-shellscript $index times";
    @ c = $c + 1
end


Let us view another example for c-shell

filename: while8.sh


#!/bin/csh
set yourname="eshellscript"
while ( $yourname != "" )
    echo -n "Enter your cute name: "
    set yourname = $<
    if ($yname != "" ) then
        echo "Hello , $yourname"
    endif
end
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Sunday, 9 October 2016

12 differnet examples of For Loop in Shell Script

 Niraj Bhagchandani     08:46:00     No comments   

The Link to programs given in this tutorial is:  https://goo.gl/akbcTw


Well, to begin with FOR LOOP, I would tell that there are 2 different types of For loop in shell script
  • First is using "in" keyword with list of values
  • C like syntax of FOR LOOP
 I would recommend you to bookmark this article for future use as, we are going to explore more about looping structure.

Procedure 1: For loop using "in" keyword and list of values.
The syntax for for loop using "in" keyword is:

for var in list
do
      command 1;
      command 2;
      command 3;
done

in the above example
  • for, in, do and done are the keywords
  • list is the collection set of values which can be used for looping. This collection of the loop is separated by space.
  • variable is any variable which will contain a single value from the list at a given point of time.
The above syntax tells that list will be stored in variable during each cycle one by one and then executed in the body of the loop between do-done clause. Say for example there are 10 items in a loop then the cycle will execute the loop 10 times and every time one item is picked and stored in var variable. 

Procedure 2: For loop with C-like Syntax.

This is the second type of the FOR LOOP, this contain three expression just like in c i.e. initialization, condition and increment.

for (( expr1; exp2; expr3 ))
do
      command 1;
      command 2;
      command 3;
done

Note here, the spaces between (( and )) because they have some significance over there.
  • Well, for starters, the initialization takes place during expr1 which will be evaluated at the beginning of the loop
  •  The second i.e. expr2 will deal with the condition checking and the loop will execute until the condition does not set itself to false.
  • The third i.e. expr3 will deal with the increment of the variable and thus fulfill the requirement of the looping structure.
Now Let us see the examples one by one.

Example 1: List of static values after "in" keyword

The code is feed with the list of items such as { Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto} (Space separated) and thus printed using for loop

Filename: for1.sh 

for planets in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto
do
    echo $planets
done

The above example would print the name of the planets one by one.

Example 2: Listing of the values using the variable after "in" keyword:
Now let us use the values stored in a variable and then execute the for loop

Filename: for2.sh

index=1;
planets="Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"

for planet in $planets
do
     echo "Planet $((index++)): $planet"
done

Example 3: Pass the values in the command line argument format during execution

This code is well defined by command line arguments that are to be passed during execution of the shell program and thus, make it easier.

Filename: for3.sh

index=1;
for planet
do
     echo "Planet $((index++)): $planet"
done

During Execution type:

sh-4.2 $ sh for3.sh Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto

Example 4: Use list in the form of a command as input to for loop

Here, the input for FOR LOOP  is ls command which is defined in back-tick and thus, the list of all the files that are present in current directory will be displayed one by one.

Filename: for4.sh

index=1;
for list in `ls`
do
     echo "The file/s $((index+++)) : $list"
done

Example 5: Using of wildcard as the list for getting filenames and directories.

In this example we will look after the wild character as the input to for loop in a list for that we are going to use astrick (*) sign.

Filename: for5.sh

cd ~
index=1;
for file in *
do
     echo "$((index++)) : $file"
done

Example 6: Break statement in the loop

We will have a detail discussion on if condition too but as of now please understand the code for FOR LOOP.


Filename: for6.sh

index=1;
planets="Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"

for planet in $planets
do
     if [ $index -eq 4 ]
     then
         break;
     fi
     echo "Planet $((index++)): $planet"
done

Example 7: Continue Statement in the for loop 

We will have a detail discussion on if condition too but as of now please understand the code for FOR LOOP.

Filename: for7.sh

index=1;
planets="Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto"

for planet in $planets
do
     if [ $index -eq 4 ]
     then
         index=`expr $index + 1`;
         continue;
     fi
     echo "Planet $((index++)): $planet"
done

Example 8.: C Like Syntax for FOR LOOP

This is from Procedure 2 syntax, thus let us print the numbers from 1 to 10 in FOR LOOP.

Filename: for8.sh

for (( index=1; index <10;  index++ ))
do
     echo "The number is: $index";
done

Example 9: Infinite Loop using FOR

Here, we are going to print the infinite series of number. But to stop printingg kindly press control + c so as to come out of  the infinite loop.

Filename: for9.sh

index=1
for (( ; ; ))
do
     echo "Number: $((index++))"
done  

Please press Control+C to come out of the loop

Example 10: More than 3 expression in BASH Script

Let us review the complexity of the FOR LOOP just like in C-Language.

Filename: for10.sh
 
for (( i=100, j=0; j<=50; i--,j+=5 ))
do
     echo "The value of i: $i and The value of j: $j";
done

Example 11: Give the range in FOR LOOP.

We, can also provide the range to for loop in a short and sweet manner using "in" keyword. This will make our work a lot easier and quite impressive.

Filename: for11.sh

for number in {1..15}    # Do not keep space inside {} brackets
do
     echo "The number: " $number
done


Example 12: Give the range in FOR LOOP With some increment after "in" keyword
Alongside the range, what if we want to increment the number by more than 1, then, here is the loop for increment too.

Filename: for12.sh

for number in {1..99..3}
do
     echo "The number: " $number
done

Also, do visit my blog to capture all shorts of shell programming during your study purpose.

Regards,
Niraj Bhagchandani.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Saturday, 8 October 2016

Escape character in Shell Script

 Niraj Bhagchandani     20:28:00     No comments   

Okay!! uptil now we have seen a lot of stuffs regarding Shell Script. But how about escape characters

Example: What if you want to display: Hi "Niraj" ?

So, if you type

sh-4.2 $ echo "Hi "Niraj"" 

The first and last " characters wrap the whole lot into one parameter passed to echo so that the spacing between the two word is kept as is.

But the above code would interpret as
  • "Hello "
  • World
  • ""
Thus, the output would be

Hello World

Please note here that we loose the double quotes entirely as they are not printed during this process.

Thus, if you want to display the quotes you are in great need of escape characters.

sh-4.2 $ echo "Hi \"Niraj\" "

Here the first Argument treated individual is
  •  " -> start of the string
  • Hi
  • \" is treated as "
  • Niraj
  • \" is again treated as "
  • " -> end of the string
 Now, Let us look for one more wildcards and understand.(Astrick *)

sh-4.2 $ echo *
first.sh  second.sh etc.... will list files in the current directory.
sh-4.2 $ echo *txt 
Will list all txt files in current directory
sh-4.2 $ echo "*" 
Will print * as a literal / string
 sh-4.2 $ echo "*txt"
*txt 

However, " i.e. double quotes, $ i.e. dollar, ` backtick and \ backslash are still interpreted by the shell, even when they are in double quotes.

Thus for displaying the above characters we need a backslash (\) as a special character. Thus, they wont be interpreted by the shell, but passed as a string and not as a command or wild character.

Let us see if we want an output as below:

A sentence with ", backslash \, backtick ` and few spaces are      alongwith dollar sign $

we would have to write a echo command as:

echo "
A sentence with \", backslash \\, backtick \` and few spaces are      alongwith dollar sign \$"

Thus, backslash itself must be escaped to show that it is to be taken literally.

Regards,
Niraj Bhagchandani
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Wildcards

 Niraj Bhagchandani     08:53:00     No comments   



 A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches.

Wildcards are commonly used in shell commands in Linux and other Unix-like operating systems. A shell is a program that provides a text-only user interface and whose main function is to execute commands typed in by users and display their results.

Wildcards are also used in regular expressions and programming languages. Regular expressions are a pattern matching system that uses strings (i.e., sequences of characters) constructed according to pre-defined syntax rules to find desired strings in text.

The term wildcard or wild card was originally used in card games to describe a card that can be assigned any value that its holder desires. However, its usage has spread so that it is now used to describe an unknown or unpredictable factor in a variety of fields.

Star Wildcard
 
Three types of wildcards are used with Linux commands. The most frequently employed and usually the most useful is the star wildcard, which is the same as an asterisk (*). The star wildcard has the broadest meaning of any of the wildcards, as it can represent zero characters, all single characters or any string.

As an example, the file command provides information about any filesystem object (i.e., file, directory or link) that is provided to it as an argument (i.e., input). Because the star wildcard represents every string, it can be used as the argument for file to return information about every object in the specified directory. Thus, the following would display information about every object in the current directory (i.e., the directory in which the user is currently working):
file *
If there are no matches, an error message is returned, such as *: can't stat `*' (No such file or directory).. In the case of this example, the only way that there would be no matches is if the directory were empty.
Wildcards can be combined with other characters to represent parts of strings. For example, to represent any filesystem object that has a .jpg filename extension, *.jpg would be used. Likewise, a* would represent all objects that begin with a lower case (i.e., small) letter a.
As another example, the following would tell the ls command (which is used to list files) to provide the names of all files in the current directory that have an .html or a .txt extension:
ls *.html *.txt
Likewise, the following would tell the rm command (which is used to remove files and directories) to delete all files in the current directory that have the string xxx in their name:
rm *xxx*
Question Mark Wildcard
 
The question mark (?) is used as a wildcard character in shell commands to represent exactly one character, which can be any single character. Thus, two question marks in succession would represent any two characters in succession, and three question marks in succession would represent any string consisting of three characters.
Thus, for example, the following would return data on all objects in the current directory whose names, inclusive of any extensions, are exactly three characters in length:
file ???
And the following would provide data on all objects whose names are one, two or three characters in length:
file ? ?? ???
As is the case with the star wildcard, the question mark wildcard can be used in combination with other characters. For example, the following would provide information about all objects in the current directory that begin with the letter a and are five characters in length:
file a????
The question mark wildcard can also be used in combination with other wildcards when separated by some other character. For example, the following would return a list of all files in the current directory that have a three-character filename extension:
ls *.???
Square Brackets Wildcard
 
The third type of wildcard in shell commands is a pair of square brackets, which can represent any of the characters enclosed in the brackets. Thus, for example, the following would provide information about all objects in the current directory that have an x, y and/or z in them:
file *[xyz]*
And the following would list all files that had an extension that begins with x, y or z:
ls *.[xyz]*
The same results can be achieved by merely using the star and question mark wildcards. However, it is clearly more efficient to use the bracket wildcard.
When a hyphen is used between two characters in the square brackets wildcard, it indicates a range inclusive of those two characters. For example, the following would provide information about all of the objects in the current directory that begin with any letter from a through f:
file [a-f]*
And the following would provide information about every object in the current directory whose name includes at least one numeral:
file *[0-9]*
The use of the square brackets to indicate a range can be combined with its use to indicate a list. Thus, for example, the following would provide information about all filesystem objects whose names begin with any letter from a through c or begin with s or t:
file [a-cst]*
Likewise, multiple sets of ranges can be specified. Thus, for instance, the following would return information about all objects whose names begin with the first three or the final three lower case letters of the alphabet:
file [a-cx-z]*
Sometimes it can be useful to have a succession of square bracket wildcards. For example, the following would display all filenames in the current directory that consist of jones followed by a three-digit number:
ls jones[0-9][0-9][0-9]
Now Let us view something more interesting:

sh-4.2 $ cp /tmp/a/* /tmp/b
sh-4.2 $ cp /tmp/a/.txt /tmp/b/
sh-4.2 $ cp /tmp/a/*.html /tmp/b

So, If you want to list the files from /tmp/a without using ls command then ls /tmp/a/? or you may also use echo /tmp/a*?

Also, if you want to rename a file using wildcard then

sh-4.2 $ mv *.txt *.bak

This will convert all txt files to bak file.

Regards,
Niraj Bhagchandani 





Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Older Posts Home

Amazon Ads

Popular Posts

  • 12 differnet examples of For Loop in Shell Script
    The Link to programs given in this tutorial is:  https://goo.gl/akbcTw Well, to begin with FOR LOOP, I would tell that there are 2 diff...
  • Powerline – Adds Powerful Statuslines and Prompts to Vim Editor and Bash Terminal
    Powerline is a great statusline plugin for Vim editor , which is developed in Python and provides statuslines and prompts for many other...
  • How to install Docker and run Docker containers on Linux Mint 18/18.1
         How to install Docker and run Docker containers on Linux Mint 18/18.1 So, now before going through all the steps update and upg...
  • While Loop in Shell Script
    To run the given example live online please visit the following link: https://goo.gl/mD69Gj There are basic loops in shell script. For ...
  • Until Loop in Shell Script.
    To run these programs online I request you to go to following link: https://goo.gl/xKheU3 The until loop is similar to while loop in shel...
  • Variables in Shell Script - Part 1
      Every Programming language in existence has the concepts of variable – It can be termed as a chunk of memory which can be assigned valu...
  • Escape character in Shell Script
    Okay!! uptil now we have seen a lot of stuffs regarding Shell Script. But how about escape characters Example: What if you want to displa...
  • Wildcards
     A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasin...
  • My First Shell Script.
    My First Shell script We are about to write a script for Hello World. After that we will have some flavors of Hello World Program. Fir...

Recent Posts

Categories

  • Docker in Mint 18.1
  • Docker Linux Mint
  • Install Docker
  • Install Docker in Mint 18
  • Install Docker Linux Mint
  • tecmint

Pages

  • Home

Text Widget

Blog Archive

  • ▼  2017 (1)
    • ▼  June (1)
      • How to install Docker and run Docker containers on...
  • ►  2016 (8)
    • ►  November (1)
    • ►  October (7)

Page Views:

About Me

Niraj Bhagchandani
View my complete profile

Search Here:

Powered by Blogger.

Sample Text

Copyright © Everything Shell Script | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates