ashokitech.com-
ashokitech.com-
txt
1/12
Working with Text Editors in Linux
==================================
The default editor that comes with the UNIX operating system
is called vi (visual editor).
Using vi editor, we can edit an existing file or create a new file from scratch.
1) command mode
2) insert mode
3) escape mode
Command Mode:
---------------
When vi starts up, it is in Command Mode. This mode is where vi interprets any
characters we type as commands and thus does not display them in the window
This mode allows us to move through a file, and to delete, copy, or paste a piece of
text.
To enter into Command Mode from any other mode, it requires pressing the [Esc] key.
If we press [Esc] when we are already in Command Mode, then vi will beep or flash the
screen.
Insert mode:
--------------
This mode enables you to insert text into the file.
Everything that’s typed in this mode is interpreted as input and finally, it is put
in the file.
The vi always starts in command mode. To enter text, you must be in insert mode.
To get out of insert mode, press the Esc key, which will put you back into command
mode.
The cursor will jump to the last line of the screen and vi will wait for a command.
This mode enables you to perform tasks such as saving files, executing commands.
2/12
There are following way you can start using vi editor :
vi f1.txt
=> After making changes if we don't want to save those changes then execute :q!
Usecase
-------
-> We will use 'vi' editor to perform below activities
3/12
Working with SED command
------------------------
SED command in UNIX stands for stream editor and it can perform lots of functions on
file like searching, find and replace, insertion or deletion.
Though most common use of SED command in UNIX is for substitution or for find and
replace.
By using SED you can edit files even without opening them, which is much quicker way
to find and replace something in file, than first opening that file in VI Editor and
then changing it.
SED is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).
SED command in unix supports regular expression which allows it perform complex
pattern matching.
Example:
--------
$ cat > myfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os. Learn unix. unix is a powerful.
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.
4/12
-------------------------------------
SED command can also be used for deleting lines from a particular file. SED command
is used for performing deletion operation without even opening the file
=> To make SED command changes to file permanently we will use '-i' optionn.
Note: With above command 'unix' keyword will be replaced with 'linux' keyword in the
file permanently.
File permissions
================
To create a secure environment in Linux, you need to learn about user groups and
permissions. For example, if you work in a company and you want the finance
department to read a file but not make any modification to it, then you need to use
permissions in Linux. It is a must for every programmer working with Linux nowadays.
User: the owner of the file (person who created the file).
Therefore, all users in that group will have the same permissions. It makes things
easier than assign permission for every user you want.
Other: any person has access to that file, that person has neither created the file,
nor are they in any group which has access to that file.
5/12
The characters mean:
‘r’ = read.
‘w’ = write.
‘x’ = execute.
‘-’ = no permission.
-rw-r--r--
-: It represents file
rw: User
r: Group
r: Other
As we see above, the empty first part means that it is a file. If it were a directory
then it will be the letter “d†instead.
The second part means that the user “Home†has read and write permissions but he
does not have the execute one.
This command will add the write permission for other users to my text file
“section.txtâ€.
“o†refers to others, “g†for the group, “u†for the user, and
“a†for all.
If you want to remove the permission, you can use the same method but with “-â€
instead of “+â€.
For example, let’s remove the execute permission from the user by:
Also, you can use Symbolic Mode to modify permissions like the following:
6/12
Number Permission
0 No permission
1 Execute
2 Write
3 Execute and Write
4 Read
5 Read and Execute
6 Read and Write
7 Read, Write and Execute
Let’s remove the execute from the group and the write from other by:
=> Within one Linux machine we can create multiple user accounts
=> Multiple users can access single linux machine and can perform multi-tasking
-> When we launch EC2 instance with Ubuntu OS, we got by default 'ubuntu' user
account
Create a user
+++++++++++++
$ sudo adduser <uname>
=> After creating user account we can verify useraccount details using 'id' command
$ id <uname>
$ ls -l
(We can see ubuntu folder and newly created user folder)
(That means we have 2 user accounts in our machine)
7/12
After creating a new user and setting a password to it, you can log in from the
terminal
$ su - <uname>
Delete a user
=============
$ sudo userdel <uname>
If you try that command, you will notice that the user directory has not been deleted
and you need to delete it by yourself.
User groups
============
-> A group is a collection of users.
-> The primary purpose of the groups is to define a set of privileges like read,
write, or execute permission for a given resource that can be shared among the users
within the group.
Create a group
---------------
You can see all of the groups you have by opening the following file:
$ cat /etc/group
Create a group
----------------
$ sudo groupadd <groupname>
Delete a group
--------------
$ sudo groupdel <groupname>
8/12
------------------------------------------
locate command
++++++++++++++++
The locate Command find will search for data in local db
$ locate apache
$ locate -c apache
$ locate -c *.txt
Note: when we create new files it will take some time to update those files in
mlocate db
find command
+++++++++++++
=> find command will search for the files in entire linux file system.
=> find command providing advanced searching technique
=> Using find command, we can search for the files based on name and type also.
Note: As find command is scanning entire file system, it will take more time to give
search results.
-------------------------------------------------------------------------------------
---------
-> 'man' command is like a help command. It is used to understand command syntax and
options.
$ man cat
-------------------------------------------------------------------------------------
----------
$ ifconfig
9/12
Note : ifconfig is not installed then execute below command
$ ping <ip>
$ curl <url>
$ wget <url>
-------------------------------------------------------------------------------------
----------
$ sudo su
$ apache2 -version
-> If server is not accessible then create security group with http port open
-> After adding security group try accessing EC2 instance using IP
Ex: http://52.66.101.3/
10/12
$ cd /var/www/html
$ cat > index.html
Note: If permission not available to store the data then execute below command
$ sudo su
Linux
Shell Scripting
--------------------
Start Date : 18-Apr-2022 (Mon-Sat)
AWS (15+ Services)
DevOps (10+ Tools)
Course Fee : 12,000 INR
Duration : 3 Months
Class Timings : 7:00 PM to 9:00 PM
11/12
12/12