Sie sind auf Seite 1von 168

Basic Unix and Linux Commands With Examples

Learning unix operating system is very easy. It is just that you need to understand the unix server
concepts and familiar with the unix commands. Here I am providing some important unix commands
which will be used in daily work.
Unix Commands With Examples:
1. Listing files
The first thing after logging into the unix system, everyone does is listing the files in a directory. The
ls command is used to list the files in a directory.

>ls

add.sh
logfile.txt
prime.pl

If you simply execute ls on the command prompt, then it will display the files and directories in the
current directory.

>ls /usr/local/bin

You can pass a directory as an argument to ls command. In this case, the ls command prints all the
files and directories in the specific directory you have passed.
2. Displaying the contents of a file.
The next thing is to display the contents of a file. The cat command is used to display the contents in
a file.

>cat file.txt
This is a sample unix file
Learning about unix server is awesome

3. Displaying first few lines from a file.


The head command can be used to print the specified number of lines from the starting of a file. The
below head command displays the first five lines of file.

>head -5 logfile.dat

4. Displaying last few lines from a file.


The tail command can be used to print the specified number of lines from the ending of a file. The
below tail command displays the last three lines of file.

>tail -3 logfile.dat

5. Changing the directories


The cd command can be used to change from one directory to another directory. You need to specify
the target directory where you want to go.

>cd /var/tmp

After typing this cd command you will be in /var/tmp directory.


6. Creating a file.

The touch command simply creates an empty file. The below touch command creates a new file in
the current directory.

touch new_file.txt

7. copying the contents of one file into another.


The cp command is used to copy the content of source file into the target file. If the target file already
have data, then it will be overwritten.

>cp source_file target_file

8. Creating a directory.
Directories are a way of organizing your files. The mkdir command is used to create the specified
directory.

>mkdir backup

This will create the backup directory in the current directory.


9. Renaming and moving the files.
The mv command is used to rename the files and it also used for moving the files from one directory
into another directory.

Renaming the file.

>mv file.txt new_file.txt

Moving the file to another directory.

>mv new_file.txt tmp/

10. Finding the number of lines in a file


The wc command can be used to find the number of line, words and characters in a file.

>wc logfile.txt
21

26 198 logfile.txt

To know about the unix command, it is always good to see the man pages. To see the man pages
simply pass the command as an argument to the man.

man ls

Converting Awk Script to Perl Script - Examples of a2p Unix


Command

Unix provides the a2p (awk to perl) utility for converting the awk script to perl script. The a2p
command takes an awk script and produces a comparable perl script.
Syntax of a2p:
a2p [options] [awk_script_filename]
Some of the useful options that you can pass to a2p are:

-D<number> Sets debugging flags.


-F<character> This will tell a2p that awk script is always invoked with -F option.
-<number>
This makes a2p to assume that input will always have the specified number of fields.
For more options see the man pages; man a2p
Example1:
The awk script which prints the squares of numbers up to 10 is shown below. Call the below script as
awk_squares.

#!/bin/awk -f
BEGIN
{
for (i=1; i <= 10; i++)
{
print "The square of ", i, " is ", i*i;
}
exit;
}

Run this script using awk command; awk -f awk_squares. This will produce squares of numbers up
to 10.
Now we will convert this script using the a2p as
a2p awk_squares > perl_squares
The content of converted perl script, perl_squares, is shown below:

#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"mailto:$@%22%7D'

if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches
$, = ' ';

# set output field separator

$\ = "\n";

# set output record separator

for ($i = 1; $i <= 10; $i++) {


print 'The square of ', $i, ' is ', $i * $i;
}
last line;

Run the perl script as: perl perl_squares. This will produce the same result as the awk.
Example2:
We will see an awk script which prints the first field from a file. The awk script for this is shown below.
Call this script at awk_first_field.

#!/bin/awk -f
{
print $1;
}

Run this script using awk command by passing a file as input: awk -f awk_first_field file_name. This
will prints the first field of each line from the file_name.
We will convert this awk script into per script using the a2p command as

a2p awk_first_field > perl_first_field


The content of converted perl script, perl_first_field, is shown below:

#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"mailto:$@%22%7D'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches
$, = ' ';

# set output field separator

$\ = "\n";

# set output record separator

while (<>) {
($Fld1) = split(' ', $_, -1);
print $Fld1;
}

Now run the perl script as: perl perl_first_field file_name. This will produce the same result as awk
command.
If you like this post, please share it by clicking on +1 Button.

Examples of Alias Command in Unix

Alias command is an alternative name used for long strings that are frequently used. It is mostly
used for creating a simple name for a long command.
Syntax of alias command:

alias [alias_name=['command']]
For more information on alias utility see the man pages. Type 'man alias' on the command prompt.
Examples:
1. alias
If you simply type alias on the command prompt and then enter, it will list all the aliases that were
created.
2. alias pg='ps -aef'
The ps -aef command will list all the running processes. After creating the alias pg for ps -aef, then
by using the pg on command prompt will display the running processes. The pg will work same as
the ps -aef.
By creating an alias for a command on the command prompt will be present only for that session.
Once you exit from the session, then the aliases wont take effect. To make the aliases to remain
permanent, place the alias command in the ".profile" of the user. Open the user ".profile" and place
the command alias pg="ps -aef", save the file and then source the ".profile" file. Now the alias pg will
remain forever.
To remove an alias use the unalias command
Example: unalias pg

Examples of Basename Command in Unix


The basename utility is used to

Remove any prefix ending in /.

Remove the suffix from a string.


Syntax of basename command:
basename [string] [suffix]
Here 'string' is the input string and suffix is the string which needs to removed from the input string.
Examples:

1. basename /usr/bin/perlscript
This will remove the prefix, /usr/bin/, and prints only the string 'perlscript'
2. basename perlscript script
This will remove the suffix 'script' from 'perlscript' and prints only 'perl'
3. basename /usr/bin/perlscript script
This will remove both the prefix and suffix and prints only 'perl'
basename command is mostly used in shell scripts to get the name of the shell script file you are
running. Sample shell script code is shown below

#!/usr/bin/sh
filename=`basename $0`
echo $filename

Top Examples of Awk Command in Unix


Awk is one of the most powerful tools in Unix used for processing the rows and columns in a file.
Awk has built in string functions and associative arrays. Awk supports most of the operators,
conditional blocks, and loops available in C language.
One of the good things is that you can convert Awk scripts into Perl scripts using a2p utility.
The basic syntax of AWK:

awk 'BEGIN {start_action} {action} END {stop_action}' filename

Here the actions in the begin block are performed before processing the file and the actions in the
end block are performed after processing the file. The rest of the actions are performed while
processing the file.
Examples:
Create a file input_file with the following data. This file can be easily created using the output of ls -l.

-rw-r--r-- 1 center center

0 Dec

8 21:39 p1

-rw-r--r-- 1 center center 17 Dec

8 21:15 t1

-rw-r--r-- 1 center center 26 Dec

8 21:38 t2

-rw-r--r-- 1 center center 25 Dec

8 21:38 t3

-rw-r--r-- 1 center center 43 Dec

8 21:39 t4

-rw-r--r-- 1 center center 48 Dec

8 21:39 t5

From the data, you can observe that this file has rows and columns. The rows are separated by a
new line character and the columns are separated by a space characters. We will use this file as the
input for the examples discussed here.
1. awk '{print $1}' input_file
Here $1 has a meaning. $1, $2, $3... represents the first, second, third columns... in a row
respectively. This awk command will print the first column in each row as shown below.

-rw-r--r--rw-r--r--rw-r--r--

-rw-r--r--rw-r--r--rw-r--r--

To print the 4th and 6th columns in a file use awk '{print $4,$5}' input_file
Here the Begin and End blocks are not used in awk. So, the print command will be executed for each
row it reads from the file. In the next example we will see how to use the Begin and End blocks.
2. awk 'BEGIN {sum=0} {sum=sum+$5} END {print sum}' input_file
This will prints the sum of the value in the 5th column. In the Begin block the variable sum is
assigned with value 0. In the next block the value of 5th column is added to the sum variable. This
addition of the 5th column to the sum variable repeats for every row it processed. When all the rows
are processed the sum variable will hold the sum of the values in the 5th column. This value is
printed in the End block.
3. In this example we will see how to execute the awk script written in a file. Create a file
sum_column and paste the below script in that file

#!/usr/bin/awk -f
BEGIN {sum=0}
{sum=sum+$5}
END {print sum}

Now execute the the script using awk command as


awk -f sum_column input_file.
This will run the script in sum_column file and displays the sum of the 5th column in the input_file.

4. awk '{ if($9 == "t4") print $0;}' input_file


This awk command checks for the string "t4" in the 9th column and if it finds a match then it will print
the entire line. The output of this awk command is

-rw-r--r-- 1 pcenter pcenter 43 Dec

8 21:39 t4

5. awk 'BEGIN { for(i=1;i<=5;i++) print "square of", i, "is",i*i; }'


This will print the squares of first numbers from 1 to 5. The output of the command is

square of 1 is 1
square of 2 is 4
square of 3 is 9
square of 4 is 16
square of 5 is 25

Notice that the syntax of if and for are similar to the C language.
Awk Built in Variables:
You have already seen $0, $1, $2... which prints the entire line, first column, second column...
respectively. Now we will see other built in variables with examples.
FS - Input field separator variable:
So far, we have seen the fields separted by a space character. By default Awk assumes that fields in
a file are separted by space characters. If the fields in the file are separted by any other character,

we can use the FS variable to tell about the delimiter.


6. awk 'BEGIN {FS=":"} {print $2}' input_file
OR
awk -F: '{print $2} input_file
This will print the result as

39 p1
15 t1
38 t2
38 t3
39 t4
39 t5

OFS - Output field separator variable:


By default whenever we printed the fields using the print statement the fields are displayed with
space character as delimiter. For example
7. awk '{print $4,$5}' input_file
The output of this command will be

center 0
center 17
center 26
center 25

center 43
center 48

We can change this default behavior using the OFS variable as


awk 'BEGIN {OFS=":"} {print $4,$5}' input_file

center:0
center:17
center:26
center:25
center:43
center:48

Note: print $4,$5 and print $4$5 will not work the same way. The first one displays the output with
space as delimiter. The second one displays the output without any delimiter.
NF - Number of fileds variable:
The NF can be used to know the number of fields in line
8. awk '{print NF}' input_file
This will display the number of columns in each row.
NR - number of records variable:
The NR can be used to know the line number or count of lines in a file.
9. awk '{print NR}' input_file
This will display the line numbers from 1.

10. awk 'END {print NR}' input_file


This will display the total number of lines in the file.
String functions in Awk:
Some of the string functions in awk are:
index(string,search)
length(string)
split(string,array,separator)
substr(string,position)
substr(string,position,max)
tolower(string)
toupper(string)
Advanced Examples:
1. Filtering lines using Awk split function
The awk split function splits a string into an array using the delimiter.
The syntax of split function is
split(string, array, delimiter)
Now we will see how to filter the lines using the split function with an example.
The input "file.txt" contains the data in the following format

1 U,N,UNIX,000
2 N,P,SHELL,111
3 I,M,UNIX,222
4 X,Y,BASH,333
5 P,R,SCRIPT,444

Required output: Now we have to print only the lines in which whose 2nd field has the string "UNIX"
as the 3rd field( The 2nd filed in the line is separated by comma delimiter ).
The ouptut is:

1 U,N,UNIX,000
3 I,M,UNIX,222

The awk command for getting the output is:

awk '{
split($2,arr,",");
if(arr[3] == "UNIX")
print $0
} ' file.txt

Recommended Posts:

Find Command
Grep Command
Awk command Examples - Part2

Find Command in Unix and Linux Examples

Find is one of the powerful utility of Unix (or Linux) used for searching the files in a directory
hierarchy. The syntax of find command is

find [pathnames] [conditions]

Let see some practical exercises on using find command.


1. How to run the last executed find command?

!find

This will execute the last find command. It also displays the last find command executed along with
the result on the terminal.
2. How to find for a file using name?

find -name "sum.java"


./bkp/sum.java
./sum.java

This will find all the files with name "sum.java" in the current directory and sub-directories.
3. How to find for files using name and ignoring case?

find -iname "sum.java"

./SUM.java
./bkp/sum.java
./sum.java

This will find all the files with name "sum.java" while ignoring the case in the current directory and
sub-directories.
4. How to find for a file in the current directory only?

find -maxdepth 1 -name "sum.java"


./sum.java

This will find for the file "sum.java" in the current directory only
5. How to find for files containing a specific word in its name?

find -name "*java*"


./SUM.java
./bkp/sum.java
./sum.java
./multiply.java

It displayed all the files which have the word "java" in the filename
6. How to find for files in a specific directory?

find /etc -name "*java*"

This will look for the files in the /etc directory with "java" in the filename
7. How to find the files whose name are not "sum.java"?

find -not -name "sum.java"


.
./SUM.java
./bkp
./multiply.java

This is like inverting the match. It prints all the files except the given file "sum.java".
8. How to limit the file searches to specific directories?

find -name "sum.java"


./tmp/sum.java
./bkp/var/tmp/files/sum.java
./bkp/var/tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java

You can see here the find command displayed all the files with name "sum.java" in the current
directory and sub-directories.
a. How to print the files in the current directory and one level down to the current directory?

find -maxdepth 2 -name "sum.java"


./tmp/sum.java
./bkp/sum.java
./sum.java

b. How to print the files in the current directory and two levels down to the current directory?

find -maxdepth 3 -name "sum.java"


./tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java
./sum.java

c. How to print the files in the subdirectories between level 1 and 4?

find -mindepth 2 -maxdepth 5 -name "sum.java"


./tmp/sum.java
./bkp/var/tmp/files/sum.java

./bkp/var/tmp/sum.java
./bkp/var/sum.java
./bkp/sum.java

9. How to find the empty files in a directory?

find . -maxdepth 1 -empty


./empty_file

10. How to find the largest file in the current directory and sub directories

find . -type f -exec ls -s {} \; | sort -n -r | head -1

The find command "find . -type f -exec ls -s {} \;" will list all the files along with the size of the file.
Then the sort command will sort the files based on the size. The head command will pick only the
first line from the output of sort.
11. How to find the smallest file in the current directory and sub directories

find . -type f -exec ls -s {} \; | sort -n -r | tail -1

Another method using find is

find . -type f -exec ls -s {} \; | sort -n

12. How to find files based on the file type?


a. Finding socket files

find . -type s

b. Finding directories

find . -type d

c. Finding hidden directories

find -type d -name ".*"

d. Finding regular files

find . -type f

e. Finding hidden files

| head -1

find . -type f -name ".*"

13. How to find files based on the size?


a. Finding files whose size is exactly 10M

find . -size 10M

b. Finding files larger than 10M size

find . -size +10M

c. Finding files smaller than 10M size

find . -size -10M

14. How to find the files which are modified after the modification of a give file.

find -newer "sum.java"

This will display all the files which are modified after the file "sum.java"
15. Display the files which are accessed after the modification of a give file.

find -anewer "sum.java"

16. Display the files which are changed after the modification of a give file.

find -cnewer "sum.java"

17. How to find the files based on the file permissions?

find . -perm 777

This will display the files which have read, write, and execute permissions. To know the permissions
of files and directories use the command "ls -l".
18. Find the files which are modified within 30 minutes.

find . -mmin -30

19. Find the files which are modified within 1 day.

find . -mtime -1

20. How to find the files which are modified 30 minutes back

find . -not -mmin -30

21. How to find the files which are modified 1 day back.

find . -not -mtime -1

22. Print the files which are accessed within 1 hour.

find . -amin -60

23. Print the files which are accessed within 1 day.

find . -atime -1

24. Display the files which are changed within 2 hours.

find . -cmin -120

25. Display the files which are changed within 2 days.

find . -ctime -2

26. How to find the files which are created between two files.

find . -cnewer f1 -and ! -cnewer f2

So far we have just find the files and displayed on the terminal. Now we will see how to perform
some operations on the files.
1. How to find the permissions of the files which contain the name "java"?

find -name "*java*"|xargs ls -l

Alternate method is

find -name "*java*" -exec ls -l {} \;

2. Find the files which have the name "java" in it and then display only the files which have "class"
word in them?

find -name "*java*" -exec grep -H class {} \;

3. How to remove files which contain the name "java".

find -name "*java*" -exec rm -r {} \;

This will delete all the files which have the word java" in the file name in the current directory and
sub-directories.
Similarly you can apply other Unix commands on the files found using the find command. I will add
more examples as and when i found.

Grep Command in Unix and Linux Examples


Grep is the frequently used command in Unix (or Linux). Most of us use grep just for finding the
words in a file. The power of grep comes with using its options and regular expressions. You can
analyze large sets of log files with the help of grep command.
Grep stands for Global search for Regular Expressions and Print.
The basic syntax of grep command is
grep [options] pattern [list of files]
Let see some practical examples on grep command.
1. Running the last executed grep command
This saves a lot of time if you are executing the same command again and again.

!grep

This displays the last executed grep command and also prints the result set of the command on the
terminal.
2. Search for a string in a file
This is the basic usage of grep command. It searches for the given string in the specified file.

grep "Error" logfile.txt

This searches for the string "Error" in the log file and prints all the lines that has the word "Error".
3. Searching for a string in multiple files.

grep "string" file1 file2


grep "string" file_pattern

This is also the basic usage of the grep command. You can manually specify the list of files you want
to search or you can specify a file pattern (use regular expressions) to search for.
4. Case insensitive search
The -i option enables to search for a string case insensitively in the give file. It matches the words
like "UNIX", "Unix", "unix".

grep -i "UNix" file.txt

5. Specifying the search string as a regular expression pattern.

grep "^[0-9].*" file.txt

This will search for the lines which starts with a number. Regular expressions is huge topic and I am
not covering it here. This example is just for providing the usage of regular expressions.
6. Checking for the whole words in a file.
By default, grep matches the given string/pattern even if it found as a substring in a file. The -w
option to grep makes it match only the whole words.

grep -w "world" file.txt

7. Displaying the lines before the match.


Some times, if you are searching for an error in a log file; it is always good to know the lines around
the error lines to know the cause of the error.

grep -B 2 "Error" file.txt

This will prints the matched lines along with the two lines before the matched lines.
8. Displaying the lines after the match.

grep -A 3 "Error" file.txt

This will display the matched lines along with the three lines after the matched lines.
9. Displaying the lines around the match

grep -C 5 "Error" file.txt

This will display the matched lines and also five lines before and after the matched lines.
10. Searching for a sting in all files recursively

You can search for a string in all the files under the current directory and sub-directories with the help
-r option.

grep -r "string" *

11. Inverting the pattern match


You can display the lines that are not matched with the specified search sting pattern using the -v
option.

grep -v "string" file.txt

12. Displaying the non-empty lines


You can remove the blank lines using the grep command.

grep -v "^$" file.txt

13. Displaying the count of number of matches.


We can find the number of lines that matches the given string/pattern

grep -c "sting" file.txt

14. Display the file names that matches the pattern.


We can just display the files that contains the given string/pattern.

grep -l "string" *

15. Display the file names that do not contain the pattern.
We can display the files which do not contain the matched string/pattern.

grep -L "string" *

16. Displaying only the matched pattern.


By default, grep displays the entire line which has the matched string. We can make the grep to
display only the matched string by using the -o option.

grep -o "string" file.txt

17. Displaying the line numbers.


We can make the grep command to display the position of the line which contains the matched string
in a file using the -n option

grep -n "string" file.txt

18. Displaying the position of the matched string in the line


The -b option allows the grep command to display the character position of the matched string in a
file.

grep -o -b "string" file.txt

19. Matching the lines that start with a string


The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the
lines which start with the given string or pattern.

grep "^start" file.txt

20. Matching the lines that end with a string


The $ regular expression pattern specifies the end of a line. This can be used in grep to match the
lines which end with the given string or pattern.

grep "end$" file.txt

Date Command in Unix and Linux Examples


Date command is used to print the date and time in unix. By default the date command displays the
date in the time zone that the unix operating system is configured.
Now let see the date command usage in unix
Date Command Examples:
1. Write a unix/linux date command to print the date on the terminal?

>date
Mon Jan 23 01:37:51 PST 2012

This is the default format in which the date command print the date and time. Here the unix server is
configured in pacific standard time.
2. Write a unix/linux date command to print the date in GMT/UTC time zone?

>date -u
Mon Jan 23 09:40:21 UTC 2012

The -u option to the date command tells it to display the time in Greenwich Mean Time.
3. Write a unix/linux date command to sett the date in unix?
You can change the date and time by using the -s option to the date command.

>date -s "01/01/2000 12:12:12"

4. Write a unix/linux date command to display only the date part and ignore the time part?

>date '+%m-%d-%Y'
01-23-2012

You can format the output of date command by using the %. Here %m for month, %d for day and %Y
for year.
5. Write a unix/linux date command to display only the time part and ignore the date part?

>date '+%H-%M-%S'
01-48-45

Here %H is for hours in 24 hour format, %M is for minutes and %S for seconds
6. Write a unix/linux date command to format both the date and time part.

>date '+%m-%d-%Y %H-%M-%S'


01-23-2012 01-49-59

7. Write a unix/linux date command to find the number of seconds from unix epoch.

>date '+%s'
1327312228

Unix epoch is the date on January 1st, 1970. The %s option is used to find the number of seconds
between the current date and unix epoch.

Sed Command in Unix and Linux Examples


Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to make
changes to the file automatically, sed comes in handy to do this. Most people never learn its power;
they just simply use sed to replace text. You can do many things apart from replacing text with sed.
Here I will describe the features of sed with examples.

Consider the below text file as an input.

>cat file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.

Sed Command Examples


1. Replacing or substituting string
Sed command is mostly used to replace the text in a file. The below simple sed command replaces
the word "unix" with "linux" in the file.

>sed 's/unix/linux/' file.txt


linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search
pattern and the "linux" is the replacement string.
By default, the sed command replaces the first occurrence of the pattern in each line and it won't
replace the second, third...occurrence in the line.
2. Replacing the nth occurrence of a pattern in a line.

Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below
command replaces the second occurrence of the word "unix" with "linux" in a line.

>sed 's/unix/linux/2' file.txt


unix is great os. linux is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.

3. Replacing all the occurrence of the pattern in a line.


The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences
of the string in the line.

>sed 's/unix/linux/g' file.txt


linux is great os. linux is opensource. linux is free os.
learn operating system.
linuxlinux which one you choose.

4. Replacing from nth occurrence to all occurrences in a line.


Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a
pattern in a line. The following sed command replaces the third, fourth, fifth... "unix" word with "linux"
word in a line.

>sed 's/unix/linux/3g' file.txt

unix is great os. unix is opensource. linux is free os.


learn operating system.
unixlinux which one you choose.

5. Changing the slash (/) delimiter


You can use any delimiter other than the slash. As an example if you want to change the web url to
another url as

>sed 's/http:\/\//www/' file.txt

In this case the url consists the delimiter character which we used. In that case you have to escape
the slash with backslash character, otherwise the substitution won't work.
Using too many backslashes makes the sed command look awkward. In this case we can change
the delimiter to another character as shown in the below example.

>sed 's_http://_www_' file.txt


>sed 's|http://|www|' file.txt

6. Using & as the matched string


There might be cases where you want to search for the pattern and replace that pattern by adding
some extra characters to it. In such cases & comes in handy. The & represents the matched string.

>sed 's/unix/{&}/' file.txt

{unix} is great os. unix is opensource. unix is free os.


learn operating system.
{unix}linux which one you choose.

>sed 's/unix/{&&}/' file.txt


{unixunix} is great os. unix is opensource. unix is free os.
learn operating system.
{unixunix}linux which one you choose.

7. Using \1,\2 and so on to \9


The first pair of parenthesis specified in the pattern represents the \1, the second represents the \2
and so on. The \1,\2 can be used in the replacement string to make changes to the source string. As
an example, if you want to replace the word "unix" in a line with twice as the word like "unixunix" use
the sed command as below.

>sed 's/\(unix\)/\1\1/' file.txt


unixunix is great os. unix is opensource. unix is free os.
learn operating system.
unixunixlinux which one you choose.

The parenthesis needs to be escaped with the backslash character. Another example is if you want
to switch the words "unixlinux" as "linuxunix", the sed command is

>sed 's/\(unix\)\(linux\)/\2\1/' file.txt

unix is great os. unix is opensource. unix is free os.


learn operating system.
linuxunix which one you choose.

Another example is switching the first three characters in a line

>sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt


inux is great os. unix is opensource. unix is free os.
aelrn operating system.
inuxlinux which one you choose.

8. Duplicating the replaced line with /p flag


The /p print flag prints the replaced line twice on the terminal. If a line does not have the search
pattern and is not replaced, then the /p prints that line only once.

>sed 's/unix/linux/p' file.txt


linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
linuxlinux which one you choose.

9. Printing only the replaced lines

Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option
suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.

>sed -n 's/unix/linux/p' file.txt


linux is great os. unix is opensource. unix is free os.
linuxlinux which one you choose.

If you use -n alone without /p, then the sed does not print anything.
10. Running multiple sed commands.
You can run multiple sed commands by piping the output of one sed command as input to another
sed command.

>sed 's/unix/linux/' file.txt| sed 's/os/system/'


linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.

Sed provides -e option to run multiple sed commands in a single sed command. The above output
can be achieved in a single sed command as shown below.

>sed -e 's/unix/linux/' -e 's/os/system/' file.txt


linux is great system. unix is opensource. unix is free os.
learn operating system.

linuxlinux which one you chosysteme.

11. Replacing string on a specific line number.


You can restrict the sed command to replace the string on a specific line number. An example is

>sed '3 s/unix/linux/' file.txt


unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

The above sed command replaces the string only on the third line.
12. Replacing string on a range of lines.
You can specify a range of line numbers to the sed command for replacing a string.

>sed '1,3 s/unix/linux/' file.txt


linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

Here the sed command replaces the lines with range from 1 to 3. Another example is

>sed '2,$ s/unix/linux/' file.txt


linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

Here $ indicates the last line in the file. So the sed command replaces the text from second line to
last line in the file.
13. Replace on a lines which matches a pattern.
You can specify a pattern to the sed command to match in a line. If the pattern match occurs, then
only the sed command looks for the string to be replaced and if it finds, then the sed command
replaces the string.

>sed '/linux/ s/unix/centos/' file.txt


unix is great os. unix is opensource. unix is free os.
learn operating system.
centoslinux which one you choose.

Here the sed command first looks for the lines which has the pattern "linux" and then replaces the
word "unix" with "centos".
14. Deleting lines.
You can delete the lines a file by specifying the line number or a range or numbers.

>sed '2 d' file.txt

>sed '5,$ d' file.txt

15. Duplicating lines


You can make the sed command to print each line of a file two times.

>sed 'p' file.txt

16. Sed as grep command


You can make sed command to work as similar to grep command.

>grep 'unix' file.txt


>sed -n '/unix/ p' file.txt

Here the sed command looks for the pattern "unix" in each line of a file and prints those lines that
has the pattern.
You can also make the sed command to work as grep -v, just by using the reversing the sed with
NOT (!).

>grep -v 'unix' file.txt


>sed -n '/unix/ !p' file.txt

The ! here inverts the pattern match.

17. Add a line after a match.


The sed command can add a new line after a pattern match is found. The "a" command to sed tells it
to add a new line after a match is found.

>sed '/unix/ a "Add a new line"' file.txt


unix is great os. unix is opensource. unix is free os.
"Add a new line"
learn operating system.
unixlinux which one you choose.
"Add a new line"

18. Add a line before a match


The sed command can add a new line before a pattern match is found. The "i" command to sed tells
it to add a new line before a match is found.

>sed '/unix/ i "Add a new line"' file.txt


"Add a new line"
unix is great os. unix is opensource. unix is free os.
learn operating system.
"Add a new line"
unixlinux which one you choose.

19. Change a line

The sed command can be used to replace an entire line with a new line. The "c" command to sed
tells it to change the line.

>sed '/unix/ c "Change line"' file.txt


"Change line"
learn operating system.
"Change line"

20. Transform like tr command


The sed command can be used to convert the lower case letters to upper case letters by using the
transform "y" option.

>sed 'y/ul/UL/' file.txt


Unix is great os. Unix is opensoUrce. Unix is free os.
Learn operating system.
UnixLinUx which one yoU choose.

Here the sed command transforms the alphabets "ul" into their uppercase format "UL"

ls Command in Unix and Linux Examples

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

Copy (cp) File And Directory Examples | Unix and Linux Command
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/

Change Directory (cd) Examples | Unix and Linux Command


The Change directory (cd) command is one of the simple commands in Unix (or Linux) and it is very
easy to use. The cd command is used to change from the current directory to another directory. The
syntax of cd command is

cd [directory]
Here directory is the name of the directory where you wish to go.

CD Command Examples
1. Write a unix/linux cd command to change to home directory?
Just simply type cd command on the unix terminal and then press the enter key. This will change
your directory to home directory.

> pwd
/usr/local/bin

Now i am in the /usr/local/bin directory. After typing the cd command and unix window, you will go to
your home directory.

> cd
> pwd
/home/matt

Here pwd command displays the present working directory.


2. Write a unix/linux cd command to go back to one directory?
The cd .. changes the directory to its parent directory by going back one level. The space between
the cd and .. is must.

> pwd
/var/tmp
> cd ..
> pwd
/var

3. Write a unix/linux cd command to go back to two directories?


The cd ../../ takes you back to two directories. You can extend this cd command to go back to n
number of directories.

> pwd
/usr/local/bin
> cd ../../
> pwd
/usr

4. Write a unix/linux cd command to change the directory using the absolute path?

In case of changing directory using absolute path you have to specify the full directory path. Absolute
path directories always start with a slash (/). An example is changing your directory to /usr/bin from
your home directory.

> cd /usr/bin

5. Write a unix/linux cd command to change the directory using the relative path?
In relative path, you have to specify the directory path relative to your current directory. For example,
you are in /var/tmp directory and you want to go to /var/lib directory, then you can use the relative
path.

> pwd
/var/tmp
> cd ../lib
> pwd
/var/lib

Here the cd ../lib, first takes you to the parent directory which is /var and then changes the directory
to the lib.
6. Write a unix/linux cd command to change back to previous directory.
As an example, i am in the directory /home/matt/documents and i changed to a new directory
/home/matt/backup. Now i want to go back to my previous directory /home/matt/documents. In this
case, you can use the cd - command to go back to the previous directory.

> pwd
/home/matt/documents
> cd /home/matt/backup
>pwd
/home/matt/backup
> cd > pwd
/home/matt/documents

Cut Command in Unix ( Linux) Examples


Cut command in unix (or linux) is used to select sections of text from each line of files. You can use
the cut command to select fields or columns from a line by specifying a delimiter or you can select a
portion of text by specifying the range or characters. Basically the cut command slices a line and
extracts the text.
Unix Cut Command Example
We will see the usage of cut command by considering the below text file as an example

> cat file.txt


unix or linux os
is unix good os
is linux good os

1. Write a unix/linux cut command to print characters by position?

The cut command can be used to print characters in a line by specifying the position of the
characters. To print the characters in a line, use the -c option in cut command

cut -c4 file.txt


x
u
l

The above cut command prints the fourth character in each line of the file. You can print more than
one character at a time by specifying the character positions in a comma separated list as shown in
the below example

cut -c4,6 file.txt


xo
ui
ln

This command prints the fourth and sixth character in each line.
2.Write a unix/linux cut command to print characters by range?
You can print a range of characters in a line by specifying the start and end position of the
characters.

cut -c4-7 file.txt

x or
unix
linu

The above cut command prints the characters from fourth position to the seventh position in each
line. To print the first six characters in a line, omit the start position and specify only the end position.

cut -c-6 file.txt


unix o
is uni
is lin

To print the characters from tenth position to the end, specify only the start position and omit the end
position.

cut -c10- file.txt


inux os
ood os
good os

If you omit the start and end positions, then the cut command prints the entire line.

cut -c- file.txt

3.Write a unix/linux cut command to print the fields using the delimiter?
You can use the cut command just as awk command to extract the fields in a file using a delimiter.
The -d option in cut command can be used to specify the delimiter and -f option is used to specify
the field position.

cut -d' ' -f2 file.txt


or
unix
linux

This command prints the second field in each line by treating the space as delimiter. You can print
more than one field by specifying the position of the fields in a comma delimited list.

cut -d' ' -f2,3 file.txt


or linux
unix good
linux good

The above command prints the second and third field in each line.
Note: If the delimiter you specified is not exists in the line, then the cut command prints the entire
line. To suppress these lines use the -s option in cut command.
4. Write a unix/linux cut command to display range of fields?
You can print a range of fields by specifying the start and end position.

cut -d' ' -f1-3 file.txt

The above command prints the first, second and third fields. To print the first three fields, you can
ignore the start position and specify only the end position.

cut -d' ' -f-3 file.txt

To print the fields from second fields to last field, you can omit the last field position.

cut -d' ' -f2- file.txt

5. Write a unix/linux cut command to display the first field from /etc/passwd file?
The /etc/passwd is a delimited file and the delimiter is a colon (:). The cut command to display the
first field in /etc/passwd file is

cut -d':' -f1 /etc/passwd

6. The input file contains the below text

> cat filenames.txt

logfile.dat
sum.pl
add_int.sh

Using the cut command extract the portion after the dot.
First reverse the text in each line and then apply the command on it.

rev filenames.txt | cut -d'.' -f1

Delete Directory, Files - rm, rmdir command in Unix / Linux


Q. How to delete directories and files in unix/linux
Unix provides rmdir and rm commands to remove the directories and files. Let see each command in
detail.
Unix rmdir command syntax
The syntax of rmdir command is

rmdir [options] directories

The rmdir command options are

-p : Removes directory and its parent directories

-v : Provides the diagnostic information of the directory processed

Unix rmdir command examples


1. Write a unix/linux command to remove a directory?
The rmdir command deletes only the empty directories. If a directory contains files or sub directories,
then the rmdir command fails.

rmdir docs/
rmdir: docs/: Directory not empty

Here the docs directory is not empty, that is why the rmdir command failed to remove the directory.
To remove the docs directory first we have to make the directory empty and then delete the directory.

rm doc/*
rmdir docs/

We will see later how to remove non-empty directories with a single command.
2. Write a unix/linux command to remove the directory and its parent directories?
As mentioned earlier the -p option allows the rmdir command to delete the directory and also its
parent directories.

rmdir -p docs/entertainment/movies/

This rmdir command removes the docs directory completely. If you dont use the -p option, then it
only deletes the movies directory.
3. Write a unix/linux command to remove directories using pattern matching?
You can specify the directory names using the regular expressions and can delete them.

rm doc*

This rm command deletes the directories like doc, documents, doc_1 etc.
Now we will see the rm command in unix.
Unix rm command syntax
The syntax of rm command is

rm [options] [directory|file]

The rm command options are

f : Removes all files in a directory without prompting the user.


i : Interactive: prompts the user for confirmation before deleting a file.
R or r : Recursively remove directories and sub directories.

The rm command can be used to delete both the files and directories. The rm command also deletes

the non-empty directories.


Unix rm command examples
1. Write a unix/linux command to remove a file?
This is the basic feature of rm command. To remove a file, logfile.dat, in the current directory use the
below rm command

rm logfile.dat

2. Write a unix/linux command to remove all the files in a directory?


use the * regular pattern as the file list in rm command for deleting all the files in the current directory.

rm *

3. Write a unix/linux command to delete empty directory?


The rm command can also be used to delete the empty directory. The command for this is

rm docs/

If the directory is non-empty, then the above command fails to remove the directories.
4. Write a unix/linux command to delete directories recursively (delete non empty directories)?
As mentioned earlier, the -r option can be used to remove the directories and sub directories.

rm -r docs

This removes the docs directory even if it is non-empty.

Move / Rename files, Directory - MV Command in Unix / Linux

Q. How to rename a file or directory in unix (or linux) and how to move a file or directory from the
current directory to another directory?
Unix provides a simple mv (move) command which can be used to rename or move files and
directories. The syntax of mv command is

mv [options] oldname newname

The options of mv command are

f : Do not prompt before overwriting a file.


i : Prompts for the user input before overwriting a file.

If the newname already exists, then the mv command overwrites that file. Let see some examples on
how to use mv command.
Unix mv command examples
1. Write a unix/linux command to rename a file?
Renaming a file is one of the basic features of the mv command. To rename a file from "log.dat" to
"bad.dat", use the below mv command

> mv log.dat bad.dat

Note that if the "bad.dat" file already exists, then its contents will be overwritten by "log.dat". To avoid
this use the -i option, which prompts you before overwriting the file.

mv -i log.dat bad.dat
mv: overwrite `bad.dat'?

2. Write a unix/linux command to rename a directory?


Just as renaming a file, you can use the mv command to rename a directory. To rename the directory
from docs to documents, run the below command

mv docs/ documents/

If the documents directory already exists, then the docs directory will be moved in to the documents
directory.
3. Write a unix/linux command to move a file into another directory?
The mv command can also be used to move the file from one directory to another directory. The
below command moves the sum.pl file in the current directory to /var/tmp directory.

mv sum.pl /var/tmp/

If the sum.pl file already exists in the /var/tmp directory, then the contents of that file will be
overwritten.
4. Write a unix/linux command to move a directory in to another directory?

Just as moving a file, you can move a directory into another directory. The below mv command
moves the documents directory into the tmp directory

mv documents /tmp/

5. Write a unix/linux command to move all the files in the current directory to another directory?
You can use the regular expression pattern * to move all the files from one directory to another
directory.

mv * /var/tmp/

The above command moves all the files and directories in the current directory to the /var/tmp/
directory.
6. mv *
What happens if you simply type mv * and then press enter?

It depends on the files you have in the directory. The * expands to all the files and directories. Three
scenarios are possible.
If the current directory has only files, then the contents of all the files (except one file) will be
written in to the one file. The one file is the last file which depends on the pattern *.

If the current directory contains only directories, then all the directories (except one directory)
will be moved to another directory.

If the current directory contains both files and directories, then it depends on the expansion
of the *. If the pattern * gives the last one as directory then all the files will be moved to that directory.
Otherwise the mv command will fail.

Some Tips:
Try to avoid mv *
Avoid moving large number of files.

Join Command in Unix/Linux Examples


Join command is one of the text processing utility in Unix/Linux. Join command is used to combine
two files based on a matching fields in the files. If you know SQL, the join command is similar to
joining two tables in a database.
The syntax of join command is

join [options] file1 file2

The join command options are

-1 field number : Join on the specified field number in the first file
-2 field number : Join on the specified field number in the second file
-j field number : Equivalent to -1 fieldnumber and -2 fieldnumber
-o list : displays only the specified fields from both the files
-t char : input and output field delimiter
-a filenumber : Prints non matched lines in a file
-i : ignore case while joining

Unix Join Command Examples


1. Write a join command to join two files on the first field?
The basic usage of join command is to join two files on the first field. By default the join command
matches the files on the first fields when we do not specify the field numbers explicitly. Let's say we
have two files emp.txt and dept.txt

> cat emp.txt


10 mark
10 steve
20 scott
30 chris
> cat dept.txt
10 hr
20 finance
30 db

Here we will join on the first field and see the output. By default, the join command treats the field
delimiter as space or tab.

> join emp.txt dept.txt


10 mark hr
10 steve hr
20 scott finance
30 chris db

Important Note: Before joining the files, make sure to sort the fields on the joining fields. Otherwise
you will get incorrect result.
2. Write a join command to join the two files? Here use the second field from the first file and the first
field from the second file to join.

In this example, we will see how to join two files on different fields rather than the first field. For this
consider the below two files as an example

> cat emp.txt


mark 10 1
steve 10 1
scott 20 2
chris 30 3
> cat dept.txt
10 hr 1
20 finance 2
30 db 3

From the above, you can see the join fields are the second field from the emp.txt and the first field
from the dept.txt. The join command to match these two files is

> join -1 2 -2 1 emp.txt dept.txt


10 mark 1 hr 1
10 steve 1 hr 1
20 scott 2 finance 2
30 chris 3 db 3

You can also see that the two files can also be joined on the third filed. As the both the files have the
matching join field, you can use the j option in the join command.

Here -1 2 specifies the second field from the first file (emp.txt) and -2 1 specifies the first field from
the second file (dept.txt)

> join -j 3 emp.txt dept.txt


1 mark 10 10 hr
1 steve 10 10 hr
2 scott 20 20 finance
3 chris 30 30 db

3. Write a join command to select the required fields from the input files in the output? Select first
filed from first file and second field from second file in the output.
By default, the join command prints all the fields from both the files (except the join field is printed
once). We can choose what fields to be printed on the terminal with the -o option. We will use the
same files from the above example.

> join -o 1.1 2.2 -1 2 -2 1 emp.txt dept.txt


mark hr
steve hr
scott finance
chris db

Here 1.1 means in the first file select the first field. Similarly, 2.2 means in the second file select the
second field
4. Write a command to join two delimited files? Here the delimiter is colon (:)

So far we have joined files with space delimiter. Here we will see how to join files with a colon as
delimiter. Consider the below two files.

> cat emp.txt


mark:10
steve:10
scott:20
chris:30
> cat dept.txt
10:hr
20:finance
30:db

The -t option is used to specify the delimiter. The join command for joining the files is

> join -t: -1 2 -2 1 emp.txt dept.txt


10:mark:hr
10:steve:hr
20:scott:finance
30:chris:db

5. Write a command to ignore case when joining the files?


If the join fields are in different cases, then the join will not be performed properly. To ignore the case

in join use the -i option.

> cat emp.txt


mark,A
steve,a
scott,b
chris,C
> cat dept.txt
a,hr
B,finance
c,db

> join -t, -i -1 2 -2 1 emp.txt dept.txt


A,mark,hr
a,steve,hr
b,scott,finance
C,chris,db

6. Write a join command to print the lines which do not match the values in joining fields?
By default the join command prints only the matched lines from both the files which means prints the
matched lines that passed the join condition. We can use the -a option to print the non-matched
lines.

> cat P.txt

A 1
B 2
C 3
> cat Q.txt
B 2
C 3
D 4

Print non pairable lines from first file.

> join -a 1 P.txt Q.txt


A 1
B 2 2
C 3 3

Print non pairable lines from second file.

> join -a 2 P.txt Q.txt


B 2 2
C 3 3
D 4

Print non pairable lines from both file.

> join -a 1 -a 2 P.txt Q.txt


A 1
B 2 2
C 3 3
D 4

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.
As an example, lets 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

Add Job to Cron (Crontab Command Examples) - Unix / Linux


Tutorials

Unix or Linux operating system provides a feature for scheduling the jobs. You can setup command
or scripts which will run periodically at the specified time. The Crontab is command used to add or
remove jobs from the cron. The cron service is a daemon runs in the background and checks for
/etc/crontab file, /etc/con.*/ directories and /var/spool/cron/ directory for any scheduled jobs.
Each user has a separate /var/spool/cron/crontab file. Users are not allowed directly to modify the
files. The crontab command is used for setting up the jobs in the cron.
The format of crontab command is

* * * * * command to be executed

You can easily remember this command in the below format

MI HH DOM MON DOW command

The field descriptions of the crontab are explained below:

MI

: Minutes

from 0 to 59

HH

: Hours

from 0 to 23

DOM : Day of month from 0 to 31


MON : Months

from 1 to 12

DOW : Day of week

from 0 to 7

(0 or 7 represents Sunday)

Command: Any command or script to be scheduled

Let see the usage of crontab command with examples.


1. List crontab entries
You can list out all the jobs which are already scheduled in cron. Use "crontab -l" for listing the jobs.

crontab -l
0 0 * * *

/usr/local/bin/list_unix_versions.sh

The above contab command displays the cron entries. Here the shell script for listing the unix
versions (list_unix_version.sh) is scheduled to run daily at midnight.

2. List crontab entries of other users


To list the corntab entries of other user in the unix, use the -u option with crontab. The syntax is
shown below:

crontab -u username -l

3. Removing all crontab entries


You can un-schedule all the jobs by removing them from the crontab. The syntax for removing all the
crontab entries is

crontab -r

For removing other users crontab entries:


crontab -u username -r

4. Editing the crontab


You can edit the crontab and add a new job to it. You can also remove an existing job from the
crontab. Use the -e option for editing the crontab.

crontab -e

For editing other users crontab entries:


crontab -u username -e

This will open a file in VI editor. Now use the VI commands for adding, removing the jobs and for
saving the crontab entries.
5. Schedule a job to take oracle backup on every Sunday at midnight
Edit crontab using "crontab -e" and append the following entry in the file.

0 0 * * 0 /usr/local/bin/oracle_backup.sh

6. Schedule a job to run every six hours in a day


You can schedule a job to run more than once in a day. As an example the following crontab entry
takes the mysql backup more than once in a day.

0 0,6,12,18 * * * /usr/bin/mysql_backup.sh

Here the list 0,6,12,18 indicates midnight, 6am, 12pm and 6pm respectively.
7. Schedule job to run for the first 15 days of the month.
You can schedule a job by specifying the range of values for a field. The following example takes the
sql server backup daily at midnight for the first 15 days in a month.

0 0 * 1-15 * /usr/bin/sql_server_backup.sh

8. Schedule job to run every minute.


The following crontab command runs the command to send emails to group of users for every
minute.

* * * * * /bin/batch_email_send.sh

9. Taking backup of cron entries


Before editing the cron entries, it is good to take backup of the cron entries. So that even if you do
mistake you can get back those entries from the backup.

crontab -l > /var/tmp/cron_backup.dat

10. Restoring the cron entries


You can restore the cron entries from the backup as

crontab cron_backup.dat

Understanding the Operators:

There are three operators allowed for specifying the scheduling times. They are:
Asterisk (*) : Indicates all possible values for a field. An asterisk in the month field indicates
all possible months (January to December).

Comma (,) : Indicates list of values. See example 6 above.

Hyphen (-): Indicates range of values. See example 7 above.


Disabling Emails:
By default the crontab sends emails to the local user if the commands or scripts produce any output.
To disable sending of emails redirect the output of commands to /dev/null 2>&1.

0 0 * 20 * /usr/bin/online_backup.sh > /dev/null 2>&1

Note: you cannot schedule a job to run at seconds level as the minimum allowed scheduling is at
minute level.

xargs command examples in Unix / Linux Tutorial


Xargs command in unix or linux operating system is used to pass the output of one command as an
argument to another command. Some of the unix or linux commands like ls and find produces a long
list of filenames. We want to do some operation on this list of file names like searching for a pattern,
removing and renaming files etc. The xargs command provide this capability by taking the huge list
of arguments as input , divides the list into small chunks and then passes them as arguments to
other unix commands.
Unix Xargs Command Examples:
1. Renaming files with xargs
We have to first list the files to be renamed either by using the ls or find command and then pipe the
output to xargs command to rename the files. First list the files which end with ".log" using the ls
command.

ls *.log
oracle.log

storage.log

> ls *.log | xargs -i mv {} {}_bkp


> ls *_bkp
oracle.log_bkp

storage.log_bkp

You can see how the log files are renamed with backup (bkp) suffix. Here the option "i" tells the xargs
command to replace the {} with the each file returned by the ls command.
2. Searching for a pattern
We can combine the grep command with xargs to search for a pattern in a list of files returned by
another unix command (ls or find). Lets list out all the bash files in the current directory with the find
command in unix.

find . -name "*.bash"


./sql_server.bash
./mysql_backup.bash
./oracle_backup.bash

Now we grep for the "echo" statements from the list of files returned by the find command with the
help of xargs. The command is shown below:

find . -name "*.bash" |xargs grep "echo"

If you dont use xargs and piped the output of find command to grep command directly, then the grep
command treats each file returned by the find command as a line of string and searches for the word
"echo" in that line rather in that file.
3. Removing files using xargs
We can remove the temporary files in a directory using the rm command along with the xargs
command. This is shown below:

ls "*.tmp" | xargs rm

This removes all the files with ".tmp" suffix.


4. Converting Multi-line output to Single line output.
If you run the ls -1 command, it will list each file on a separate line. This is shown below:

ls -1
oracle.txt
online_backup.dat
mysql_storage.bat

We can convert this multi-line output to single line output using the xargs command. This is shown
below:

ls -1 | xargs
oracle.txt online_backup.dat mysql_storage.bat

5. Handling spaces in file names


By default the xargs command treats the space as a delimiter and sends each item as an argument
to the unix command. If the file name contains a space (example: "oracle storage"), then each item
will be treated as a separate file and will be passed as an argument. This will cause an issue. Let
see how to handle the spaces in file names with an example.

Creating a file which contains space in the name


> touch "oracle storage"

> ls oracle\ storage | xargs grep "log"


grep: oracle: No such file or directory
grep: storage: No such file or directory

You can see that grep command is treating oracle as separate file and storage as separate file. This
is because of xargs treats space as delimiter. To avoid this kind of errors use the -i option with braces
as shown in below:

> ls oracle\ storage | xargs -i grep "log" {}

If you want to know what command the xargs is executing use the -t option with xargs. This will print
the command on the terminal before executing it.
6. Passing subset of arguments
We can pass only a subset of arguments from a long list of arguments using the -n option with xargs
command. This is shown in below.

> ls -1
backup
mysql
network
online

oracle
storage
wireless

> ls -1 | xargs -n 3 echo


backup mysql network
online oracle storage
wireless

You can see from the above output that 3 arguments are passed at a time to the echo statement.
Important Notes on Xargs Command:
1. Xargs directly cannot handle files which contain new lines or spaces in their names. To handle this
kind of files use the -i option with xargs command. Another way to handle these characters is to treat
the new line or spaces as null characters using th -0 option with xargs. However this requires that the
input to xargs should also use the null as separator. An example is shown below

find . -print0 | xargs -0 rm

The print0 in find command makes the newline or space characters as null separator.
2. By default the xargs uses the end of the file string as "_". If this string appears in input string, then
xargs command stops reading the input and rest of the input is ignored. You can change the end of
file string by using the "-eof" option.
3. To know more about xargs command, run the xargs --help on the unix or linux terminal.

SCP Command Examples - Linux / Unix Tutorials

SCP stands for secure copy is used to copy data (files or directories) from one unix or linux system
to another unix or linux server. SCP uses secured shell (ssh) to transfer the data between the remote
hosts. The features of SCP are:

Copies files within in the same machine

Copies files from local machine to remote machine.

Copies files from remote machine to local machine.

Copies files between two different remote servers.


SCP Command Syntax:
The syntax of SCP command is

scp [Options] [[User@]From_Host:]Source_File [[User@]To_Host:]


[Destination_File]

Each element of the scp command is explained in detail below:

User is the one who have the permissions to access the files and directories. User should
have read permissions if it is a source and write permissions if it is the destination.

From_Host: hostname or Ip address where the source file or directory resides. This is
optional if the from host is the host where you are running the scp command.

Source_File: Files or directories to be copied to the destination.


To_Host: Destination host where you want to copy the files. You can omit this when you want
to copy the files to the host where you are issuing the scp command.
Destination_File: Name of the file or directory in the target host.
SCP Command Options:
The important SCP command options are listed below:

-r : Recursively copies the contents of source files or directories.


-p : Preserves the access time, modification time, permissions of the source files in the
destination.

-q : Progress bar in not displayed

-v : verbose mode. Displays debugging messages.

-P : copy files using the specified port number.


SCP Command Examples:
Let see the examples of scp command in unix or linux system.
1. Copying with in the same system
You can use the scp command just like the cp command to copy files from one directory to another
directory.

scp Unix-storage.dat /var/tmp/

This command copies the file unix-storage.dat from current directory to the /var/tmp directory.
2. Copy file from local host to remote server
This is most frequently used operation to transfer files in unix system.

scp filename user@remotehost:/remote/directory/

This command connects to the remote host and copies the specified file to the /remote/directory/.
3. Copy files from remote host to local server.
This operation is used when taking backup of the files in remote server.

scp user@remotehost:/usr/backup/oracle_backup.dat .

This command copies the oracle backup file in the remote host to the current directory.
4. Copying files between two remote servers

The scp command can also be used to copy files between two remote hosts.

scp source_user@source_remote_host:/usr/bin/mysql_backup.sh
target_user@target_remote_host:/var/tmp/

The above command copies the mysql bakup shell script from the source remote host the /var/tmp
directory of target remote host.
5. Copying a directory.
To copy all the files in a directory, use the -r option with the scp command. This makes the scp
command to copy the directory recursively.

scp -r directory user@remotehost:/var/tmp/

The above command copies the directory from local server to the remote host.
6. Improving performance of scp command
By default the scp command uses the Triple-DES cipher/AES-128 to encrypt the data. Using the
blowfish or arcfour encryption will improve the performance of the scp command.

scp -c blowfish filename


scp -c arcfour

user@remoteserver:/var/

localfile user@remoteserver:/var/

7. Limit bandwidth
You can limit the bandwidth used by the scp command using the -l option.

scp -l bandwidth_limit filename user@hostname:/usr/backup/


Here bandwidth_limit is numeric to be specified in kilobits per second.

8. Specifying the port number


We can make the scp command to copy the files over a specified port number using the -P option.

scp -P 6001 storage_backup.bat username@hostname:/tmp/

WC Command Examples - Count of Lines, Words, Characters - Unix /


Linux
WC command in unix or linux is used to find the number of lines, words and characters in a file. The
syntax of wc command is shown below:

wc [options] filenames

You can use the following options with the wc command.

-l : Prints the number of lines in a file.


-w : prints the number of words in a file.
-c : Displays the count of bytes in a file.
-m : prints the count of characters from a file.
-L : prints only the length of the longest line in a file.

Let see how to use the wc command with few examples. Create the following file in your unix or linux
operating system.

> cat unix_wc.bat


Oracle Storage
unix distributed system
linux file server
debian server
Oracle backup server

WC Command Examples:
1. Printing count of lines
This is the most commonly used operation to find the number of lines from a file. Run the below
command to display the number of lines:

wc -l unix_wc.bat
5 unix_wc.bat

Here in the output, the first field indicates count and second field is the filename
2. Displaying the number of words.
Just use the -w option to find the count of words in a file. This is shown below:

wc -w unix_wc.bat
13 unix_wc.bat

3. Print count of bytes, count of characters from a file


We can use the -c and -m options to find the number of bytes and characters respectively in a file.

> wc -c unix_wc.bat
92 unix_wc.bat
> wc -m unix_wc.bat
92 unix_wc.bat

4. Print the length of longest line


The -L option is used to print the number of characters in the longest line from a file.

wc -L unix_wc.bat
23 unix_wc.bat

In this example, the second line is the longest line with 23 characters.
5. Print count of lines, words and characters.
If you dont specify any option to the wc command, by default it prints the count of lines, words and
characters. This is shown below:

wc unix_wc.bat
5 13 92 unix_wc.bat

6. Wc help
For any help on the wc command, just run the wc --help on the unix terminal.

SSH Command Examples - Unix / Linux Tutorials


SSH client utility in unix or linux server is used to logging into a remote host and execute commands
on the remote machine. The rlogin and rsh commands can also be used to login into the remote
machine. However these are not secure. The ssh command provides a secure connection between
two hosts over a insecure network.
The syntax ssh command is

ssh [-l username] hostname | user@remote-hostname [command]

Let see the examples of ssh command.


SSH Command Examples:
1. Logging to a remote server
You can login to a remote server from the local host as shown below:

localhost:[~]> ssh -l username remote-server


username@remote-server password:

remote-server:[~]>

Alternatively you can use the below ssh command for connecting to remote host:

localhost:[~]> ssh username@remote-server


username@remote-server password:
remote-server:[~]>

Note: If you are logging for the first time, then it will prints a message that host key not found and you
can give yes to continue. The host key of the remote server will be cached and added to the
.ssh2/hostkeys directory in your home directory. From second time onwards you just need to enter
the password.
2. Logging out from remote server
Simply enter the exit command on the terminal to close the connection. This is shown below:

remote-server:[~]>exit
logout
Connection to remote-server closed.
localhost:[~]>

3. Running remote commands from local host


Sometimes it is necessary to run the unix commands on the remote server from the local host. An
example is shown below:

localhost:[~]> ssh user@remote-host "ls test"


online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt

The ssh command connects to the remote host, runs the ls command, prints the output on the local
host terminal and exits the connection from remote host.
Let see whether the ls command actually displayed the correct result or not by connecting to the
remote host.

localhost:[~]> ssh user@remote-host


user@remotehost password:
remotehost:[~]> cd test
remotehost:[~/test]> ls
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt

4. Version of the SSH command


We can find the version of SSH installed on the unix system using the -V option to the ssh. This is
shown below:

> ssh -V
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008

5. Debugging the SSH Client


When we are not able to connect to the remote host, it is good to debug and find the exact error
messages that causing the issue. Use the -v option for debugging the ssh client.

ssh -v user@remote-host
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to remote-host [172.22.200.140] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/identity type -1
debug1: identity file /home/user/.ssh/id_rsa type -1
debug1: identity file /home/user/.ssh/id_dsa type 2
debug1: loaded 3 keys
..........
..........

6. Copying files between remote host and local host.


We can use the scp command to copy the files securely between the local host and remote host
using the ssh authentication.
To copy the file from local host to remote hosts /var/tmp/ directory, run the below scp command.

scp filename user@remote-host:/var/tmp/

To copy the file from remote hosts /usr/local/bin/ directory to local hosts current directory, run the
below scp command.

scp user@remote-host:/usr/local/bin/add.sh .

Kill Command Examples in Unix / Linux Tutorials

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

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.

CVS command Examples - Unix / Linux Tutorials


CVS (concurrent Version Control system) is a version controlling system used to record the history of
the files. Whenever a code is changed in software, there might be chance of bugs creeping into that.
With CVS, you can easily get the old version of the code and see what part of the code exactly
created the bug.
In CVS, we can save every version of the file. The CVS only stores the differences between the files.
This saves a huge amount of disk space.
The general syntax of CVS command is

cvs [option] filename

CVS Command Examples:


1. Checking out file
You can check out a file from the CVS repository with the checkout (co) option. This is shown below:

cvs co oracle_storage.dat

2. Adding a file to the repository


Use the add option to add a new file to the cvs repository.

cvs add mysql.bat

This will not commit the file to the cvs. It just simply adds the file.
3. Committing the file.
Once you have added a file to the CVS repository, you have to commit the file. Use the commit
option with cvs command for committing a file.

cvs commit msql.bat

This will open an editor. Enter the comments and save by using the :wq.
4. Difference between files
You can find the differences between the local file with the latest version of the file in the cvs
repository using the diff option.

cvs diff wireless.php

5. Update the file


You can update the local file with the latest version of the file from CVS repository using the update
option.

cvs update -A network.dat

6. Update to particular version

You can get a particular version of the file from the cvs. Specify the version number of the file with -j
option.

cvs update -j version-number network.bat

7. Adding binary files


You can add binary or image files to the CVS repository. Use the -kb option to add binary files.

cvs add -kb unix.png

8. Removing file from CVS


You can remove unwanted files permanently from the CVS repository using the remove option.

cvs remove linux_system.dat

After issuing this command you have to do a cvs commit. Otherwise the file will not be removed from
the repository.

FTP (File Transfer Protocol) Command Examples


The FTP (file transfer program) utility is used to transfer files between a local machine and remote
network machine Using the File Transfer protocol. In simple terms it transfers / copies files between
two computers. You can transfer files between unix systems and also non-unix systems like windows
operating system using FTP.

The FTP command is simple to use and easy to learn. Let see useful examples of FTP command in
detail.
FTP Command Examples:
If you are using windows operating system, open the command prompt and practice the below FTP
commands. If you are using unix or linux operating systems, just simply type the ftp command on the
terminal.
1. Connecting to Remote Host
First you need to connect to a remote host before doing any operations. You can use any one of the
following methods to connect to a remote host. First method is

> ftp remote-server-name


connected to remote-server-name
User-Name:
Password:
ftp>

Once the ftp connects to the remote server name, it will prompt you to enter the user name and
password. After successful login, your terminal or prompt changes to "ftp>".
Another method is to use the open option with ftp command. This is shown below:

>ftp
ftp>open remote-server-name
connected to remote-server-name

User-Name:
Password:
ftp>

If the ftp command fails to connect to the remote server, then you will get the below error:

ftp: connect: Connection refused

2. Copy file from remote machine to local machine.


The get option is used to download or transfer a file from the remote system to the local system.

ftp> get windows-cleveland.bat

This will download the specified file (windows-cleveland.bat) from the remote systems current
directory.
3. Copying multiple files from remote machine to local machine.
You can use the mget to transfer multiple files from the remote host to local host.

ftp>mget *.png

This will download all the png images to the local machine.
4. Transferring file from local server to remote server

The put option is used to copy the file from the local host to the remote host.

ftp>put linux-virtual-server.rpm

This command puts the rpm file into the remote machine.
5. Transferring multiple files to the remote server.
You can use the mput option to transfer more than one file from local system to the remote system.

ftp>put *.rpm

6. Executing commands in remote machine.


After connecting to the remote network machine using the ftp, you can run commands like ls to list
the files, cd to change directory and many more.

ftp> ls

This will list the files and directories in the remote machines current directory.
7. Executing commands in local machine.
Once you have connected to the remote host, to run the commands on local machine you need to
exit from the ftp connection. Instead of this, there is a way to run commands on local host without
exiting from the ftp connection. Use the ! symbol before the command you want to run.

ftp> !ls

Now this will list the files in the local machines current directory.
8. Changing the file transferring mode.
You can change the file transfer modes to ascii and binary modes. Use the below commands to
change the mode.

ftp>ascii
ftp>binary

9. Deleting files on remote machine


You can use the delete or mdelete to remove a single file or multiple files in the remote machine.

ftp>delete linux-dedicated-server.dat
ftp>mdelete *.dat

10. Disconnecting from ftp connection.


Use the quit command to close the ftp connection.

ftp>quit

11. Using FTP command in batch scripts


The following script reads the instructions from the dat file and executes them on the remote
machine.

echo "Ftp command batch script"


echo "start"
ftp -s:instructions.dat remote-host
echo "End"

The contents of the instructions.dat file is

user
password
cd /var/tmp
put oracle_storage.exe
quit

12. Getting the help about ftp command.


To know more about the ftp command, just type the help on the prompt. It will display the
options/commands that you can use with ftp command.

ftp>help

Commands may be abbreviated.

Commands are:

disconnect mdelete

preserve

runique

edit

mdir

progress

send

account exit

mget

prompt

sendport

append

form

mkdir

proxy

site

ascii

ftp

mls

put

size

bell

get

mode

pwd

sndbuf

binary

gate

modtime

quit

status

bye

glob

more

quote

struct

case

hash

mput

rcvbuf

sunique

cd

help

msend

recv

system

cdup

idle

newer

reget

tenex

chmod

image

nlist

rename

trace

close

lcd

nmap

reset

type

cr

less

ntrans

restart

umask

debug

lpwd

open

rhelp

user

delete

ls

page

rmdir

verbose

dir

macdef

passive

rstatus

Tar Command Examples in Unix / Linux Tutorials


In windows operating system, you might have used the winzip and winrar softwares for extracting
and archiving the files. Similarly in unix or linux operating system, the tar command is used for
creating archive files and also extracting files from the archives.

With the tar command, you can also create compressed archive files. In unix or linux operating
system, there are many other commands like gzip and gunzip for creating and extracting archive
files. Here we will see the important tar command examples in unix and linux systems which are
used frequently in our daily work.
The syntax of tar command is

tar [options] [Archive file] [files list]

The options of tar command are:

c : creates a tar file.


v : verbose. Displays the files information.
f : Specify the tar file name.
r : updates the tar file with new files.
x : Extracts files from the archive (tar file).
t : view contents of tar file.
z : Specify the tar command to create a tar file using gzip in unix.
j : uses bzip2 to create the tar file.

Tar Command Examples:


1. Creating a tar file
Let see a sample example by archiving all the files in my current directory. The ls -l command
displays the files and directories in the current directory.

> ls -l
drwxr-xr-x 2 user group 4096 Aug

8 03:23 debian

-rw-r--r-- 1 user group

174 Aug

2 23:39 file

-rw-r--r-- 1 user group

0 Aug

-rw-r--r-- 1 user group

76 Aug

-rw-r--r-- 1 user group

0 Aug

8 03:22 linux_server.bat
2 02:21 test.sh
8 03:22 unix_distro

We see how to tar all these files using the -c option with the tar command. This is shown below:

> tar -cvf archive.tar *


debian/
file
linux_server.bat
test.sh
unix_distro

> ls
archive.tar

debian

file

linux_server.bat

test.sh

unix_distro

Observe the output of ls command and see the archive.tar file is created.
2. Printing the contents of tar file
We have created the tar file and we dont know whether it contains the actual files or not. To view the
contents of the tar file use the -t option as

> tar -tvf archive.tar


drwxr-xr-x user/group

0 2012-08-08 03:23:07 debian/

-rw-r--r-- user/group 174 2012-08-02 23:39:51 file


-rw-r--r-- user/group
-rw-r--r-- user/group

0 2012-08-08 03:22:19 linux_server.bat


76 2012-08-02 02:21:32 test.sh

-rw-r--r-- user/group

0 2012-08-08 03:22:09 unix_distro

3. Updating the tar file with new contents.


You can add new files to the existing archive (tar) file using the -r option.

>touch red-hat-linux.dat

>tar -rvf archive.tar red-hat-linux.dat


red-hat-linux.dat

>tar -tvf archive.tar


drwxr-xr-x pcenter/pcenter

0 2012-08-08 03:23:07 debian/

-rw-r--r-- pcenter/pcenter 174 2012-08-02 23:39:51 file


-rw-r--r-- pcenter/pcenter
-rw-r--r-- pcenter/pcenter

0 2012-08-08 03:22:19 linux_server.bat


76 2012-08-02 02:21:32 test.sh

-rw-r--r-- pcenter/pcenter

0 2012-08-08 03:22:09 unix_distro

-rw-r--r-- pcenter/pcenter

0 2012-08-08 04:00:00 red-hat-linux.dat

Here the touch command creates a new file. The first tar command adds the new file to the existing
archive file. The second command displays the contents of the tar file.
4. Extracting the contents of tar file
In the first example, we have created the archive file. Now we will see how to extract the set of files
from the archive. To extract the contents of the tar file use the -x option.

> tar -xvf archive.tar


debian/
file
linux_server.bat
test.sh
unix_distro

5. Creating compressed tar file


So far we have created a uncompressed tar file in the above examples. We can create a
compressed tar file using the gzip or bzip2.

Compressing files using gzip

> tar -zcvf new_tar_file.tar.gz *

Compressing files using bzip2

> tar -jcvf new_tar_file.tar.bz2 *

To extract or to view the files in a compressed tar file use the appropriate compression option (z or j).

To view files in a gzip compressed tar file


> tar -ztvf new_tar_file.tar.gz

To extract files from a gip compressed tar file


> tar -zxvf new_tar_file.tar.gz

To view files in a bzip2 compressed tar file


> tar -jtvf new_tar_file.tar.bz2

To extract files from a bzip2 compressed tar file


> tar -jxvf new_tar_file.tar.bz2

6. Creating tar file with specified list of files


You can specify a list of files to be included in the newly created tar file.

> tar -cvf unix_files.tar unix_server.bat unix_system.dat

Here the tar command creates the unix_files.tar file which contains only the files unix_server.bat and
unix_system.dat

7. Extracting specific files from the tar


You can extract a specific file or a set of files from the archived file.

To extract a specifi file

> tar -xvf unix_files.tar unix_server.bat

To extract all files that start with name unix

> tar -xvf unix_files.tar --wildcards "unix*"

8. Extracting files from multiple archive files.


To extract the files from multiple archive files use the -M option with each -f option. This is shown
below:

> tar -xv -Mf archive.tar -Mf unix_files.tar

Sort Command Examples in Unix / Linux Tutorials


Sort command in unix or linux system is used to order the elements or text. Sort command has the
capability of sorting numerical values and strings. The sort command can order the lines in a text file.
The syntax of sort command is:

sort [options] filename

The options are:

-b : Ignores leading spaces in each line


-d : Uses dictionary sort order. Conisders only spaces and alphanumeric
characters in sorting
-f : Uses case insensitive sorting.
-M : Sorts based on months. Considers only first 3 letters as month. Eg: JAN,
FEB
-n : Uses numeric sorting
-R : Sorts the input file randomly.
-r : Reverse order sorting
-k : Sorts file based on the data in the specified field positions.
-u : Suppresses duplicate lines
-t : input field separator

Sort Command Examples:


Before practicing the examples create the below two files in your unix system:

> cat order.txt


Unix distributed 05 server
Linux virtual 3 server
Unix distributed 05 server

Distributed processing 6 system

> cat delim_sort.txt


Mayday|4
Janmon|1
Declast|12

1. Sorting lines of text


The default sort command uses alphabetical order (ASCII order) to sort the file. It treats each line as
a string and then sorts the lines.

> sort order.txt


Distributed processing 6 system
Linux virtual 3 server
Unix distributed 05 server
Unix distributed 05 server

2. Sorting based on the field positions.


You can specify the field postions using the -k option of sort command. The sort command uses the
space or tab as the default delimiter. To sort based on the data in the second field, run the below
command:

> sort -k2 order.txt

Unix distributed 05 server


Unix distributed 05 server
Distributed processing 6 system
Linux virtual 3 server

You can also pecify more than field with k option as a comma separated list. The below command
uses the second and fourth fields to sort the data.

> sort -k2,4 order.txt

3. Numeric sorting
Instead of the default alphabetical sorting order, you can make the sort command to sort in numeric
order using the -n option. This is shown below:

> sort -nk3 order.txt


Linux virtual 3 server
Unix distributed 05 server
Unix distributed 05 server
Distributed processing 6 system

4. Sort in reverse order


By default, the sort command sorts the data in ascending order. You can change this to descending
order using the -r option.

> sort -nrk3 order.txt


Distributed processing 6 system
Unix distributed 05 server
Unix distributed 05 server
Linux virtual 3 server

5. Suppressing duplicates or Print only unique values


You can produce only unique values in the output using the - u option of the sort command.

> sort -u order.txt


Distributed processing 6 system
Linux virtual 3 server
Unix distributed 05 server

Another way is piping the output of sort command to uniq command.

> sort order.txt | uniq

6. Delimited file input


In the second, third and fourth examples we have sorted the data based on the field positions. Here
the fields are separted by space or tab character. What if the fields are specifed by any other

character? In such cases, we have to specify the input delimiter with the -t option. An example is
shown below:

> sort -t'|' -nrk2 delim_sort.txt


Declast|12
Mayday|4
Janmon|1

7. Sorting on months.
We can sort the data in the monthwise using the -M option of the sort command. This is shown
below:

> sort -M delim_sort.txt


Janmon|1
Mayday|4
Declast|12

Treats the first 3 characters in the string as month and then sorts in months order.

Zip Command Examples in Unix / Linux Tutorials


zip is used to compress the files to reduce file size and also used as file package utility. zip is
available in many operating systems like unix, linux, windows etc.
If you have a limited bandwidth between two servers and want to transfer the files faster, then zip the
files and transfer.

The syntax of zip command is

zip [options] zipfile files_list

The options of zip command are:

-d : Removes the file from the zip archive


-u : Updates the file in the zip archive
-m : Deletes the original files after zipping.
-r : Recursively zips the files in a directory
-x : Exclude the files in creating the zip
-v : verbose mode
-1 : Compresses the files faster
-9 : Compresses the files better
-f : freshen only changed files.
zipfile : creates the zip file with name as zipfile.zip
files_list : list of files to be zipped.

Zip Command Examples:


The files in my current directory are listed below:

docs/linux.pdf

docs/oracle.pdf
docs/unix.pdf
linux-virtual-server.bat
unix-server.dat

Here docs is a directory which contains the files linux.pdf, unix.pdf and oracle.pdf. We will see how to
use zip command with examples.
1. Creating a zip file
The zip command in unix or linux system creates an archive with the specified files. This is shown
below:

> zip archive linux-virtual-server.bat unix-server.dat


adding: linux-virtual-server.bat (deflated 80%)
adding: unix-server.dat (deflated 80%)
> ls
archive.zip

docs

linux-virtual-server.bat

unix-server.dat

The above command creates the zip file with name archive.zip
2. Extracting files from zip
To extract files from the zip, use the unzip command in unix system. This is shown below:

> unzip archive.zip

Archive:

archive.zip

inflating: linux-virtual-server.bat
inflating: unix-server.dat
> ls
archive.zip

linux-virtual-server.bat

unix-server.dat

3. Removing file from a zip file


After creating a zip file, you can remove a file from the archive using the -d option. To remove the file
unix-server.dat from the archive, run the below zip command:

> zip -d archive.zip unix-server.dat


deleting: unix-server.dat

> unzip archive.zip


Archive:

archive.zip

inflating: linux-virtual-server.bat

4. Update existing zip file


You can update the files in already created zip file. If any of the files are modified after zipping, you
can fresh the zip file with only those modified files using the -f option.

> zip -f archive.zip


freshening: linux-virtual-server.bat (stored 0%)

Another way is using the -u option. This option can be used to update the specified list of files or add
new files to the existing zip file.

> zip -u archive.zip

linux-virtual-server.bat temp

updating: linux-virtual-server.bat (deflated 79%)


adding: temp (stored 0%)

5. Recursively zip files in directory.


To zip a directory recursively, use the -r option with the zip command. This example is shown below:

> zip -r dir_archive docs


adding: docs/ (stored 0%)
adding: docs/unix.pdf (stored 0%)
adding: docs/oracle.pdf (stored 0%)
adding: docs/linux.pdf (stored 0%)

6. Excluding files in zipping


Let say you are zipping all the files in the current directory and want to exclude some unwanted files.
You can exclude these unwanted files using the -x option.

zip exclude_archive * -x linux-virtual-server.bat

The above command zips all the files in the current directory except the file linux-virtual-server.bat
7. Faster compressing
You can compress the files very fast using the -1 option with zip command. An example is shown
below with and without using fast compression.

> zip -1 fast_archive linux-virtual-server.bat


adding: linux-virtual-server.bat (deflated 79%)

>zip normal_archive linux-virtual-server.bat


adding: linux-virtual-server.bat (deflated 80%)

If you use fast compression, the archive file created will occupy more space (size) when compared to
normal compression.
8. Better compression.
To reduce more amount of size the files occupied, you can use the -9 option with the zip command.
This gives a better compression.

> zip -9 better_archive linux-virtual-server.bat


adding: linux-virtual-server.bat (deflated 81%)

Compare the deflated percentages in the example 7 and 8.

Mail Command Examples in Unix / Linux Tutorial


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 "fromuser@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"

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"

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 *

Hostname Command Examples in Unix / Linux Tutorials


Hostname is the name of the system or server you are logged into. The hostname can also refer to
the sitename or computer name. As an example, if an organization domain name is "google.com"
and a specific computer name in that doman is "unix-box", then the hostname of the computer is
"unix-box.google.com".
The syntax of hostname command in unix or linux system is

hostname [options] [file]

The options of hostname command are:

-a : Prints the alisa name of the host if created any.


-d : prints the domain name
-i : prints the ip address of the host
-s : prints the shortname of the host.
-v : verbose data
-V : version information
-h : help about hostname command

Hostname Command Examples:


1. Print the hostname of the system The basic functionality of the hostname command is to display
the name of the system on the terminal. Just type the hostname on the unix terminal and press enter
to print the hostname.

> hostname
unix-box.google.com

2. Ip address of the computer You can find the ip address of the computer by using the -i option with
hostname command.

> hostname -i
125.20.223.69

3. Print the domain name To know the domain name where the computer resides, use the -d option
with hostname command.

> hostname -d
google.com

4. Short hostname By default the hostname command prints the complete name of the computer.
You can print a short name by using the -s option. This prints the name upto the first dot in the full
hostname.

> hostname -s
unix-box

5. Getting help To get help about the hostanme command either use the man command or the -h
option with hostname command.

> man hostname


> hostname -h

Paste Command Examples in Unix / Linux Tutorials

Paste command is one of the useful commands in unix or linux operating system. The paste
command merges the lines from multiple files. The paste command sequentially writes the
corresponding lines from each file separated by a TAB delimiter on the unix terminal.
The syntax of the paste command is

paste [options] files-list

The options of paste command are:

-d : Specify of a list of delimiters.


-s : Paste one file at a time instead of in parallel.
--version : version information
--help : Help about the paste command.

Paste Command Examples:


Create the following three files in your unix or linux servers to practice to practice the examples:

> cat file1


Unix
Linux
Windows

> cat file2

Dedicated server
Virtual server

> cat file3


Hosting
Machine
Operating system

1. Merging files in parallel


By default, the paste command merges the files in parallel. The paste command writes
corresponding lines from the files as a tab delimited on the terminal.

> paste file1 file2


Unix

Dedicated server

Linux

Virtual server

Windows

> paste file2 file1


Dedicated server

Unix

Virtual server

Linux
Windows

2. Specifying the delimiter


Paste command uses the tab delimiter by default for merging the files. You can change the delimiter

to any other character by using the -d option.

> paste -d"|" file1 file2


Unix|Dedicated server
Linux|Virtual server
Windows|

In the above example, pipe delimiter is specified


3. Merging files in sequentially.
You can merge the files in sequentially using the -s option. The paste command reads each file in
sequentially. It reads all the lines from a single file and merges all these lines into a single line.

> paste -s file1 file2


Unix

Linux

Windows

Dedicated server

Virtual server

The following example shows how to specify a delimiter for sequential merging of files:

> paste -s -d"," file1 file2


Unix,Linux,Windows
Dedicated server,Virtual server

4. Specifying multiple delimiters.


Multiple delimiters come in handy when you want to merge more than two files with different
delimiters. For example I want to merge file1, file2 with pipe delimiter and file2, file3 with comma
delimiter. In this case multiple delimiters will be helpful.

> paste -d"|," file1 file2 file3


Unix|Dedicated server,Hosting
Linux|Virtual server,Machine
Windows|,Operating system

5. Combining N consecutive lines


The paste command can also be used to merge N consecutive lines from a file into a single line. The
following example merges 2 consecutive lines into a single line

> cat file1 | paste - Unix

Linux

Windows

Related Commands:
Join command examples in unix

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

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

$ expr 1 + 2 + 3

$ 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

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

The division operator returns only the arithmetic quotient.


5. Remainder or modulus

$ expr 6 % 4

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

$ expr 1 \<= 1

$ expr 2 \> 5

$ expr 2 \>= 5

$ expr 7 = 7

$ expr 9 != 18

$ expr 2 \| 5

$ expr 0 \| 5

$ expr 2 \& 5

$ expr 6 \& 3

$ expr 6 \& 0

$ expr 0 \& 3

String Function Examples:


1. Length of string
The length function is used to find the number of characters in a string.

$ expr length linux

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

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,
Tasks: 174 total,
Cpu(s):

5.3%us,

4 users,

load average: 0.02, 0.07, 0.15

2 running, 172 sleeping,


2.6%sy,

0.1%ni, 88.0%id,

0 stopped,
3.8%wa,

1990204k total,

756084k used,

1234120k free,

Swap:

3984080k total,

0k used,

3984080k free,

0.2%hi,

Mem:

PID

USER

PR

NI VIRT

RES

SHR

1156 root

20

94144

35m

9m

1908 user

20

38604

13m

2315 user

20

2468

1092

%CPU %MEM

0 zombie

TIME+

0.1%si,

74648k buffers

0.0%st

391680k cached

COMMAND

10

1.8

1:14.27 Xorg

9428 S

0.7

0:03.41 gnome-terminal

784

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

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

Tail Command Examples in Unix / Linux Tutorials

The tail command in unix or linux system is used to print the last N lines from the file on the terminal.
Tail command is especially used with log files to read the last few lines to know about the error
messages. The syntax of tail command is

tail [options] [files]

The tail command options are:


c : Prints the last N bytes of file; With leading +, prints the characters from the N byte in the
file.

n : Prints last N lines; With leading + prints lines from the Nth line in the file.

f : Prints the appended lines on the terminal as the file grows.


Tail Command Examples
Create the following file in your linux or unix operating system for practising the examples:

> cat example.txt


virtual storage
oracle virtual instance
mysql backup
dedicated hosting server
cloud servers

1. Display last 10 lines


By default, the tail command prints the last 10 lines from the file.

> tail example.txt

2. Display last N lines


Use the -n option to print the last n lines from the file. The following example prints the last 2 lines
from the file:

> tail -n2 example.txt


dedicated hosting server

cloud servers

3. Print lines from the Nth line


You can print lines starting from the Nth line in a file. The following example prints lines from the
2nd line.

> tail -n+2 example.txt


oracle virtual instance
mysql backup
dedicated hosting server
cloud servers

4. Print the last n bytes.


use the -c option to print the last N bytes from the file. The following example prints the last 8
bytes from the file.

> tail -c8 example.txt


servers

5. Print characters from the Nth byte.


Use the leading "+" with -c option to print the characters from the Nth byte. The following example
prints the characters from the 79th byte.

> tail -c+79 example.txt


cloud servers

6. Print last lines from dynamically changing file.


The -f option print the lines from file that is growing dynamically. When you run the tail -f
filename command, it prints the last 10 lines and waits for new lines to be added to the file.
Whenever the new lines are appended to the file, the tail command also appends the new lines on
the standard output. The -f option is useful when debugging applications. In general, the applications
writes error messages to log files. You can use the -f option to check for the error messages as and
when they appear in the log file.

> tail -f logfile

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

Dirname Command Examples in Unix / Linux


The unix dirname command strips non-directory suffix from a file name.
The syntax of dirname command is

dirname NAME

The dirname command removes the trailing / component from the NAME and prints the remaining
portion. If the NAME does not contain / component then it prints '.' (means current directory).
Dirname command is useful when dealing with directory paths in unix or linux operating systems.
Some examples on dirname command are shown below:
Dirname Command Examples:

1. Remove the file name from absolute path.


Let say my directory path is /usr/local/bin/add.sh. Now i want to remove /add.sh and display only
/usr/local/bin, then we can use the dirname command.

> dirname /usr/local/bin/add.sh


/usr/local/bin

2. dirname sum.pl
Here you can see that the NAME does not contain the / component. In this case the dirname
produces '.' as the output.

> dirname sum.pl


.

Note: The directories and filename which i have passed as arguments to dirname command in the
above examples are just strings. There is no need of these directories or files to exist in the unix
machine.