Sunday 30 December 2018

Commands in Unix when things go wrong

Linux Commands, LPI Guides, LPI Tutorial and Material, LPI Study Materials

Terminals and keyboards has no uniform behavioral pattern. There are some commands to wriggle out of some common traps. You must know which keys you need to press when things don’t quite work as expected.

1. Backspacing doesn’t work: Consider you misspelled command stty as ssty and when you press backspace to erase the command, an unexpected symbol(^?) are printed:

$ stty^?^?^?^?^?^?^?

Backspacing is not working here, that’s why ^? is printed whenever we pressed backspace. To get rid of this unexpected behavior, we can use any of the following keys:

[Ctrl-c] OR [Delete]

2. Killing a line: If a command line contains many mistakes, a person will prefer to kill or erase the line instead of executing it. For this we have to press:

[Ctrl-u]

Above command kills everything in a line and returns the cursor to the beginning of the line.

3. Interrupt command: Sometimes a program goes into infinite loop. We can interrupt the program and bring back the prompt by using either of the following keys:

[Ctrl-c] OR [Delete]

This a important command and always recommended to use this in case of anything goes wrong.

NOTE: If delete works as a erase character on your machine then it doesn’t work as interrupt key at the same time.

4. Terminating command’s input: As we know that cat command require at least one filename as an argument. What will happen if we doesn’t give any file name as an argument and simply press enter:

$ cat
..

Nothing happens, terminal waits for us to enter something. To bring back the prompt, in case of commands that expect user input, either of the following keys are used:

[Ctrl-d] OR [Ctrl-c]

5. Lock Keyboard: For locking the keyboard, [Ctrl-s] command is used. After this you wouldn’t able to insert anything on the terminal. For restoring keyboard normal operation, [Ctrl-q] command is used.

6. When [Enter] key doesn’t work: Enter key is used to complete the command line or to run the command. If this doesn’t work, we can use any of the following commands:

[Ctrl-j] OR [Ctrl-m]

These keys generate the linefeed and carriage return characters respectively.

Linux Commands, LPI Guides, LPI Tutorial and Material, LPI Study Materials

Don’t surprised if some of the commands behaves differently in your system. Much of the Unix is configurable by the user, you can use stty command to change these settings.

Tuesday 18 December 2018

Run commands as root with sudo

Introduction


Linux follows the very tough permission model. A root user can do anything but normal user has no permissions. To run any command, they need to ask for permissions from the superuser. The easy and common way to grant administrative privileges to non-root users is, a user can use su command and temporarily become the root but users must know the root’s password. In corporate world this is very dangerous because all the privileges of root are granted to any user, who can do anything. For

Run Command, LPI Tutorial and Materials, LPI Guides, LPI Learning, LPI Certification

Example –

[userA@rhel7 ~]$ su -
Password:

It’s asking for the password of superuser.

To overcome above mentioned risk, sudo command comes in trend. It allows a user to run a command as a root or as any other user after providing the user’s own password for authentication. These information are defined in the /etc/sudoers file. Before describing “sudo” command I want to talk a bit about visudo

What is visudo


visudo is a command to edit configuration file for sudo command located at /etc/sudoers.You should not edit this file directly with normal editor, always use visudo for safety and security. Eiditing /etc/sudoers file requires superuser’s privileges.

visudo command cannot allow to edit /etc/sudoers file simultaneously by just locking the file and if someone tries to access the same it will get a message to try later.

[root@rhel7 ~]# visudo
visudo: /etc/sudoers busy, try again later

It also checks the syntax of edits and provide basic sanity checks which are very helpful. If it identifies any error, then visudo won’t allow to save the file with edits.

Set rules in sudoers file


A common question arises in everyone’s mind, how we define the rules in sudoers file? So, before editing it’s better to understand the existing configuration which defines which users can run what software on which machines. Syntax of pre-defined rule is given below –

root    ALL=(ALL:ALL) ALL

This allows root to run any command anywhere.Meaning of this line is –

username    hosts=(users:groups)    commands

ALL means, the user can run all commands on all hosts, as all users and groups. So, root has all the privileges to run any command as any user or group.

Let considered an example and provide ALL power to userA as root.

userA   ALL=(ALL:ALL)   ALL

If you wish to use command without password then use PASSWD parameter –

userA    ALL(ALL:ALL)    NOPASSWD:ALL

In below example userA only start, stop and restart the “httpd” service

userA   ALL=(root)      /usr/bin/systemctl, /usr/sbin/httpd start stop restart

User can check wether the command is working or not. Then follow the below procedure to check

[root@rhel7 ~]# su - userA
Last login: Thu Sep 13 15:01:18 EDT 2018 on pts/0
[userA@rhel7 ~]$ sudo -u root systemctl stop httpd
[sudo] password for userA:
[userA@rhel7 ~]$

Note – We can also use vim with visudo.

export VISUAL=vim; visudo

Using nano with visudo

export VISUAL=nano; visudo

Assign privileges to a group


You can asign similar privileges to multiple users just by making a group them. There is one predefined group is in sudoers file. Members of this group can use sudo to run any commands as any user, including superuser. We can add users to this group. It is normally configured like –

%wheel  ALL=(ALL)       ALL

Use command to add user in wheel group –

usermod -aG wheel username

Sunday 16 December 2018

less command in Linux with Examples

Less command is linux utility which can be used to read contents of text file one page(one screen) per time. It has faster access because if file is large, it don’t access complete file, but access it page by page.

less command, Linux Tutorial and Material, Linux Guides, Linux Certification, Linux Live

For example, if it’s a large file and you are reading it using any text editor, then the complete file will be loaded to main memory, but less command don’t load entire file, but load it part by part, which makes it faster.

syntax :


less filename

Note : I’m using dmesg output as input to less command in following examples.

For Example : If you want to read contents of dmesg command, it’s better to use it with less command

dmesg | less


Output :


[    0.000000] microcode: microcode updated early to revision 0x21, date = 2017-11-20
[    0.000000] random: get_random_bytes called from start_kernel+0x42/0x504 with crng_init=0
[    0.000000] Linux version 4.13.0-26-generic (buildd@lgw01-amd64-031) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)) #29~16.04.
2-Ubuntu SMP Tue Jan 9 22:00:44 UTC 2018 (Ubuntu 4.13.0-26.29~16.04.2-generic 4.13.13)
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.13.0-26-generic.efi.signed root=UUID=993a37f2-7ea9-43a3-b652-5b26bb879797 ro quiet splash vt.handoff=7
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Centaur CentaurHauls
[    0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
[    0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
[    0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
[    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256
[    0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format.
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000006efff] usable
[    0.000000] BIOS-e820: [mem 0x000000000006f000-0x000000000006ffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000070000-0x0000000000087fff] usable
[    0.000000] BIOS-e820: [mem 0x0000000000088000-0x00000000000bffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000000100000-0x0000000094d5ffff] usable
[    0.000000] BIOS-e820: [mem 0x0000000094d60000-0x0000000095d5ffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000095d60000-0x000000009a36efff] usable
[    0.000000] BIOS-e820: [mem 0x000000009a36f000-0x000000009aebefff] reserved
[    0.000000] BIOS-e820: [mem 0x000000009aebf000-0x000000009afbefff] ACPI NVS
[    0.000000] BIOS-e820: [mem 0x000000009afbf000-0x000000009affefff] ACPI data
[    0.000000] BIOS-e820: [mem 0x000000009afff000-0x000000009affffff] usable
[    0.000000] BIOS-e820: [mem 0x000000009b000000-0x000000009f9fffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000e0000000-0x00000000efffffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fe101000-0x00000000fe112fff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000feb00000-0x00000000feb0ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fec00000-0x00000000fec00fff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000fed00000-0x00000000fee00fff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000ffc00000-0x00000000ffffffff] reserved
[    0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000025f5fffff] usable
[    0.000000] NX (Execute Disable) protection: active
[    0.000000] e820: update [mem 0x93c97018-0x93ca7057] usable ==> usable
[    0.000000] e820: update [mem 0x93c97018-0x93ca7057] usable ==> usable
[    0.000000] e820: update [mem 0x93c8a018-0x93c96057] usable ==> usable
[    0.000000] e820: update [mem 0x93c8a018-0x93c96057] usable ==> usable
:


mostly used Options :



-E : causes less to automatically exit the first time it reaches end of file.
-f : forces non-regular file to open.
-F : causes less to exit if entire file can be displayed on first screen
-g : highlight the string which was found by last search command
-G : suppresses all highlighting of strings found by search commands
-i : cause searches to ignore case
-n : suppresses line numbers
-p pattern : it tells less to start at the first occurrence of pattern in the file
-s : causes consecutive blank lines to be squeezed into a single blank line

Command Usage with options :


dmesg | less -p "failure"

The above command tells less to start at first occurrence of pattern “failure” in the file.

Output :

[  368.748104] wlp2s0: failed to remove key (1, ff:ff:ff:ff:ff:ff) from hardware (-22)
[  372.254014] wlp2s0: authenticate with a0:55:4f:27:bd:01
[  372.257112] wlp2s0: send auth to a0:55:4f:27:bd:01 (try 1/3)
[  372.261055] wlp2s0: authenticated
[  372.264307] wlp2s0: associate with a0:55:4f:27:bd:01 (try 1/3)
[  372.270621] wlp2s0: RX AssocResp from a0:55:4f:27:bd:01 (capab=0x431 status=0 aid=199)
[  372.272312] wlp2s0: associated
[  372.357068] wlp2s0: Limiting TX power to 30 (30 - 0) dBm as advertised by a0:55:4f:27:bd:01
[  682.255302] wlp2s0: disassociated from a0:55:4f:27:bd:01 (Reason: 1=UNSPECIFIED)
[  682.304134] wlp2s0: failed to remove key (1, ff:ff:ff:ff:ff:ff) from hardware (-22)
[  685.809837] wlp2s0: authenticate with a0:55:4f:27:bd:01
dmesg | less -N

It will show output along with line numbers

Output :

1 [    0.000000] microcode: microcode updated early to revision 0x21, date = 2017-11-20
      2 [    0.000000] random: get_random_bytes called from start_kernel+0x42/0x504 with crng_init=0
      3 [    0.000000] Linux version 4.13.0-26-generic (buildd@lgw01-amd64-031) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)) #2      3 9~16.04.2-Ubuntu SMP Tue Jan 9 22:00:44 UTC 2018 (Ubuntu 4.13.0-26.29~16.04.2-generic 4.13.13)
      4 [    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.13.0-26-generic.efi.signed root=UUID=993a37f2-7ea9-43a3-b652-5b26bb879797 ro qu      4 iet splash vt.handoff=7
      5 [    0.000000] KERNEL supported cpus:
      6 [    0.000000]   Intel GenuineIntel
      7 [    0.000000]   AMD AuthenticAMD
      8 [    0.000000]   Centaur CentaurHauls
      9 [    0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
     10 [    0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
     11 [    0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
     12 [    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256

less -F filename

eg. less -F /home/mandeep/test/first.erl

It will not give any output, since file can be displayed in single screen.

less /home/mandeep/test/first.erl

These are contents of above tested file, it can be displayed on single screen.

-module(first).
-export([fib/1]).

fib(X) when X
    1;
fib(X) when X >= 2 ->
    fib(X - 1) + fib(X - 2).

References : wikipedia

Friday 14 December 2018

Cat command in Linux with examples

Cat Command, Linux Study Materials, Linux Certification, LPI Guides

Cat(concatenate ) command is very frequently used in linux.It reads data from file and give their content as output.It helps us to create,view,concatenate files.So let us see some frequently used cat commands.

1) To view a single file

Command:

$cat filename

Output

It will show content of given filename

2) To view multiple files

Command:

$cat file1 file2

Output

This will show content of file1 and file2.

3) To view contents of a file preceding with line numbers.

Command:

$cat -n filename

Output

It will show content with line number
example:-cat-n  geeks.txt
1)This is geeks
2)A unique array

4) Create a file 

Command:

$ cat >newfile

Output

Will create and a file named newfile

5) Copy the contents of one file to another file.

Command:

$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]

Output

The content will be copied in destination file

6) Cat command can suppress repeated empty lines in output

Command:

$cat -s geeks.txt

Output

Will suppress repeated empty lines in output

7) Cat command can append the contents of one file to the end of another file.

Command:

$cat file1 >> file2

Output

Will append the contents of one file to the end of another file

8) Cat command can display content in reverse order using tac command.

Command:

$tac filename

Output

Will display content in reverse order

9) Cat command can highlight the end of line.

Command:

$cat -E "filename"

Output

Will highlight the end of line

10) If you want to use the -v, -E and -T option together, then instead of writing -vET in the command, you can just use the -A command line option.

Command:

$cat -A  "filename"

Wednesday 12 December 2018

rm command in Linux with examples

rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file with two different names). By default, it does not remove directories.

rm Command, Linux Certification, Linux Tutorial and Material

This command normally works silently and you should be very careful while running rm command because once you delete the files then you are not able to recover the contents of files and directories.

Syntax:


rm [OPTION]... FILE...

Let us consider 5 files having name a.txt, b.txt and so on till e.txt.

$ ls
a.txt  b.txt  c.txt  d.txt  e.txt

Removing one file at a time
$ rm a.txt

$ ls
b.txt  c.txt  d.txt  e.txt

Removing more than one file at a time
$ rm b.txt c.txt

$ ls
d.txt  e.txt

Note: No output is produced by rm, since it typically only generates messages in the case of an error.

Options:


1. -i (Interactive Deletion): Like in cp, the -i option makes the command ask the user for confirmation before removing each file, you have to press y for confirm deletion, any other key leaves the file un-deleted.

$ rm -i d.txt
rm: remove regular empty file 'd.txt'? y

$ ls
e.txt

2. -f (Force Deletion): rm prompts for confirmation removal if a file is write protected. The -f option overrides this minor protection and removes the file forcefully.

$ ls -l
total 0
-r--r--r--+ 1 User User 0 Jan  2 22:56 e.txt

$ rm e.txt
rm: remove write-protected regular empty file 'e.txt'? n

$ ls
e.txt

$ rm -f e.txt

$ ls

Note: -f option of rm command will not work for write-protect directories.

3. -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will delete all the files and sub-directories recursively of the parent directory. At each stage it deletes everything it finds. Normally, rm wouldn’t delete the directories but when used with this option, it will delete.

Below is the tree of directories and files:

$ ls
A

$ cd A

$ ls
B  C

$ ls B
a.txt  b.txt

$ ls C
c.txt  d.txt

Now, deletion from A directory(as parent directory) will be done as:

$ rm *
rm: cannot remove 'B': Is a directory
rm: cannot remove 'C': Is a directory

$ rm -r *

$ ls


Every directory and file inside A directory is deleted.

4. –version: This option is used to display the version of rm which is currently running on your system.

$ rm --version
rm (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


Applications of wc Command


Delete file whose name starting with a hyphen symbol (-): To remove a file whose name begins with a dash (“-“), you can specify a double dash (“–“) separately before the file name. This extra dash is necessary so that rm does not misinterpret the file name as an option. Let say their is a file name -file.txt, to delete this file write command as:

$ ls
-file.txt

$ rm -file.txt
rm: unknown option -- l
Try 'rm ./-file.txt' to remove the file '-file.txt'.
Try 'rm --help' for more information.

$ rm -- -file.txt

$ ls

Friday 7 December 2018

mv command in Linux with examples

mv stands for move. mv is used to move one or more files or directories from one place to another in file system like UNIX. It has two distinct functions:

mv command in Linux, Linux Study Materials, Linux Guides, Linux Learning

(i) It rename a file or folder.
(ii) It moves group of files to different directory.

No additional space is consumed on a disk during renaming. This command normally works silently means no prompt for confirmation.

Syntax:


mv [Option] source destination

Let us consider 5 files having name a.txt, b.txt and so on till e.txt.

To rename the file a.txt to geek.txt(not exist):

$ ls
a.txt  b.txt  c.txt  d.txt

$ mv a.txt geek.txt

$ ls
b.txt  c.txt  d.txt  geek.txt

If the destination file doesn’t exist, it will be created. In the above command mv simply replaces the source filename in the directory with the destination filename(new name). If the destination file exist, then it will be overwrite and the source file will be deleted. By default, mv doesn’t prompt for overwriting the existing file, So be careful !!

Let’s try to understand with example, moving geeks.txt to b.txt(exist):

$ ls
b.txt  c.txt  d.txt  geek.txt

$ cat geek.txt
India

$ cat b.txt
geeksforgeeks

$ mv geek.txt b.txt

$ ls
b.txt  c.txt  d.txt

$ cat b.txt
India

Options:

1. -i (Interactive): Like in cp, the -i option makes the command ask the user for confirmation before moving a file that would overwrite an existing file, you have to press y for confirm moving, any other key leaves the file as it is. This option doesn’t work if the file doesn’t exist, it simply rename it or move it to new location.

$ ls
b.txt  c.txt  d.txt  geek.txt

$ cat geek.txt
India

$ cat b.txt
geeksforgeeks

$ mv -i geek.txt b.txt
mv: overwrite 'b.txt'? y

$ ls
b.txt  c.txt  d.txt

$ cat b.txt
India

2. -f (Force): mv prompts for confirmation overwriting the destination file if a file is write protected. The -f option overrides this minor protection and overwrite the destination file forcefully and delete the source file.

$ ls
b.txt  c.txt  d.txt  geek.txt

$ cat b.txt
geeksforgeeks

$ ls -l b.txt
-r--r--r--+ 1 User User 13 Jan  9 13:37 b.txt

$ mv geek.txt b.txt
mv: replace 'b.txt', overriding mode 0444 (r--r--r--)? n

$ ls
b.txt  c.txt  d.txt  geek.txt

$ mv -f geek.txt b.txt

$ ls
b.txt  c.txt  d.txt

$ cat b.txt
India

3. -n (no-clobber): With -n option, mv prevent an existing file from being overwritten.
In the following example the effect is for nothing to happen as a file would be overwritten.

$ ls
b.txt  c.txt  d.txt  geek.txt

$ cat b.txt
geeksforgeeks

$ mv -n geek.txt b.txt

$ ls
b.txt  c.txt  d.txt  geek.txt

$ cat b.txt
geeksforgeeks

4. -b(backup): With this option it is easier to take a backup of an existing file that will be overwritten as a result of the mv command. This will create a backup file with the tilde character(~) appended to it.

$ ls
b.txt  c.txt  d.txt  geek.txt

$ mv -b geek.txt b.txt

$ ls
b.txt  b.txt~  c.txt  d.txt

5. –version: This option is used to display the version of mv which is currently running on your system.

$ mv --version
mv (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Wednesday 5 December 2018

How to define and use functions in Linux Shell Script

Linux Tutorial and Material, Linux Study Materials, Linux Guides

In this article we’ll discuss more about functions and recipes. For demonstration purpose I’ll be using Bourne Again SHell(Bash) on Ubuntu machine.

Calling function


In Shell calling function is exactly same as calling any other command. For instance, if your function name is my_func then it can be execute as follows:

$ my_func

If any function accepts arguments then those can be provided from command line as follows:

$ my_func arg1 arg2 arg3

Defining function


We can use below syntax to define function:

 function function_name {
            Body of function
 }

Body of function can contain any valid command, loop constrain, other function or script. Now let us create simple function which displays message on screen.

 function print_msg {
       echo "Hello, World"
 }

Now let us execute this function:

 $ print_msg
 Hello, World

As expected, this function displays message on screen.

In above example we have created function directly on terminal. We can store this function in file as well. Below example demonstrates this.

 #! /bin/bash
 function print_msg {
       echo "Hello, World"
 }
 print_msg

We have defined this function inside function.sh file. Now let us execute this script:

 $ chmod +x function.sh
 $ ./function.sh
 Hello, World

If you observe, above output is exactly identical to previous one.

More about functions


In previous section we have defined very basic function. However during software development we need more advanced functions which can accept various parameters and return values. In this section we’ll discuss such functions.

Passing arguments to function

We can provide arguments to function same as other commands. We can access these arguments from function using dollar($) symbol. For instance, $1 represents first argument, $2 represents second argument and so on.

Let us modify above function to accept message as an argument. Our modified function will look like this:

 function print_msg {
       echo "Hello $1"
 }

In above function we are accessing first argument using $1. Let us execute this function:

 $ print_msg "LPICentral"

When you execute this function, it will generate following output:

 Hello LPICentral

Returning value from function

Like other programming languages, Bash provides return statement using that we can return value to the caller. Let us understand this with example:

function func_return_value {
      return 10
 }

Above function returns value 10 to its caller. Let us execute this function:

 $ func_return_value
 $ echo "Value returned by function is: $?"

When you execute above function, it will generate following output:

 Value returned by function is: 10

NOTE: In bash we have to use $? to capture return value of function

Function recipes


So far we got fair idea about bash functions. Now let us create some useful bash functions which can be used to make our lives easier.

Logger

Let us create logger function which will print date and time along with log message.

 function log_msg {
        echo "[`date '+ %F %T'` ]: $@"
 }

Let us execute this function:

 $ log_msg "This is sample log message"

When you execute this function, it will generate following output:

 [ 2018-08-16 19:56:34 ]: This is sample log message

Display system information

Let us create a function to display information about GNU/Linux system

 function system_info {
       echo "### OS information ###"
       lsb_release -a

       echo
       echo "### Processor information ###"
       processor=`grep -wc "processor" /proc/cpuinfo`
       model=`grep -w "model name" /proc/cpuinfo  | awk -F: '{print $2}'`
       echo "Processor = $processor"
       echo "Model     = $model"

       echo
       echo "### Memory information ###"
       total=`grep -w "MemTotal" /proc/meminfo | awk '{print $2}'`
       free=`grep -w "MemFree" /proc/meminfo | awk '{print $2}'`
       echo "Total memory: $total kB"
       echo "Free memory : $free kB"
 }

When you execute above function it will generate following output:

### OS information ###
No LSB modules are available.
Distributor ID:           Ubuntu
Description:   Ubuntu 18.04.1 LTS
Release:         18.04
Codename:    bionic

### Processor information ###
Processor = 1
Model     =  Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz

### Memory information ###
Total memory: 4015648 kB
Free memory : 2915428 kB

Find file or directory from current directory

Linux Tutorial and Material, Linux Study Materials, Linux Guides

Below function searches file or directory from current directory:

 function search {
      find . -name $1
 }

Let us search directory namely dir4 using below command:

 $ search dir4

When you execute above command, it will generate following output:

 ./dir1/dir2/dir3/dir4

Digital clock

Below function creates a simple digital clock on terminal

 function digital_clock {
            clear
            while [ 1 ]
            do
                  date +'%T'
                  sleep 1
                  clear
            done
 }

Creating library


Library is a collection of functions. To create library – define functions in a file and import that file in current environment.

Let us suppose we have defined all functions in utils.sh file then use below command to import functions in current environment:

$ source utils.sh

Hereafter you can execute any function from library just like any other bash command.

Tuesday 4 December 2018

bc command in Linux with examples

bc command is used for command line calculator. It is similar to basic calculator by using which we can do basic mathematical calculations.

bc command, Linux Tutorial and Material, Linux Guides, Linux Certification

Arithmetic operations are the most basic in any kind of programming language. Linux or Unix operating system provides the bc command and expr command for doing arithmetic calculations. You can use these commands in bash or shell script also for evaluating arithmetic expressions.

Syntax:


bc [ -hlwsqv ] [long-options] [ file ... ]

Options:


-h, {- -help } : Print the usage and exit
-i, {- -interactive } : Force interactive mode
-l, {- -mathlib } : Define the standard math library
-w, {- -warn } : Give warnings for extensions to POSIX bc
-s, {- -standard } : Process exactly the POSIX bc language
-q, {- -quiet } : Do not print the normal GNU bc welcome
-v, {- -version } : Print the version number and copyright and quit

The bc command supports the following features:

◈ Arithmetic operators
◈ Increment or Decrement operators
◈ Assignment operators
◈ Comparison or Relational operators
◈ Logical or Boolean operators
◈ Math functions
◈ Conditional statements
◈ Iterative statements

1. Arithmetic Operators


Examples:

Input : $ echo "12+5" | bc
Output : 17

Input : $ echo "10^2" | bc
Output : 100

How to store the result of complete operation in variable?

Example:

Input:
$ x=`echo "12+5" | bc`
$ echo $x
Output:17

Explanation: Stores the result of first line of input in variable x and then display variable x as $x.

2. Assignment Operators


The list of assignments operators supported are:

◈ var = value : Assign the vale to the variable
◈ var += value : similar to var = var + value
◈ var -= value : similar to var = var – value
◈ var *= value : similar to var = var * value
◈ var /= value : similar to var = var / value
◈ var ^= value : similar to var = var ^ value
◈ var %= value : similar to var = var % value

Examples:

Input: $ echo "var=10;var" | bc
Output: 10

Explanation: Assign 10 to the variable and print the value on the terminal.

Input: $ echo "var=10;var^=2;var" | bc
Output: 100

Explanation: Squares the value of the variable and print the value on the terminal.

How to store the result of complete operation in variable?

Example:

Input:
$ x=`echo "var=500;var%=7;var" | bc`
$ echo $x
Output:3

Explanation: Stores the result of 500 modulo 7 i.e. remainder of 500/7 in variable x and then display variable x as $x.

3. Increment Operators


There are 2 kinds of increment operators:

◈ ++var : Pre increment operator, variable is increased first and then result of variable is stored.
◈ var++ : Post increment operator, result of the variable is used first and then variable is incremented.

Examples:

Input: $ echo "var=10;++var" | bc
Output: 11
Explanation: Variable is increased first and then result of variable is stored.

Input: $ echo "var=10;var++" | bc
Output: 10

Explanation: Result of the variable is used first and then variable is incremented.

4. Decrement Operators


There are 2 kinds of decrement operators:

◈ – – var : Pre decrement operator, variable is decreased first and then result of variable is stored.
◈ var – – : Post decrement operator, result of the variable is used first and then variable is decremented.

Examples:

Input: $ echo "var=10;--var" | bc
Output: 9

Explanation: Variable is decreased first and then result of variable is stored.

Input: $ echo "var=10;var--" | bc
Output: 10

Explanation: Result of the variable is used first and then variable is decremented.

5. Comparison or Relational Operators


Relational operators are used to compare 2 numbers. If the comparison is true, then result is 1. Otherwise(false), returns 0. These operators are generally used in conditional statements like if.
The list of relational operators supported in bc command are shown below:

◈ expr1<expr2 : Result is 1 if expr1 is strictly less than expr2.
◈ expr1<=expr2 : Result is 1 if expr1 is less than or equal to expr2.
◈ expr1>expr2 : Result is 1 if expr1 is strictly greater than expr2.
◈ expr1>=expr2 : Result is 1 if expr1 is greater than or equal to expr2.
◈ expr1==expr2 : Result is 1 if expr1 is equal to expr2.
◈ expr1!=expr2 : Result is 1 if expr1 is not equal to expr2.

Examples:

Input: $ echo "10>5" | bc
Output: 1

Input: $ echo "1==2" | bc
Output: 0

6. Logical or Boolean Operators


Logical operators are mostly used in conditional statements. The result of the logical operators is either 1(TRUE) or 0(FALSE).

◈ expr1 && expr2 : Result is 1 if both expressions are non-zero.
◈ expr1 || expr2 : Result is 1 if either expression is non-zero.
◈ ! expr : Result is 1 if expr is 0.

Examples:

Input: $ echo "10 && 5" | bc
Output: 1

Input: $ echo "0 || 0" | bc
Output: 0

Input: $ echo "! 0" | bc
Output: 1

7. Mathematical Functions


The built-in math functions supported are :

◈ s (x): The sine of x, x is in radians.
◈ c (x) : The cosine of x, x is in radians.
◈ a (x) : The arctangent of x, arctangent returns radians.
◈ l (x) : The natural logarithm of x.
◈ e (x) : The exponential function of raising e to the value x.
◈ j (n,x) : The bessel function of integer order n of x.
◈ sqrt(x) : Square root of the number x. If the expression is negative, a run time error is generated.

In addition to the math functions, the following functions are also supported :

◈ length(x) : returns the number of digits in x.
◈ read() : Reads the number from the standard input.
◈ scale(expression) : The value of the scale function is the number of digits after the decimal point in the expression.
◈ ibase and obase define the conversion base for input and output numbers. The default for both input and output is base 10.
◈ last (an extension) is a variable that has the value of the last printed number.

Examples:

Input: 
$ pi=`echo "h=10;4*a(1)" | bc -l`
$ echo $pi
Output: 3.14159265358979323844

Explanation: Assign the value of “pi” to the shell variable pi. Here, a refers to the arctangent function, which is part of the math library loaded with the -l option.

Input: $ echo "scale($pi)" | bc -l
Output: 20

Explanation: Gives the number of digits after decimal point in value of “pi” calculated in previous example.

Input: $ echo "s($pi/3)" | bc -l
Output: .86602540378443864675

Explanation: Gives sine values at “pi/3” angle. Angle must be in radians. Here, s refers to the sine function

Input: $ echo "c($pi/3)" | bc -l
Output: .50000000000000000001

Explanation: Gives cosine values at “pi/3” angle. Angle must be in radians. Here, c refers to the cosine function.

Input: $ echo "e(3)" | bc -l
Output:20.08553692318766774092

Explanation: Gives exponential^value as output.

Input: $ echo "l(e(1))" | bc -l
Output: .99999999999999999999

Explanation: Gives natural logarithm of the value i.e. w.r.t. base ‘e’.

Input: $ echo "obase=2;15" | bc -l
Output: 1111

Explanation: Convert Decimal to Binary.

Input: $ echo "obase=8;9" | bc -l
Output: 11

Explanation: Convert Decimal to Octal.

Input: $ echo "ibase=2;1111" | bc -l
Output: 15

Explanation: Convert Binary to Decimal.

Input: $ echo "ibase=2;obase=8;10" | bc -l
Output: 2
Explanation: Convert Binary to Octal.

8. Conditional Statements


Conditional Statements are used to take decisions and execute statements based on these decisions. bc command supports the if condition.

Syntax:

if(condition) {statements} else {statemnts}

Example:

Input: $ echo 'n=8;m=10;if(n>m) print "n is greater" else print "m is greater" ' | bc -l
Output: m is greater

9. Iterative statements


bc command supports the for loop and while loop for doing iterations.

Syntax:

for(assignment; condition; updation)
{
      statements.....
      .......
      ........
}

OR

while(condition)
{
      statements.....
      .......
      ........
}

Examples:

Input: $ echo "for(i=1; i<=10; i++) {i;}" | bc
Output:
1
2
3
4
5
6
7
8
9
10

Input: $ echo "i=1;while(i<=10) {i; i+=1}" | bc
Output:
1
2
3
4
5
6
7
8
9
10

Explanation: Both examples prints numbers from 1 to 10 using the respective looping syntax.
Some Pseudo Statements:

◈ break : This statement causes a forced exit of the most recent enclosing while statement or for statement.
◈ continue : The continue statement (an extension) causes the most recent enclosing for statement to start the next iteration.
◈ halt : The halt statement (an extension) is an executed statement that causes the bc processor to quit only when it is executed. For example, “if (0 == 1) halt” will not cause bc to terminate because the halt is not executed.
◈ return : Return the value 0 from a function. (See the section on functions).
◈ return(expression) : Return the value of the expression from a function. (See the section on functions). As an extension, the parenthesis are not required.
◈ limits : Print the local limits enforced by the local version of bc. (This is an extension).
◈ quit : When the quit statement is read, the bc processor is terminated, regardless of where the quit statement is found. For example, “if (0 == 1) quit” will cause bc to terminate.
◈ warranty : Print a warranty notice. (This is an extension).

10. Functions : Functions provide a method of defining a computation that can be executed later. Functions in bc always compute a value and return it to the caller. Function definitions are “dynamic” in the sense that a function is undefined until a definition is encountered in the input. That definition is then used until another definition function for the same name is encountered. The new definition then replaces the older definition.

Syntax:

define name (parameters)
{
        statements.......
        .......
        ........
        return statement

}

11. We can write our arithmetic expressions in a file and then execute those statements by providing the filename to the bc command.


Example:

Input :
$ cat >> example.txt
2+5;
var = 10*3
var
print var
quit

Press ctrl+D

$ bc example.txt

Output : 
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
7
30

TO AVOID SYSTEM GENERATED MESSAGE ON OUTPUT SCREEN, USE:

$ bc -q example.txt

Output :
7
30

12. Important Points:


1. Bc command treats the semicolon (;) or newline as the statement separator.
2. To group statements use the curly braces. Use with functions, if statement, for and while loops.
3. If only an expression is specified as a statement, then bc command evaluates the expression and prints the result on the standard output.
4. If an assignment operator is found. Bc command assigns the value to the variable and do not print the value on the terminal.
5. A function should be defined before calling it. Always the function definition should appear first before the calling statements.
6. If a standalone variable is found as a statement, bc command prints the value of the variable. You can also Use the print statement for displaying the list of values on the terminal.

Saturday 1 December 2018

tar command in Linux with examples

tar command, Linux Command, Linux Tutorial and Material

The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the Archive files. tar command in Linux is one of the important command which provides archiving functionality in Linux. We can use Linux tar command to create compressed or uncompressed Archive files and also maintain and modify them.

Syntax:

tar [options] [archive-file] [file or directory to be archived]

Options:

◈ -c : Creates Archive
◈ -x : Extract the archive
◈ -f : creates archive with given filename
◈ -t : displays or lists files in archived file
◈ -u : archives and adds to an existing archive file
◈ -v : Displays Verbose Information
◈ -A : Concatenates the archive files
◈ -z : zip, tells tar command that create tar file using gzip
◈ -j : filter archive tar file using tbzip
◈ -W : Verify a archive file
◈ -r : update or add file or directory in already existed .tar file

What is an Archive file?


An Archive file is a file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space.

Examples:


1. Creating an uncompressed tar Archive using option -cvf : This command creates a tar file called file.tar which is the Archive of all .c files in current directory.

$ tar cvf file.tar *.c

Output :

os2.c
os3.c
os4.c

2. Extracting files from Archive using option -xvf : This command extracts files from Archives.

$ tar xvf file.tar

Output :

os2.c
os3.c
os4.c

3. gzip compression on the tar Archive, using option -z : This command creates a tar file called file.tar.gz which is the Archive of .c files.

$ tar cvzf file.tar.gz *.c

4. Extracting a gzip tar Archive *.tar.gz using option -xvzf : This command extracts files from tar archived file.tar.gz files.

$ tar xvzf file.tar.gz

5. Creating compressed tar archive file in Linux using option -j : This command compresses and creates archive file less than the size of the gzip. Both compress and decompress takes more time then gzip.

$ tar cvfj file.tar.tbz example.cpp

Output :

$tar cvfj file.tar.tbz example.cpp
example.cpp
$tar tvf file.tar.tbz
-rwxrwxrwx root/root        94 2017-09-17 02:47 example.cpp

6. Untar single tar file or specified directory in Linux : This command will Untar a file in current directory or in a specified directory using -C option.

$ tar xvfj file.tar
or
$ tar xvfj file.tar -C path of file in directoy

7. Untar multiple .tar, .tar.gz, .tar.tbz file in Linux : This command will extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the above command will extract “fileA” “fileB” from the archive files.

$ tar xvf file.tar "fileA" "fileB"
or
$ tar zxvf file1.tar.gz "fileA" "fileB"
or
$ tar jxvf file2.tar.tbz "fileA" "fileB"

8. Check size of existing tar, tar.gz, tar.tbz file in Linux : The above command will display the size of archive file in Kilobytes(KB).

$ tar czf file.tar | wc -c
or
$ tar czf file1.tar.gz | wc -c
or
$ tar czf file2.tar.tbz | wc -c

9. Update existing tar file in Linux

$ tar rvf file.tar *.c
Output :

os1.c

10. list the contents and specify the tarfile using option -tf : This command will list the entire list of archived file. We can also list for specific content in a tarfile

$ tar tf file.tar

Output :

example.cpp

11. Applying pipe to through ‘grep command’ to find what we are looking for : This command will list only for the mentioned text or image in grep from archived file.

$ tar tvf file.tar | grep "text to find"
or
$ tar tvf file.tar | grep "filename.file extension"

12. We can pass a file name as an argument to search a tarfile : This command views the archived files along with their details.

$ tar tvf file.tar filename

13. Viewing the Archive using option -tvf

$ tar tvf file.tar

Output :

-rwxrwxrwx root/root       191 2017-09-17 02:20 os2.c
-rwxrwxrwx root/root       218 2017-09-17 02:20 os3.c
-rwxrwxrwx root/root       493 2017-09-17 02:20 os4.c

What are wildcards in Linux


Alternatively referred to as a ‘wild character’ or ‘wildcard character’, a wildcard is a symbol used to replace or represent one or more characters. Wildcards are typically either an asterisk (*), which represents one or more characters or question mark (?),which represents a single character.

Example :

14. To search for an image in .png format : This will extract only files with the extension .png from the archive file.tar. The –wildcards option tells tar to interpret wildcards in the name of the files
to be extracted; the filename (*.png) is enclosed in single-quotes to protect the wildcard (*) from being expanded incorrectly by the shell.

$ tar tvf file.tar --wildcards '*.png'

Note: In above commands ” * ” is used in place of file name to take all the files present in that particular directory.