Bash Scripting | Beginner Tutorial

Posted by in Articles, Bash Scripting, Programming

In this beginner tutorial I will show you some of the basics of shell script programming, and hopefully to introduce some of the possibilities of simple but powerful programming available under the shell.
Bash scripting is very useful, with simple commands you can do a lot, for example: create folder with name of current date , count lines, find words in files, search files and many more.

1)MKDIR

- mkdir dirname is bash command for creating folders
-dirname is the name of the directory that you wish to create

Example 1:

$ mkdir /home/ivan/hackspc.com/folder1
$ mkdir /home/ivan/hackspc.com/folder2
$ mkdir /home/ivan/hackspc.com/folder3

Example 2: Create folder with name of current date

mkdir $(date “+%d.%m.%Y”)

Example 3: Create folder with name of current day

$ mkdir $(date “+%A”)

Example 4: Create folders using brace expansion

$ mkdir /home/ivan/hackspc.com/{1..10}

$ mkdir /home/ivan/hackspc.com/project{1..10}

$ mkdir /home/ivan/hackspc.com/web{tools,masters,deseign}

Example 5: make all parent directories along with their children in a single command.

$ mkdir -p programming/BASHscripting/tutorials/StepByStep

$ mkdir -p programming/BASHscripting/tutorials/StepByStep is same as
~ $ mkdir programming
~ $ cd programming
~/programming $ mkdir BASHscripting
~/programming $ cd BASHscripting
~/programming/BASHscripting $ mkdir tutorials
~/programming/BASHscripting $ cd tutorials
~/programming/BASHscripting/tutorials/ $ mkdir StepByStep
~/programming/BASHscripting/tutorials/ $ cd StepByStep

As you can see above, mkdir with -p option can save you a lot of time an efforts.

2)WC (word count)

- wc displays a count of lines, words, and characters in a file.
-wc [options] [file]

-options: -c Count charachters
-l Count lines
-w Count words

- without options count Lines Words Characters
-in examples we’ll use file hackspc.txt as show below

Example 1: Count lines in hackspc.txt

$ wc -l hackspc.txt

Output:

Example 2: Count words in hackspc.txt

$ wc -w hackspc.txt

Output:

Example 3: Count characters in hackspc.txt

$ wc -c hackspc.txt

Output:

Example 4: Count lines, words, characters

$ wc hackspc.txt

Example 5: count number of lines, words, characters from hackspc.txt and save it into filecounting.txt

$ wc hackspc.txt>filecounting.txt

Output: filecounting.txt

3)GREP

-grep command is one of the easiest methods of locating text contained within a file
-grep [options] PATTERN [FILE...]
Options:
-v invert the sense of matching, to select non-matching lines
-i ignore case distinctions in both the PATTERN and the input files
-c Suppress normal output; instead print a count of matching lines for each input file
-E Interpret PATTERN as an extended regular expression
-q do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.

Let’s look .txt file called hackspc.txt

Example 1 : find word hack in file hackspc.txt,

$ grep hack hackspc.txt

Output will be all lines that contains word hack

This command is case sensitive, so hack isn’t same as Hack

Example 2: find lines that don’t contain word hack

$ grep hack hackspc.txt

Output:

Example 3 : find word hack in file hackspc.txt, command isn’t case sensitive

$ grep -i hack hackspc.txt

Output will be all lines that contains word hack, Hack, HACK,hAcK,HaCK…

Example 4 : find lines that contains words that end with g

$ grep -E “[a-z]+g” hackspc.txt

Output:

In this example I used regular expression , [a-z] all combinations of charachters a,b,c,d,e…z ,
+ repeat at least one or more. Regular expressions are very useful and complicated. In this beginner tutorial I will not write about them.

4)FIND

- Linux / Unix and their variants have several different ways of locating files find, locate, wheris. I mostly use find command. It offer a lot options and it can easly search for files that meet a desired criteria.
- find [option] [directory] [conditions]
-conditions: -name true if pattern matches the current file name
-type -d (find folders) , -f (find files)
-size
-mtime n True if the file’s data was modified n days ago.
-regex regular expression

Example 1: Search for any file named hackspc.txt in the directory home/ivan/hackspc.com/ and any subdirectory.

$ find /home/ivan/hackspc.com -name hackspc.txt

Output:

Example 2: search for any file that is larger then 2k

$ find /home/ivan/hackspc.com -name ‘*’ -size +2k

Output:

Example 3: search folders

$ find /home/ivan/hackspc.com -type d

Output:

Example 4: Search .txt files that contains only 4 digits (dddd)

$ find /home/ivan/hackspc.com -regex “.*/[0-9][0-9][0-9][0-9]\.txt”

Output:

5)IF, ECHO, VARIABLES

Example 1: if variable a equal to 5 then print five if not then write not five

$ if [ "$a" = 5 ]; then echo “five”; else echo “not five”; fi

Output :

Output :

Example 2: If you find file called hackspc.txt in diretcory /home/ivan/hackspc.com/ then print “I found file called hackspc.txt” if not then print “File Not Found”

$ if find /home/ivan/hackspc.com -name hackspc.txt
lled hackspc.txt”; else echo “File Not Found”; fi