0% found this document useful (0 votes)
139 views

Linux Commands1

Here are some tips to help with command line issues: - Use the man/info pages - type "man command" or "info command" for documentation on how to use a command - Tab completion - start typing a command or file name and hit tab to auto-complete - History command - use "history" and arrow keys to see and re-run recent commands - Aliases - create aliases for common commands you use frequently - Google - search online for command examples and cheat sheets - Ask for help - communities like StackOverflow and Reddit can help answer specific questions - Practice - the more you use the command line, the more familiar the commands will become Don't get discouraged if it seems

Uploaded by

AASTHA JOSHI
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Linux Commands1

Here are some tips to help with command line issues: - Use the man/info pages - type "man command" or "info command" for documentation on how to use a command - Tab completion - start typing a command or file name and hit tab to auto-complete - History command - use "history" and arrow keys to see and re-run recent commands - Aliases - create aliases for common commands you use frequently - Google - search online for command examples and cheat sheets - Ask for help - communities like StackOverflow and Reddit can help answer specific questions - Practice - the more you use the command line, the more familiar the commands will become Don't get discouraged if it seems

Uploaded by

AASTHA JOSHI
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 120

Linux Commands

Mr. M.R. Solanki


Sr. Lecturer, Information Technology,
[email protected]
SBMP
Learning Outcomes

Students will be able to:


• Use Linux utility commands to achieve desired
tasks
• Use Linux system commands to achieve desired
tasks
• Use Linux File commands to achieve desired
tasks
• Use Linux networking to debug the network
Linux Utility Commands
Linux Command Format
Linux based Operating Systems are very powerful but their
true power lies in the command line
Syntax
The commands in Linux have the following syntax:
$command options arguments
The options can modify the behavior of a command.
The arguments may be files or directories or some other data
on which the command acts.
Every command might not need arguments. Some commands
work with or without them (e.g. ‘ls’ command).
The options can be provided in two ways: full word options
with -- (e.g. --help), or single letter options with - (e.g. -a -b -c or
multiple options, -abc).
cal command
Displays the calendar of the current month.
$ cal
July 2012
Su Mo Tu We Th Fr Sa
1234567
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
cal command
‘cal ’ can display calendar for the specified month and year
$ cal 08 1991
August 1991
Su Mo Tu We Th Fr Sa
123
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
date command
Displays current time and date.
$ date
Fri Jul 6 01:07:09 IST 2012
If you are interested only in time, you can use 'date +%T' (in
hh:mm:ss):
$ date +%T
01:13:14
Working with Calculator
Working with Calculator
Qalc is the terminal version of the calculator app,
Qalculate
It doesn’t come pre-installed like gcalccmd
The command line calculator also allows you to do
currency conversions besides regular calculation work
 You can install Qalc using the following command in the
terminal:
sudo apt-get install qalc
Working with Calculator
Also, it remembers the answer from your last calculation.
So, you can go like this:

Press CTRL+D to exit qalc command line calculator


utility
You can access the help section by using the help
command inside the qalc utility
Linux System Commands
The echo command
This command will echo whatever you provide it.
$ echo "linoxide.com"
linoxide.com
The ‘echo’ command is used to display the values of a
variable. One such variable is ‘HOME’.
To check the value of a variable precede the variable with
a $ sign.
$echo $HOME
/home/raghu

$echo $PATH
/home/mrs/bin:/home/mrs/.local/bin:/usr/local/sbin:/usr/loc
al/bin:/usr/sbin:/usr/bin:/sbin:
/bin:/usr/games:/usr/local/games:/snap/bin
The history command
History Events:
In the BASH shell, the history utility keeps a record of the
most recent commands you have executed.
The commands are numbered starting at 1, and a limit
exists to the number of commands remembered—the
default is 500
The history utility is a kind of short-term memory,
keeping track of the most recent commands you have
executed.

$ history
1 cp mydata today
2 vi mydata
10 mv mydata reports
4 cd reports
5 ls
System Commands
 To clear the screen
$clear
 To change your password with 'passwd' command
$passwd
 To shut down your system
$shutdown -h now
 Executing commands with admin priviledges
$sudo
sudo (SuperUser DO) Linux command allows you to run
programs or other commands with administrative
privileges like “Run as administrator” in Windows.

 To Display the amount of free space available on the


system $free –m (gives info in MB)

 To know basic information about the system


$uname -a
Shutdown Commands
 To shut down your system

$shutdown -h now

 Shutdown the system after 10 minutes

$sudo shutdown –h +10

 Rebbot the system

$sudo shutdown –r now

 Force the filesystem check during reboot.

$sudo shutdown –Fr now


ps and kill command
ps (Process) gives the status of running processes with a
unique Id called PID.

kill
 This command is used to kill process which is not relevant
now or is not responding.
 We can kill a process and start it without restarting the whole
system.
ps and kill command

#pkill rhythmbox
alias command
alias is a built in shell command that lets you assign name for
a long command or frequently used command.
 ‘ls -l‘ command is used frequently, which
includes 5 characters including space.
unalias command
To remove alias ‘l‘, use the following ‘unalias‘ command

 check, if ‘l‘ still is alias or not.


Printing a document
This command prints files named on command line, to
named printer.

Note: The ‘lpq‘ command lets you view the status of a


printer (whether it’s up or not), and the jobs (files) waiting
to be printed.
lsblk command
The “lsblk” stands for (List Block Devices), print block
devices by their assigned name (but not RAM) on the
standard output in a tree-like fashion

 The “lsblk -l” command list block devices in ‘list‘


structure
lsof Command
 Displays List of all open files
whereis command
The ‘whereis‘ command is used to locate
the Binary, Sources and Manual Pages of the command.
For example, to locate the Binary, Sources and Manual
Pages of the command ‘ls‘ and ‘kill‘
service command
 This command make it possible to start, restart or stop a
service without restarting the system, for the changes to
be taken into effect.
Mount command
attaching of an additional filesystem to
the currently accessible filesystem of a computer
The mount command mounts a storage device
or file system, making it accessible and attaching
it to an existing directory structure
 The standard form of the mount command is:
mount -t type device dir(destination folder)

This tells the kernel to attach the filesystem found


on device (which is of type type) at the
directory dir. If dir is not existing, we have to
create it first
Mount command
mount -t type device dir(destination folder)
Example:

mount -t ntfs /dev/sda1 /mnt/

Note: Used to mount windows partition in


Linux
Accessing Windows Partitions in Ubuntu

To mount as read only

$sudo mount -t vfat -o ro /dev/sdb1 /media/aaronkilik/WIN_PAR

$ sudo mount -t ntfs-3g -o ro /dev/sdb1 /media/aaro


Accessing Windows Partitions in Ubuntu

Install ntfs-3g (if not installed):


sudo apt-get install ntfs-3g

run the ntfsfix command on your NTFS


partition:

ntfsfix /dev/sda4
Archiving Files
 The tar (tape archive) utilities is an archiving tool. It puts one
or more files into an archive, and the common filename suffix
for that is .tar
 The files that tar produces are uncompressed

c – Creates a new .tar archive file.


v – Verbosely show the .tar file progress.
f – File name type of the archive file
Compressing Files
 A .gz file was produced by gzip, which is a compression tool.
 It does not archive multiple files together.
It compresses single files
A .tar.gz file is a tar archive that has been compressed by
gzip.
Compressing Files

Note:

Other compression tools, for example compress (.Z) and


bzip2 (.bz2) perform the same things conceptually, but with
different algorithms and file formats
Uncompressing Files
 Same command for .tar and .tar.gz
x – extract
 c – If we want to extract in different directory
last command
 With last command we can watch user’s activity in the
system.
Displays complete user info i.e. time, date, system
reboot or boot and kernel version
Useful command to troubleshoot
last command
 To get activity info of a specific user
Adding Users
 To add a new user
#adduser newuser or
$sudo adduser newuser

 Answer the following questions:

 Assign and confirm a password for the new user


 Enter any additional information about the new user. This is
entirely optional and can be skipped by hitting "ENTER" if
you don't wish to utilize these fields.
 Finally, you'll be asked to confirm that the information you
provided was correct. Type "Y" to continue.
Assigning sudo permission

 Type the following commands:


$sudo visudo

 Search for the line that looks like this:

root ALL=(ALL:ALL) ALL

Below this line, copy the format you see here,


root ALL=(ALL:ALL) ALL
newuser ALL=(ALL:ALL) ALL
Add a new line like this for each user that should be given full
sudo privileges.

 When you are finished, you can save and close the file by
hitting CTRL-X, followed by "Y", and then hit "ENTER" to
confirm.
Deleting a user

 Type the following commands:


$sudo deluser newuser

 If you want to delete user’s home directory then:


$sudo deluser --remove-home newuser

Note: Remove the sudo permissions assigned to the user by


removing the following entry:
newuser ALL=(ALL:ALL) ALL
Getting help of commands
Problems with Command Line

 A big disadvantage of the command line is that there


are a lot of commands with too many options and
usage. But nobody can remember all the commands.
There are some smart ways of using command line
in Linux
 Fortunately Linux provides some commands to help
us
help option

With almost every command, ‘--help’ option shows


usage summary for that command

$ date --help
Usage: date [OPTION]... [+FORMAT] or: date [-u|--utc|--
universal] [MMDDhhmm[[CC]YY][.ss]] Display the current
time in the given FORMAT, or set the system date.
whatis command

This command gives a one line description about the


command. It can be used as a quick reference for any
command.

$ whatis date
date (1) - print or set the system date and time

$ whatis whatis
whatis (1) - display manual page descriptions

‘--help’ option and ‘whatis’ command do not provide
thorough information about the command. For more
detailed information, Linux provides man pages and info
pages
man command
man command gives detailed information of a given
command. Format $man command
The man pages are properly documented pages. They have
following sections:
NAME: The name and one line description of the command.
SYNOPSIS: The command syntax.
DESCRIPTION: Detailed description about what a command does.
OPTIONS: A list and description of all of the command's options.
EXAMPLES: Examples of command usage.
FILES: Any file associated with the command.
AUTHOR: Author of the man page
REPORTING BUGS: Link of website or mail-id where you can report
any bug.
SEE ALSO: Any commands related to the command, for further
reference.
With -k option, a search through man pages can be performed. This
searches for a pattern in the name and short description of a man
page.
man command
Example of man command:

$ man -k gzip
gzip (1) - compress or expand files
lz (1) - gunzips and shows a listing of a gzip'd tar'd archive
tgz (1) - makes a gzip'd tar archive
uz (1) - gunzips and extracts a gzip'd tar'd archive
zforce (1) - force a '.gz' extension on all gzip files
info pages
Info documents are sometimes more elaborate than the
man pages.
But for some commands, info pages are just the same as
man pages.
These are like web pages. Internal links are present
within the info pages. These links are called nodes.
Info pages can be navigated from one page to another
through these nodes
Example: $ info ls
help command
with man, info etc commands you can not get much
information about the inbuilt commands.
If you want to know what an inbuilt command do, you have to
use help command
File Commands
pwd

 To know which directory you are in, you can use


the “pwd” command
 When you first open the terminal, you are in the home
directory of your user
Changing Directories
cd - Change the current working directory to the directory
provided as argument.
If no argument is given to ‘cd’, it changes the directory to the
user's home directory.
The directory path can be an absolute path or relative to
current directory
Example: If we are in home directory and we want to move
to Downloads:

Note:# If directory name contains space then use ‘\’ before


space as shown in the diagram
#To go back from a folder to the folder before that, you can
type “cd ..”. The two dots represent back
Changing Directories

$ pwd
/home/raghu
$ cd /usr/share/
$ pwd
/usr/share
$ cd doc
$ pwd
/usr/share/doc
Listing File And Directories Command
$ ls [files-or-directories]
List files and/or directories. If no argument is given, the
contents of current directory are shown

If a directory is given as an argument, files and


directories in that directory are shown.
$ ls /usr
bin games include lib lib64 local sbin share src
Listing File And Directories Command
‘ls -l’ displays a long listing of the files.
$ ls -l
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 12:52 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file1.txt
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file2.txt
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file3.txt
 The first character is 'd' or '-'. It distinguishes between
file types. The entries with a '-' (dash) are regular files, and
ones with 'd' are directories.
The next 9 characters are permissions ('rwxr-xr-x' in first
listing).
The number following the permissions is the link count.
Link count is followed by user and group owner. In the
above example, the file owner is 'raghu' and group owner
is 'raghu' as well. Next is the size of the file. And then time
stamp before the name of file (or directory).
Listing File And Directories Command
By default, hidden files or directories are not shown, to
see hidden files as well, -a option is used.
Hidden files in Linux start with a period sign (.).
Any file that starts with a period is hidden. So, to hide a
file, you just need to rename it (and put a period before it)
$ ls -la odesk
total 16
drwxr-xr-x 4 raghu raghu 4096 2012-07-06 13:46 .
drwxr-xr-x 11 raghu raghu 4096 2012-07-06 13:15 ..
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 12:52 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file1.txt
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file2.txt
-rw-r--r-- 1 raghu raghu 0 2012-07-06 12:52 file3.txt
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 13:46 .hiddendir
-rw-r--r-- 1 raghu raghu 0 2012-07-06 13:46 .hiddenfile1.txt
-rw-r--r-- 1 raghu raghu 0 2012-07-06 13:46 .hiddenfile2.txt
Listing File And Directories Command
If you want to see the properties of a directory instead of
the files contained in it, use -d (with -l) option:

$ ls -ld odesk/
drwxr-xr-x 4 raghu raghu 4096 2012-07-06 13:46 odesk/
Creating Directories
To create a directory, the ‘mkdir’ command is used.

$ mkdir example
$ ls -l
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 example
Creating Files
For creating an empty file, use the touch command.

$ touch file1 file2 file3


$ ls -l
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file2
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3

Note: If a file already exists, touch will update its time


stamp. There are a lot of other methods to create a new file,
e.g. using a text editor like vi or gedit, or using redirection.
Copying Files & Directories
$cp source destination
Copy files and directories.
$ cp usrlisting listing_copy.txt
$ ls -l
total 12
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 14:09 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file2
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:02 listing_copy.txt
-rw-r--r-- 1 raghu raghu 491 2012-07-06 14:23 usrlisting

Note: If the source is a file, and the destination (file) name


does not exist, then source is copied with new name
Copying Files & Directories
If the destination is a directory, then the file is copied with
its original name in that directory.
$ cp listing_copy.txt example/
$ ls -l example/
total 4
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:07 listing_copy.txt
Multiple files can also be copied, but in that case, the last
argument will be expected to be a directory where all the files
are to be copied. And the rest of the arguments will be treated
as file names.
$ cp file1 file2 example/
$ ls -l example/
total 4
-rw-r--r-- 1 raghu raghu 0 2012-07-06 16:10 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 16:10 file2
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:07 listing_copy.txt
Copying Files & Directories
If a directory is to be copied, then it must be copied
recursively with the files contained in it. To copy a directory
recursively, use -r option with ‘cp’ command:

$ cp -r example /tmp/expertslogin/
$ ls -l /tmp/expertslogin
total 4
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 16:12 example
Moving a file or directory
$ mv source destination
Move files or directories. The 'mv' command works like 'cp'
command, except that the original file is removed.
But, the mv command can be used to rename the files (or
directories)
$ mv listing_copy.txt usrcopy
$ ls -l
total 12
drwxr-xr-x 2 raghu raghu 4096 2012-07-06 16:10 example
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file2
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:02 usrcopy
-rw-r--r-- 1 raghu raghu 491 2012-07-06 14:23 usrlisting
Here, 'listing_copy.txt' is moved with the name 'usrcopy' in
the same directory (or you can say that it has been renamed)
Removing a file or directory

$ rmdir dir
'rmdir' command removes any empty directories, but
cannot delete a directory if a file is present in it.
To use ‘rmdir’ command, you must first remove all the files
present in the directory you wish to remove (and possibly
directories if any).
Removing a file or directory
$ rm files|directories
A directory must be removed recursively with -r option.
$ rm file2
$ rm -r example/
$ ls -l
total 8
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file1
-rw-r--r-- 1 raghu raghu 0 2012-07-06 14:20 file3
-rw-r--r-- 1 raghu raghu 491 2012-07-06 16:02 usrcopy
-rw-r--r-- 1 raghu raghu 491 2012-07-06 14:23 usrlisting

Here, the file named 'file2' is removed first, and then the
directory 'example' is removed recursively. This can be seen
in the output of ‘ls -l’ command where these two are no
longer present.
Removing a file or directory
Other File Commands
file command
Determines the file type of a given file.
$ file /etc/passwd
/etc/passwd: ASCII text
You can provide one or more than one file as an argument
to the file command.
$ file td.c td.out ARP.java Screenshot.png StringTokenizing.class
idl.rar List.pdf
td.c: ASCII C program text, with CRLF line terminators
td.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not
stripped
ARP.java: ASCII Java program text, with CRLF line terminators
Screenshot.png: PNG image data, 1366 x 768, 8-bit/color RGB, non-
interlaced
StringTokenizing.class: compiled Java class data, version 50.0
(Java 1.6)
idl.rar: RAR archive data, v1d, os: Win32
List.pdf: PDF document, version 1.4
stat command
To check the status of a file.
This provides more detailed information about a file than ‘ls -l’
output.

$ stat usrcopy
File: `usrcopy'
Size: 491 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 149452 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ raghu) Gid: ( 1000/ raghu)
Access: 2012-07-06 16:07:06.413522009 +0530
Modify: 2012-07-06 16:02:30.204152386 +0530
Change: 2012-07-06 16:17:18.992559654 +0530
cat command
The 'cat' command is actually a concatenator, but can be
used to view the contents of a file.

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
pagers
The cat command lists file as a whole. But if the file is big
enough to fit into one screen, then we will be able to see only
the last page of the file.
The commands 'less' and 'more' display files one page at a
time. So they are also called pagers.
You can navigate through a file using arrow keys. To quit
from a pager, hit 'q'.
 In less command, to see remaining pages, we can press
“space bar” etc.
Use ctrl+f to go to next page
Ctrl+b to go to previous page
head command
Displays the first few lines of a file. By default, the ‘head’
command displays the first 10 lines of a file
$ head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh

But with -n option, the number of lines to be viewed can be


specified.
tail command

Similar to ‘head’; the ‘tail’ command shows the last 10 lines


by default, and -n option is available as well.

$ tail -n 4 /etc/passwd

raghu:x:1000:1000:Raghu Sharma,,,:/home/raghu:/bin/bash
sshd:x:113:65534::/var/run/sshd:/usr/sbin/nologin
dictd:x:114:123:Dictd Server,,,:/var/lib/dictd:/bin/false
mysql:x:115:124:MySQL Server,,,:/nonexistent:/bin/false
word count command
This command counts lines, words and letters of the input
given to it.

$ wc /etc/passwd

35 57 1698 /etc/passwd

The /etc/passwd file has 35 lines, 57 words, and 1698 letters


present in it.
grep command
The ‘grep’ command searches for a pattern in a file (or standard
input).
It supports regular expressions.
It returns a line if it matches the pattern in that line.

Example: If we wish to find the lines containing the word ‘nologin’,


we use ‘grep’ as follows:
$ grep nologin /etc/passwd
sshd:x:113:65534::/var/run/sshd:/usr/sbin/nologin
Grep Examples
1. Searching for the given string in the specified file
grep “linux” index.html
2. searches for the words like “LINUX”, “Linux”, “linux” case
insensitively
grep -i “linux” index.html
3. Searching for a string in multiple files
grep “linux” file*.*
4. Searching with regular expression: searches for all the
pattern that starts with “fast” and ends with “host” with
anything in-between. i.e To search “fast[anything in-
between]host” in index.html file
grep “fast.*host” index.html

5. Display the line number which contains the matched string


in a file using the -n option
grep -n “word*” file.txt
Grep Examples
6. Highlighting the search using grep
grep –color “linux” index.html
7. Print the line excluding the pattern using -v option
Example: List all the lines of the file /etc/passwd that does
not contain specific word “string”
grep -v linux /etc/passwd
8. Display all the lines that starts with specified pattern using
^ symbol
grep ^root /etc/passwd
9. Display all the lines that ends with specified pattern using
$ symbol
grep bash$ /etc/passwd
10. Search the pattern recursively using -r option
grep -r linux /etc/
11. Counting the lines when words match
grep -c 'test' /home/example/test.txt
Redirection and Piping
streams
Input and output in the Linux environment is distributed across
three streams. These streams are:
standard input (stdin)
standard output (stdout)
standard error (stderr)
The streams are also numbered:
stdin (0)
stdout (1)
stderr (2)

During standard interactions between the user and the
terminal, standard input is transmitted through the user's
keyboard. Standard output and standard error are displayed
on the user's terminal as text. Collectively, the three streams
are referred to as the standard streams.
standard input
The standard input stream typically carries data from a user
to a program.
Programs that expect standard input usually receive input
from a device, such as a keyboard.
Standard input is terminated by reaching EOF (end-of-file).
As described by its name, EOF indicates that there is no
more data to be read
(After opening cat, type a series of numbers as it is running)
$cat.
1
2
3
Ctrl+d
In turn, the cat program is sending your input back to the
terminal display as standard output
EOF can be input by the user by pressing ctrl-d. After the
cat program receives EOF, it stops.
standard output
Standard output writes the data that is generated by a
program.
 When the standard output stream is not redirected, it will
output text to the terminal. Try the following example:

$echo Sent to the terminal through standard output

When used without any additional options, the echo


command displays any argument that is passed to it on the
command line.
Run echo without any arguments:

$echo

It will return an empty line, since there are no arguments


standard error
Standard error writes the errors generated by a program
that has failed at some point in its execution.
Like standard output, the default destination for this stream
is the terminal display
Example:
ls %

Since % is not an existing directory, this will send the


following text to standard error:
ls: cannot access %: No such file or directory
Stream Redirection
Linux includes redirection commands for each stream. These
commands write standard output to a file. If a non-existent
file is targetted (either by a single-bracket or double-bracket
command), a new file with that name will be created prior to
writing.
Commands with a single bracket overwrite the destination's
existing contents.
Overwrite
> - standard output
< - standard input
2> - standard error
Example:
cat > write_to_me.txt
abc
Ctrl+d
Here, cat is being used to write to a file
Stream Redirection
View the contents of writetome.txt using cat:
cat write_to_me.txt
It should have the following contents:
abc
Redirect cat to writetome.txt again, and enter three numbers.
cat > write_to_me.txt
123
ctrl+d
When you use cat to view writetome.txt, you will see the
following:
123
The prior contents are no longer there, as the file was
overwritten by the single-bracket command
Stream Redirection
Commands with a double bracket do not overwrite the
destination's existing contents
Append
>> - standard output
<< - standard input
2>> - standard error

Example: Consider earlier file of numbers and write following:


cat >> write_to_me.txt
abc
Ctrl+d

Open writetome.txt again, and you will see this:


123abc
Other Examples
Other Examples
Redirecting from a File
If we use the less than operator ( < ) then we can send data the
other way. We will read data from the file and feed it into the
program via it's STDIN stream

We may easily combine the two forms of redirection:


Redirecting STDERR
STDERR is stream number 2 and if we use it before the >
operator then it will redirect that stream (if we don't use a
number then it defaults to stream 1)

Syntax:
command 2> file

#This pattern redirects the standard error stream of a


command to a file, overwriting existing contents

mkdir '' 2> mkdir_log.txt

This redirects the error raised by the invalid directory


name '', and writes it to log.txt. Note that the error is still
sent to the terminal and displayed as text.
Redirecting STDERR Example

If we want to save both normal output and error messages


into a single file. This can be done by redirecting the
STDERR stream to the STDOUT stream and redirecting
STDOUT to a file. We redirect to a file first then redirect the
error stream. We identify the redirection to a stream by
placing an & in front of the stream number (otherwise it
would redirect to a file called 1).
Redirecting STDERR Example

Note: To append errors to the single file, we can use >>


operator
Piping
It is a mechanism for sending data from one program to
another
 It's called piping and the operator we use is ( |).
What this operator does is feed the output from the
program on the left as input to the program on the right.
Below example will list only the first 3 files in the directory:
Piping Examples
Networking Commands
Computer Network

Computers are connected in a network to exchange information


or resources each other. Two or more computer connected
through network media called computer network.
 There are number of network devices or media are involved to
form computer network.
Operating System is also part of Computer network
Maintaining of system and network up and running is a task
of System / Network Administrator’s job and it is achieved by
Networking Commands
hostname
Hostname is to identify in a network
 Execute hostname command to see the hostname of your
box.You can set hostname permanently
in /etc/sysconfig/network. Need to reboot box once set a
proper hostname.
host Command
host command to find name to IP or IP to name
in IPv4 or IPv6 and also query DNS records
Ifconfig(Interface Configurator)
With this command you can view IP
Address and Hardware / MAC address assigned to interface and
also MTU (Maximum transmission unit) size.
# ifconfig
MAC Address
It is a 48 bit (6 byte address)
Format MM:MM:MM:SS:SS:SS
MM- Manufacturer
SS – Serial
 ZigBee wireless home automation and other similar
networks based on IEEE 802.15.4, for example, require 64-
bit MAC addresses be configured on their hardware
devices.
IP Address
Internet Protocol Address
IP addressing is a hardware-independent convention which
in principle allows every computer attached to the Internet to
be given a unique logical address

Some address ranges are excluded from the above
classes
 All addresses of the form 127.xx.yy.xx are used for
diagnostics.
The following address ranges are designated as
"private": meaning they can only be used on networks
which are not connected to the global Internet:
10.0.0.0 - 10.255.255.255
172.16.0.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255
Ping(Packet INternet Groper)
PING command is the best way to test connectivity
between two nodes. Whether it is Local Area Network (LAN)
or Wide Area Network (WAN)
Ping use ICMP (Internet Control Message Protocol) to
communicate to other devices.
Ping(Packet INternet Groper)
In Linux ping command keep executing until you interrupt
 Ping with -c option exit after N number of request (success or
error respond).
Traceroute Command
traceroute is a network troubleshooting utility which
shows number of hops taken to reach destination also
determine packets traveling path
Netstat Command
Netstat command displays different information including
open sockets and routing tables
Netstat Command
Add the option -r to display information on the routing
table
Netstat Command
Add the option -p to display information of programs
connected to the open sockets.

# -a option will list all connections
#To get only tcp, connections, use –t option
# To get only udp connections, use –u option
# To list only listening connections, use –tnl option
# To list statistics, -s option
Ethtool Command
Ethtool is a Network Interface Cards (NICs)
utility/configuration tool. Ethtool allows you to query and
change your NIC settings such as the Speed, Port, auto-
negotiation and many other parameters.
Ethtool Command
Display NIC settings
Ethtool Command
Display interface Device Driver
Ethtool Command
Display interface statistics

We can use –S option as well


NMAP Command(Tool)
NMAP(Network Mapping) which checks for what ports are
opened on a machine
 Used to scan ports on a machine, either local or remote
machine (just you require IP/hostname to scan).
 Can be installed on windows, Sun Solaris machines too.
 Can be used to scan large networks
 Can be used to get operating system details such as open
ports, software used for a service and it’s version no,
vendor of network card and up time of that system
 Can be used by hackers to scan for systems for
vulnerability.

Just a funny note : You can see this NMAP used by Trinity in
Matrix-II movie, when she tries to hack in to electric grid
super computer
NMAP Command(Tool)
 Scan a System with Hostname and IP Address
nmap hostname/ip_add

-v option gives more details


Note: We can scan multiple hosts in the same command
NMAP Command(Tool)
 Scan list of Hosts from a File
NMAP Command(Tool)
 Scan an IP Address Range
NMAP Command(Tool)
 Scan for specific Port
NMAP Command(Tool)
 To get OS information –O option
TELNET Command(Tool)
 Connect to a remote Linux computer
 Run programs remotely and conduct administration
Thanks!
Any questions?
You can find me at:
[email protected]
Credits
https://fossbytes.com/how-to-use-calculator-in-linux-command-line/
 https://www.tecmint.com/60-commands-of-linux-a-guide-from-newbies-
to-system-administrator/
 https://www.tecmint.com/18-tar-command-examples-in-linux/
https://www.digitalocean.com/community/tutorials/how-to-add-and-
delete-users-on-an-ubuntu-14-04-vps
Additional Reference: https://www.fastwebhost.in/blog/143-linux-
commands-with-examples/
https://www.fastwebhost.in/blog/grep-command-in-unix-linux-with-11-
simple-examples/
▷https://www.digitalocean.com/community/tutorials/an-introduction-to-
linux-i-o-redirection
▷http://ryanstutorials.net/linuxtutorial/piping.php

You might also like