Sunday 29 July 2018

Copy (cp) File And Directory Examples

LPI Certification, LPI Guides, LPI Tutorial and Material, LPI Learning

Copy (cp) is the frequently used command in Unix (or Linux). The cp Command is used to copy the files from one directory to another directory. The cp command can also be used to copy the directories also. The syntax of cp command is

cp [options] source destination

Examples of cp Command


1. Write a unix/linux cp command to copy file in to a directory?


The basic usage of cp command is to copy a file from the current directory to another directory.

cp sum.pl tmp/

The cp command copies the file sum.pl into the tmp directory. The cp command does not remove the source file. It just copies the file into a new location. If a file with the same name as the source exists in the destination location, then by default the cp command overwrites that new file

2. Write a unix/linux cp to prompt for user before overwriting a file ( Interactive cp command)?


The -i option to the cp command provides the ability to prompt for a user input whether to overwrite the destination file or not.

> cp sum.pl tmp/
cp: overwrite `tmp/sum.pl'?

If you enter y, then the cp command overwrites the destination file, otherwise the cp command does not copy the file.

3. Write a unix/linux cp command to copy multiple files in to a new directory?


You can specify multiple files as the source and can copy to the new location.

cp log.dat bad.dat tmp/

The cp command copies the log.dat, bad.dat files in the current directory to the tmp directory.

4. Write a unix/linux cp command to do a Regular expression copy?


You can copy a set of files by specifying a regular expression pattern.

cp *.dat tmp/

Here the cp command copies all the files which has "dat" as suffix to the destination directory.

5. Write a unix/linux cp command to copy a file in to the current directory?


You can copy a file from a different directory to the current directory.

cp /usr/local/bin/multiply.sh.

Here the cp command copies the multiply.sh file in the /usr/local/bin directory the current directory. The dot (.) indicates the current directory.

6. Write a unix/linux cp command to copy all the files in a directory?


The cp command can be used to copy all the files in directory to another directory.

cp docs/* tmp/

This command copies all the files in the docs directory to the tmp directory.

7. Write a unix/linux cp command to copy files from multiple directories?


You can copy the files from different directories into a new location.

cp docs/* scripts/* tmp/

The command copies the files from docs and script directories to the destination directory tmp.

8. Write a unix/linux cp command to Copy a directory.


You can recursively copy a complete directory and its sub directory to another location using the cp command

cp -r docs tmp/

This copies the complete directory docs into the new directory tmp

9. Write a unix/linux cp command to Forcibly copy a file with -f option?


You can force the cp command to copy an existing destination file even it cannot be opened.

cp -f force_file.txt /var/tmp/

Friday 27 July 2018

ls Command in Unix and Linux Examples

ls Command, Linux Command, Unix Command, LPI Study Materials

ls is the most widely used command in unix or linux. ls command is used to list the contents of a directory. Learn the power of ls command to make your life easy. The syntax of ls command is

ls [options] [pathnames]


1. Write a unix/linux ls command to display the hidden files and directories?


To display the hidden files and directories in the current directory use the -a option of the ls command.

> ls -a
.  ..  documents  .hidden_file  sum.pl

Hidden files are the one whose name starts with dot (.). The las -a displays the current directory (.) and parent directory (..) also. If you want to exclude the current directory, parent directory, then use -A option.

> ls -A
documents  .hidden_file  sum.pl

2. Write a unix/linux ls command to classify the files with special characters


The -F option to ls command classifies the files. It marks the

◈ Directories with trailing slash (/)
◈ Executable files with trailing asterisk (*)
◈ FIFOs with trailing vertical bar (|)
◈ Symbolic links with trailing at the rate sign (@)
◈ Regular files with nothing

> ls -F
documents/  sum.pl link@

3. Write a unix/linux ls command to print each file in a separate line?


The -1 option to the ls command specifies that each file should be displayed on a separate line

> ls -1
documents
sum.pl

4. Write a unix/linux ls command to display the inode number of file?


In some cases, you want to know the inode number of a file. Use -i option to the ls command to print the inode number of a file.

> ls -i1
10584066 documents
3482450 sum.pl

5. Write a unix/linux ls command to display complete information about the files?


The -l option provides lots of information about the file type, owner, group, permissions, file size, last modification date.

> ls -l
total 16
drwxr-xr-x 2 matt db 4096 Jan 30 23:08 documents
-rw-r--r-- 1 matt db   49 Jan 31 01:17 sum.pl

◈ The first character indicates the type of the file. - for normal file, d for directory, l for link file and s for socket file
◈ The next 9 characters in the first field represent the permissions. Each 3 characters refers the read (r), write (w), execute (x) permissions on owner, group and others. - means no permission.
◈ The second field indicates the number of links to that file.
◈ The third field indicates the owner name.
◈ The fourth field indicates the group name.
◈ The fifth field represents the file size in bytes.
◈ The sixth field represents the last modification date and time of the file.
◈ And finally the seventh field is the name of the file.

6. Write a unix/linux ls command to sort the files by their modification time?


The -t option allows the ls command to sort the files in descending order based on the modification time.

> ls -t1
sum.pl
documents

7. Write a unix/linux ls command to sort the files in ascending order of modification time?


The -r option reverses the order of the files displayed. Combine the -t and -r options to sort the files in ascending order.

> ls -rt1
documents
sum.pl

8. Write a unix/linux ls command to print the files recursively?


So far the ls command prints the files in the current directory. Use the -R option to recursively print the files in the sub-directories also.

> ls -R
.:
documents  sum.pl

./documents:
file.txt

9. Write a unix/linux ls command to print the files in a specific directory?


You can pass a directory to the ls command as an argument to print for the files in it.

> ls /usr/local/bin

10. Write a unix/linux ls command to display files in columns?


The -x option specifies the ls command to display the files in columns.

> ls -x

Tuesday 24 July 2018

Linux I/O Redirection

Redirection can be defined as changing the way from where commands read input to where commands sends output. You can redirect input and output of a command.

LPI Study Materials, LPI Guides, LPI Learning, LPI Tutorial and Material

For redirection, meta characters are used. Redirection can be into a file (shell meta characters are angle brackets '<', '>') or a program ( shell meta characters are pipesymbol '|').

Standard Streams In I/O Redirection


The bash shell has three standard streams in I/O redirection:

◈ standard input (stdin) : The stdin stream is numbered as stdin (0). The bash shell takes input from stdin. By default, keyboard is used as input.
◈ standard output (stdout) : The stdout stream is numbered as stdout (1). The bash shell sends output to stdout. Output goes to display.
◈ standard error (stderr) : The stderr stream is numbered as stderr (2). The bash shell sends error message to stderr. Error message goes to display.

Redirection Into A File


Each stream uses redirection commands. Single bracket '>' or double bracket '>>' can be used to redirect standard output. If the target file doesn't exist, a new file with the same name will be created.

Overwrite

Commands with a single bracket '>' overwrite existing file content.

◈ > : standard output
◈ < : standard input
◈ 2> : standard error

Note: Writing '1>' or '>' and '0<' or '<' is same thing. But for stderr you have to write '2>'.

Syntax:

cat > <fileName> 

Example:

cat > sample.txt 

LPI Study Materials, LPI Guides, LPI Learning, LPI Tutorial and Material

Look at the above snapshot, command "cat > sample.txt" has created 'sample.txt' with content 'a, b, c'. Same file 'sample.txt' is created again with command "cat > sample.txt" and this time it overwrites earlier file content and only displays 'd, e, f '.

Append


Commands with a double bracket '>>' do not overwrite the existing file content.

◈ >> - standard output
◈ << - standard input
◈ 2>> - standard error

Syntax:

cat >> <fileName>  

Example:

cat >> sample.txt  

LPI Study Materials, LPI Guides, LPI Learning, LPI Tutorial and Material

Look at the above snapshot, here again we have created two files with the same name using '>>' in command "cat >> sample.txt". But this time, content doesn't overwrite and everything is displayed.

Redirection Into A Program


Pipe redirects a stream from one program to another. When pipe is used to send standard output of one program to another program, first program's data will not be displayed on the terminal, only the second program's data will be displayed.

Although the functionality of pipe may look similar to that of '>' and '>>' but has a significance difference. Pipe redirects data from one program to another while brackets are only used in redirection of files.

Example:

ls *.txt | cat > txtFile  

LPI Study Materials, LPI Guides, LPI Learning, LPI Tutorial and Material

Look at the above snapshot, command "ls *.txt | cat > txtFile" has put all the '.txt' files into a newly created file 'txtFile'.

Sunday 22 July 2018

Linux Shell Commands

In Linux, commands are ways or instructions through which you can instruct your system to do some action. Commands are executed in the command line.

Syntax:


command [option] [argument]

There are some commands which don't have any option or don't accept any argument such as 'clear' and 'pwd'.

clear

The 'clear' command clears out all the previous commands and outputs from terminal display.

pwd

The 'pwd' command stands for 'print working directory'. It doesn't accept any option or argument and displays the detail of current working directory.

Types of Commands


External or built-in commands

Built-in commands are internal commands that are built-in the shell. Built-in commands are called from the shell and executed directly within the shell itself. You can list all built-in commands with the help of 'help' and 'compgen -b' command. Some example of built-in commands are 'pwd', 'help', 'type', 'set', 'unset', etc.

External commands are other than built-in commands. These commands are programs which have their own binary and located in the filesystem. These are the commands that your system offer and are totally shell independent. Mostly these commands reside in /bin, /sbin, /usr/sbin.

type command


Linux 'type' command tell us whether a command given to the shell is a built-in or external command.

Syntax:

type <command>

Example:

type pwd
type cd
type man
type cat
type file

Linux Shell Commands, LPI Study Materials, LPI Guides, LPI Learning, LPI Certification

Look at above snapshot, commands like 'pwd' and 'cd' are built-in commands while commands 'man', 'cat', and 'file' are external commands.

Linux 'type' command also tells whether a command is aliased or not.

Example:

type ls  

Linux Shell Commands, LPI Study Materials, LPI Guides, LPI Learning, LPI Certification

Look at the above snapshot, 'type' command shows that 'ls' is an aliased command.

type -a


The 'type -a' option tells about all type of command whether it is built-in, external, or aliased. Some commands are both external and built-in commands. But built-in command will always takes priority until and unless path of external command is mentioned.

Syntax:

type -a <command>  

Example:

type -a echo  

Linux Shell Commands, LPI Study Materials, LPI Guides, LPI Learning, LPI Certification

Look at the above snapshot, 'echo' command is internal as well as external. To use external 'echo' command, path "/bin/echo" is mentioned.

which


Linux 'which' command locates the path of a command.

Syntax:

which <command1> <command2> <command3>....  

Example:

which ls pwd rmdir mkdir cp cd file man  

Linux Shell Commands, LPI Study Materials, LPI Guides, LPI Learning, LPI Certification

Look at the above snapshot, except 'cd' command, all other commands are external commands because bash has displayed their external path.

Thursday 19 July 2018

Unix / Linux - File Permission / Access Modes

LPI Study Materials, LPI Guides, LPI Certifications, LPI Learning, LPI Tutorials and Materials

We will discuss in detail about file permission and access modes in Unix. File ownership is an important component of Unix that provides a secure method for storing files. Every file in Unix has the following attributes.

◈ Owner permissions − The owner's permissions determine what actions the owner of the file can perform on the file.

◈ Group permissions − The group's permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.

◈ Other (world) permissions − The permissions for others indicate what action all other users can perform on the file.

The Permission Indicators


While using ls -l command, it displays various information related to file permission as follows.

$ls -l /home/amrood
-rwxr-xr--  1 amrood   users 1024  Nov 2 00:10  myfile
drwxr-xr--- 1 amrood   users 1024  Nov 2 00:10  mydir

Here, the first column represents different access modes, i.e., the permission associated with a file or a directory.

The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order: read (r), write (w), execute (x)

◈ The first three characters (2-4) represent the permissions for the file's owner. For example, -rwxr-xr-- represents that the owner has read (r), write (w) and execute (x) permission.

◈ The second group of three characters (5-7) consists of the permissions for the group to which the file belongs. For example, -rwxr-xr-- represents that the group has read (r) and execute (x) permission, but no write permission.

◈ The last group of three characters (8-10) represents the permissions for everyone else. For example, -rwxr-xr-- represents that there is read (r) only permission.

File Access Modes


The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the read, write, and execute permissions, which have been described below.

Read

Grants the capability to read, i.e., view the contents of the file.

Write

Grants the capability to modify, or remove the content of the file.

Execute

User with execute permissions can run a file as a program.

Directory Access Modes


Directory access modes are listed and organized in the same manner as any other file. There are a few differences that need to be mentioned.

Read

Access to a directory means that the user can read the contents. The user can look at the filenames inside the directory.

Write

Access means that the user can add or delete files from the directory.

Execute

Executing a directory doesn't really make sense, so think of this as a traverse permission.

A user must have execute access to the bin directory in order to execute the ls or the cd command.

Changing Permissions


To change the file or the directory permissions, you use the chmod (change mode) command. There are two ways to use chmod — the symbolic mode and the absolute mode.

Using chmod in Symbolic Mode

The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.

No Chmod operator & Description
+
Adds the designated permission(s) to a file or directory. 
-
Removes the designated permission(s) from a file or directory. 
=
Sets the designated permission(s).

Here's an example using testfile. Running ls -1 on the testfile shows that the file's permissions are as follows.

$ls -l testfile
-rwxrwxr--  1 amrood   users 1024  Nov 2 00:10  testfile

Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes.

$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod g = rx testfile
$ls -l testfile
-rw-r-xrwx  1 amrood   users 1024  Nov 2 00:10  testfile

Here's how you can combine these commands on a single line.

$chmod o+wx,u-x,g = rx testfile
$ls -l testfile
-rw-r-xrwx  1 amrood   users 1024  Nov 2 00:10  testfile

Using chmod with Absolute Permissions


The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.

Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.

Number Octal Permission Representation  Ref 
No permission  --- 
Execute permission  --x 
Write permission  -w- 
Execute and write permission: 1 (execute) + 2 (write) = 3  -wx 
Read permission  r-- 
Read and execute permission: 4 (read) + 1 (execute) = 5  r-x 
Read and write permission: 4 (read) + 2 (write) = 6  rw- 
All permissions: 4 (read) + 2 (write) + 1 (execute) = 7  rwx 

Here's an example using the testfile. Running ls -1 on the testfile shows that the file's permissions are as follows.

$ls -l testfile
-rwxrwxr--  1 amrood   users 1024  Nov 2 00:10  testfile

Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes.

$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod 743 testfile
$ls -l testfile
-rwxr---wx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod 043 testfile
$ls -l testfile
----r---wx  1 amrood   users 1024  Nov 2 00:10  testfile

Changing Owners and Groups


While creating an account on Unix, it assigns a owner ID and a group ID to each user. All the permissions mentioned above are also assigned based on the Owner and the Groups.

Two commands are available to change the owner and the group of files.

◈ chown − The chown command stands for "change owner" and is used to change the owner of a file.

◈ chgrp − The chgrp command stands for "change group" and is used to change the group of a file.

Changing Ownership


The chown command changes the ownership of a file. The basic syntax is as follows.

$ chown user filelist

The value of the user can be either the name of a user on the system or the user id (uid) of a user on the system.

The following example will help you understand the concept.

$ chown amrood testfile
$

Changes the owner of the given file to the user amrood.

NOTE − The super user, root, has the unrestricted capability to change the ownership of any file but normal users can change the ownership of only those files that they own.

Changing Group Ownership


The chgrp command changes the group ownership of a file. The basic syntax is as follows.

$ chgrp group filelist

The value of group can be the name of a group on the system or the group ID (GID) of a group on the system.

Following example helps you understand the concept.

$ chgrp special testfile
$

Changes the group of the given file to special group.

SUID and SGID File Permission


Often when a command is executed, it will have to be executed with special privileges in order to accomplish its task.

As an example, when you change your password with the passwd command, your new password is stored in the file /etc/shadow.

As a regular user, you do not have read or write access to this file for security reasons, but when you change your password, you need to have the write permission to this file. This means that the passwd program has to give you additional permissions so that you can write to the file /etc/shadow.

Additional permissions are given to programs via a mechanism known as the Set User ID (SUID) and Set Group ID (SGID) bits.

When you execute a program that has the SUID bit enabled, you inherit the permissions of that program's owner. Programs that do not have the SUID bit set are run with the permissions of the user who started the program.

This is the case with SGID as well. Normally, programs execute with your group permissions, but instead your group will be changed just for this program to the group owner of the program.

The SUID and SGID bits will appear as the letter "s" if the permission is available. The SUID "s" bit will be located in the permission bits where the owners’ execute permission normally resides.

For example, the command.

$ ls -l /usr/bin/passwd
-r-sr-xr-x  1   root   bin  19031 Feb 7 13:47  /usr/bin/passwd*
$

Shows that the SUID bit is set and that the command is owned by the root. A capital letter S in the execute position instead of a lowercase s indicates that the execute bit is not set.

If the sticky bit is enabled on the directory, files can only be removed if you are one of the following users.

◈ The owner of the sticky directory
◈ The owner of the file being removed
◈ The super user, root

To set the SUID and SGID bits for any directory try the following command.

$ chmod ug+s dirname
$ ls -l
drwsr-sr-x 2 root root  4096 Jun 19 06:45 dirname
$

Wednesday 18 July 2018

Split Command Examples in Unix / Linux

The Split command in unix or linux operating system splits a file into many pieces (multiple files). We can split a file based on the number of lines or bytes. We will see how to use the split command with an example.

Split Command, Unix Command, Linux Command, LPI Study Materials

As an example, let’s take the below text file as the source file which we want to split:

> cat textfile
unix linux os
windows mac os
linux environment

There are three lines in that file and the size of the file is 47 bytes.

Split Command Examples:


1. Splitting file on number of lines.


The Split command has an option -l to split the file based on the number of lines. Let say i want to split the text file with number of lines in each file as 2. The split command for this is

split -l2 textfile

The new files created are xaa and xab. Always the newly created (partitioned) file names start with x. We will see the contents of these files by doing a cat operation.

> cat xaa
unix linux os
windows mac os

> cat xab
linux environment

As there only three lines in the source file we got only one line in the last created file.

2. Splitting file on the number of bytes


We can use the -b option to specify the number of bytes that each partitioned file should contains. As an example we will split the source files on 10 bytes as

split -b10 textfile

The files created are xaa, xab, xac, xad, xae. The first four files contain 10 bytes and the last file contains 7 bytes as the source file size is 47 bytes.

3. Changing the newly created file names from character sequences to numeric sequences.


So far we have seen that the newly created file names are created in character sequences like xaa, Xab and so on. We can change this to numeric sequence by using the -d option as

split -l2 -d textfile

The names of the new files created are x00 and x01.

4. Changing the number of digits in the sequence of filenames.


In the above example, you can observe that the sequences have two digits (00 and 01) in the file names. You can change the number of digits in the sequence by using the -a option as

split -l2 -d -a3 textfile

Now the files created are x000 and x001

Sunday 15 July 2018

Split File Using Awk Command - Unix / Linux

The split command in unix is used for creating fixed size pieces of output files from a given input file. However there is no option in the split command for creating output files based on some conditions on the input file data.

AWS Command, Split Command, Unix Command, Linux Command

The awk command can be used to split a file based on some conditions into multiple output files. Let see this with an example. The following file regions.dat has data about two regions Asia and Europe:

> cat regions.dat
Asia 123
Asia 456
Europe 789
Europe 956

The requirement is to create a separate file for each region. This means one file for Asia region and another file for Europe region.

Split Using Awk Command


The following awk command splits the input file on the region condition and creates two separate files:

> awk '{filename=$1; print $0 > filename;}' regions.dat
> ls 
Asia Europe regions.dat

> cat Asia
Asia 123
Asia 456

> cat Europe
Europe 789
Europe 956

In the above command, we assigned the first column of the input, which is region, to the filename variable and then printed each line to the filename.

Adding Header and Footer Records


We can modify the above command and add header and footer records to each output file. The below awk command adds header and footer records:

> awk '{filename=$1; Curr_filename =$1;if(Curr_filename != Prev_filename) { if(flag == 1) print "footer" > Prev_filename; print "Header" > filename; } else {flag=1} print $0 > filename; Prev_filename=$1} END{print "footer" > filename}' regions.dat

> ls 
Asia Europe regions.dat

> cat Asia
Header
Asia 123
Asia 456
Footer

> cat Europe
Header
Europe 789
Europe 956
Footer

Thursday 12 July 2018

Different Types of Unix files

Unix Files, Linux Files, LPI Certifications, LPI Guides, LPI Learning, LPI Study Materials

There are mainly three types of Unix files. They are

◈ Regular files
◈ Directories
◈ Special or Device files

Regular Files


Regular files hold data and executable programs. Executable programs are the commands (ls) that you enter on the prompt. The data can be anything and there is no specific format enforced in the way the data is stored.

The regular files can be visualized as the leaves in the UNIX tree.

Directories


Directories are files that contain other files and sub-directories. Directories are used to organize the data by keeping closely related files in the same place. The directories are just like the folders in windows operating system.

The kernel alone can write the directory file. When a file is added to or deleted from this directory, the kernel makes an entry.

A directory file can be visualized as the branch of the UNIX tree.

Special Or Device Files


These files represent the physical devices. Files can also refer to computer hardware such as terminals and printers. These device files can also refer to tape and disk drives, CD-ROM players, modems, network interfaces, scanners, and any other piece of computer hardware. When a process writes to a special file, the data is sent to the physical device associated with it. Special files are not literally files, but are pointers that point to the device drivers located in the kernel. The protection applicable to files is also applicable to physical devices.

Thursday 5 July 2018

Kill Command Examples in Unix / Linux

Kill Command, Linux Tutorial and Material, LPI Study Materials

The Kill command in unix or linux operating system is used to send a signal to the specified process or group. If we dont specify any signal, then the kill command passes the SIGTERM signal. We mostly use the kill command for terminating or killing a process. However we can also use the kill command for running a stopped process.

The syntax of kill command is

kill [-s signal] pid
kill -l

The options to the kill command are:

◈ pid : list of process that kill command should send a signal
◈ -s signal : send the specified signal to the process
◈ -l : list all the available signals.

Let see some of the useful kill command examples in unix or linux system.

Kill Command Examples:


1. Listing all the signal names.


Run the kill command with -l option to list all the available signal names.

> kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM USR1 USR2
CLD PWR WINCH URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING
LWP FREEZE THAW CANCEL LOST RTMIN RTMIN+1 RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2
RTMAX-1 RTMAX

Some the important signals which we use in our daily usage ar listed below:

Number Signal Name Description
SIGNULL  Used to check access to the process id 
SIGHUP   Hup signal. Terminates the process. 
SIGNINT  Interrupt signal. Terminating the process 
SIGQUIT  Quit signal. Terminate process with core dump 
SIGKILL  Forcibly killing a process 
24  SIGSTOP  Pausing the process 
26  SIGCONT  Runs a stopped process

To know more about a signal, check in man pages. To know about the signal 9, run the below man command:

man 5 signal

2. Getting the process id


To know the process id of a process running in the unix system, use the ps command as

ps -aef
root  4529   657   0   Jul 20 ?           0:06 /usr/local/sbin/sshd -R
....
....

The second field in the output is the process Id. Here the /usr/local/sbin/sshd -R is running with the process id 4529.

3. Killing a process.


To kill processes simply pass the process id to the kill command. This is shown below:

kill 4529

4. Forcefully killing a process.


Use the -9 option with the kill command to kill a process force fully. The following kill command terminates the process forcefully:

kill -9 1567
kill -SIGKILL 1567
kill -KILL 1567
kill -s SIGKILL 1567
kill -s KILL 1567

Avoid using the kill -9 for terminating a process. This will cause memory leaks in the operating system and leads to many other issues.

Tuesday 3 July 2018

Mail Command Examples in Unix / Linux

Mail Command, Linux Study Materials, LPI Certification, LPI Guides, LPI Tutorials and Materials

The Mail command in unix or linux system is used to send emails to the users, to read the received emails, to delete the emails etc. Mail command will come in handy especially when writing automated scripts. For example, you have written an automated script for taking weekly backup of oracle database. How to know the status of backup, whether it is succeeded or not? In this case, sending an email from the automated script at the end of the backup will be helpful in knowing the status.

The syntax of mail command is:

mail [options] to-address [-- sendmail-options]

The options of mail command are listed below:

-v : Verbose mode. Delivery details are displayed on the terminal.
-s : Specify the subject of the mail
-c : Send carbon copies of the mail to the list of users. This is like cc option in Microsoft outlook.
-b : Send blind copies of the mail to the list of users. This is like bcc option in outlook.
-f : Read the contents of the mailbox
-r : Specify the from address in send mail options.

Mail Command Examples - Sending Emails:


1. Sending sample email to user

The basic functionality of the mail command in unix or linux system is to send an email to the user.

echo "Mail body" | mail -s "Mail subject" to@example.com

Here the echo statement is used for specifying the body of the email. The -s option is used for specifying the mail subject. The mail command sends the email to the user to@example.com

2. Specifying the body in a file

You want to compose a mail which contains 100 lines in the body. Specifying the body with the echo statement is a tedious process. So write the contents of the body in a file and send the mail using one of the following options:

Using cat statement:

cat body.txt | mail -s "Mail subject" to@example.com

Using input redirection operator

mail -s "Mail subject" to@example.com < body.txt

Here the body.txt file contains the body of the email.

3. Send mail to more than one user You can send email to more than one user by specifying the users in comma separated list. 

mail -s "Mail subject" "user1@example.com,user2@example.com" < body.txt

4. Using the cc and bcc option You can copy the emails to more number of users by using the -c and -b options. An example is shown below: 

mail -s "Mail subject" -c "ccuser@gmail.com" -b "bccuser@yahoo.com" "user@example.com" < body.txt

5. Specifying the from address So far the above examples send the emails with from address as the logged in user. You can explicitly specify the from-address using the -r option. 

cat body.txt | mail -s "Mail subject" "to-user@example.com" -- -r "from-user@example.com"

6. Attaching files. The mail command does not provide an option for attaching files. There is a workaround for attaching files using the uuencode command. Pipe the output of uuencode command for attaching files. 

uuencode attachment-file | mail -s "Mail subject" "to-user@example.com" < body.txt

Mail Command Examples - Reading Emails: 


1. Viewing all the received emails Simply type the mail and then press enter to view the received emails. 

mail

Another way of viewing the emails is using the -f option. This is shown below:

> mail -f /var/spool/mail/user

Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/user": 2 messages 2 new
>N  1 root@hostname  Tue May 17 00:00  21/1013  "Mail subject 1"
 N  2 root@hostname  Wed May 18 00:00  21/1053  "Mail subject 2"
&

From the above output, you can see that, it displays the from-address, date and subject of the emails in the inbox. It also displays the ampersand (&) prompt at the end. To go back to the main prompt, type CTRL+z or CTRL+d depending on your operating system and press enter. The ampersand prompt allows you to read, reply, navigate and delete the emails.

2. Reading an email.

To read the Nth email, just enter the mail number at the ampersand prompt and press enter. This is shown below:

> mail -f /var/spool/mail/user

Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/user": 2 messages 2 new
>N  1 root@hostname  Tue May 17 00:00  21/1013  "Mail subject 1"
 N  2 root@hostname  Wed May 18 00:00  21/1053  "Mail subject 2"
&2
Message 2:
From root@hostname  Wed May 18 00:00  21/1053
---------------
Subject: Mail subject 2
------------

This displays the second email details.

3. Navigating through inbox emails. To go to the next email, enter the + symbol. To go back to the previous email, enter the - symbol at the ampersand prompt. 

&-
Message 1:
From root@hostname  Tue May 17 00:00  21/1013
---------------
Subject: Mail subject 1
------------

4. Replying email. Once you have read an email, you can give reply to the mail by typing "reply" and pressing enter. 

&reply
To: root@hostname
    root@hostname
Subject: Re: Mail subject1

5. Deleting emails. You can delete a read email by typing the d and pressing enter. You can also specify the email numbers to d option for deleting them. 

To delete read email
&d
To delete emails 1 and 2
&d 1 2
To delete range emails from 10 to 30
&d 10-30
To delete all emails in the mbox (mail box)
&d *