Saturday, 16 June 2018

Basic Unix and Linux Commands with Examples


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.
Example:
>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.
Example:
>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.
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
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 anotherlocation 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/
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 thefields 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
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.
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
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
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 don’t 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.
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
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
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.
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 thefourth 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
Chmod Command Examples in Unix / Linux Tutorials
Chmod (change mode) is one of the most frequently used commands in unix or linux operating system. The chmod command is used to change the file or directory access permissions. To know about the access permissions of a file or directory, use the ls -l command as shown below: 
$ ls -l sample.sh
-rwx-rw-r-- 1 matt deploy 94 Oct  4 03:12 sample.sh
Here in the above example: Use matt has the read, write and execute permissions on the file. Group deploy has read and write permissions. Others have only the read permission.
File and Directory permissions:
There are three different permissions. They are: 
Read (4): Permitted to read the contents of the file. In case of directory, you can view all the files and sub-directories in that directory.
Write (2): Permitted to write to the file. In case of directory, you can create files and sub-directories.
Execute (1): Execute the file as a program/shell script. In case of directory, You can enter into that directory.
Here in the above, the numbers in the brackets represents the numeric values for the corresponding permissions. If you want to have a combination of permissions add the required numbers. For example, for read and execute, it is 4+1=5.
The syntax of chmod command is 
chmod [options] mode filename
The important options are: 
-R : recursively change the permissions of a directory.
-v : Verbose
Chmod Examples in Linux / Unix:
1. Give read, write and execute permissions to everyone.
Read, write and execute: 4+2+1=7 
$ chmod 777 sample.sh
In the above example, you can see that the permissions are specified with a three digit number. The first digit is for user permissions, second is for group and third is for others permission. This type of representation is called octal representation. Alternatively, you can use the symbolic representation to give the permissions. 

chmod ugo+rwx sample.sh
We will see more details about the symbolic representation later.
2. Give read permission to user, write permission to group and execute permission to others. 
$ chmod 421 sample.sh
3. Recursive permissions to directory
To give read and write permissions to all the users to a directory (including files and subdirectories) use the recursive option -R. 
chmod -R 666 /dir
Symbolic Representation of Permissions:
The following symbols are used to represent the users, groups and others: 
u : User
g : Group
o : Others a : All (user, group and others)
The following symbols represent the permissions: 
r : read
w : write
x : execute
The following symbols represent the permissions grant or revoke: 
+ : Additional permissions. Selected permissions are added.
- : Revoke the permissions. Selected permissions are revoked.
= : Specific permissions. Only selected permissions are assigned.
Examples:
1. Remove write permission from group 
$ chmod g-w sample.sh
This will only removes the write permission for the group.
2. Add new permission execute to others 
$ chmod o+x sample.sh
In addition to the existing permissions, this will add execute permission to others.
3. Give only read permissions to the user 
$ chmod u=w sample.sh
This will remove the existing permissions to the user and gives only write permission to the user.
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
If you like this post, please share it on google by clicking on the +1 button.
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
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"
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
Examples of Awk Command in Unix - Part 2
1. Inserting a new line after every 2 lines
We will see how to implement this using the awk command with an example.
The input "file.txt" contains the below data:
1 A
2 B
3 C
4 D
5 E
6 F
Let say, we want to insert the new line "9 Z" after every two lines in the input file. The required output data after inserting a new line looks as
1 A
2 B
9 Z
3 C
4 D
9 Z
5 E
6 F
9 Z
The awk command for getting this output is 
awk '{
 if(NR%2 == 0)
 {
  print $0"\n9 Z";
 }
 else
 {
  print $0
 }
     }' file.txt
2. Replace the Nth occurrence of a pattern
The input file contains the data.
AAA 1
BBB 2
CCC 3
AAA 4
AAA 5
BBB 6
CCC 7
AAA 8
BBB 9
AAA 0
Now we want to replace the fourth occurrence of the first filed "AAA" with "ZZZ" in the file.
The required output is:
AAA 1
BBB 2
CCC 3
AAA 4
AAA 5
BBB 6
CCC 7
ZZZ 8
BBB 9
AAA 0

The awk command for getting this output is
awk 'BEGIN {count=0}
 {
  if($1 == "AAA")
  {
   count++
  }
  if(count == 4)
  {
   sub("AAA","ZZZ",$1)
  }
 }
 {
  print $0
 }' file.txt
3. Find the sum of even and odd lines separately
The input file data:
A 10
B 39
C 22
D 44
E 75
F 89
G 67
You have to get the second field and then find the sum the even and odd lines.
The required output is
174, 172
The awk command for producing this output is
awk '{
 if(NR%2 == 1)
 {
  sum_e = sum_e + $2
 }
 else
 {
  sum_o = sum_o + $2
 }
      }
      END { print sum_e,sum_o }' file.txt

4. Fibonacci series using awk command
Now we will produce the Fibonacci series using the awk command.
awk ' BEGIN{
 for(i=0;i<=10;i++)
 {
  if (i <=1 )
  {
   x=0;
   y=1;
   print i;
  }
  else
  {
   z=x+y;
   print z;
   x=y;
   y=z;
  }
 }
    }'
The output is 
0
1
1
2
3
5
8
13
21
34
55
5. Remove leading zeros from a file using the awk command. The input file contains the below data. 
0012345
05678
01010
00001
After removing the leading zeros, the output should contain the below data. 
12345
5678
1010
1
The awk command for this is. 
awk '{print $1 + 0}' file.txt
awk '{printf "%d\n",$0}' file.txt
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  | head -1
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
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.
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 user’s 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 user’s 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


Top Command Examples in Unix / Linux Tutorials
Top command in unix or linux operating system is one of the useful commands to know about the system information. The top command provides real-time view of the running system and also the list of tasks currently managed by the kernel. Top is a non-interactive command and provides limited interactive options to the users.
The syntax of top command is 
top [options]
The options are: 
-b : Starts top command in batch mode. Useful for sending top output to other programs or file.
-d : specify the delay time between the screen updates.
-n : Number of iterations, the top should produce before ending.
-u : Monitor only the specified user processes.
-p : Monitor only the specified processes. Specify the process ID
Top Command Examples:
1. Monitor system information
The basic functionality of the top command is to monitor the system information. Just run the top command on the terminal to print the system information. 
$ top
top - 19:05:50 up 21 min,  4 users,  load average: 0.02, 0.07, 0.15
Tasks: 174 total,   2 running, 172 sleeping,   0 stopped,   0 zombie
Cpu(s):  5.3%us,  2.6%sy,  0.1%ni, 88.0%id,  3.8%wa,  0.2%hi,  0.1%si,  0.0%st
Mem:   1990204k total,   756084k used,  1234120k free,    74648k buffers
Swap:  3984080k total,        0k used,  3984080k free,   391680k cached
PID  USER  PR  NI VIRT   RES   SHR  S  %CPU %MEM  TIME+   COMMAND
1156 root  20  0  94144  35m   9m   S   10  1.8   1:14.27 Xorg
1908 user  20  0  38604  13m   9428 S   2   0.7   0:03.41 gnome-terminal                         
2315 user  20  0  2468   1092  784  R   2   0.1   0:00.01 top
Top provides dynamic information of the system. As and when the system information changes, it keeps on updating the information on the terminal. The fields of top command are explained below: 
PID: Tasks process id.
USER: User name of the owner who started the process.
PR: priority of the task.
NI : Nice value of the task. Negative value means highest priority. Positive value means lowest priority. Zero means priority cant be determined.
VIRT: virtual image. Total amount of memory used by the task.
RES: Resident size. Non-swapped memory used by the task.
SHR : Shared memory used by the task.
S : Status of the process.D - uninterruptible sleep;R - running; S - sleeping; T - traced or stopped; Z - zombie.
%CPU : Cpu usage.
%MEM: Usage of physical memory.
TIME: Cpu time. Time of the task since it started.
COMMAND: Program name or command name.
2. Redirect top command output to a file.
If you write the output of top command to a file, the data is written in binary format and is not readable by the user. Use the -b option to write the output of the top command in text format. 
$ top -n 1 -b > top_output.dat
Here the -n 1 option specifies the top command to run for only one iteration.
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
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.
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, let’s 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
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

No comments:

Post a Comment