Unix Commands

LINUX CHECK DISK DRIVE FOR BADBLOCKS AND ERRORS

Bad Sectors are common in a computers hard disk drive. Bad sectors are caused due to damage to the hard drive surface or operating system inability to access the sectors. These bad sectors can be identified by the disk utility softwares. Periodically checking for bad sectors prevents losing of valuable data.

In windows operating system CHKDSK or SCANDISK are used to detect the bad sectors. In unix or linux like operating systems (ubuntu, fedora, debian etc) badblocks utility is used to check for the bad sectors on a hard disk drive.

Here we will see about the badblocks and the fsck utilities. The badblocks utility scans the disk for errors and crates a list of bad sectors. This bad sectors list can be used by programs like fsck and mkfs, so that the OS do not use them in future and thus do not cause any corruption of data.

The following command creates a file with a list of bad sectors:

sudo badblocks -v /dev/disk > bad-blocks.dat

The fsck command is used to record the bad blocks.

sudo fsck -t ext3 -l bad-blocks.dat /dev/disk

Another way to record the bad blocks is to use the e2fsck command with -c option. This prevents the data from being stored on these bad blocks.

e2fsck -c /dev/drive

GREP / PRINT LINES BEFORE AND AFTER MATCH - UNIX / LINUX COMMAND

Q) How to display the lines that are above and below the matched line from a file using unix or linux commands?

The grep command in unix or linux system is used to print the lines that match a given pattern. By default the grep command displays only the matching lines. We can change the behaviour of the grep command to print the lines that are above and below the matched line.

Create the following file in linux or unix server:

> cat sample.dat
unix operating system
linux virtual server
fedora dedicated server
debian system
ubuntu host

Let see the below grep command examples"

1. Print only matched lines

As said earlier the grep command by default only displays the matched lines.

> grep "linux" sample.dat
linux virtual server

2. Print lines after the match

Use the -A option with grep command to print the lines after matched line. The syntax and the example are shown below:

syntax:
grep -An "pattern" filename

Here n is the number of lines to print after the matched line.

Example:

> grep -A1 "fedora" sample.dat
fedora dedicated server
debian system

3. Display lines before the match

To print the lines before the matched line use the -B option with grep command. The syntax and the example are shown below:

Syntax:

grep -Bn "pattern" filename

Example:

> grep -B1 "ubuntu" sample.dat
debian system
ubuntu host

4. Print lines before and after match

We can print both the lines above and below the matched line. Use the -a option with the grep command.

> grep -a1 "fedora" sample.dat
linux virtual server
fedora dedicated server
debian system

READ COMMAND LINE ARGUMENTS - UNIX / LINUX BASH SCRIPT

Q) How to read the arguments or parameters passed to a shell script from the command line?

In general the command line arguments are passed to the bash or shell script to change the behavior of the script. In this article, I am going to provide a simple shell script to read and print the command line parameters. 

Take a look at the following unix shell script:

> cat OS_Print.sh
#!/bin/bash
echo "Script execution starts"
echo "$@"
echo "$0"
echo "$1"
echo "$2"
echo "$#"
echo "Script execution ends"

The basic functionality of the above script is to print the values stored in the $ variables. Now we will run the above script by passing some arguments.

> OS_Print.sh unix linux
Script execution starts
unix linux
OS_Print.sh
unix
linux
2
Script execution ends

You can see, the command line arguments passed here are unix and linux. Command line arguments are a list of parameters separated by space delimiters passed to the shell script.

Explanation of $ variables:
  • $@ : contains all the arguments
  • $0 : contains script name
  • $1 : First argument
  • $2 : Second argument
  • $n : Nth argument
  • $# : Count of arguments passed.

Examples:

1. Script to iterate through arguments.

The following script prints the parameters using for loop.

#!/bin/bash

for value in $@
do
  echo $value
done

2. Print only the last argument.

There are many ways to display the last argument. The following script shows the different ways of printing the last argument.

#!/bin/bash

echo "${@: -1}"
echo "${BASH_ARGV[0]}"
echo "${@: $#}"
echo "${!#}"

for value in $@; do :; done
echo $value


BC COMMAND EXAMPLES IN UNIX / LINUX TUTORIALS

Arithmetic operations are the most common in any kind of programming language. Unix or linux operating system provides the bc command and expr command for doing arithmetic calculations. You can use these commands in bash or shell script also for evaluating arithmetic expressions. 

Here we will see only about the bc command. The bc command evaluates expressions similar to the c programming language. The bc command supports the following features.

  • Arithmetic operators
  • Increment and decrement operators
  • Assignment operators
  • Comparision or Relational Operators
  • Logical or Boolean operators
  • Math Functions
  • Conditional statements
  • Iterative statements
  • Functions
Arithmetic operator Examples:

The following example shows how to use various arithmetic operators. The examples are pretty straight forward. So, I will provide explanation only when required. In most of the examples the echo statment is used to provide the expressions to the bc command.

1. Finding Sum of Two expressions 

> echo "2+5" | bc
7

2. Difference of Two numbers

> echo "10-4" | bc
6

3. Multiplying two numbers

> echo "3*8" | bc
24

4. Dividing two numbers 

When you divide two numbers, the bc command Ignores the decimal part and returns only the integral part as the output. See the below examples

> echo "2/3" | bc
0

> echo "5/4" | bc
1

Use the scale function to specify the number of decimal digits that the bc command should return.

> echo "scale=2;2/3" | bc
.66

5. Finding the remainder using modulus operator

> echo "6%4" | bc
2

6. Using exponent operator

> echo "10^2" | bc
100

Here the expression is evaluated as 10 to the power of 2.

Assignment Operator Examples: 

Assignment operators are used to assign a value to the variable. The following example shows how to use the assignment operators:

Assigns 10 to the variable and prints the value on the terminal.
> echo "var=10;var" | bc

Increment the value of the variable by 5
> echo "var=10; var+=5;var | bc
15

The lists of assignment operators supported are: 
  • var = value   : Assign the value to the variable
  • var += value : similar to var = var + value
  • var -= value  : similar to var = var - value
  • var *= value  : similar to var = var * value
  • var /= value   : similar to var = var / value
  • var ^= value  : similar to var = var ^ value
  • var %= value : similar to var = var % value
Increment Operator Examples: 

There are two kinds of increment operators. They are pre increment and post increment operators. 

  • ++var : Pre increment operator. The variable is incremented first and then the result of the variable is used.
  • var++ : Post increment operator. The result of the variable is used first and then the variable is incremented.
> echo "var=5;++var" | bc
6

> echo "var=5;var++" | bc
5

Here, in the second example the value of var is printed first and then it is incremented. See the below example, to see the complete incremental effect.

> echo "var=5;var++;var" | bc
5
6

Decrement Operator Examples: 

Similar to the increment operators, there are two types of decrement operators.
  • --var : Pre decrement operator. The variable is decremented first and then the result of the variable is used.
  • var-- : Post decrement operator. The result of the variable is used first and then the variable is decremented.
> echo "var=5;--var"| bc
4
> echo "var=5;var--"| bc
5

Relational Operators Examples: 

Relational operators are used to compare two numbers. If the comparison is true, then it returns 1. Otherwise (false), it returns 0. The relational operators are mostly used in conditional statements like if. The list of relational operators supported in bc command are shown below: 

  • expr1 < expr2 : Result is 1 if expr1 is strictly less than expr2.
  • expr1 <= expr2 : Result is 1 if expr1 is less than or equal to expr2.
  • expr1 > expr2 : Result is 1 if expr1 is strictly greater than expr2.
  • expr1 >= expr2 : Result is 1 if expr1 is greater than or equal to expr2.
  • expr1 == expr2 : Result is 1 if expr1 is equal to expr2.
  • expr1 != expr2 : Result is 1 if expr1 is not equal to expr2.

> echo "10 > 5" | bc
1

> echo "1 == 2" | bc
0

Logical Operator Examples:

Logical operators are also mostly used in conditional statements. The result of the logical operators is either 1 (True) or 0 (false) ! expr : Result is 1 if expr is 0. 

  • expr && expr : Result is 1 if both expressions are non-zero.
  • expr || expr : Result is 1 if either expression is non-zero.

> echo "4 && 10" | bc
1
> echo "0 || 0" | bc
0

Math Functions: 

The built-in math functions supported are: 

  • s (x) : The sine of x, x is in radians.
  • c (x) : The cosine of x, x is in radians.
  • a (x) : The arctangent of x, arctangent returns radians.
  • l (x) : The natural logarithm of x.
  • e (x) : The exponential function of raising e to the value x.
  • j (n,x): The bessel function of integer order n of x.
  • sqrt(x): Square root of the number x.

In addition to the math functions, the following functions are also supported. 

  • length(x) : returns the number of digits in x
  • read() : Reads the number from the standard input.

Conditional Statement Examples: 

Conditional statements are used to take decisions and execute statements based on these decisions. Bc command supports the if condition. The syntax of if statement is

if(condition) { statements} else {statements}

The following example shows show to use the if condition

> echo 'if(1 == 2) print "true" else print "false"' | bc
false

Iterative Statements:

Bc command supports the for and while loop for doing iterations. The syntax of for and while loop are shown below:

for (assignment; condition; increment) {
statements
}

while (condition) {
statements
}

The following examples prints numbers from 1 to 10 using the for and while loops

> echo "for(i=1;i<=10;i++) {i;}" | bc
> echo "i=1; while(i<=10) { i; i+=1}" | bc

Functions: 

A function is a code block which executes logically related functionality and returns a value. The syntax of creating a function is

define function-name(comma separated parameters list) {
statements
return statement
}

So far we have provided the arithmetic expressions to the bc command by using the echo statement. We can write these arithmetic expressions in a file and then execute those statements by providing the filename to the bc command. This is shown below:

> cat arth_expr.dat
2+5;
var = 10*3
var
print var
define sum(a,b) {
return a+b
}
sum(3,5)
quit

Now see how to execute these statements:

> bc arth_expr.dat
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
7
30
30
8

Be default the bc command prints the welcome message(version, copyright message. You can suppress this welcome message by using the -q option with bc command

> bc -q arth_expr.dat

Important Points: 

  • Bc command treats the semicolon (;) or newline as the statement separator.
  • To group statements use the curly braces. Use with functions, if statement, for and while loops.
  • If only an expression is specified as a statement, then bc command evaluates the expression and prints the result on the standard output.
  • If an assignment operator is found. Bc command assigns the value to the variable and do not print the value on the terminal.
  • A function should be defined before calling it. Always the function definition should appear first before the calling statements.
  • If a standalone variable is found as a statement, bc command prints the value of the variable. You can also Use the print statement for displaying the list of values on the terminal.



EXPR COMMAND EXAMPLES IN UNIX / LINUX TUTORIALS

This continuation to my previous post on bc command - calculator. In this article we will see how to use the expr command in unix or linux system for doing arithmetic operations.

The syntax of expr command is 
expr [expression]


Let see how to use the expr command in unix or linux with examples. Most of the examples are straightforward. I will provide description as and when required.


Note: You have to provide the space between the values and the operands. Otherwise the expr command may throw error or print them as a string. 

Arithmetic Operator Examples:

1. Sum of numbers 
$ expr 5 + 3

8

$ expr 1 + 2 + 3

6

$ expr 5+3

5+3

Here in the third expr command, space is not provided between the literals. The expr command treated it as a string and printed on the terminal. 

2. Difference between two numbers 
$ expr 10 - 6

4

3. Multiplying numbers 
$ expr 7 \* 9

63

Here the * is shell builtin operator, that is why it needs to escaped with backslash. 

4. Dividing numbers 
$ expr 6 / 4

1

The division operator returns only the arithmetic quotient. 

5. Remainder or modulus 
$ expr 6 % 4

2

Comparision or Relational Operator Examples: 

You can use the following comparision operators with the expr command: 

  • Val1 < Val2 : Returns 1 if val1 is less than val2. otherwise zero.
  • Val1 <= Val2 : Returns 1 if val1 is less than or equal to val2. otherwise zero.
  • Val1 > Val2 : Returns 1 if val1 is greater than val2. otherwise zero.
  • Val1 >= Val2 : Returns 1 if val1 is greater than or equal to val2. otherwise zero.
  • Val1 = Val2 : Returns 1 if val1 is equal to val2. otherwise zero.
  • Val1 != Val2 : Returns 1 if val1 is equal to val2. otherwise zero.
  • val1 | val2 : Returns val1 if val1 is neither null nor zero. Otherwise val2.
  • val1 & val2 : Returns val1 if both val1 and val2 is neither null nor zero. Otherwise 0.

Note: You have to escape most of the operators with backslash as they are shell built in. 
$ expr 1 \< 2

1

$ expr 1 \<= 1

1

$ expr 2 \> 5

0

$ expr 2 \>= 5

0

$ expr 7 = 7

1

$ expr 9 != 18

1

$ expr 2 \| 5

2

$ expr 0 \| 5

5

$ expr 2 \& 5

2

$ expr 6 \& 3

6

$ expr 6 \& 0

0

$ expr 0 \& 3

0


String Function Examples: 

1. Length of string

The length function is used to find the number of characters in a string. 
$ expr length linux

5

$expr length linux\ system

12

$expr length "linux system"

If you have spaces in your string escape them with backslash or quote them with double quotes. 

2. Find Substring 

You can extract a portion of the string by using the substr function. The syntax of substr function is

substr string position length

Here position is the character position in the string. length is the number of chracters to extract from the main string. An example is shown below:

$ expr substr unixserver 5 6
server

3. Index of the substring 

You can find the position of a string in the main string using the index function. The syntax of index function is shown below:

index string chars

If the chars string is found in the main string, then the index function returns the position of the chars. Otherwise it returns 0. See the following examples:

$ expr index linux nux
3

$expr index linux win
0

4. Matching a regexp 

The match function is used to find anchored pattern match of regexp in the string. The syntax of match function is shown below:

match string pattern

The match function returns the number of characters in the pattern is a match is found. Otherwise, it returns 0. Alternative synatx is

string : pattern

The following examples shows how to use the match function:

$ expr match linuxserver lin
3

$ expr match linuxserver server
0

Here in the second expr, the pattern (server) exists in the main string. However the pattern does not start from the beggining of the main string. Thats why the match function returns 0.



ARITHMETIC OPERATIONS / EXPRESSIONS IN UNIX / LINUX BASH SCRIPT

Unix or linux operating systems provides the bc and expr commands for doing arithmetic calculations. In this article, we will see how to do arithmetic operations in the unix shell script. I recommend the following articles before going to use the bc and expr commands in the bash script: 

bc command tutorial 
expr command tutorial 


The following basic examples shows how to do arithmetic calculations in the shell scripts using the bc and expr command:


1. Sum of numbers 

#!/bin/bash
#assigning values to the variables
a=9
b=7
res=`expr $a + $b`
echo $res
res=`echo "$a+$b" | bc`
echo $res
#using the let command to find the sum
let z=$a+$b
echo $z
#using braces
echo $(($a+$b))


2. Difference between two numbers 

#!/bin/bash
#assign values to the variables
a=10
b=7
res=`expr $a - $b`
echo $res
res=`echo "$a-$b" | bc`
echo $res


3. Multiplication and division 

#!/bin/bash
#assign values to the variables
a=2
b=3
res=`expr $a * $b`
echo $res
res=`echo "$a*$b" | bc`
echo $res


4. Length of the string 

#!/bin/bash

str="linux dedicated server"
len=`expr length "$str"`
echo $len


5. Printing numbers from 1 to 10 using for loop 

#!/bin/bash

echo 'for(start=1;start <= 10;start++) {start;}' | bc

FILE TEST OPERATORS / OPERATIONS EXAMPLES IN UNIX / LINUX SHELL SCRIPT

 In linux and unix operating systems every thing is a file. When you are using files in your shell or bash script, it is a good idea to do some tests on the file before using it.

The file tests include:

  • Checking for existence of the file.
  • File is readable, writeable or executable.
  • Type of the file and so on.

The file test operators are mostly used in the if clause of the bash script. The syntax is shown below:

if [ -option filename ]
then
  do something
else
  do something
fi

The different file test operators are listed below:
  • a : True if the file exists.
  • b : True if the file exists and is a block special file.
  • c : True if the file exists and is a character special file.
  • d : True if the file exists and is a directory.
  • e : True if the file exists.
  • f : True if the file exists and is a regular file.
  • g : True if the file exists and its SGID bit is set.
  • h : True if the file exists and is a symbolic link.
  • k : True if the file exists and its sticky bit is set.
  • p : True if the file exists and is a named pipe (FIFO).
  • r : True if the file exists and is readable.
  • s : True if the file exists and has a size greater than zero.
  • t : True if file descriptor is open and refers to a terminal.
  • u : True if the file exists and its SUID (set user ID) bit is set.
  • w : True if the file exists and is writable.
  • x : True if the file exists and is executable.
  • O : True if the file exists and is owned by the effective user ID.
  • G : True if the file exists and is owned by the effective group ID.
  • L : True if the file exists and is a symbolic link.
  • N : True if the file exists and has been modified since it was last read.
  • S : True if the file exists and is a socket.

File Test Operator Example:

The following shell script checks for the existence of a regular file:

#!/bin/bash
#assign file name to the variable
FILE="linux-server.dat"

if [ -f $FILE ]
then
  echo "$FILE exists and is a regular file"
else
  echo "Either $FILE does not exist or is not a regular file"
fi 

REMOVE DUPLICATE STRINGS / WORDS FROM LINE IN UNIX / LINUX

Q) I have the following file with data in my linux system:

$ cat sample.dat
unix,linux,linux,server
unix,unix,dedicated server

Here in the first line the word linux is duplicated. In the second line the pattern unix is duplicated. Now in the output, I want to suppress the duplicates and print the strings only once. The output should look as

unix,linux,server
unix,dedicated server

Solution: 

Here I am providing an awk solution. The below awk command supress the duplicate patterns and prints the pattern only once in each line. 

awk '{str="";c=0;split($0,arr,","); for (v in arr) c++; for (m=c;m >= 1;m--) for (n=1; n<m;n++) if (arr[m] == arr[n]) delete arr[m]; for (k=1;k<=c;k++) {if (k ==1 ) {s=arr[k] } else if (arr[k] != "") str=str" "arr[k] } print str}' sample.dat

The awk command is formatted and shown below:

awk '{
   str="";
   c=0;
   split($0,arr,",");
   for (v in arr) 
      c++;
   for (m=c;m >= 1;m--) 
     for (n=1; n<m;n++) 
        if (arr[m] == arr[n])
           delete arr[m];
   for (k=1;k<=c;k++) 
   {
      if (k ==1 ) 
      {
         s=arr[k]
      } 
      else if (arr[k] != "") 
          str=str""arr[k]"," 
    } 
    print substr(str,1,length(str)-1)
   }' sample.dat

Explanation of the Awk Command:
  1. The split function splits the each line from the file into tokens and stores the tokens as elements in the array (arr).
  2. The for loop is used to find the number of elements in the array. The value c contains the total number of elements in the array.
  3. Next, two for loops are used. The outer for loop is used to read the array elements from the last. The inner for loop reads the elements from the starting of the array. In the if condition, the last element in the array is compared with the remaining elements in the array. If a match is found, then the element is removed from the array. This causes the duplicate words to be removed from the line. This comparison is done for each element in the array and the duplicate elements removed.
  4. The final for loop creates string with all the elements in the array.

TOP COMMAND EXAMPLES IN UNIX / LINUX TUTORIALS

Top command in unix or linux operating system is one of the useful commands to know about the system information. The top command provides real-time view of the running system and also the list of tasks currently managed by the kernel. Top is a non-interactive command and provides limited interactive options to the users. 

The syntax of top command is 
top [options]

The options are: 
  • -b : Starts top command in batch mode. Useful for sending top output to other programs or file.
  • -d : specify the delay time between the screen updates.
  • -n : Number of iterations, the top should produce before ending.
  • -u : Monitor only the specified user processes.
  • -p : Monitor only the specified processes. Specify the process ID

Top Command Examples: 

1. Monitor system information 

The basic functionality of the top command is to monitor the system information. Just run the top command on the terminal to print the system information. 
$ top
top - 19:05:50 up 21 min,  4 users,  load average: 0.02, 0.07, 0.15
Tasks: 174 total,   2 running, 172 sleeping,   0 stopped,   0 zombie
Cpu(s):  5.3%us,  2.6%sy,  0.1%ni, 88.0%id,  3.8%wa,  0.2%hi,  0.1%si,  0.0%st
Mem:   1990204k total,   756084k used,  1234120k free,    74648k buffers
Swap:  3984080k total,        0k used,  3984080k free,   391680k cached

PID  USER  PR  NI VIRT   RES   SHR  S  %CPU %MEM  TIME+   COMMAND
1156 root  20  0  94144  35m   9m   S   10  1.8   1:14.27 Xorg
1908 user  20  0  38604  13m   9428 S   2   0.7   0:03.41 gnome-terminal                          
2315 user  20  0  2468   1092  784  R   2   0.1   0:00.01 top

Top provides dynamic information of the system. As and when the system information changes, it keeps on updating the information on the terminal. The fields of top command are explained below: 
  • PID: Tasks process id.
  • USER: User name of the owner who started the process.
  • PR: priority of the task.
  • NI : Nice value of the task. Negative value means highest priority. Positive value means lowest priority. Zero means priority cant be determined.
  • VIRT: virtual image. Total amount of memory used by the task.
  • RES: Resident size. Non-swapped memory used by the task.
  • SHR : Shared memory used by the task.
  • S : Status of the process.D - uninterruptible sleep;R - running; S - sleeping; T - traced or stopped; Z - zombie.
  • %CPU : Cpu usage.
  • %MEM: Usage of physical memory.
  • TIME: Cpu time. Time of the task since it started.
  • COMMAND: Program name or command name.

2. Redirect top command output to a file. 

If you write the output of top command to a file, the data is written in binary format and is not readable by the user. Use the -b option to write the output of the top command in text format. 
$ top -n 1 -b > top_output.dat

Here the -n 1 option specifies the top command to run for only one iteration.


CHMOD COMMAND EXAMPLES IN UNIX / LINUX TUTORIALS

Chmod (change mode) is one of the most frequently used commands in unix or linux operating system. The chmod command is used to change the file or directory access permissions. To know about the access permissions of a file or directory, use the ls -l command as shown below: 
$ ls -l sample.sh
-rwx-rw-r-- 1 matt deploy 94 Oct  4 03:12 sample.sh

Here in the above example: Use matt has the read, write and execute permissions on the file. Group deploy has read and write permissions. Others have only the read permission. 
File and Directory permissions: 

There are three different permissions. They are: 
  • Read (4): Permitted to read the contents of the file. In case of directory, you can view all the files and sub-directories in that directory.
  • Write (2): Permitted to write to the file. In case of directory, you can create files and sub-directories.
  • Execute (1): Execute the file as a program/shell script. In case of directory, You can enter into that directory.
Here in the above, the numbers in the brackets represents the numeric values for the corresponding permissions. If you want to have a combination of permissions add the required numbers. For example, for read and execute, it is 4+1=5. 

The syntax of chmod command is 
chmod [options] mode filename

THe important options are: 
-R : recursively change the permissions of a directory.
-v : Verbose

Chmod Examples in Linux / Unix: 

1. Give read, write and execute permissions to everyone. 

Read, write and execute: 4+2+1=7 
$ chmod 777 sample.sh

In the above example, you can see that the permissions are specified with a three digit number. The first digit is for user permissions, second is for group and third is for others permission. This type of representation is called octal representation. Alternatively, you can use the symbolic representation to give the permissions. 
chmod ugo+rwx sample.sh

We will see more details about the symbolic representation later. 

2. Give read permission to user, write permission to group and execute permission to others. 
$ chmod 421 sample.sh

3. Recursive permissions to directory 

To give read and write permissions to all the users to a directory (including files and subdirectories) use the recursive option -R. 
chmod -R 666 /dir

Symbolic Representation of Permissions: 

The following symbols are used to represent the users, groups and others: 
  • u : User
  • g : Group
  • o : Others a : All (user, group and others)
The following symbols represent the permissions: 
  • r : read
  • w : write
  • x : execute
The following symbols represent the permissions grant or revoke: 
  • + : Additional permissions. Selected permissions are added.
  • - : Revoke the permissions. Selected permissions are revoked.
  • = : Specific permissions. Only selected permissions are assigned.
Examples: 

1. Remove write permission from group 
$ chmod g-w sample.sh

This will only removes the write permission for the group. 

2. Add new permission execute to others 
$ chmod o+x sample.sh

In addition to the existing permissions, this will add execute permission to others. 

3. Give only read permissions to the user 
$ chmod u=w sample.sh

This will remove the existing permissions to the user and gives only write permission to the user.



TRANSLATE/ TR COMMAND EXAMPLES IN UNIX AND LINUX TUTORIALS

Tr stands for translate or transliterate. The tr utility in unix or linux system is used to translate, delete or squeeze characters. The syntax of tr command is 
tr [options] set1 [set2]

The options of tr command are: 
  • -c : complements the set of characters in string.
  • -d : deletes the characters in set1
  • -s : replaces repeated characters listed in the set1 with single occurrence
  • -t : truncates set1
Tr command Examples:

1. Convert lower case letters to upper case 

The following tr command translates the lower case letters to capital letters in the give string: 
> echo "linux dedicated server" | tr "[:lower:]" "[:upper:]"
LINUX DEDICATED SERVER
> echo "linux dedicated server" | tr "[a-z]" "[A-Z]"
LINUX DEDICATED SERVER

2. Transform upper case letters to lower case. 

Similar to the above example, you can translate the uppercase letters to small letters. 
> echo "UNIX DEDICATED SERVER" | tr "[:upper:]" "[:lower:]"
unix dedicated server
> echo "UNIX DEDICATED SERVER" | tr "[A-Z]" "[a-z]"
unix dedicated server

3. Replace non-matching characters. 

The -c option is used to replace the non-matching characters with another set of characters. 
> echo "unix" | tr -c "u" "a"
uaaa

In the above example, except the character "c" other characters are replaced with "a" 

4. Delete non-printable characters 

The -d option can be used to delete characters. The following example deletes all the non-printable characters from a file. 
> tr -cd "[:print:]" < filename
5. Squeezing characters 

You can squeeze more than one occurrence of continuous characters with single occurrence. The following example squeezes two or more successive blank spaces into a single space. 
> echo "linux    server" | tr -s " "
linux server

Here you can replace the space character with any other character by specifying in set2. 
> "linux    server" | tr -s " " ","
linux,server

6. Delete characters 

The following example removes the word linux from the string. 
> echo "linuxserver" | tr -d "linux"
server


UNIQ COMMAND EXAMPLES IN UNIX AND LINUX TUTORIALS

Uniq command in unix or linux system is used to suppress the duplicate lines from a file. It discards all the successive identical lines except one from the input and writes the output. 

The syntax of uniq command is 
uniq [option] filename

The options of uniq command are:
  • c : Count of occurrence of each line.
  • d : Prints only duplicate lines.
  • D : Print all duplicate lines
  • f : Avoid comparing first N fields.
  • i : Ignore case when comparing.
  • s : Avoid comparing first N characters.
  • u : Prints only unique lines.
  • w : Compare no more than N characters in lines
Uniq Command Examples:

First create the following example.txt file in your unix or linux operating system. 
> cat example.txt
Unix operating system
unix operating system
unix dedicated server
linux dedicated server

1. Suppress duplicate lines 

The default behavior of the uniq command is to suppress the duplicate line. Note that, you have to pass sorted input to the uniq, as it compares only successive lines. 
> uniq example.txt
unix operating system
unix dedicated server
linux dedicated server

If the lines in the file are not in sorted order, then use the sort command and then pipe the output to the uniq command. 
> sort example.txt | uniq

2. Count of lines. 

The -c option is used to find how many times each line occurs in the file. It prefixes each line with the count. 
> uniq -c example.txt
      2 unix operating system
      1 unix dedicated server
      1 linux dedicated server

3. Display only duplicate lines. 

You can print only the lines that occur more than once in a file using the -d option. 
> uniq -d example.txt
unix operating system

> uniq -D example.txt
unix operating system
unix operating system

The -D option prints all the duplicate lines. 

4. Skip first N fields in comparison. 

The -f option is used to skip the first N columns in comparison. Here the fields are delimited by the space character. 
> uniq -f2 example.txt
unix operating system
unix dedicated server

In the above example the uniq command, just compares the last fields. For the first two lines, the last field contains the string "system". Uniq prints the first line and skips the second. Similarly it prints the third line and skips the fourth line. 

5. Print only unique lines. 

You can skip the duplicate lines and print only unique lines using the -u option 
> uniq -u example.txt
unix dedicated server
linux dedicated server



HEAD COMMAND EXAMPLES IN UNIX / LINUX TUTORIALS

The head command in unix or linux system is used to print the first N lines from the file to the terminal. The syntax of head command is 
head [options] [files]

The head command options are: 
  • c : Prints the first N bytes of file; With leading -, prints all but the last N bytes of the file.
  • n : Prints first N lines; With leading - print all but the last N lines of each file.

Head Command Examples: 
Create the following file in your linux or unix operating system for practicing the examples:
> cat example.txt
linux storage
ubuntu os
fedora

1. Display first 10 lines 

By default, the head command prints the first 10 lines from a file. 
> head example.txt

2. Display first N lines 

Use the -n option to print the first n lines from a file. The following example prints the first 2 lines from the file: 
> head -n2 example.txt
linux storage
ubuntu os

3. Skip last N lines 

You can skip the last N lines from a file and print the remaining lines. The following example skips the last 2 lines and prints the remaining lines. 
> head -n-2 example.txt
linux storage

4. Print the first n bytes. 

use the -c option to print the first N bytes from the file. The following example prints the first 5 bytes from the file. 
> head -c5 example.txt
linux

5. Skip printing last n bytes. 

Use the leading "-", to skip printing last N bytes. 
> head -c-7 example.txt
linux storage
ubuntu os

6. Print line between M and N lines. 

You can combine the head command with tail command to print lines between the line numbers M and N. The following command prints the lines between numbers 5 and 10. 
> head -n10 filename | tail -5
Related Posts Plugin for WordPress, Blogger...