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

Fundamentals 1st Module

The document discusses various Linux commands and concepts like text editors Vi and Vim, soft links vs hard links, file permissions and ownership, partitions and file systems, compression tools like gzip and bzip2, and I/O redirection.

Uploaded by

Umar Farooq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Fundamentals 1st Module

The document discusses various Linux commands and concepts like text editors Vi and Vim, soft links vs hard links, file permissions and ownership, partitions and file systems, compression tools like gzip and bzip2, and I/O redirection.

Uploaded by

Umar Farooq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1

===>>>> TEXT EDITORS


1 = Vi : -

=> deleting : [dd] it will delete single line how much line want to
delete we use “3dd” for 3 lines ll “x” to delete single word

=> undo ; “u”

=> copy paste lines ; [yy] to copy line and [p] for paste

=> replacing; press (esc) then [ :%s/word which u want replace


/with which word want to replace /g]

=> to set no at start of line [: set nu]


If want to remove [: set nonu]
Now we can find line by jst [: 20 (line no) ]

=> searching : [/wat to search] “n” will select next and “shift
n” will select back

=> to open 1 file in split many windows : [: vsplit]


> And we can also open any other file as well [: split /path of
other file ]

> And can change window with [ ctrl ww ]

2 = vim : -

=> Password protectin : [ : ctrl+x ]

< -------------------------------------------------------------------- >


2

===>>> SOFTLINK (SYMBOLIC LINK) HARDLINK

softlink:
Think of a soft link as a pointer to a book in a library. The pointer contains
the book's title, author, and location in the library, but it doesn't contain the
actual book. When you use a soft link, you're essentially saying, "Hey, I want
to read this book, but I don't want to carry it around with me. Can you just
give me a pointer to where it's located in the library?"

>> Advantages
1 = easy to access
2= can cross partitions && can be created over directory

>> Disadvantages
=> If parren file is deleted the child is also deleted

Command
[ ln -s /path of which want to make symlink of /path where want to
makeit]

Recognition : -
1 = Through arrow in ‘“ls” ’ “- >”
2 = different inode no

Note :
=> We can also make shortcut of any command
=> 1st we look for , from where it comes or location of that command
with “which” command then can make shortcut of that command with
any other name
3

hard link
A hard link, on the other hand, is like a photocopy of the book. It contains the
same information as the original book, but it's a separate physical copy.
When you use a hard link, you're essentially saying, "Hey, I want to read this
book, and I also want to have a copy of it that I can carry around with me."

>> Advantages
1 = type of backup
2 = if parrent is deleted no impact on child

>> Disadvantages
Cannot cross partitions & cannot be created by over directory

Command
[ ln /path of which want to make symlink of /path where want to makeit]
Bt it will not cross the dir or partition as well

Recognition : -
1 = Through blue or any color shade or counts seen in permission portion
of file
2 = and we can also check with inode no it must be same as parent
inode no [ ls -i /path of file ]
3 = same inode no

—---------------------------------------------------------------

Summary of soft and hardlink:::


So, in summary, soft links are useful when you want to provide a convenient
name for a file or directory that may be located in a different directory or on
a different file system, while hard links are useful when you want to create a
second reference to a file or directory that is identical to the original file.

< —--------------------------------------------------------- >


4

===>> Cp and mv :

A = Cp :

File copying : [cp /path /where want to copy path]


Dir copying : [cp -r /path /where want to copy path]

B = mv : (cut and rename )


Using for cut paste : [mv /path /where want to paste path]
For rename : [mv original name rename naam]

< —------------------------------------------------- >

=====>>> OWNER SHIP AND PERMISSIONS

> result in ls -l it wil show us complete info of permissions


=> there are 9 bits
Read = r
Write = w
Execute = x
====> in binary it will show as on -off 0 and 1
It is in sequence like :
rwx- -xrw-

⇒>>> when we do ls -l there are also 9 fields


(-) (rwxrwxrwx) (.) (2) (cent 7) ( cent 7) (3) (may 10 08:04) (filename)

1 =(-) = type of file


2 = (rwxrwxrwx) = permissions
3 = (.) = selinux
4 = ( 2 ) = link count
5

5 = (cent 7 ) = owner
6 = (cent 7) = group name
7 = (3) = size in bytes
8 = (may 10 08:04 ) =accestime
9 = (filename) = (filename)

===>> CHANGING OWNER


[chown root filename]

===>> CHANGING GROUP


[chgrp groupname filename]

{rwx rwx rwx


User grp others}
===>>>> Giving permissions to the file “rwx”
[chmod u+rwx filename] adding
[chmod u-rwx filename] removing
Same as we can apply it on grp and others fields

+===>>>> And if want to give all of permissions to all User group


others we use for that “ugo”

⇒>> octal way of permission

>r=4
>w=2
>x=1

So we add these then run command in which 777 1st one u


2nd g , 3rd o

NOTE:
We can use command [chmod -Rv 777 /path]
> If we want to give permission to all files or directory by 1 command
6

===>>>CHATTR:
WE use this command on files which are important and we
dont want to access it to someone else other than root

===>>
> [chattr +i /path of file which u want inaccessible by other]
> [chattr -i /path of file which u want inaccessible by other]
> (+) to add immutability or chattr commands funtion
> (-) to sub immutability or chattr commands funtion

(1 =(-) = type of file): -


There are 7 types of file
1 = “-” = regular file
2 = “d” = directory
3 = “l” = symlink
4 =”b” = block file
5 = “c” = character file
6 = “s” = socket file
7 = “p” = pipe device file

< —-------------------------------------------------- >

=====>>> RUNLEVEL (6) AND TARGETS


(7&8)
>> 7 runlevels total
0 = halt
1 = single user mode (safe mode)
2=
3=
4=
5=
7

6 = reboot
## Total terminals :
Runlevel 5 : Gnome terminal = approx 400
Runlevel 3 cli (virtual console) = 6 terminals (can switch with
“alt + f1,f2…f6”)

====>>> how to reset root password (interview question)

##>> general :

1 = power on or reboot
2 = press any key at splash screen
3 = now read the text there which key is to press for modification the
kernel arguments
4 =editing or adding lines there

###>>> RHEL 7 & 8 :

1 = on splash screen interrupt “press e”


2 = find line start with “linux16………” add “rd.break” at the end of line
3 = ctrl +x
4 = sh# here run these commands [ mount -o remount rw /sysroot ]
> [ chroot /sysroot ]
> [ passwd root ]
Now from there we can change root passwd
5 = touch / .autorelable
6 = exit

====>>> how to reset root password (interview question)


Secure single user mode
8

### for rhel 6:


1= go to “/etc /sysconfig /init”
> in that file shift +g will bring u at the end where;
2 = “SINGLE=/sbin /sushell” at the end that line present where we
replace “sushell” with “sulogin” (it will ask passwd on single user mode
as well)

< —------------------------------------------------------- >

==>># PARTITIONS

Hard
disk
partition

Each partition Associate with


specific driver (/dev/sdX)

Each driver is further


Divided in (/dev/sdX1 )
And so on

>> COMMANDS:

> [ fdisk -l ] >> to check for all partions


> [ ]
9

>># create partition task

1 = adding disk (pysically)


2 = discovering [fdisk - l]
> if perform on live then
[ echo “- - -” > /sys /class /scsi_host /host0 /scan
echo “- - -” > /sys /class /scsi_host /host1 /scan
echo “- - -” > /sys /class /scsi_host /host2 /scan ]
3 = make partition [fdisk /dev /sdX]
> then save or write it on harddisk
4 = then update kernel [partx -a /dev /sdX
OR partprobe /dev /sdX]
5 = formatting [ mkfs.ext4(xfs,etc) /dev /sdX(1) ]
6 = mounting [ mount /<device-driver> /<dir name or mount point> ]
> it is temporary on reboot it unmont automatically
7 = entry in “fstab” to make it permanent
8 = [ mount -a ]

<—----------------------------------------------------->
10

==##>>> COMPRESSION TOOLS


⇒ NOTE :
>> [ touch file{1..10} ] to create multiple files at
once
> [ Mkdir <name of directory>{1..1000} ]
> [rmdir ……………………..]

== >>## REDIRECTION

># [ ls > filename or path ] here “>” redirecdting symbol


It will carry all out put of “ls” into a file or path given
=> it “>” will overwrite the data present before and the
output data or bla blaaaaaaaaa will be replace by it
===>># we can use “>>” append or add it will add bt will
not overwrite the data present there in that file
=># we use it like to send out put of any command etc in
a file through gmail

=##>>= zipping and unzipping with gzip,bzip2,

=># “gzip” is commonly used for fast speed, and smaller files
⇒ ## “bzip2” is for higher compression and used for larger files to
compress

>> [ gzip filename ] > gzip file compression


> [ gzip * ] > it will compress all files size wilthin your “pwd”
># [ gunzip filename ] to decompress
11

>> [ zat filename,bzcat filename ] used to read compress file without


decompressing

>> GENERAL FILE SELECTION ETC


> [ just(*) action or command] will select all files in current dir
>> [ b* (command) ] will select files start with “b” at your “pwd”
and we can select it with any alphabat etc
> [ stat filename ] to see access time etc

<—---------------------------------------------------------------->

⇒## ARCHIVE AND COMPRESSION TOOLS

=>## zip

=># to carry many files in one single file called archiving

=>## zip command 1st compress file then place it in given file

>># [ file filename ] to check file type (zip etc)


># [ zip <distination (to)> <source (from)> ] archiving files etc
># [ unzip -d <distination (to)> <source (from)> ] to unzip
>>>>>>> “-d use when u want to unzip it some where else other than
‘pwd’ ” [ unzip filename ] > to unzip in current dir

=>## tar:

>> it just archive and not do compression


>> [ tar -cvf <distination (to)> <source (from) ]
12

>> [ - - - - “-tvf”- - - - - ] to list


>> [ - - - - “-xvf” - - - - ]
Tar

Gzip bzip2
[tar -zcvf - - -] [tar -jcvf - - -]

< —-------------------------------------------------------->

=>>>## RSYNC

>> it is powerful backup tool

>>> it see the modify time of source and if find any changes in source
file it will copy just the modified content not already copied content again
( after running command “rsync -avz <path(from)> <path(to)>” )

>> [ rsync -avz <path(from)> <path(to)> ] > it help us to copy files


but will never overwrite use to daily backup of any file so for that if new
files etc added in that file it will copy when we run this command and will
not copy the same files again
> important thing is that it will affect when any new thing added to
source (from) not affect if new thing added to destination (to)

>> [ zip, tar, cp, rsync, scp ] > backup tools


13

ACCESS TIME - -> atime MODIFIED TIME - -


>mtime CHANGE TIME - ->ctime

Change in
owner &

>># remote backup

>>##

>> [ rsync -avz -e ssh user@remote-server:/path/to/backup(from)


/local/backup/directory(to) ]

<------------------------------------------------------------------------->

=>>>## LUNSCANING && FDISK

>> in every harddisk 4 partition can be made as “primary”

>> we can make more when make one of them “extended” and these
partition which is made in extended is called “logical partitions” and
these are “63” in no

>> it is good to make extended on 4th primary partition

>> extended partition can make in primary bt in extended making


primary is not possible
14

>>
hard disk

4 primary partition

from these we can make more


partition which is called
“logical partition” by making
one of them extended “63” “LP”

>> the limitation of primary partition is just in “mbr” partition table in


“GPT” we can make “128” partition
> [ parted -l /dev /sdX ] to check partition table

< —------------------------------------------------------->

=>## FSCK
> file system check & repair
>> to check file system [ df -Th ]
>> [ badblocks -v /dev /sdX ] to check for badblocks within
partition or filesystem
>> if find any badblocks then we do clean it with [ fsck /dev /sdX ] or
the advance one [ e2fsck /dev /sdX ] and to check complete details
[ e2fsck -yc /dev /sdX ]
> bt it is must to unmount the partition 1st then run this
command
15

Q: why badblocks made in our


filesytem
ANS:

=>## To automate fsck work on boot


>> we can do it by looking for “/etc /fstab” file
> there are six fields in that file the “6th” one field is for
“fsck”
> if that value we keep “0” it will never do it if “1” it means it

>> there are 6 fields in fstab in which the 6 th one is for “fsck”

>> it is recommended to give UUID instead of device driver name


“/dev /sdX ”
> to check for “device driver ” UUID [ blkid /dev /sdX ]
>> every driver have their own UUID
>> UUID path [ /dev /disk /by-uuid ]
> then [ ls -l ] run this to see the details
>>>>## [ lsblk -f ] all partition info with their UUID

>>$ in fstab “4th” field is for permission like [ defaults,ro ] > it will not
allow us to write or execute in that folder or partition for which we set
these seting

<-- —------------------------------------------------------------------->
16

=>$## PIPE

To use for 2 or more commands “ |” run or integrate

=>## grep , locate , find

$#>> grep
>> to catch specific files or word from a long list [ grep
gdm /etc /passwd ]
> if want to see with highlighted so for this [ grep - -color
“gdm” /etc /passwd ]
>> [ ls /etc / * | grep -i host ] it will grep files which contain
name “host”
> [ ls /etc / * | grep -i host | wc -l ] > will count lines
“wc -l” as well
<
—------------------------------------------------------------------------ >
$#>> find (its good command find 15 examples
for that)
>> [ find /etc / -iname <hosts* (filename)> ] > to find
files name with “hosts”
> [ find /etc / -iname < “*.txt” > ] > it wil find files
ends with .txt
>> it is little bit slow when want to find things in full OS bcz
for our result find visited all “dir”
1 by 1 and thats y the new files can find with “find”
command bt cant be with “locate”

=>## QUERY:
search 15 examples for each ( find ,ps ,rsync )
>> limit the “forkbombing” search for that

< —----------------------------------------------------------------------->
17

=>$## PROCESS && DAEMONS


(services)

Process: it give output then stoped e.g ; “fdisk”


Daimons : it is running until we stop it e.g ; “firefox”

>> [ lsof ] > to check files which is in RAM


> [ lsof | wc -l ] > to count words “wc -l”

>## PROCESS INFO


>> [ ps ] > show running process in RAM
> [ ps -el ]
> [ ps -ef ]
> [ ps -aux ]
>> [ pstree ] > all process
>## to kill process we can kill it with [ kill <pid> ]
> [ kill -9 <pid> ] > to kill process forcfully
> [ pkill <process name> ] > to kill process with name

>## DEVICE INFO


>> [ cat /proc /cpuinfo ] to check cpu info
> [ lscpu ]
> [ lsusb ] > usb info

>## PROCESS ID INFO


>> [ pidof < firefox (name of process)> ] > to check pid of service or
process
> [ pgrep <name of process> ] > to check pid of process with name
18

>>## NOTE :
>> performanc monitoring tools :
>> [ top , ps , pstree , lsof ]

>> [ firefox & ] > to run the process in background within the shell
terminal closing can close every thing start with terminal
> [ fg ] > to check the background process
> [ bg ] > as well && [ jobs ]
>> [ echo $$ ] > to see current process

>>## TOP (comand)


> “through top we see the load on system”
> “checking load ”

>> [ htop, sar , iostat -> hdd performance check, vmstat, iometer ,
ipref -> NW bandwidth, tcdump ] > performance monitoring tools

>> all the info of “top ” command is coming from [ /proc ]

>> all waiting process are called load

>> [ free -m ] > to check the buffers and cache etc

>> [ top -n1 > /opt /top.txt ] > we cant copy the output of top
command unless and until we use “-n1” to backup
19

< - —---------------------------------------------------------------- >

You might also like