-->

How to change output text color in Linux Shell

Author Piyush Gupta

You can use these ANSI escape codes:
Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

Example:

# echo -e “e[0;31mText in Red Colore[0m

The Above commands are showing to set foreground ( Text ) color of output on Linux bash shell. The output of above command will be in red color. There are many other colors available which you can use defined as below. Just replace “0;31” with other color values in above text.
Foreground ( Text ) Color’s:
   Black        0;30
   Red          0;31
   Green        0;32
   Yellow       0;33
   Blue         0;34
   Purple       0;35
   Cyan         0;36
   White        0;37

   Reset Text Color   0m
Use following codes to show text in Bold with same colors.
   Black        1;30
   Red          1;31
   Green        1;32
   Yellow       1;33
   Blue         1;34
   Purple       1;35
   Cyan         1;36
   White        1;37

How to prompt for User Input in Linux shell script

Author Piyush Gupta

Command:

# read var

# read -s “Waiting for input: ” var

Uses:

read command is used for getting user input in a Linux shell script. -p switch with read command is used for showing some helpful text on-screen. Create a shell script named input.sh and add following content.
#!/bin/bash

read -p "Enter Your Name: "  username
echo "Welcome $username!"
Lets execute the shell script
# sh input.sh

Enter Your Name: Piyush
Welcome Piyush!

Input Password in Shell Script

If you want to take input of password in shell script. You must want to not to show any character on-screen entered by user. Use -s for silent mode. Using -s input characters are not echoed.
#!/bin/bash

read -s -p "Enter Password: " pswd
echo $pswd

How to extract .gz File in Linux

Author Piyush Gupta
GZ files are archive files compressed with the "gzip" program, similar to zip files. These archive files contain one or more files, compressed into a smaller file size for faster download times from the Internet. Source code and other software program files for Linux are often distributed in .gz or .tar.gz format. Unzip a GZ file using the "gunzip" command or a .tar.gz file using the "tar" command in a Linux terminal.

Command:

$ gunzip archive.gz

Uses:

.gz is files are compressed with gzip in linux. To extract .gz files we use gunzip command.
First use following command to create gzip (.gz) archive of access.log file. Keep remember that below command will remove original file.
gzip access.log
Above command will create a archive file named access.log.gz in current directory.
ls -l access.log.gz
-rw-r--r-- 1 root root 37 Sep 14 04:02 access.log.gz
Now use gunzip command to extract access.log.gz file using command. This will extract the file from archive and remove .gz file automatically.
gunzip access.log.gz

How to remove all spaces from file in Notepad++?

Author Piyush Gupta

There are two way to remove all spaces from file in Notepad.
  1. Using Regular expression 
  2. Direct Method
Using Regular expression:


To delete all spaces in the file, replace ' +' with '' (quotes only for demonstration, please remove them). You need to have the checkbox "Regular expression" checked.
To remove all spaces and tabs, replace '[ \t]+' with '' (remove quotes).
This works for big files, too, where the solution of direct method will be tiresome.
Direct Method:
No need for regular expressions. In Find (Used Ctrl + H) what add a white space character  and then replace with nothing.

How to find a String in all files in Linux

Author Piyush Gupta

How to Find all Files Containing a String in Linux. This tutorial will help you to search all files containing specific text string recursively. This tutorial uses “find” command to search string in files. Alternatively, You can also use grep command to search text.

Command:

# find / -exec grep -l “string-to-search” {} ;

Uses:

Below is an example command which searches text ‘redhat’ in / filesystem. This command will search for all files containing string ‘redhat’ and list file names only like below.
# find / -exec grep -l "redhat" {} ; 

/lib64/security/pam_oddjob_mkhomedir.so
/lib64/libdevmapper.a.1.02
/lib64/libdevmapper-event.a.1.02
/lib64/libdevmapper.a
/lib64/libdevmapper-event.a
...
To Search in specific file extension files. Like search ‘redhat’ in all php files only, use following command.
# find / -name '*.php' -exec grep -l "redhat" {} ; 

/var/www/projects/index.php
/var/www/projects/new.php
...

How to use DPKG commands in Linux

Author Piyush Gupta

DPKG is the main package management program in Debian and Debian based System. It is used to install, build, remove, and manage packages. Aptitude is the primary front-end to dpkg.

Some the most commonly used dpkg commands along with their usages are listed in below table:

COMMAND DETAILS
DPKG COMMAND
Install a package
dpkg -i {file.deb}
Update package
dpkg -i {file.deb}
Remove an installed package
dpkg -r {package}
List all installed packages
dpkg -l
List files in an installed package
dpkg -L {package}
Show information about installed package
dpkg -p {package}
Show information about package file
dpkg -I {file.deb}
List files in a package file
dpkg -c {file.deb}

Example:

For installing packages on debian based system we use dpkg (Debian Package Management System) command.
sudo dpkg -i package.deb

Installing Package:

To install a Google chrome from a deabian package file (google-chrome-stable_current_i386.deb) use -i command line switch.
$ sudo dpkg -i google-chrome-stable_current_i386.deb

Removing Package:

In case if we need to remove any package use -r command line switch with package name.
$ sudo dpkg -r google-chrome-stable_current_i386.deb

How to use RPM Commands in Linux

Author Piyush Gupta

RPM command is used for installing, uninstalling, upgrading, querying, listing, and checking RPM packages on your Linux system. RPM stands for Red Hat Package Manager.
With root privilege, you can use the rpm command with appropriate options to manage the RPM software packages.

In this article, let us review command details of rpm command.

COMMAND DETAILS
RPM COMMAND
Install a package
rpm -i {package.rpm}
Update package
rpm -U {file.rpm}
Remove an installed package
rpm -e {package}
List all installed packages
rpm -qa
List files in an installed package
rpm -ql {package}
Show information about installed package
rpm -qi
Show information about package file
rpm -qpi {file.rpm}
List files in a package file
rpm -qpl {file.rpm}
Verify all installed packages
rpm -Va
Verify installed package
rpm -V {package}

Example:

For installing packages on RedHat based systems, we use rpm (Red Hat Package Manager) command.
rpm -i package-1.2.3.rpm

Installing Package:

To install a rpm package using command line on redhat based system use -i command line switch with rpm command.
rpm -i package-1.2.3.rpm
You can also use YUM or DNF package manager to install downloaded rpm file. Its benefit to resolve dependencies required for the package.
yum localinstall package-1.2.3.rpm     ## CentOS, RHEL systems 
dnf localinstall package-1.2.3.rpm     ## Fedora systems 

Removing Package:

In case if we need to remove any package use -e command line switch with the package name.
rpm -e package-1.2.3.rpm

How to Exit Vim Text Editor

Author Piyush Gupta

VIM is an enhanced version of VI text editor. It is highly configurable text editor to edit files very efficiently. Many of Linux beginners faces issue with exit form vi/vim editor. This tutorial will help you to exit Vim text editor in Linux command line.

How to Exit Vim?

As we have already seen in advanced Vim tips, there are multiple ways to quit Vim. Let me list them one by one along with the procedure.
You have several ways to exit from Vim text editor. But you need to choose the right command to exit from Vim editor based on your decision. Remember that exit command always run from Vim command mode. So you need to press ESC to go to command mode. Now type a colon :to before writing exit command.
  • ESC + :q – Simple exit from a file, only if no changes made to file.
  • ESC + :wq – Write changes and quite from Vim editor.
  • ESC + :q! – Quit Vim editor without saving any changes (discard all changes made after edit file or last saved).

Using Keyboard Shortcuts

You can also use keyboard shortcuts to exit from vim editor. No need to type any thing. Press ESC to make sure you are in command mode.
  • Shift + z + z – save changes and exit
  • Shift + z + q – exit without saving changes
Reference: http://vimdoc.sourceforge.net/

How to use cut Command in Linux

Author Piyush Gupta

Linux cut command is used for extracting file content on fields basis. text files do not have row/column like databases and some times we need the data of single column only. Cut identified columns on basis of separator (eg: colon ‘:’, semicolon ‘;’, comma ‘,’ etc).
For this example we are taking /etc/passwd file. All the rows are stored as below format with colon (:) separated like below. We use -f to specify field number and -d for delimiter (separator).

As per above screen-cast this file has 7 fields. Cut also support to fetch values on character basis suing -c command line switch. Lets take below examples, for which I am using /etc/passwd file.

1. Select Single Field from File –

For example we need the list of usernames from our /etc/passwd file. We know that first column stored username, Entire file is separated by colon (:).
# cut -d":" -f1  < /etc/passwd

root
bin
daemon
adm
lp
sync
shutdown
halt
We can also use pipeline " | " for passing the file content as input to cut command, like below -
# cat  /etc/passwd | cut -d":" -f1

2. Select Multiple Columns from File -

We can specify multiple field names with command separated, like below example will show the 1'st, 2'nd and 7'th fields only.
# cut -d":" -f1,2,7 < /etc/passwd

root:x:/bin/bash
bin:x:/sbin/nologin
daemon:x:/sbin/nologin
adm:x:/sbin/nologin
lp:x:/sbin/nologin
sync:x:/bin/sync
shutdown:x:/sbin/shutdown
halt:x:/sbin/halt
mail:x:/sbin/nologin
uucp:x:/sbin/nologin
We can also specify the range of columns with hyphen (-) on fields as well as both option's together like below example commands.
    • Here first command will select 1'st, 2'nd,3'rd and 4'th fields.
    • Second command will select 3'rd, 4'th and 5'th fields.
    • Last command will show 2'nd, 3'rd, 4'th, 6'th and 10'th fields.
# cut -d":" -f1-4 < /etc/passwd
# cut -d":" -f3-5 < /etc/passwd
# cut -d":" -f2-4,6,10 < /etc/passwd
To get values of all columns except one use following command. For example if we need to select all columns but not 6.
# cut -d":" --complement -s -f6 < /etc/passwd

3. Selecting Single Character's from File -

Except fields we can also select values from file on basis of single characters, while using characters we don't need to specify separator.
# cut -c1 < /etc/passwd

r
b
d
a
l
s
s
Similarly fields we can also specify multiple comma separated characters or range of characters.
# cut -c1,2,3,6,7 < /etc/passwd
# cut -c1-3,6,7 < /etc/passwd