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

cal command to display 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 May 2021]

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.

cal command to  display the days of the year
Fig: Calendar for the year 2021

Display the calander for current year

Q: Display the previous, current and next month calendar of the year 1987 August

cal command to show  previous, current and next month calendar

Q: write a command to display the previous, current and next month calendar of the year 2015 december.

Q: What is the command to display the days of the year 2016

Read More

Linux and Unix rm command tutorial with examples
Linux and Unix mv command tutorial with examples
Linux and Unix tail command tutorial with examples

Reference

cal(1) – Linux man page
cal(1) — Linux manual page

Linux and Unix mv command tutorial with examples

“mv” command is used to moves a group of file to a another/different directory.

“mv” command can also use to rename file and directory.

This command does not create a new copy of file , it only rename it.

When we rename it does not consume any additional space on the disk.

Syntax:

Example of mv command in Linux and Unix

Rename File using mv command

Suppose we want to rename a file Mybook01 to MyNotes01 than command is as follows:

Rename Directory using mv command

Suppose we want to rename a directory dir01 to Mydir01 than command is as follows:

Move Number Of Files To a Directory using mv command

Suppose we want to move three files Mybook01 , MyBook02 & MyBook03 into a directory dir01 than command is as follows:

Above command moves three files Mybook01 , MyBook02 & MyBook03 into a directory dir01.

Linux Unix mv -i command

-i, –interactive option is used for interactive mode. It Prompts before override a file

Linux Unix mv -n command

-n , –no-clobber don’t override the file while moving

Linux Unix mv -f command

-f, –force option do not prompt before override

Linux Unix mv -t command

-t , –target-directort=DIRECTORY option moves all to directory

Linux Unix mv -v command

-v –verbose explains what is being done

Read More

Linux and Unix rm command tutorial with examples

Linux and Unix tail command tutorial with examples

Reference

mv(1) – Linux man page

Linux and Unix tail command tutorial with examples

Linux and Unix tail command is one of the important command.

Command tail displays the bottom/end of the file at the terminal.

When “tail” command is used without any option it will display the bottom/end ten lines of the file.

Syntax of tail Command in Unix and Linux


Syntax:

Example of tail Command in Unix and Linux

Suppose we have a file Employee.txt as shown below and we want to display the bottom/end of this file.

By default tail command will display the bottom/end ten lines of the file.

Output

By default tail displayed bottom ten lies of file.

Options of tail command

tail -n option in Unix and Linux

Option (-n): potion “–n” is used to specify the number of lines to be display from bottom. Suppose we want to display the bottom 4 lines of file Employee.txt then the command is as follows:

Or

Output

Option (+ count): potion “+” is used to specify the line number from where the file to be displayed. Suppose we want to display line number 4 to onward of the file Employee.txt then the command is as follows:

Above command will display line number 4 to end of the file of file Employee.txt.

tail -f command option use and example in Unix and Linux

tail -f to show /var/log/auth.log updates

It will show real time file updated if any new entry added then it shows the updated data.

tail -f to show tomcat/logs/catalina.out updates

used to show real time updates in catalina.out

Show n lines with tail -f command

tail -f can be used with -n to show number of lines

tail -n0 -f Shows 0 lines with -f option

tail -n5 -f shows 5 lines with -f option

Tail -f output with line number

tail -f | nl display the file content with line number.

Searching Pattern Using tail -f and grep

we can use grep with tail -f to search specific pattern

Reference

tail -f

tail(1) – Linux man page

Read More

Linux and Unix head command tutorial with examples
Linux and Unix sort command tutorial with examples

Linux and Unix sort command tutorial with examples

Command sort is used to display/arrange the content of the text file in ascending or descending order.
By default, sort command sorts the entire lines.

Syntax:

Example:

Suppose we have a file Employee.txt as shown below and we want to sort the content of the file and display at the terminal. By default sort command sorts the entire lines.

File 1: Employee.txt

Output

Options of sort command

Primary key (-k): Option (–k) is used to sort specific field. Suppose we want to perform shorting according to second field then the command is as follows:

Output:

NOTE: Option –t is used to specify delimiter. Here in the above file Employee.txt, we have used “|” as a delimiter. We can also other delimiters.

Numeric Sort (-n): If the file contains a numeric value and we want to perform shorting on a specific field contains numeric data then –n option is used.
Suppose we want to sort 3rd field of file Employee.txt contains numeric data the command is as follows:

Output

Reverse the Order (-r): Option –r is used to reverse the order of short option.

Other options of sort command:

[table id=8 /]

Linux and Unix head command tutorial with examples

Command head displays the beginning/top of the file at the terminal.
When “head” command is used without any option it will display the beginning/top ten lines of the file.

Syntax:

Example:
Suppose we have a file Employee.txt as shown below and we want to see the beginning/top of this file by default it will display the beginning/top ten lines of the file.

File 1: Employee.txt

Options of head command
Line count option (-n): potion “–n” is used to specify the number of lines to be display. Suppose we want to display the top 4 lines of file Employee.txt then the command is as follows:

Or

Output

Linux and Unix tee command tutorial with examples

Command tee displays the output of the command and at the same time it saves this output in a file specified by the user.
“tee” command can be used anywhere along with pipeline.

Example:

Suppose we have a file Data.txt as shown below and we want to see the content of this file and at the same time save the output/content of the file in a new file User.txt.
We can see the content of the file by using “cat” command.

File: Data.txt

In the above command “cat” will display the content of file Data.txt at the terminal and “tee” command will save this content on Data.txt file as shown below file. If the User.txt does not exist it will create automatically.

File: User.txt
The user.txt file contains the content of file Data.txt but how we can sure. There are two way to confirm that User.txt file contains the content of file Data.txt :

  1. By using cat command
  2. By using cmp (comparison) command

1. By using cat command: cat command display the content of file by which we sure.

2. By using cmp (comparison) command: cmp command compares two files byte by byte. If both files is same/identical cmp command will display no message and simply return the prompt.

Here cmp command compares the output of “ $ cat Data.txt ” to the content of User.txt file.

Linux and Unix rm command tutorial with examples

“rm” command is used to remove/delete files.
this command can delete more than one file at a time.
“rm” command can also delete the specific file from the directory.

Syntax:

Example:

Suppose we want to delete a file Mybook01 then the command is as follows:

Note: File ones delete can’t be regained.

Delete more than one file at a time: “rm” command can also delete more then one file at a time.

Suppose we want to delete files Mybook01, Mybook02 and Mybook03 than the command is as follows:

Above command delete three files (Mybook01, Mybook02 and Mybook03 ) at a time.

Delete all file at a time: “rm” command can also delete all file at a time using *.

Above command delete all files in a directory.

Delete a specific file from a directory:
Suppose we want to delete a file Mybook01 and Mybook02 from Book directory then the command as follows:

Option of “rm” command

Interactive Deletion (i): option –i(interactive) ask and confirms from the user before deleting a file.
Suppose we want to delete two files Myook01 and Mybook02 than interactive deletion option “-i ” ask from the user before delete this files as shown in below command

If we press y then the file will be deleted and if press n then it will leave file undeleted.

Linux and Unix cat command tutorial with examples

cat command is used to see the content of the file.
cat command is also used to concatenate two or more file.
cat command is also used to create new files.

Syntax:

Suppose we have two files Employee.txt and Data.txt as shown below:

File 1: Employee.txt

File 2: Data.txt

Example: To see the content of file write command as follows.

Note: Above command display the content of file Data.txt.
Display more than one file at a time / or concatenate two or more file
“cat” command display more than one file at a time or concatenate more than two files. “cat” command display content of the second file immediately after the first file without any information.

Note: Above command concatenated both Data.txt, Employee.txt files. Or it displays the content of two files one after another immediately after the first file without any information.
Create a new file using cat command: To create a new file write cat command followed by “>” sign and then the name of the file.

When we enter command and press enter new file will be created (in above command new file is “myfile.txt will create ) and it will wait for input/ or write something on newly created file.
We have entered two lines it will write on myfile.txt.
When the write is over press “ctrl+d” to terminate cat command.

Linux and Unix ls command tutorial with examples

ls command list all the file and directory name present in the current directory.
Syntax:

Example:
Print the name of all the directory and file available in the current directory.

Above command display four file and directory name.

Check availability of file or directory: We can check whether a particular file or directory is present in the current directory or not. To know we just specify a file name with ls command.

If Employee Record file is not available, the system will clearly say:

Details of file and directory (-l option): We can check the details of the file with (-l) option

First character “-“ or “d”: The first character represents the type of file. The symbol “-“represents a regular file and “d” denotes a directory.
Next Nine Character: Next 9 character (for example rwxr-xr– )represent the file or directory permission permissions.
The first group of 3 characters(for example rwxr-xr–) represent permissions for owner means the owner has a permission to read, write and execute.
The second group of 3 characters(for example rwxr-xr–) represent permissions for members of the file group means the group has a permission only to read and execute.
The third group of 3 characters (for example rwxr-xr–) represent permissions for others means others have a permission only to read a file.

Second column: Represent the of the number of links to that file.
Third column: Represent the owner of the file.
Fourth Column: Represent the group of the file.
Fifth Column: Represent the size of the file. Here file size for the file 2.txt is 13 bytes.
Sixth Column: Represent the last modified date and time of the file or directory.
Seventh column: Tells the name of the file or directory in current directory.

Options of ls command

[table id=7 /]

Linux and Unix cd command tutorial with examples

Command cd stands for Change Directory.
You can change directory or move from one directory to another directory by using cd command.

Syntax:

Example:
pwd command display the absolute path name of current directory. To see the current directory absolute path press pwd command on terminal.

We are currently in “ it ’ directory which resides in home directory.

Suppose we want to move to directory “abc” . Note directory “abc” must be in the current directory. The command to move in the current directory shown below

Now we are inside the directory “abc”. To confirm press command “pwd”.

Back To Home Directory: Suppose now we are inside sub-sub directory and willing to go back to the home directory the simply press command cd.

Change Directory With Absolute Path Name: Suppose we want to move in the random directory which is not present in the current directory then absolute pathname must be specified with “cd” command.