Transfer files using SCP Command in Linux

Author : Piyush Gupta

SCP (Secure Copy) is a command line tool for Linux systems for securely transfer files from local to remote server or vice a versa. SCP uses SSH protocol for transferring files between two systems which is more secure than ftp.

  • Syntax: (Local to Remote)

scp /path/to/local/file.txt user@192.168.10.100:/remote/path/

  • Syntax: (Remote to Local)

scp user@192.168.10.100:/remote/file.txt /path/to/local/

Transfer File Local to Remote Server

Following command will copy myfile.txt from current directory of local system to remote server’s /opt directory using root authentication. We are assuming remote server hostname is example.com.

$ scp myfile.txt root@example.com:/opt/ 

Transfer File Remote Server to Local

Following command will copy /opt/myfile.txt from remote system to local system’s /opt directory.

$ scp root@example.com:/opt/myfile.txt /opt/

Define Port with SCP Command

In case SSH is running on different port on remote server, use -P switch followed by port number with scp command.
$ scp -P 2344 myfile.txt root@example.com:/opt/myfile.txt

Transfer Directory Local to Remote Server Recursively

Following command will copy /opt/mydir directory from local system to remote system’s /opt directory recursively.

$ scp -r /opt/mydir root@example.com:/opt/

Transfer Directory Remote Server to Local Recursively

Following command will copy /opt/mydir directory from remote system to remote system’s /opt directory recursively.
$ scp -r root@example.com:/opt/mydir /opt/

How to Kill a Process by Name in Linux

Command line users rely on the ‘kill’ command to terminate a process as defined by the appropriate process identifier (PID). While there’s nothing wrong with targeting processes by their PID, another approach which is often easier is to target a process by name, rather than its unique identifier.

There are a few ways to kill a process by process name, we’ll review two primary methods using killall and pkill. These will work the same in Mac OS / X and linux, and they can be used to target GUI apps and processes as well as those running in the background or exclusively at the command line. Either command can be prefixed with sudo to terminate root level tasks or those owned by another user.

Killing a Process by Name with killall

The killall command is the most commonly used way to kill a process by its name:
  • From the Terminal, type the following command (in this example using task “process name” as the targeted process to kill)

killall 

  • Hit return to instantly kill the ‘process name’ process (replace “process name” with any other process name to kill it)
  NOTE: killing a process is instantaneous and unforgiving, it immediately terminates the process without saving any data. This can result in data loss and other irregularities if you’re not sure what you’re doing.

Kill a Process by Name with pkill

The pkill command also offers an approach to terminate processes by name rather than targeting a PID. One of the perks of pkill is that it makes it easier to target processes with spaces in their names since you only need to use quotes around the task name to kill.
  • From the Terminal, type the following command:

pkill

  • Hit Return to immediately terminate the named process
NOTE: Like killall, pkill will immediately terminate the process that has been targeted with no confirmations, dialog, saves, or anything else.
Examples:
For example to kill all processes running with name java, use the following command.
sudo pkill java
To kill all processes running with name httpd use the following command.
sudo pkill httpd

grep command in Linux

Author : Piyush Gupta

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for globally search for regular expression and print out).

Syntax:

grep [options] pattern [files]
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.

Examples:

We assuming file name is piyush.txt

1. Case insensitive search : 

The -i option enables to search for a string case insensitively in the give file. It matches the words like “UNIX”, “Unix”, “unix”.
$grep -i "UNix" piyush.txt

2. Displaying the count of number of matches : 

We can find the number of lines that matches the given string/pattern
$grep -c "unix" piyush.txt

3. Display the file names that matches the pattern : 

We can just display the files that contains the given string/pattern.
$grep -l "unix" *

or

$grep -l "unix" f1.txt f2.txt f3.xt f4.txt

4. Checking for the whole words in a file : 

By default, grep matches the given string/pattern even if it found as a sub-string in a file. The -w option to grep makes it match only the whole words.
$ grep -w "unix" piyush.txt

5. Displaying only the matched pattern : 

By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.
$ grep -o "unix" piyush.txt

6. Show line number while displaying the output using grep -n : 

To show the line number of file with the line matched.
$ grep -n "unix" piyush.txt

7. Inverting the pattern match : 

You can display the lines that are not matched with the specified search sting pattern using the -v option.
$ grep -v "unix" piyush.txt

8. Matching the lines that start with a string : 

The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.
$ grep "^unix" piyush.txt

9. Matching the lines that end with a string : 

The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.
$ grep "os$" piyush.txt

How to count the number of Lines, Words, and, Characters in a File using terminal

Author Piyush Gupta

The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal.
The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.
To count the number of lines, use “wc” with “l” as
$ wc -l yourTextFile
To count the number of words, use “wc” with “w” option as
$ wc -w yourTextFile
And to count the total number of characters, use “wc” with “c” as
$ wc -c yourTextFile
Using wc with no options will get you the counts of bytes, lines, and words (-c, -l and -w option).
$ wc sample.txt 
1065 5343 40559 sample.txt

Example:

Following command will count number of lines in /etc/passwd files and print on terminal. We can also use –lines in place of -l as command line switch.
$ wc -l /etc/passwd

Count Total Character in File:

Use -m or –chars switch with wc command to count number of characters in a file and print on screen.
$ wc -m /etc/passwd

Count Total Words in File:

Use -w or –words switch with wc command to count number of words in a file and print on screen.
$ wc -w /etc/passwd

What is Soft Links and Hard Links in Linux File System

Author Piyush Gupta

To make links between files you need to use ln command. A symbolic link (also known as a soft link or symlink) consists of a special type of file that serves as a reference to another file or directory. Unix/Linux like operating systems often uses symbolic links.

Two types of links

There are two types of links
  • symbolic links: Refer to a symbolic path indicating the abstract location of another file
  • hard links : Refer to the specific location of physical data.

How do I create soft link / symbolic link?

Soft links are created with the ln command. For example, the following would create a soft link named link1 to a file named file1, both in the current directory
$ ln -s file1 link1
To verify new soft link run:
$ ls -l file1 link1
Use following command to create a soft link of Apache configuration file under /etc directory. While creating softlink of file inode number will be different that original file.
ln -s /etc/httpd/conf/httpd.conf /etc/httpd.conf
Check soft link and original file inode number.
ls -li /etc/httpd/conf/httpd.conf /etc/httpd.conf

4035744 lrwxrwxrwx 1 root root 19 Sep 19 18:19 /etc/httpd.conf -> /etc/httpd/conf/httpd.conf
6130556 -rw-r--r-- 1 root root 19 Sep 19 18:59 /etc/httpd/conf/httpd.conf

How do I create Hard link?


Use following command to create a hard-link of Apache configuration file under /etc directory. While creating hard-link of file inode number will be same as original file.
ln /etc/httpd/conf/httpd.conf /etc/httpd.conf
Check soft link and original file inode number.
ls -li /etc/httpd/conf/httpd.conf /etc/httpd.conf

6130556 -rw-r--r-- 2 root root 19 Sep 19 18:19 /etc/httpd.conf
6130556 -rw-r--r-- 2 root root 19 Sep 19 18:59 /etc/httpd/conf/httpd.conf

Find the list of all installed packages in Linux

Author Piyush Gupta


Some times we need to check how many packages are installed in our Linux machine. This article will help you to find all installed packages on Redhat and Debian based Linux systems.

Find the list of all installed packages

Use one of following commands to list all installed packages on CentOS, RedHat, Fedora, Ubuntu, Debian and LinuxMint systems
On CentOS, RHEL & Fedora:
# rpm -qa    # Using rpm command 

# yum list installed # Using yum command
On Ubuntu & Debian:
# dpkg --get-selections

$ apt list --installed # Ubuntu 14.04 or above

How to check Linux System is 64 bit or 32 bit

Author : Piyush Gupta


This article describes how to find out whether your Linux system’s OS is 32-bit or 64-bit. This will be helpful if you wanted to download or install an application in your Linux system. As we all know, we can’t install 64-bit applications into a 32-bit OS type. That’s why knowing your Linux system’s OS type is important.
Here are the five easy and simple methods to verify your Linux system’s OS type. It doesn’t matter whether you’re using a GUI or CLI type systems, the following commands will work on almost all Linux operating systems such as RHEL, CentOS, Fedora, Scientific Linux, Debian, Ubuntu, Linux Mint, openSUSE etc.

1. uname Command

uname -a command will display your Linux system’s OS type. This is the universal command and it will work on almost all Linux/Unix operating systems.
To find out the system’s OS type, run:
# uname -a

#64-Ubuntu Mon Sep 28 20:29:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

2. dpkg Command

dpkg command will also display whether your Debian/Ubuntu operating system is 32-bit or 64-bit. This command will work only on Debian and Ubuntu based distributions and it’s derivatives.
Open your Terminal, and run:
$ dpkg --print-architecture
If your OS is 64-bit, you’ll get the following output:
amd64
If your OS is 32-bit, then the output will be:
i386

3. getconf Command

getconf command will also display the system configuration variables. Now, let me show you how to find out the Linux system arch using getconf command.
$ getconf LONG_BIT

64
For more details refer the man pages.
$ man getconf

4. arch Command

arch command will display your OS type. This command is similar to uname -m command. If its output is x86_64 then it’s 64-bit OS. If the output is i686 or i386, then it’s 32-bit OS.
$ arch

x86_64

5. file Command

file command with with a special argument /sbin/init will display the OS type.
$ file /sbin/init

/sbin/init: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV),
dynamically linked (uses shared libs), for GNU/Linux 2.6.24,
BuildID[sha1]=*****, stripped

How to archive files and directories in Linux

Author Piyush Gupta

Archive files and directories In Linux, There are multiple tools available in Linux system for creating archive files. In this article we will see how to archive files and directories using tar, zip and gzip command.
Below is the most common programs to archive files and directories are;
  1. zip
  2. tar
  3. gzip

1 – Zip

zip is the most popular command line archiving utility for Linux systems. zip program comes pre-installed with some Linux distros. Just in case if it is not available, you can install it using the distribution’s default package manager.

On Arch Linux and variants:

$ sudo pacman -S zip unzip

On RHEL, CentOS, Fedora:

$ sudo yum install zip unzip

On Debian, Ubuntu, Linux Mint:

$ sudo apt-get install zip unzip

On SUSE/openSUSE:

$ sudo zypper install zip unzip
Now let us see some examples.

Create Archive of File

# zip output.zip /var/log/*.log

Create Archive of Directory

# zip -r output.zip /var/log

Extract Archive

# unzip output.zip

2 – Tar

Tar is an Unix command which stands for Tape Archive. It is used to combine or store multiple files (same or different size) into a single file. There are 4 main operating modes in tar utility.


  1. c – Create an archive from a file(s) or directory(s).
  2. x – Extract an archive.
  3. r – Append files to the end of an archive.
  4. t – List the contents of the archive.

Create Archive of File

# tar -cf output.tar /var/log/*.log

Create Archive of Directory

# zip -cf output.tar /var/log

Extract Archive

# tar -xf output.tar

3 – gzip

gzip is a utility to compress and decompress files using Lempel-Ziv coding (LZ77) algorithm.
gzip is one more tool for command line users for making archive files. gzip also supports to take input of data as standard input or through pipe and create zip file.

Create Archive of File

# gzip -k access.log

Create zip file of piped data

# cat /var/log/messages | gzip > messages.gz

Extract Archive

# gunzip access.log.gz

Combined – Tar + gzip

Tar also combined gzip program to enable higher compression level of files. Using tar with gzip created files extension will be .tar.gz.

Create Archive of File

# tar -czf output.tar.gz /var/log/*.log

Create Archive of Directory

# zip -czf output.tar.gz /var/log

Extract Archive

# tar -xzf output.tar.gz

How to change background color of text in Linux Shell

Author Piyush Gupta

Information as found on this page, excluding preview column:
Sequences are composed of the Escape character (often represented by ”^[” or ””) followed by some other characters: ”^[FCm” (where FC is one of the numbers in the bulleted list below).
In bash, the Esc code can be either of the following:
  1. \e
  2. \033 (octal)
  3. \x1B (hexadecimal)
Note 1: The “\e[0m” sequence removes all attributes (formatting and colors). It can be a good idea to add it at the end of each colored text.
Note 2: Foreground and background colours may vary, depending on the terminal’s configuration and not all colours are supported.

Set/Reset

  • 0: Reset/remove all modifier, foreground and background attributes: echo -e "\e[0mNormal Text"
  • 1: Bold/Bright: echo -e "Normal \e[1mBold"
  • 2: Dim: echo -e "Normal \e[2mDim"
  • 4: Underlined: echo -e "Normal \e[4mUnderlined"
  • 5: Blink (doesn’t work in most terminals except XTerm): echo -e "Normal \e[5mBlink"
  • 7: Reverse/Invert: echo -e "Normal \e[7minverted"
  • 8: Hidden (useful for sensitive info): echo -e "Normal \e[8mHidden Input"
  • 21: Reset/Remove bold/bright: echo -e "Normal \e[1mBold \e[21mNormal"
  • 22: Reset/Remove dim: echo -e "Normal \e[2mDim \e[22mNormal"
  • 24: Reset/Remove underline: echo -e "Normal \e[4mUnderlined \e[24mNormal"
  • 25: Reset/Remove blink: echo -e "Normal \e[5mBlink \e[25mNormal"
  • 27: Reset/Remove reverse/invert: echo -e "Normal \e[7minverted \e[27mNormal"
  • 28: Reset/Remove hidden: echo -e "Normal \e[8mHidden \e[28mNormal"

Foreground

  • 39: Default (usually green, white or light gray): echo -e "Default \e[39mDefault"
  • 30: Black: echo -e "Default \e[30mBlack" (best combined with a background colour: echo -e "Default \e[30;107mBlack on white")
  • 31: Red (don’t use with green background)
  • 32: Green
  • 33: Yellow
  • 34: Blue
  • 35: Magenta/Purple
  • 36: Cyan
  • 37: Light Gray
  • 90: Dark Gray
  • 91: Light Red
  • 92: Light Green
  • 93: Light Yellow
  • 94: Light Blue
  • 95: Light Magenta/Pink
  • 96: Light Cyan
  • 97: White

Background

  • 49: Default background color (usually black or blue)
  • 40: Black
  • 41: Red
  • 42: Green
  • 43: Yellow
  • 44: Blue
  • 45: Magenta/Purple
  • 46: Cyan
  • 47: Light Gray (don’t use with white foreground)
  • 100: Dark Gray (don’t use with black foreground)
  • 101: Light Red
  • 102: Light Green (don’t use with white foreground)
  • 103: Light Yellow (don’t use with white foreground)
  • 104: Light Blue (don’t use with light yellow foreground)
  • 105: Light Magenta/Pink (don’t use with light foreground)
  • 106: Light Cyan (don’t use with white foreground)
  • 107: White (don’t use with light foreground)
To set both the foreground and background colours at once, use ther form echo -e "\e[S;FG;BGm". For example: echo -e "\e[1;97;41m" (bold white foreground on red background)
For 256 colour options, see the source page.

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
Design a site like this with WordPress.com
Get started