Home » linux

Category Archives: linux

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:

Sr NoOptionMeaning
1-kSort nth field
2-fFold lower case to upper case characters
3-n Numerical value sort
4-rReverse the shorted order
5-cCheck if the file is sorted
6-uRepeated lines removes
7-mMerge sorted file in the list

Linux and Unix tail command tutorial with examples

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:

Example:

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

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.

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 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 :

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

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

Move Number Of Files To a Directory

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 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

Sr NoOptionsDescription
1-aShow all the files start starting with '.' dot
2-FDenote executable file with *, directory with / and symbolic line with @
3-lRepresent the details of file/directory.
4-rShort list in reverse order
5-RRepresent the recursive list.
6-sRepresent the size of the file.
7-Ssort by size of file
8-tsort by date and time of the system.
9-Xsort by extension.

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.

Linux and Unix rmdir command tutorial with examples

rmdir stands for remove directory.
In the UNIX file system, we can remove directory by using rmdir command.

Syntax:

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

Example:

The directory abc is removed. To confirm directory is created press “ls” command which prints the name of all files and directory.
Remove multiple directories: We can remove multiple directories with the single invocation of the command. Suppose we want to remove three directory abc1, abc2 and abc3 then the command for removing three directories shown in below.

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

Above command remove directory abc and subdirectory abc1 and abc2.

Note: The order of specifying argument important because we can not remove directory without removing its subdirectory. For example, we can not write

Command rmdir failed to remove directory abc because abc is not empty but it will remove two subdirectory abc1 and abc2.

Managed by MochaHost - Fast & Easy WordPress Hosting