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.

Friday, 4 January 2019

List of useful Github Commands

Github is a distributed version control system which helps to manage the repositories.

Github Commands, LPI Guides, LPI Study Materials, LPI Tutorial and Materials

These are a list of few commands that you can use frequently on github(git bash)

1.git help

Take help from github help section for different commands and other errors

2.git config

To set the basic configurations on github like your name and email.

3.git config –global user.name “xyz”

Sets configuration values for your user name on git.

4.git config –global user.email xyz01@gmail.com

Sets configuration values for your user email on git.

5.git config –global color.ui true

To see different colours on command line for different outputs.

6.mkdir store

Create a directory if not created initially.

7.cd store

To go inside the directory and work upon its contents.

8.git init

To create a local git repository for us in our store folder.This will help to manage the git commands for that particular repository.

9.git status

To see whats changed since last commit.It shows all the files that have been added and modified and ready to be commmitted and files which are untracked

10.git add Readme.txt

To add a file Readme.txt to the staging area to track its changes.

11.git commit -m “Created a Readme.txt”

To commit our changes(taking a snapshot) and providing a message to remember for future reference.

12.git log

To check the history of commits for our reference.

Different ways to use add command:


13.git add

To add a specific list of files to staging area.

14.git add --all

To add all files of current directory to staging area.

15.git add *.txt

To add all text files of the current directory to staging area.

16.git add docs/*.txt

To add all text files of a particular directory(docs) to staging area.

17.git add docs/

To add all files in a particular directory(docs) to staging area.

18.git add “*.txt”

To add text files of entire project to staging area.


More Commands:


19.git diff

To figure out what changes you made since last commit.

20.git reset head license

To undo staging of the file that was added in the staging area.

21.git checkout –license

To Blow away all changes since the last commit of the file.

22.git commit -a -m “Readme.md”

To add any of our tracked files to staging area and commit them by providing a message to remember.

23.git reset –soft HEAD^

To undo last commit and bring file to staging area.

24.git reset –hard HEAD^

To undo last commit and remove file from the staging area as well(In case we went horribly wrong).

25.git reset –hard HEAD^^

To undo last 2 commits and all changes.

26.git remote add origin https://github.com/madaan123/MyAlgorithms.git

This commands make a bookmark which signifies that this particular remote refers to this URL.

This remote will be used to pull any content from the directory and push our local content to the global server.

27.git remote add <address>

To add new remotes to our local repository for a particular git address.

28.git remove rm

To remove a remote from our local repository.

29.git push -u origin master

To push all the contents of our local repository that belong to master branch to the server(Global repository).

30.git clone https://github.com/xyz123/MyAlgorithms.git

Github Commands, LPI Guides, LPI Study Materials, LPI Tutorial and Materials

To clone or make a local copy of the global repository in your system
(git clone command downloads the repository and creates a remote named as origin which can be checked by command – git remote -v).

31.git branch Testing

To create a new branch named as Testing.

32.git branch

To see all the branches present and current branch that we are working on.

33.git checkout Testing

To switch to branch Testing from master branch.

34.ls

To see directories and files in the current directory.

35.git merge Testing

To merge Testing branch with master branch.

36.git branch -d Testing

To delete Testing branch.

37.git checkout -b admin

To create a new branch admin and set it as current branch.

38.git branch -r

To look at all the remote branches.

39.git branch -D Testing

To forcefully delete a branch without making commmits.

40.git tag

To see the list of available tags.

41.git checkout v0.0.1

To set the current tag to v0.0.1.

42.git tag -a v0.0.3 -m “version 0.0.3”

To create a new tag.

43.git push –tags

To push the tags to remote repository.

44.git fetch

To fetch down any changes from global repository to current repository

45.git rebase

Three tasks are performed by git rebase

1. Move all changes to master which are not in origin/master to a temporary area.

2. Run all origin master commits.

3. Run all commits in the temporary area on top of our master one at a time, so it avoids merge commits.

Wednesday, 2 January 2019

Useful CMD commands for daily use in Windows OS

List of uncommon useful cmd commands are as follows:

1. Cipher:


Cipher.exe is a built-in command line tool in the Windows operating system that can be used to encrypt or decrypt data on NTFS drives. This tool also lets to securely delete data by overwriting it.
Cipher Switches:

◈ /? : Displays help at the command prompt.
◈ /e : Encrypts the specified folders. Folders are marked so that files that are added to the folder later are encrypted too.
◈ /d : Decrypts the specified folders. Folders are marked so that files that are added to the folder later are encrypted too.
◈ /w : PathName – Removes data on unused portions of a volume. PathName can indicate any directory on the desired volume.

Syntax:

cipher /w:c

This will wipe free space on the drive. The command does not overwrite undeleted data, so you will not wipe out files you need by running this command.

2. File compare:


File compare (or fc) is a great command line tool that can be used to compare files to see if there are any content or binary code differences that you can access if you are using a PC. It is a simple program that will compare the contents of text or binary files and is capable of comparing both ASCII and Unicode text. You can use this tool to display any lines from two files or two sets of files that do not match up with the others.

Switches:

◈ /b – This switch will perform a binary comparison.
◈ /c – If you need to do a case insensitive comparison, use this switch.
◈ /a – This switch will make FC show only the first and last lines for each group of differences.
◈ /u – Use this switch to compare files as Unicode text files.
◈ /l – This will compare your files as ASCII text.
◈ /n – This switch can only be used with ASCII but it will show all the corresponding line numbers.

Syntax:

Simply type “fc” and then the directory path and file name of the two files you want to compare.

fc [switches] [pathname1] [pathname2]


3. Tasklist:


In Windows, we can get the list of processes running on the system from command prompt also. We can use ‘tasklist‘ command for this purpose.
Using this command we can selectively list the processes based on criteria like the memory space used, running time, image file name, services running in the process etc.

Parameters:

◈ /s Computer : Specifies the name or IP address of a remote computer (do not use backslashes). The default is the local computer.
◈ /u Domain \ User : Runs the command with the account permissions of the user specified by User or Domain\User. The default is the permissions of the current logged on user on the computer issuing the command.
◈ /p Password : Specifies the password of the user account that is specified in the /u parameter.
◈ /fo { TABLE | LIST | CSV } : Specifies the format to use for the output. Valid values are TABLE, LIST, and CSV. The default format for output is TABLE.
◈ /nh : Suppresses column headers in the output. Valid when the /fo parameter is set to TABLE or CSV.
◈ /fi FilterName : Specifies the types of process(es) to include in or exclude from the query. The following table lists valid filter names, operators, and values.
◈ /svc : Lists all the service information for each process without truncation. Valid when the /fo parameter is set to TABLE. Cannot be used with the /m or the /v parameter.
◈ /v : Specifies that verbose task information be displayed in the output. Cannot be used with the /svc or the /m parameter.
◈ /m [ ModuleName ] : Specifies to show module information for each process. When a module is specified, all the processes using that module are shown. When a module is not specified, all the processes for all the modules are shown. Cannot be used with the /svc or the /v parameter.

Filters:

LPI Tutorial and Material, LPI Guides, LPI Study Material, LPI Certifications

Syntax:

 tasklist[.exe] [/s computer] [/u domain\user [/p password]] 
    [/fo {TABLE|LIST|CSV}] [/nh] [/fi FilterName [/fi FilterName2 
    [ ... ]]] [/m [ModuleName] | /svc | /v]   

Example:

tasklist /v /fi “PID gt 1000” /fo csv
tasklist /fi “USERNAME ne NT AUTHORITY\SYSTEM” /fi “STATUS eq running”
tasklist /v /fi “STATUS eq running”
tasklist /s srvmain /nh
tasklist /s srvmain /s srvny
tasklist /s srvmain /u maindom\hiropln /p p@ssW23 /nh

4. Taskkill:


Its sure that you are familiar with the traditional way to kill or end a process in Windows using Task Manager. This method is effective but not nearly as fun as killing a process in Command Prompt. Additionally, killing processes in Command Prompt provides much more control and the ability to end multiple processes at once.

Parameters:

◈ /s Computer : Specifies the name or IP address of a remote computer (do not use backslashes). The default is the local computer.
◈ /u Domain \ User : Runs the command with the account permissions of the user specified by User or Domain\User. The default is the permissions of the current logged on user on the computer issuing the command.
◈ /p Password : Specifies the password of the user account that is specified in the /u parameter.
◈ /fi FilterName : Specifies the types of process(es) to include in or exclude from termination. The following are valid filter names, operators, and values.
◈ /pid ProcessID : Specifies the process ID of the process to be terminated.
◈ /im ImageName : Specifies the image name of the process to be terminated. Use the wildcard (*) to specify all image names.
◈ /f : Specifies that process(es) be forcefully terminated. This parameter is ignored for remote processes; all remote processes are forcefully terminated.
◈ /t : Specifies to terminate all child processes along with the parent process, commonly known as a tree kill.

Filters:

LPI Tutorial and Material, LPI Guides, LPI Study Material, LPI Certifications

Syntax:

 taskkill [/s Computer] [/u Domain\User [/p Password]]] [/fi FilterName] 
  [/pid ProcessID]|[/im ImageName] [/f][/t] 

Examples:
C:\>taskkill /pid 26356 /f
C:\>taskkill /fi “USERNAME eq Pratik” /f
C:\>taskkill /s VictimsDesktop /u RemoteAccountName /p RemoteAccountPassword /im notepad.exe /f

5. System File Checker:


System File Checker is an automatic scan and repair tool that focuses on Windows system files. You will need to run the command prompt with administrator privileges and enter the command “sfc /scannow”. If any corrupt or missing files are found, they’ll be automatically replaced using cached copies kept by Windows for just that purpose. The command can require a half-hour to run on older notebooks.

Syntax:

sfc /scannow  

6. Driverquery:


Drivers remain among the most important software installed on a PC. Improperly configured or missing drivers can cause all sorts of trouble, so its good to have access to a list of what’s on your PC. That’s exactly what the “driverquery” command does. You can extend it to “driverquery -v” to obtain more information including the directory in which the driver is installed.

Parameters:

◈ /s Computer : Specifies the name or IP address of a remote computer (do not use backslashes). The default is the local computer.
◈ /u Domain \ User : Runs the command with the account permissions of the user specified by User or Domain\User. The default is the permissions of the current logged on user on the computer issuing the command.
◈ /p Password : Specifies the password of the user account that is specified in the /u parameter.
◈ /fo { TABLE | LIST | CSV } : Specifies the format to display the driver information. Valid values are TABLE, LIST, and CSV. The default format for output is TABLE.
◈ /nh : Omits the header row from the displayed driver information. Valid when the /fo parameter is set to TABLE or CSV.
◈ /v : Specifies that detailed driver information be displayed.
◈ /si : Displays digital signature information for both signed and unsigned device drivers.

Syntax:

 driverquery  [/s Computer] [/u Domain\User /p Password] 
   [/fo {TABLE|LIST|CSV}] [/nh] [/v] [/si]  

Examples:
driverquery
driverquery /fo csv
driverquery /nh
driverquery /s ipaddress

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