You are on page 1of 13

LINUX

Linux is an open-source operating system like other operating systems such as Microsoft Window ,Mac OS,
iOS, Google android, etc. An operating system is a software that enables the communication between
computer hardware and software

A. Linux Features
Multitasking: More than one function can be performed simultaneously by dividing the CPU time intelligently.
Portability: Portability doesn't mean it is smaller in file size or can be carried in pen drives or memory cards. It
means that it support different types of hardware.
Security: It provides security in three ways namely authenticating (by assigning password and login ID),
authorization (by assigning permission to read, write and execute) and encryption (converts file into an
unreadable format).
Open Source: Linux code is freely available to all
File System: Provides hierarchical file system in which files and directories are arranged

B. Why Use Linux


It is highly secured and robust so you don't need an anti virus to scan it regularly. Companies like Google,
Amazon and Facebook use linux in order to protect their servers as it is highly reliable and stable.

C. What is Shell

The terminal contains the shell; it allows us to execute the commands to interact with the system. We can
perform various operations such as store and retrieve data, process information. Some basic operations such
as date, cal, ls, and pwd

D. What is Scripting

Suppose we are required to execute some basic commands every day, for example above four commands.
Linux supports a feature called scripting that allows us to execute more than one task at once. So, it is good to
define a script rather than performing repetitive tasks.

To understand how to use Linux script, let's define a script in a combination of some tasks. To define a script,
create a file with a .sh extension. Consider below command:

Command :- vi tasks.sh  

The above command will open the vi editor in normal mode. Switch it to insert mode by pressing ESC, and
after that enter 'i' keys, enter your desired tasks. Every task should be defined in a new line. Consider the
below tasks:

1. date  
2. cal  
3. pwd  
4. ls  
After entering the tasks, press ESC and :wq! Keys to save and exit from the editor.Now, make the created file
as executable, use the chmod command with +x option as follows:

chmod +x tasks.sh  

We have created our first script. To execute the shell script, execute the file name by predefining './ ' with
the file name. Consider the below command:

./task.sh  

E. Difference between Linux and Unix


Comparison Linux Unix

Definition It is an open-source operating system which It is an operating system which can be


is freely available to everyone. only used by its copyrighters.

Examples It has different distros/Varients like Ubuntu, IBM AIX, HP-UX and Sun Solaris.
Redhat, Fedora, etc

Users Nowadays, Linux is in great demand. Anyone can It was developed mainly for servers,
use Linux whether a home user, developer or a workstations and mainframes.
student.

Usage Linux is used everywhere from servers, PC, It is used in servers, workstations and
smartphones, tablets to mainframes and PCs.
supercomputers.

Cost Linux is freely distributed, downloaded, and Unix copyright vendors decide
distributed through magazines also. And priced different costs for their respective Unix
distros of Linux are also cheaper than Windows. Operating systems.

Development As it is open source, it is developed by sharing Unix was developed by AT&T Labs,
and collaboration of codes by world-wide various commercial vendors and non-
developers. profit organizations.

Interface The default interface is BASH (Bourne Again It originally used Bourne shell. But is
SHell). But some distros have developed their also compatible with other GUIs.
own interfaces.

File system Linux supports more file system than Unix. It also supports file system but lesser
support than Linux.

Operating Linux is just the kernel. Unix is a complete package of


system Operating system.

Security It provides higher security. Linux has about 60- Unix is also highly secured. It has about
100 viruses listed till date. 85-120 viruses listed till date

F. Linux vs windows

Parameter Linux Windows

Access Users can access the source code of Usually, users cannot access the source
kernel in Linux and can alter the kernel code.
according to need.

Installation The Linux installation process is a bit Windows OS is easy to install and set
complicated to set up as it requires many up on a machine; it requires fewer user
user inputs. It takes less time than input options during installation.
Windows to install. However, it takes more time to install
as compared to Linux.

Ease of use The Linux OS is meant to be for the Windows comes with simple and rich
technical user because you must have GUI options, so it is easy to use it. It
some exposure to various Linux can be simply used by technical as well
commands. Users may take more time to as non-technical users. The
be a handy user of Linux. The troubleshooting process is also much
troubleshooting process is also easy than Linux.
complicated as compared to Windows.

Written in Linux is written in assembly Windows is written in C+


language and C. + and assembly language.

Reliability Linux is highly reliable and secure. It has Windows is not as much reliable as
well-established system security, process Linux. However, now Windows has
management, and uptime. improved reliability but still has some
security weaknesses and system
instabilities.

Support Linux has a Windows also provide good support to


its user. It provides free as well as paid
good support as it has a huge community support. It has an easily accessible
of user forums and online search. online forum.

Security Linux OS is more secure than Windows. Windows is less secure than Linux.
It is hard for the hackers and attackers to Attackers primarily target the Windows
find a loophole in it. So, Linux is hard to for malware and virus. Windows is
breakthrough. most vulnerable without anti-virus.
G. Linux Distro
Distribution Why To Use

UBuntu It works like Mac OS and easy to use.

Linux mint It works like windows and should be use by new comers.

Debian It provides stability but not recommended to a new user.

Fedora If you want to use red hat and latest software.

Red hat enterprise To be used commercially.

CentOS If you want to use red hat but without its trademark.

OpenSUSE It works same as Fedora but slightly older and more stable.

Arch Linux It is not for the beginners because every package has to be installed by yourself.

H. Linux Directory Commands


Directory Description
Command

pwd The pwd command stands for (print working directory). It displays the current working location
or directory of the user. It displays the whole working path starting with /. It is a built-in
command.

ls The ls command is used to show the list of a folder. It will list out all the files in the directed
folder.

cd The cd command stands for (change directory). It is used to change to the directory you want to
work from the present directory.

mkdir With mkdir command you can create your own directory.
mkdir <dirname1> <dirname2> <dirname3> ...  

rmdir The rmdir command is used to remove a directory from your system.

This command is used to delete a directory. But will not be able to delete a directory
including a sub-directory. It means, a directory has to be empty to be deleted.

Syntax: rmdir <dirname>  

Change directory using an absolute path

To change the directory by using an absolute path, we have to mention the whole path starting from the root.
Consider the below example:

From the above output, we are changing our directory to 'certs' from 'cups.' So, we have provided the whole
path "/run/cups/certs" starting from the root (/). This is called an absolute path.

Change directory using a relative path


We can change our directory by using a relative path; a relative path is a location that is relative to the
current directory. Consider the below example:

1. cd certs  

From the above output, we are changing the directory by using a relative path. Like the above example, we
have changed our directory from 'cups' to 'certs,' but have not mentioned the whole path. This is the
relative path.

Linux ls command options

ls Description
option
ls -a In Linux, hidden files start with . (dot) symbol and they are not visible in the regular directory. The (ls -a)
command will enlist the whole list of the current directory including the hidden files.

ls -l It will show the list in a long list format.

ls -lh This command will show you the file sizes in human readable format. Size of the file is very difficult to read
when displayed in terms of byte. The (ls -lh)command will give you the data in terms of Mb, Gb, Tb, etc.

I. Linux Create File


Linux file system considers everything as a file in Linux; whether it is text file images, partitions, compiled
programs, directories, or hardware devices. If it is not a file, then it must be a process. To manage the data, it
forms a tree structure.

There are multiple ways to create a file in Linux. Some conventional methods are as follows:

o using cat command


o using touch command
o using redirect '>' symbol

1.Using cat command


It is used to create a file, display the content of the file, concatenate the contents of multiple files, display
the line numbers, and more.

First of all, create a directory and named it as New_directory,: mkdir New_directory  

Output:

Now execute the cat command to create a file:

1. cat > test.txt  

The above command will create a text file and will enter in the editor mode. Now, enter the desired text and
press CTRL + D key to save and exit the file and it will return to the command line.

To display the content of the file, execute the cat command as follows: cat test.txt  
2. Using the touch command It is used to create a new file, update the time stamp on
existing files and directories. It can also create empty files in Linux.

To create a file, execute the touch command followed by the file name as given below:

1. touch test1.txt  

To list the information of the created file, execute the below command:

1. ls - l test1.txt  

To create multiple files at once, specify files and their extensions after the touch command along with single
space.

1. touch test1.txt test2.txt test3.txt   

To create two different types of file, execute the command as follows:

1. touch test4.txt test.odt  

The above command will create two different files named as test4.txt and test.odt.

To display the file and its timestamp, execute the ls command as follows:

1. ls -l  
Note: The significant difference between touch and cat command is that using cat command, we can specify the
content of the file from the command prompt comparatively the touch command creates a blank file.

3. Using the redirect (>) symbol


We can also create a file using the redirect symbol (>) on the command line. To create a file, we just have to
type a redirect symbol (>) followed by the file name. If we use > operator, it will overwrite the existing file,
and >> operator will append the output.

To create a file with redirect (>) operator, execute the command as follows: > test5.txt  
4.Using Vi editor
To create a file with Vi editor, execute the below command: vi test10.txt  

The above command will open the Vi editor. Press i key for the insert mode and enter the file content. Press
Esc key and :wq to save and exit the file from the editor.

To display the file content, execute the below command: cat test10.txt  

J. Linux file command


file command is used to determine the file type. It does not care about the extension used for file. It simply
uses file command and tell us the file type. It has several options.

Syntax file <filename>  

K. Linux rm | Linux Delete File


The 'rm' means remove. This command is used to remove a file. The command line doesn't have a recycle bin
or trash unlike other GUI's to recover the files. Hence, be very much careful while using this command. Once
you have deleted a file, it is removed permanently. Syntax: rm myfile1  

L. Linux cp | Linux Copy File


'cp' means copy. 'cp' command is used to copy a file or a directory.
cp <existing file name> <new file name>  

In above snapshot, we have created a copy of 'docu' and named it as 'newdocu'. If in case, (in our case it is
'newdocu') alreade exists, then it will simply over write the earlier file.

L1. To copy a file in a different directory


In the snapshot below, earlier there is no 'text' file. After giving the command, 'text' file has been copied to
the destination directory that is 'Desktop'.

L2. cp Options
Option Function

cp -r To copy a directory along with its sub dirctories.

cp file1 file 2 directory name To copy multiple file or directories in a directory.

cp -backup To backup the existing file before over writing it.

M. Linux mv | Linux Move File


Linux mv command is used to move existing file or directory from one location to another. It is also used to
rename a file or directory. If you want to rename a single directory or file then 'mv' option will be better to
use.

N. Linux Rename File and Directory


.Basic syntax: rename 's/old-name/new-name/' files  

In the example below we have converted all the files ending with '.txt' into files ending with '.pdf'.

O. Linux Man Command


The "man" is a short term for manual page. In unix like operating systems such as linux, man is an interface to
view the system's reference manual.

Syntax of man: man [option(s)] keyword(s)  

But generally [option(s)] are not used. Only keyword is written as an argument.

For example, man ls  

P. Linux File Contents Command


There are many commands which help to look at the contents of a file. Now we'll look at some of the
commands like head, tac, cat, less & more and strings.

P1. Linux head command

The 'head' command displays the starting content of a file. By default, it displays starting 10 lines of any file.

1. Syntax head <file name>   head jtp.txt  


Skip Ad

Head command for multiple files

If we'll write two file names then it will display first ten lines (in this case file has five lines only) of each file
separated by a heading.

Syntax: head <file name> <file name>   head doc1.txt doc2.txt  

P2. Linux tail command


Linux tail command is used to display the last ten lines of one or more files. Its main purpose is to read the
error message.

Syntax: tail <file name>  

P3. Track a file for changes

To track a file for changes, the '-f' option is used. Here, '-f' stands for the following. It is useful for monitoring
log files. tail -f num.txt  
Q. Linux Cat Command
It can be used to display the content of a file, copy content from one file to another, concatenate the contents
of multiple files, display the line number, display $ at the end of the line, etc.

Q1. Linux cat command: to display file content


The 'cat' command can be used to display the content of a file.

Syntax: cat <fileName>  Note: To display the content of multiple files at once, type file names in one single
line like "cat file1 file2 file3... fileN.

Linux cat command usage


Option Function

cat > [fileName] To create a file.

cat [oldfile] > [newfile] To copy content from older to new file.

cat [file1 file2 and so on] > [new file name] To concatenate contents of multiple files into one.

Q2. Linux cat command (to create a file)

The 'cat' command can be used to create a new file with greater than sign (>).

Syntax: cat > <file name>   Example: cat > javatpoint  

In the above snapshot, we have created a new file called "javatpoint". Now let's see how to create it.Type the
command "cat >javatpoint" and press 'enter'. You will be directed to the next line.Press 'enter' after every
line and you will be directed to the next line. To save your file, go to the next line, press 'ctrl+d' and your file
will be saved.
Q3. To Append the Content of A File

The 'cat' command with double greater than sign (>>) append (add something in the last of a file) something in
your already existing file.

Syntax: cat >> (file name)   Example: cat >> javatpoint  

Q4. Linux cat command (to concatenate files)

The 'cat' command can be used to concatenate the contents of multiple files in a single new file.

Syntax: cat <filename1> <filename2>....  > <newFilename>  Example: cat file1 file2 file3 > combo  

R. Linux tac command


The 'tac' command is the reverse of the 'cat' command. It will display the file content in reverse order. It prints
the last line first, then second last and so on. Such way, it prints the first line at last.

Syntax: tac <file name>  

You might also like