LINUX ADVANCED COMMANDS
cat command is one of the most frequently used command in linux/unix operating system. cat command allows us to create single or multiple files, view the contain of file, concatenate file and redirect output to terminal or file.
The basic syntax of the cat command
cat [OPTION]... [FILE]...
1. DISPLAY CONTENT OF FILE:
The simplest way to use cat command is to give a file name of text file to it. It will display the contents of the text file on the screen. Example is shown below.
2. CONTENTS OF MULTIPLE FILES IN THE TERMINAL:
If more than one file name is specified, cat will display one file after the other in the screen, Example is shown below.
3. CREATE A FILE WITH cat COMMAND:
A new file can be created with the help of the cat command, for instance if cat > [text file name] is given, the command waits for the input from the user, type the desired text and press ctrl + D to exit. The content will be written to the text file. Below is an example.
4. WITH MORE OR LESS OPTION:
If the file has the large number of contents that will not fit into the output screen, so the more or less command can be used to scroll the files which have large contents.
5. DISPLAY LINE NUMBERS:
With the –n option with the cat command the line numbers of the file will be displayed. Below is the example.
6. STANDARD OUTPUT WITH REDIRECTION OPERATOR:
cat [file 1] > [file 2]
The standard output of the file can be redirected into new file or existing file using ‘>’ symbol. By using this symbol, existing contents in the file 2 will be overwritten. Below is the example.
7. REDIRECTING MULTIPLE FILE INTO SINGLE FILE:
cat [file 1] [file 2] [file 3] > [new file]
The content from one or more file can be concatenated to a single file using the above command. The below is the example shown.
8. APPENDING STANDARD OUTPUT WITH REDIRECTION OPERATOR:
cat [file 1] >> [file 2]
The standard output of the file can be redirected into new file or existing file using ‘>>’ symbol. As shown the output content from file 1 will append to the content of file 2. Below is the example.
Head command is used to output the first part of the files. The syntax for head is.
head [OPTION]... [FILE]...
Head prints the first N number of data of given input. By default, it prints first 10 lines of each given file.
It can be used for viewing Huge Log files in Unix.
To print the first particular number of lines option –n is used with head command. The below is the example. head -n [file name]
tail command is used to output the last part of the files. The Syntax of the tail command
tail [OPTION]... [FILE]...
Tail prints the last N numbers of the data of given input, By default it prints the last 10 lines of each given file.
To print the last particular number of lines for the given input the option with –n can be used. tail –n [file name].
COMPRESSION AND DE-COMPRESSION OF FILE:
tar command in Linux is often used to create the file in format .tar.gz or .tgz archive files also called as “tarballs”. The GNU tar command included with the Linux distributions that can create a.tar archive and then compress it with the gzip or bzip2 command in a single command line. Command to compress the directory or the files in the Linux.
tar –cvf [archive file name].tar [files]/[Directories]
c – Creates a new .tar archive file
v – Verbosely show the .tar file progress
f – File name type of the archive file
In order to compress the archived file to tar.gz format gzip is used. The format is
gzip [file name]
In order to decompress the file, first the compressed file should be unzipped using the command gunzip. The format is
gunzip [compressed file]
After unzip the files should be extracted using the tar command which is used for compression of the file, but here the option added to the tar command will differ as format shown below,
tar –xvf [decompressed file]
grep command in Linux is a command line utility for printing lines that matches a pattern. It can be used to find text in file and search a directory structure of files recursively. It also supports showing context of match by showing lines before and after the result and has support for regular expressions in pattern matching.
grep [OPTIONS] PATTERN [FILE...]
1. SEARCHING FOR GIVEN STRING IN THE FILE:
Basic usage of the grep command is to search for the specific string in the specific file.
2. SEARCH STRING AS A REGULAR EXPRESSION PATTERN:
It is a powerful feature and can use a regular expression with much effectively. In the example shown, it searches for all patterns that start with “In” and ends with “voltage” with anything in between.
3. DISPLAYING LINE NUMBERS OF MATCHED PATTERN:
The grep command with the option –n can be used to display the line numbers of the string searched in file. The example is shown below.
4. EXCLUDING THE PATTERN MATCHED LINE:
The grep command with the option –v can be used display the all lines excluding the search string line (commonly known as invert match display).
Linux ssh command allows you to log into and work on the remote computer, which can be located anywhere in world. The command (syntax: ssh hostname) opens a window on your local machine through which you can run and interact with programs on the remote machine just as if it were right in front of you. You can use the remote computer's software; access its files, transfer files, and more.
An ssh session is encrypted and requires authentication. Ssh stands for Secure Shell, referring to the operation's inherent security.
To login into the remote host.
ssh remote_username @ remote_host