Wednesday, 30 January 2019

enable and disable command in Linux

Linux Study Materials, LPI Guides, LPI Certification, LPI Tutorial and Materials

Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the the printers or classes.

Syntax For enable Command:


enable [-a] [-dnps] [-f filename][name ...]

Syntax For disable Command:


disable [-c] [-W] [ -r [ reason ] ] printer


Options:


◈ -a : List all builtins with the message of whether it is enabled or not.
◈ -c : It will cancel the requests which are currently printing.
◈ -d : Delete a builtin loaded with `-f’.
◈ -W : Wait until the request currently being printed is finished before disabling printer. This option cannot be used with the -c option. If the printer is remote then the -W option will be silently ignored.
◈ -r reason : It assigns a reason for the disabling of the printer(s).
◈ printer : The name of the printer to be enabled or disabled.
◈ -n: Disable the names listed, otherwise names are enabled.

Saturday, 19 January 2019

Some useful Linux Hacks

Linux is the best-known and most-used open source operating system. As an operating system, Linux is software that sits underneath all of the other software on a computer, receiving requests from those programs and relaying these requests to the computer’s hardware.

Linux Command, Linux Tutorial and Material, LPI Guides, LPI Certification

For the purposes of this page, we use the term “Linux” to refer to the Linux kernel, but also the set of programs, tools, and services that are typically bundled together with the Linux kernel to provide all of the necessary components of a fully functional operating system. Some people, particularly members of the Free Software Foundation, refer to this collection as GNU/Linux, because many of the tools included are GNU components. However, not all Linux installations use GNU components as a part of their operating system. Android, for example, uses a Linux kernel but relies very little on GNU tools.

Here are some of my favorites LINUX HACKS (in no particular order) :

1. Paste Code: Directly paste any code/text into terminal use Ctrl + Shift + v

2. Sharing Server:  You can make a quick HTTP server for sharing in Python 2 using following command:

python -m SimpleHTTPServer

3. Increase Speaker Volume: Increase the maximum volume of your speakers by a certain percentage (150 in this case) :

pactl set-sink-volume 0 150%

4. Fortune Messages: Make your terminal output you random fortune messages. On a ubuntu system do:

sudo apt-get install fortune

and add fortune to the last line of your .bashrc if you are using Bash (default in my linux distros ). If you love cows then you can install cowsay pipe the output of fortune to cowsay , add following line to the end of your .bashrc

fortune | cowsay

5. Starwars: Want to watch StarWars in terminal ? Just type:
telnet towel.blinkenlights.nl

6. Shorten Lengthy Command: Have a lengthy command and want to short it down ? Use aliases
Add all aliases to your .bashrc so that they remain permanent. Like we used sudo apt-get install to install packages we can slim it done by defining an alias in our bashrc like

alias install = 'sudo apt-get install'

After saving the file and restarting the terminal you can do things like

install cowsay fortune figlet

instead of typing

sudo apt-get install fortune figlet cowsay

You can do it for many commands with the syntax  alias short-name=’long command’.

# Notice there is no space between long command and equal sign ( = ) Plus some commands are pre-aliased like la is an alias for ls -a. You can this out by typing la in your terminal .

7. Ping and traceroute together: Want to ping and traceroute/tracert at the same time. Use mtr
Install it with

sudo apt-get install mtr

or if you aliased the command use install mtr .
Usage mtr

8. Download files: Want to download files with a specific pattern like:

File1 at http://example.com/files/file1.zip
File2 at http://example.com/files/file2.zip
Use wget ( comes pre-installed on most distros ) as following

wget Page on example.com

Wget can be used for crawling and other awesome automated stuff.

9. Download accelerator: Want a stable download accelerator use axel?
Install with sudo apt-get install axel
use as follows

axel -a

10. Enable Scroll lock: Scroll Lock is disabled by default in Linux Distros. Even though the Scroll Lock doesn’t serve much of a purpose in the modern day applications. But still we might need it for some purposes like toggling the back light on our illuminated keyboards. To enable scroll lock

xmodmap -e 'add mod3 = Scroll_Lock' link

11. Download files using the command line : This one is pretty common. wget is used to download files using the command line via HTP, HTTPS and FTP.

wget file.link
where file.link is the link from where the file has to be downloaded.

12. Download YouTube videos: You can also download YouTube videos straight from the command line using a package by the name of youtube-dl. First install the package using apt-get or pip

sudo apt-get install youtube-dl
OR
sudo pip install youtube-dl

Your package is ready to be used. You can now use it to download videos

youtube-dl Youtube-link

13. Change terminal look and feel: If you want to edit the way your terminal looks, or the font sizes and other features. You have two options. gsettings or dconf. In layman terms gsettings uses the command line whereas dconf uses a GUI called dconf-editor. First we need to install dconf.

sudo apt-get install dconf-tools
dconf-editor

14. Be a bond: You want to look like a badass hacker ? (Kind of)

Install hollywood, your terminal will split and look like you’re doing a lot of cool things

sudo apt-add-repository ppa:hollywood/ppa
sudo apt-get update
sudo apt-get install hollywood
sudo apt-get install byobu #Used to split your terminal, very useful

Launch byobu then hollywood (just type byobu and hollywood)

15. Current Weather: What about the knowing the current weather of any city around the world.

In your terminal, just type:

curl http://wttr.in/your_city_name

and voila, weather for next 3 days displayed elegantly in your terminal.

16. Same command at same time: Want to run a command you ran yesterday at a point of time?

Try Ctrl-R(recursive search) and then type a subset of characters in your command and boom you accessed a recent history without pressing key many times.

Thursday, 17 January 2019

diff command in Linux with examples

diff stands for difference. This command is used to display the differences in the files by comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in one file have is to be changed to make the two files identical.

diff Command, Linux Tutorial and Materials, LPI Study Materials, LPI Guides

The important thing to remember is that diff uses certain special symbols and instructions that are required to make two files identical. It tells you the instructions on how to change the first file to make it match the second file.

Special symbols are:

a : add
c : change
d : delete

Syntax :

diff [options] File1 File2

Lets say we have two files with names a.txt and b.txt containing 5 Indian states.

$ ls
a.txt  b.txt

$ cat a.txt
Gujarat
Uttar Pradesh
Kolkata
Bihar
Jammu and Kashmir

$ cat b.txt
Tamil Nadu
Gujarat
Andhra Pradesh
Bihar
Uttar pradesh

Now, applying diff command without any option we get the following output:

$ diff a.txt b.txt
0a1
> Tamil Nadu
2,3c3
< Uttar Pradesh
 Andhra Pradesh
5c5
 Uttar pradesh

Let’s take a look at what this output means. The first line of the diff output will contain:

◈ Line numbers corresponding to the first file,
◈ A special symbol and
◈ Line numbers corresponding to the second file.

Like in our case, 0a1 which means after lines 0(at the very beginning of file) you have to add Tamil Nadu to match the second file line number 1. It then tells us what those lines are in each file preceeded by the symbol:

◈ Lines preceded by a < are lines from the first file.
◈ Lines preceded by > are lines from the second file.
◈ Next line contains 2,3c3 which means from line 2 to line 3 in the first file needs to be changed to match line number 3 in the second file. It then tells us those lines with the above symbols.
◈ The three dashes (“—“) merely separate the lines of file 1 and file 2.

As a summary to make both the files identical, first add Tamil Nadu in the first file at very beginning to match line 1 of second file after that change line 2 and 3 of first file i.e. Uttar Pradesh and Kolkata with line 3 of second file i.e. Andhra Pradesh. After that change line 5 of first file i.e. Jammu and Kashmir with line 5 of second file i.e. Uttar pradesh.

Now let’s see what it looks like when diff tells us that we need to delete a line.

$ cat a.txt
Gujarat
Andhra Pradesh
Telangana
Bihar
Uttar pradesh

$ cat b.txt
Gujarat
Andhra Pradesh
Bihar
Uttar pradesh

$ diff a.txt b.txt
3d2
< Telangana

Here above output 3d2 means delete line 3rd of first file i.e. Telangana so that both the files sync up at line 2.

Options


diff Command, Linux Tutorial and Materials, LPI Study Materials, LPI Guides
Linux system offers two different ways to view the diff command output i.e. context mode and unified mode.

1. -c (context) : To view differences in context mode, use the -c option. Lets try to understand this with example, we have two files file1.txt and file2.txt:

$ cat file1.txt                                                                                                                           
cat                                                                                                                                             
mv                                                                                                                                             
comm                                                                                                                                           
cp

$ cat file2.txt
cat                                                                                                                                             
cp                                                                                                                                             
diff                                                                                                                                           
comm
                                                                                                 
$ diff -c file1.txt file2.txt                                                                                                             
*** file1.txt   Thu Jan 11 08:52:37 2018                                                                                                       
--- file2.txt   Thu Jan 11 08:53:01 2018                                                                                                       
***************                                                                                                                                 
*** 1,4 ****                                                                                                                                   
  cat                                                                                                                                           
- mv                                                                                                                                           
- comm                                                                                                                                         
  cp                                                                                                                                           
--- 1,4 ----                                                                                                                                   
  cat                                                                                                                                           
  cp                                                                                                                                           
+ diff                                                                                                                                         
+ comm

The first file is indicated by ***, and the second file is indicated by —.
The line with *************** is just a separator.

The first two lines of this output show us information about file 1 and file 2. It lists the file name, modification date, and modification time of each of our files, one per line.

The next line has three asterisks *** followed by a line range from the first file (in our case lines 1 through 4, separated by a comma). Then four asterisks ****. After that it shows the contents of the first file with the following indicators:

(i) If the line needs to be unchanged, it is prefixed by two spaces.
(ii) If the line needs to be changed, it is prefixed by an symbol and a space. The symbol means are as follows:

(a) + : It indicates a line in the second file that needs to be added to the first file to make them identical.
(b) – : It indicates a line in the first file that needs to be deleted to make them identical.

Like in our case, it is needed to delete mv and comm from first file and add diff and comm to the first file to make both of them identical.

After that there are three dashes — followed by a line range from the second file (in our case lines 1 through 4, separated by a comma). Then four dashes —-. Then it shows the contents of the second file.

2. -u (unified) : To view differences in unified mode, use the -u option. It is similar to context mode but it doesn’t display any redundant information or it shows the information in concise form.

$ cat file1.txt                                                                                                                           
cat                                                                                                                                             
mv                                                                                                                                             
comm                                                                                                                                           
cp

$ cat file2.txt                                                                                                                           
cat                                                                                                                                             
cp                                                                                                                                             
diff                                                                                                                                           
comm

$ diff -u file1.txt file2.txt                                                                                                           
--- file1.txt   2018-01-11 10:39:38.237464052 +0000                                                                                             
+++ file2.txt   2018-01-11 10:40:00.323423021 +0000                                                                                             
@@ -1,4 +1,4 @@                                                                                                                                 
 cat                                                                                                                                           
-mv                                                                                                                                             
-comm                                                                                                                                           
 cp                                                                                                                                             
+diff                                                                                                                                           
+comm

The first file is indicated by —, and the second file is indicated by +++.
The first two lines of this output show us information about file 1 and file 2. It lists the file name, modification date, and modification time of each of our files, one per line.

After that the next line has two at sign @ followed by a line range from the first file (in our case lines 1 through 4, separated by a comma) prefixed by – and then space and then again followed by a line range from the second file prefixed by + and at the end two at sign @. Followed by the file content in output tells us which line remain unchanged and which lines needs to added or deleted(indicated by symbols) in the file 1 to make it identical to file 2.

3. -i : By default this command is case sensitive. To make this command case in-sensitive use -i option with diff.

$ cat file1.txt
dog
mv
CP
comm

$ cat file2.txt
DOG
cp
diff
comm

Without using this option:
$ diff file1.txt file2.txt
1,3c1,3
< dog
< mv
 DOG
> cp
> diff

Using this option:
$ diff -i file1.txt file2.txt
2d1
 diff

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

$ diff --version
diff (GNU diffutils) 3.5
Packaged by Cygwin (3.5-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.

Friday, 11 January 2019

dos2unix and unix2dos commands

Sometimes, you will need to move files between windows and unix systems. Window files use the same format as Dos, where the end of line is signified by two characters, Carriage Return or CR or \r followed by Line Feed or LF or \n.

dos2unix, unix2dos commands, LPI Gudies, LPI Learning, LPI Tutorial and Material

Unix files, on the other hand, use only Line Feed (\n).

unix2dos is a tool to convert line breaks in a text file from Unix format (Line feed) to DOS format (carriage return + Line feed) and vice versa.

◈ dos2unix command : converts a DOS text file to UNIX format.
◈ Unix2dos command : converts a Unix text file to DOS format

Example


Task : Create a file in DOS or in notepad with following contents
hello everybody
welcome to unix
unix is easy

now copy this file in unix /home/lpi directory

$od –bc myfile.txt
0000000 150 145 154 154 157 040 145 166 145 162 171 142 157 144 171 015
          h   e   l   l   o       e   v   e   r   y   b   o   d   y  \r
0000020 012 167 145 154 143 157 155 145 040 164 157 040 165 156 151 170
         \n   w   e   l   c   o   m   e       t   o       u   n   i   x
0000040 015 012 165 156 151 170 040 151 163 040 145 141 163 171 015 012
         \r  \n   u   n   i   x       i   s       e   a   s   y  \r  \n
0000060

The CR-LF combination is represented by the octal values 015-012 and the escape sequence \r\n.

Note: The above output shows that this is a DOS format file.
Now convert DOS file to UNIX format by using dos2unix command

$dos2unix myfile.txt
$od –bc myfile.txt

Conversion of this file to UNIX is just a simple matter of removing the \r.
We can also convert UNIX file to DOS format by using unixsdos command

$unix2dos myfile.txt
$od –bc myfile.txt

After Conversion of this file to DOS, \r is added in DOS file.

Wednesday, 9 January 2019

Different Shells in Linux

Shells in Linux, Linux Certification, Linux Tutorial and Material, Linux Study Materials

SHELL is a program which provides the interface between the user and an operating system. When the user logs in OS starts a shell for user. Kernel controls all essential computer operations, and provides the restriction to hardware access, coordinates all executing utilities, and manages Resources between process. Using kernel only user can access utilities provided by operating system.

Types of Shell:


◈ The C Shell –

Denoted as csh

Bill Joy created it at the University of California at Berkeley. It incorporated features such as aliases and command history. It includes helpful programming features like built-in arithmetic and C-like expression syntax.

In C shell:

Command full-path name is /bin/csh,
Non-root user default prompt is hostname %,
Root user default prompt is hostname #.

◈ The Bourne Shell –

Denoted as sh

It was written by Steve Bourne at AT&T Bell Labs. It is the original UNIX shell. It is faster and more preferred. It lacks features for interactive use like the ability to recall previous commands. It also lacks built-in arithmetic and logical expression handling. It is default shell for Solaris OS.

For the Bourne shell the:

Command full-path name is /bin/sh and /sbin/sh,
Non-root user default prompt is $,
Root user default prompt is #.

◈ The Korn Shell

It is denoted as ksh

It Was written by David Korn at AT&T Bell LabsIt is a superset of the Bourne shell.So it supports everything in the Bourne shell.It has interactive features. It includes features like built-in arithmetic and C-like arrays, functions, and string-manipulation facilities.It is faster than C shell. It is compatible with script written for C shell.

For the Korn shell the:

Command full-path name is /bin/ksh,
Non-root user default prompt is $,
Root user default prompt is #.

◈ GNU Bourne-Again Shell –

Denoted as bash

It is compatible to the Bourne shell. It includes features from Korn and Bourbe shell.

For the GNU Bourne-Again shell the:

Command full-path name is /bin/bash,
Default prompt for a non-root user is bash-g.gg$
(g.ggindicates the shell version number like bash-3.50$),
Root user default prompt is bash-g.gg#. 

Sunday, 6 January 2019

Daily life Linux Commands

1. Clear the Terminal : In our daily life, we use to work on Terminal if we are using LINUX. Continuous working on terminal make terminal screen full with commands and for removing them and making our screen totally free of character, we often use clear command. Key combination ‘Ctrl+l‘ has the same effect as ‘clear‘ command. So from next time use ctrl+l to clear your Linux Command Line Interface.


Daily life Linux Commands, Linux Tutorial and Material, Linux Certification, Linux Guides

Note: Since ctrl+l is a key combination, so we can not use it inside a script. If we need to clear screen inside a shell script, we must have to call command ‘clear’.

2. Run command and get back to directory, together: This is also an amazing hack not known to many people. We may run a command no matter what it is and then return back to the current directory. For this, all we need to do is to run the command in parentheses i.e., in between ( and ).
For Example :

Input :

cd /home/shivam/Downloads/ && ls -l

Output :

-rw-r----- 1 shivam shivam    54272 May 3 18:37 text1.txt
-rw-r----- 1 shivam shivam    54272 May 3 18:37 text2.txt
-rw-r----- 1 shivam shivam    54272 May 3 18:37 text3.txt

Explanation : In the above command it first changed the current directory to Downloads and then list the content of that directory before returning back to the current directory.

3. Shortcut to Directories: You can create a shortcut to frequently accessed directories by adding them to the CDPATH environment variable. So, say If you frequently access “/var/www/html/”.
Instead of typing “cd /var/www/html”, you can add /var/www/ to CDPATH and then you have to type “cd html” only.
shivam:~> export CDPATH=$CDPATH:/var/www/
shivam:~> cd html
shivam:~:html>

4. Replacing words or characters:

◈ If you are working with any text file then to replace every instance of any word say “version” with “story” in myfile.txt, you can use sed command as:

# sed 's/version/story/g' myfile.txt

◈ Additionally, if you want to ignore character case then you may use gi instead of g as:

# sed 's/version/story/gi' myfile.txt

5. Here are some useful shortcuts which you may use while working on terminal:
Cursor Movement Control:

◈ Ctrl-a: Move cursor to the start of a line
◈ Ctrl-e: Move cursor to the end of a line
◈ Ctrl-Left/Right: Navigate word by word (may not work in all terminals)Modify Text:
◈ Ctrl-w: Delete the whole word to the left of the cursor
◈ Ctrl-k: Erase to end of line
◈ Ctrl-u: Erase to beginning of line

6. Run top in batch mode: ‘top’ is a handy utility for monitoring the utilization of your system. It is invoked from the command line and it works by displaying lots of useful information, including CPU and memory usage, the number of running processes, load, the top resource hitters, and other useful bits. By default, top refreshes its report every 3 seconds.
Mostly we run ‘top’ inside the terminal, look on the statistics for a few seconds and then graciously quit and continue our work.
Better yet, if we wants to run such a utility only for a given period of time, without any user interaction:

There are many possible answers:

◈ You could schedule a job via cron.
◈ You could run a shell script that runs ps every X seconds

Instead of going wild about trying to patch a script, there’s a much, much simpler solution:

top -b -d 10 -n 3 >> top-file

We have top running in batch mode (-b). It’s going to refresh every 10 seconds, as specified by the delay (-d) flag, for a total count of 3 iterations (-n). The output will be sent to a file.Here is a screenshots of outut:

Daily life Linux Commands, Linux Tutorial and Material, Linux Certification, Linux Guides

7. Duplicate pipe content: ‘tee’ is a very useful utility that duplicates pipe content. Now, what makes tee really useful is that it can append data to existing files, making it ideal for writing periodic log information to multiple files at once.

ps | tee file1 file2 file3

We’re sending the output of the ps command to three different files! Or as many as we want. As you can see in the screenshots below, all three files were created at the same time and they all contain the same data.

8. export: The ‘export’ command is one of the bash shell BUILTINS commands.It has three available command options. In general, it marks an environment variable to be exported with any newly forked child processes and thus it allows a child process to inherit all marked variables.
Frequently Used Options with ‘export’

◈ -p : List of all names that are exported in the current shell
◈ -n: Remove names from export list
◈ -f : Names are exported as functions

Example :

◈ command without ‘export’:

$ a = lpicentral.blogspot.com
$ echo $a
lpicentral.blogspot.com
$ bash
$ echo $a

From the above we can see that any new child process forked from a parent process by default does not inherit parent’s variables. This is where the export command comes handy.

$ a = lpicentral.blogspot.com
$ echo $a
lpicentral.blogspot.com$ export a
$ bash
$ echo $a
lpicentral.blogspot.com$

On line 3, we now have used the export command to make the variable “a” to be exported when a new child process is created. As a result the variable “a” still contains the string “lpicentral.blogspot.com” even after a new bash shell was created.

9. basename – Strips directory and suffix from filenames. basename prints NAME with any leading directory components removed. If Suffix is specified, it will also remove a trailing SUFFIX. For example: To get the name of the file present in test folder

$ basename test/gfg.txt
gfg.txt

10. grep: grep searches files for a given character string or pattern and can replace the string with another. This is one method of searching for files within Linux.

grep [option(s)] pattern [file(s)]

◈ Search number of files: grep can search any number of files simultaneously. Thus, for example, the following would search the three files file1, file2 and file3 for any line that contains the string GfG

grep GfG file1 file2 file3

◈ Search text in all files: To search all text files in the current directory (i.e., the directory in which the user is currently working), if there is a phrase “Linux is”

grep 'Linux is' *

There are lot of more options which can be tried using grep command.