Linux and Unix mkdir command tutorial with examples

mkdir stands for make directory.
In the UNIX file system, we can create a new directory by using mkdir command.

Syntax:

Note: In above syntax in the place of [directory name] we will specify the name of directory to be created.

Example:

The directory abc is created. To confirm directory is created press “ls” command which prints the name of all files and directory along with directory “abc” in the current directory.

Create multiple directory: We can create multiple directory with mkdir. Suppose we want to create 3 directory abc1, abc2 and abc3 then the command to create three directories as shown below.

Create subdirectory: We can create directory inside another directory by using single invocation of command.
Suppose we want to create directory abc and subdirectory abc1 and abc2 inside the directory abc then the command is as follows.

Note: The order of specifying argument important because we can not create subdirectory without creating its parent directory. For example we can not write

Command mkdir failed to create two subdirectory abc1 and abc2 but it will create directory acb.

Linux and Unix passwd command tutorial with examples

passwd command stands for the password.
By using passwd command we can change the password of a user account in Unix system.

Steps for change password.
1. Enter the command “passwd” on the terminal and press enter.
2. System asks for old Unix password then the user will enter old Unix password.
3. System asks for new Unix password then the user will enter/set the new password.
4. System asks to retype new password then the user will retype new password.

Example:

Linux and Unix bc command tutorial with examples

bc command is used to perform the basic calculation.

Example:

To add two number

write “bc” command on terminal and press enter then specify two number as mention below for addition.

Note: bc command print the result of computation in next line as shown above.

Perform multiple calculations: we can perform multiple calculations on the same line by using “; ”delimiter in bc command.
As shown below we perform three calculations (multiplication, subtraction, power) each separated by ; as the delimiter.

Note: Result of each calculation display in the separate line as shown above. ^ indicates “to the power of”.

bc command performs only integer computation: bc command can perform integer computation and if decimal portion comes in computation it truncates decimal portion. For example:

For decimal calculation by using bc command: for the decimal calculation we have to set “scale” to the number of digits of precision as shown below. We must set the scale before calculation.

Linux and Unix wc command tutorial with examples

“ wc ” command stands for “word count”.
“ wc “ command in Unix/Linux OS is used to calculate/find out and print the number of lines, number of words, number of character and byte of a file.

Syntax:
$ wc [option] [filename]

Example: There is a sample file file01.txt.

Type command on terminal…

The above command displays the output “ 3 11 44 “. Where 3 is a number of lines, 11 is the number of words and 44 is a number of character on a specified file “ file01.txt”.

Options of “wc” command are:

Sr NoOptionMeaning
1-l
used to print the number of lines in a file.
2-w
used to print the number of words in a file
3-c

used to print the number of character in a file.
4-Lused to print the length of longest line.

(-l) option: wc command with –l option is used to print the number of lines in a file. Type below command on terminal

The above command displays the output “3”. Where 3 is a number of lines on a specified file “ file01.txt”.

(-w) option: wc command with –w option is used to print the number of words in a file. Type below command on terminal…

The above command displays the output “ 11 “. Where 11 is the number of words in a specified file “file01.txt”.

(-c) option: wc command with –c option is used to print the number of character in a file. Type below command on terminal…

The above command displays the output “ 44 “. Where 44 is a number of character of words in a specified file “ file01.txt”.

Linux and Unix printf command tutorial with examples

  • Like a C language, in UNIX “ printf ” is used to print the statement.
  • “ printf “ command is an alternative of “echo” command.

Print statement: To print any statement write “printf” command in terminal and the in the double quote ” ” write a sentence to be print. Whatever you will write in double quote will be print on the terminal.

For example: in below command, we try to print “Enter any number=” on the terminal.

Escape Sequence: “printf” support all escape sequence used by “ echo ” command.
Example: “\n” escape sequence is used for the new line. Means \n direct to place the cursor at the new line as shown in below command.

Formatted String: like a C language, Unix “ printf “ command also support formatted strings.
Like a C language, %s is used for printing string.

Note: Value of variable x will not print because formatted string %s is not used in above command. To print value of x write command as:

Print integer variable value using formatted string:

Note: Value of variable x will not print because formatted string %d is not used in above command.
To print value of x write command as:

Linux and Unix echo command tutorial with examples

“echo” command in Unix often used in the shell script for display/print message on the terminal.
There are two uses of “echo” command.

  • “echo” is used for display/print message on terminal.
  • “echo” is also used for evaluate shell variable.

“echo” is used for display/print message on terminal

To print the message on terminal press echo command on terminal and message to print place within “ “ double quote as shown below.

output

Escape sequence: “echo” command interprets certain string as a escape sequence.
The escape sequence is generally (not necessarily ) a two character string which starts with a backslash ( \ ).
For example: “ \c “ is a one escape sequence and if we place “ \c ” escape sequence at the end of the line/string then this escape sequence place the cursor on the same line that displays the output.

output

Note: echo escape sequence are the feature of VSBD system. Linux Bash shell, the standard shell used in Linux interprets escape sequence if echo command is used with “–e” option

Other Escape sequences:

Sr NoEscape SequenceMeaning
1\a

Bell
2\bBackspace
3\cNo new line
4\n
New line
5\t

Tab
6\vVertical Tab

“echo” is also used to evaluate shell variable.
Assign value to variable : we can assign a value to variable on terminal as

output

Note: When we assign a value to a variable must remember there is no space either side of = sign.

Evaluate the value of a variable: to evaluate the value of variable write “echo” then place $ sign before variable as shown below:

output

Linux and Unix date command tutorial with examples

Command “date” is used to display the date of the system.
To see the current date type the command “ date” on the terminal as shown in below

output

The above command will display current day, date, month, year and time as shown above.

Option:
date command can also be used with format specified .
Each format is preceded by + symbol and followed by % operator and format described as a single character.
We want to see only current month use format +%m. as shown below.

output

output is month 01

We want to see only currant month name use format +%h. as show below.

output

output is month January

We can also combine both command as:

output

output is month 01 is January

Other options of date command

Sr NoOptionDescription
1doption used for display date of the month (1 to 31).
2yoption used for display last two digits of current year.
3Hoption used for display hour
4Moption used for display minute
5Soption used for display second
6Doption used for display date in the format mm/dd/yy
7Toption used for display the time in the format hh:mm:ss

Linux and Unix cal command tutorial with examples

Command “cal” stands for calendar.
This command is used to see the calendar of a specific month/ year or both.
The basic syntax of command “cal”.

[month]- is used for specify month
[year] – is used for specify year

To see the calendar of current month

type “cal” command on terminal and press enter and output will be calendar of the current month

Output:

To see the calendar of specific month/Year [for example march 2019]

type below command in terminal.

Output:

To see the calendar of the specific year(all 12 months)
To see the calendar of the year 2012. Type “cal” command and attach desired year as shown below

Output

In output calendar of 2012(all 12 months) will display in the terminal.