· 5 years ago · Mar 16, 2020, 11:02 AM
1Name: Gaurav Gupta
2Roll No: 26
3Subject: LINUX
4Class : F.Y.BSC (COMPUTER SCIENCE) 2019-20
5
6
7
8
9
10
11
12INDEX
13NO
14DATE
15TITLE
16PAGE NO
17SIGN
181
1929/11/2019
20
21Study of basic commands
22
23
24
25
26
27
282
296/12/2019
30
31Study of advanced commands
32
33
34
35
36
37
383
3903/01/2020
40
41Study of filter commands
42
43
44
45
46
47
484
4910/01/2020
50
51Linux Editors vi
52
53
54
55
56
57
585
5915/01/2020
60
61Basic Shell scripting
62
63
64
65
66
67
686
6923/01/2020
70Advance Shell Scripting
71
72
73
74
757
7631/01/2020
77Network Commands
78
79
80
81
828
8307/02/2020
84Advances filters Sed
85
86
87
88
899
9008/02/2020
91
92Installing and Removing Software
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119Practical No. 1
120
121Study of basic commands
122
123
124pwd command
125“pwd” command prints the absolute path of current working directory.
126
127tux@acer$ pwd
128/home/tux
129
130
131
132cal command
133It displays calender of current month.
134
135tux@acer$ cal
136February 2020
137Su Mo Tu We Th Fr Sa
1381
1392345678
1409101112131415
14116 17 18 19 20 21 22
14223 24 25 26 27 28 29
143
144echo command
145
146This command will echo whatever you provide it.It is also used to display the values of a variable.
147
148tux@acer$ echo “linux is great”
149linux is great
150
151tux@acer$ echo $HOME
152/home/tux
153
1544. date command
155Displays current time and date. It can also print only time and month also.
156
157tux@acer $ date
158Mon Feb 24 18:19:43 IST 2020
159
160tux@acer$ date +%T
16118:20:09
162
163whoami command
164The command that reveals the user who is currently logged in.
165
166tux@acer$ whoami
167tux
168
169
170clear command
171This command clears the screen.
172
173help command
174
175There are many command in linux and no one can remember it in order to know about the command help command is used in the way date --help.
176
177tux@acer $ date --help
178
179Usage: 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.
180
181
182whatis command
183
184This command returns one line description about the command.It can be used as a quick reference for any command.
185
186tux@acer $ whatis shadow
187shadow (5) - shadowed password file
188
189
190man command (Manual Pages)
191
192“help” and “whatis” command donot provide thorough information about the command. For more details ,linux provide man pages and info pages.It shows command’s manual.
193
194tux@acer $ man clear
195NAME
196clear - clear the terminal screen
197
198
199Changing Directories Command
200Change the current working directory to the directory provided as argument.
201
202If no argument is passed to “cd”, it changes the directory to the user’s home directory.
203
204tux@acer$ cd Downloads
205tux@acer:~/Downloads$ cd
206tux@acer$
207
208Listing Files and Directories Command
209
210List files and directories. If no argument is given,the contents of current directory are shown.If directory is given an argument, files and directories in that directory are shown. ‘ls -a’ displays hidden files.
211
212tux@acer$ ls
213
214Documents Downloads Music Pictures Public SoftMaker Templates Videos wingpanel-indicator-sys-monitor
215
216tux@acer$ ls Documents
217
218'Advanced shell scriptinag.doc' 'Basic shell scripting.doc' 'index fr fylinux.docx' softmaker-office-2018_974-01_amd64.deb
219
220tux@acer$ ls -a
221. .gnupg .sudo_as_admin_successful
222.. .ICEauthority Templates
223.bash_history .icons .themes
224.bash_logout .inputrc .var
225.bashrc .local Videos
226.cache Music wingpanel-indicator-sys-monitor
227
228Creating files and directories
229
230To create directory,the mkdir command is used. To remove directory, the rmdir command is used. For creating an empty file, use touch command.
231
232tux@acer$ mkdir admin
233tux@acer$ ls
234admin Downloads
235
236tux@acer$ rmdir admin
237tux@acer$ ls
238Downloads
239
240tux@acer$ touch man
241tux@acer$ ls
242Documents man
243
244
245
246
247file command
248The file command determines the file type of a given file.
249
250tux@acer$ file /etc/passwd
251/etc/passwd : ASCII texy
252
253cat command
254
255The cat command is actually a concatenator but can be used to view contents of a file.
256
257tux@acer$ cat /etc/passwd
258root:x:0:0:root:/root:/bin/bash
259daemon:x:1:1:daemon:/usr/sbin:/bin/sh
260bin:x:2:2:bin:/bin:/bin/sh
261sys:x:3:3:sys:/dev:/bin/sh
262sync:x:4:65534:sync:/bin:/bin/sync
263games:x:5:60:games:/usr/games:/bin/sh
264
265copy command
266
267Copy files and directories.If source is file and the destination name does not exist,then source is copied with new name i.e name provided as destination.
268
269tux@acer$ cp man man2
270tux@acer$ ls
271Documents Music Public man man2
272
273move command
274
275Move files or directories.It works same like cp but the original file is moved to the destination file.
276
277tux@acer$ mv man man2
278tux@acer$ ls
279Documents Music Public man2
280
281Practical No. 2
282
283Study of Advanced Commands
284
285
286ls
287
288ls commands list all things present in the user’s home directory but it can be used with many different options.
289ls -a : list hidden files
290
291ls -A : works like -a but except it does not list .(current directory) and .(parent directory)
292ls -h : list files sizes in human redable format rather than bytes.
293
294ls -s : Sort results by file size
295
296ls -t : Sort by modification time
297
298ls -l : This command gives more information about the files and directories.
299
300
301
302
303tux@acer$ ls -a
304
305
306
307
308.
309.gnupg
310SoftMaker
311..
312.ICEauthority
313.sudo_as_admin_successful
314.bash_history
315.icons
316
317
318Templates
319.bash_logout
320.inputrc
321.themes
322.bashrc
323
324
325.local
326
327
328.var
329.cache
330
331
332man2
333
334
335Videos
336.config
337
338
339Music
340
341
342wingpanel-indicator-sys-monitor
343.dmrc
344
345
346Pictures
347
348
349.Xauthority
350Documents
351
352
353.pki
354
355
356.xsession-errors
357Downloads
358
359
360.profile
361.xsession-errors.old
362.gnome
363
364
365Public
366
367
368
369
370tux@acer$ ls -A
371
372
373
374
375.bash_logout
376.icons
377
378
379.sudo_as_admin_successful
380.bashrc
381
382
383.inputrc
384
385
386Templates
387.cache
388
389
390.local
391
392
393.themes
394.config
395
396
397man2
398
399
400.var
401.dmrc
402
403
404Music
405
406
407Videos
408Documents
409
410
411Pictures
412wingpanel-indicator-sys-monitor
413Downloads
414
415
416.pki
417
418
419.Xauthority
420.gnome
421
422
423.profile
424
425
426.xsession-errors
427.gnupg
428
429
430Public
431
432
433.xsession-errors.old
434
435tux@acer$ ls -h
436Documents man2 Pictures SoftMaker Videos
437Downloads Music Public Templates wingpanel-indicator-sys-monitor
438
439tux@acer$ ls -s
440total 36
4414 Documents 0 man2 4 Pictures 4 SoftMaker 4 Videos
4424 Downloads 4 Music 4 Public 4 Templates 4 wingpanel-indicator-sys-monitor
443
444tux@acer$ ls -t
445Downloads man2 wingpanel-indicator-sys-monitor Music Templates
446Documents SoftMaker Pictures Public Videos
447
448tux@acer$ ls -l
449total 36
450drwxr-xr-x 2 root tux 4096 Feb 28 20:42 Documents
451drwxr-xr-x 2 tux tux 4096 Feb 28 20:26 Downloads
452-rw-rw-r-- 1 tux tux 0 Feb 28 18:08 man2
453drwxr-xr-x 2 tux tux 4096 Feb 22 13:39 Music
454drwxr-xr-x 3 tux tux 4096 Feb 22 19:44 Pictures
455drwxr-xr-x 2 tux tux 4096 Feb 22 13:39 Public
456drwxrwxr-x 10 tux tux 4096 Feb 28 17:41 SoftMaker
457drwxr-xr-x 2 tux tux 4096 Feb 22 13:39 Templates
458drwxr-xr-x 2 tux tux 4096 Feb 22 13:39 Videos
459
460
461rm command
462
463It creates hardlinks and symbolic links.Some rm commands are: rm file1: Delete file1 silently.
464rm -i file1: Same as rm command,except user is prompted for confirmation.
465
466rm -r file1 dir1: Delete file1 and dir1 and its contents.
467
468rm -rf file1 dir1: Same but if file1 or dir1 does not exist,rm will continue silently.
469
470tux@acer$ ls
471dir1 file1 Public Videos
472
473tux@acer$ rm file1
474tux@acer$ ls
475dir1 Public Videos
476
477tux@acer$ rm -r file1 dir1
478rm: cannot remove 'file1': No such file or directory
479tux@acer$ ls
480Public Videos
481
482tux@acer$ rm -rf file1 dir1
483tux@acer$ ls
484Public Videos
485
486
487ln command (Create Links)
488
489The ln command is used to create either hard or symbolic links. It is used in one or two ways.
490
491The following creates hard link: ln file link
492The following creates a symbolic links:
493ln -s item link
494Hard Links
495
496
497Hard Links are the original unix way of creating links, compared to symbolic links, which are more modern.By default every file has a single hard link that gives the file its name.
498
4991.A hard link cannot refrence a file outside its own file system. This means a kink cannot reference a file that is not on the same disk partition.
5002.A hard link may not reference a directory.
501
502Symbolic Links:
503
504
505Symbolic links were created to overcome the limitations of hard links. Symbolic links work by creating a special type of file that contains a text pointer to the referenced file or directory. In this regard, they operate in much the same way as a Windows shortcut, though of course they predate the Windows feature by many years.
506
507tux@acer:~plaground$ ln fun fun-hard
508tux@acer:~plaground$ ln fun dir1/gun-hard
509tux@acer:~plaground$ ls -l
510total12
511drwxrwxr-x 2 tux tux 4096 Feb 29 00:23 2
512drwxrwxr-x 2 tux tux 4096 Feb 29 00:23 dir
513drwxrwxr-x 2 tux tux 4096 Feb 29 00:24 dir1
514-rw-rw-r-- 3 tux tux 0 Feb 29 00:24 fun
515-rw-rw-r-- 3 tux tux 0 Feb 29 00:24 fun-hard
516
517tux@acer:~/plaground$ ln -s fun fun-sym tux@acer:~/plaground$ ln -s ../fun dir1/fun-sym
518tux@acer:~/plaground$ ls -l
519
520
521
522
523
524
525total 12
526
527
528
529
530
531
532
533
534drwxrwxr-x 2 root tux 4096
535Feb 29 00:23 2
536drwxrwxr-x 2 tux tux 4096 Feb 29
53700:23 dir
538drwxrwxr-x 2 tux tux 4096 Feb 29
53900:32 dir1
540-rw-rw-r-- 3 tux tux
5410 Feb
54229
54300:24 fun
544-rw-rw-r-- 3 tux tux
5450 Feb
54629
54700:24 fun-hard
548lrwxrwxrwx 1 tux tux
5493 Feb 29 00:31 fun-sym -> fun
550
551Permissions
552id: Display user identity
553
554chmod: Change a file’s mode
555
556umask: Set the default file permission
557
558su: Run a shell as a another user
559
560sudo: Execute a command as another user
561
562chown: Change a file’s owner
563
564chgrp: Change a file’s group ownership
565
566passwd: Change a user’s password
567
568
569tux@acer$ id
570uid=1000(tux)
571
572groups=1000(tux),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),118(lpadmin),128
573(sambashare)
574
575
576
577chmod Symbolic Notation
578u = Short for “user” but means the file or directory owner.
579g = Group owner
580o = Short for “others” but means world
581a = Short for “all” but means world
582Exanples:
583u+x = Add execute permission for owner
584u-x = Remove execute permission from owner
585+x = Add execute permission for owner, group and world
586o-rw = Remove read and write permission from anyone besides owner and group
587
588tux@acer:~/t$ chmod u+x txt
589tux@acer:~/t$ ls -l
590-rw-rw-r-- 1 root root 0 Feb 29 00:59 txt
591
592tux@aser:~/t$ chmod +x txt
593tux@acer:~/t$ ls -l
594-rwxrwxr-x 1 root root 0 Feb 29 00:59 txt
595
596tux@acer:~/t$ chmod o-rw txt
597tux@acer:~/t$ ls -l
598-rwxrwx--x 1 root root 0 Feb 29 00:59 txt
599
600tux@acer:~/t$ chmod -x txt
601tux@acer:~/t$ ls -l
602-rw-rw---- 1 root root 0 Feb 29 00:59 txt
603
604Redirecting Standard Output
605
606I/O redirection allows us to redefine where standard output goes.To redirect standard output to another file instead of screen we use the > redirection operator followed by the name of the file.
607
608tux@acer$ ls -l /usr/bin > ls-output.txt
609tux@acer$ ls -l ls-output.txt
610-rw-rw-r-- 1 root root 1667878 2020-02-29 15:07 ls-output.txt
611
612bc command
613It is an inbuilt calculator in the terminal and can be used for basic calculation
614
615tux@acer$ bc
616bc 1.07.1
617
618Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
619This is free software with ABSOLUTELY NO WARRANTY.
620For details type `warranty'.
62112+12
62224
62312-11
6241
62512/2
6266
62712*2
62824
629
630Compressing Files
631
632Data compression is the process of removing redudancy from data.Compression is an algorithm and falls in two categories:
633i.Lossless :Lossless compression preserves all the data contained in original form.
634
635This means once we restored the compressed file it will be be exactly in same format.
636
637ii.Lossy :Lossy compression removes data as the compresion is performed to allow more compression to be applied.Once it is restored it will be not same as original format.
638
639Eg: JPEG and MP3.
640
641i. gzip: The gzip program is used to compare one or more files.We executit and then it will replaces the original file with compressed version of original.
642
643
644
645tux@acer$cat>foot
64613456
647
648234567
649
650
651
6522345678
653i love to use linux
654tux@acer$ ls
655Documents foot Pictures Public
656tux@acer$ gzip foot
657tux@acer$ ls
658Documents Pictures Public t
659foot.gz
660
661
662
663
664
665
666t
667
668
669
670
671
672Videos
673
674
675Videos
676
677
678
679
680gunzip: This program is used to restore compressed files to their original, uncompressed form.
681
682
683tux@acer$ gunzip foot.gz
684
685
686
687Documents foot Pictures
688
689Public
690
691t
692
693Videos
694
695
696
697
698Archiving Files:
699
700A common file-management task often used in conjunction with compression is archiv-ing. Archiving is the process of gathering up many files and bundling them together into a single large file. Archiving is often done as part of system backups. It is also used when old data is moved from a system to some type of long-term storage.
701
702
703c = Create an archive from a list of files and/or directories.
704x = Extract an archive.
705r = Append specified pathnames to the end of an archive.
706t = List the contents of an archive.
707
708
709tux@acer$ mkdir -p playground/dir-{001..100} tux@acer$ touch playground/dir-{001..100}/file-{A..Z}
710
711tux@acer$ tar cf playground.tar playground
712tux@acer$ tar tf playground.tar
713
714
715
716Extracting the file in new location:
717
718tux@acer:~/t$ tar xf ../playground.tar
719tux@acer:~/t$ ls
720playground
721
722
723Printing:
724pr – Convert text files for printing
725
726lpr – Print files
727
728a2ps – Format files for printing on a PostScript printer
729
730lpstat – Show printer status information
731
732lpq – Show printer queue status
733
734lprm – Cancel print jobs
735
736
737tux@acer$ ls /usr/bin | pr -3 -w 65 | head
738
7392020-03-01 13:00
740
741
742Page 1
743411toppm
744apturl
745bsd-write
746a2p
747ar
748bsh
749ark
750ant
751ant-bloody
752mojave
753jeremy
754
755
756tux@acer$ ls /usr/bin | pr -3 | lpr
757tux@acer$ lpstat -a
758
759PDF accepting requests since Mon 08 Jan 2020 03:05:59 PM EST printer accepting requests since Tue 24 Feb 2020 08:43:22 AM EST
760
761tux@acer$ ls /usr/bin | pr -3 -t | a2ps -o ~/Desktop/ls.ps -L 66 [stdin (plain): 11 pages on 6 sheets]
762
763[Total: 11 pages on 6 sheets] saved into the file `/home/me/Desktop/ ls.ps'
764
765tux@acer$ lpq
766printer is ready
767no entries
768
769tux@acer$ lprm
770tux@acer$ lpq
771printer is ready
772no entries
773
774Metacharacters and Literals
775
776These are used to specify more complex matches. Regular expression metacharacters consist of the following:
777$.[]{}-?*+()|\
778
779
780tux@acer$ grep -h '.zip' dirlist*.txt
781bunzip2
782bzip2
783bzip2recover
784gunzip
785gzip
786funzip
787gpg-zip
788preunzip
789prezip
790prezip-bin
791unzip
792unzipsfx
793
794Anchors:
795
796The caret (^) and dollar sign ($) characters are treated as anchors in regular expressions. This means they cause the match to occur only if the regular expression is found at the beginning of the line (^) or at the end of the line ($).
797
798
799tux@acer$ grep -h '^zip' dirlist*.txt
800zip
801zipcloak
802zipgrep
803zipinfo
804zipnote
805zipsplit
806tux@acer$ grep -h 'zip$' dirlist*.txt
807gunzip
808gzip
809funzip
810gpg-zip
811preunzip
812prezip
813unzip
814zip
815tux@acer$ grep -h '^zip$' dirlist*.txt
816zip
817
818Bracket Expressions and Character Classes:
819
820In addition to matching any character at a given position in our regular expression, we can also match a single character from a specified set of characters by using bracket ex-pressions. With bracket expressions, we can specify a set of characters to be matched.
821
822tux@acer$ grep -h '[^bg]zip' dirlist*.txt
823bunzip2
824gunzip
825funzip
826gpg-zip
827preunzip
828prezip
829prezip-bin
830unzip
831unzipsfx
832
833tux@acer$ grep -h '^[A-Z]' dirlist*.txt
834MAKEDEV
835ControlPanel
836GET
837HEAD
838POST
839X
840X11
841Xorg
842MAKEFLOPPIES
843NetworkManager
844NetworkManagerDispatcher
845
846Storage Media:
847
848mount – Mount a file system
849umount – Unmount a file system
850fsck – Check and repair a file system
851fdisk – Manipulate disk partition table
852mkfs – Create a file system
853
854
855tux@acer$ mount
856/dev/sda2 on / type ext4 (rw)
857proc on /proc type proc (rw)
858sysfs on /sys type sysfs (rw)
859devpts on /dev/pts type devpts (rw,gid=5,mode=620)
860/dev/sda5 on /home type ext4 (rw)
861/dev/sda1 on /boot type ext4 (rw)
862tux@acer$ sudo umount /dev/sdb1
863tux@acer$ sudo mkfs -t ext4 /dev/sdb1
864mke2fs 2.23.2 (12-Jul-2011)
865Filesystem label=
866OS type: Linux
867Block size=1024 (log=0)
868Fragment size=1024 (log=0)
8693904 inodes, 15608 blocks
870780 blocks (5.00%) reserved for the super user
871First data block=1
872Maximum filesystem blocks=15990784
873tux@acer$ sudo fsck /dev/sdb1
874fsck 1.40.8 (01-Mar-2020)
875e2fsck 1.40.8 (01-Mar-2020)
876
877
878Adding user:
879
880Admin can add user with password within the linux maxhine to use the machine.
881
882tux@acer$ sudo -i
883root@acer# useradd deepin
884root@acer# passwd
885Enter new UNIX password: 123456
886Retype new UNIX password: 123456
887passwd: password updated successfully
888root@acer#
889
890Practical No. 3
891
892Study of filter commands
893
894Filters:
895
896
897Pipelines are often used to perform complex operations on data. It is possible to put sev-eral commands together into a pipeline. Frequently, the commands used this way are re-ferred to as filters.
898sort – Sort lines of text
899uniq – Report or omit repeated lines
900
901grep – Print lines matching a pattern
902
903wc – Print newline, word, and byte counts for each file
904
905head – Output the first part of a file
906
907tail – Output the last part of a file
908
909tee – Read from standard input and write to standard output and files
910
911
912uniq- Report or Omit Repeated Lines:
913The uniq command is often used in conjunction with sort. uniq accepts a sorted list
914
915of data from either standard input or a single filename argument (see the uniq man page for details) and, by default, removes any duplicates from the list. So, to make sure our list has no duplicates (that is, any programs of the same name that appear in both the /bin and /usr/bin directories), we will add uniq to our pipeline.
916
917
918tux@acer$ ls /bin /usr/bin | sort | uniq | less
919
920
921wc-Print Line,Word and Byte Counts:
922
923The wc (word count) command is used to display the number of lines, words, and bytes contained in files.
924
925
926tux@acer$ wc ls-output.txt
9277902 64566 503634 ls-output.txt
928
929tux@acer$ ls /bin /usr/bin | sort | uniq | wc -l
9302728
931
932grep - Print Lines Matching a Pattern:
933grep is a powerful program used to find text patterns within files.
934
935When grep encounters a “pattern” in the file, it prints out the lines containing it. The patterns that grep can match can be very complex, but for now we will concentrate on simple text matches.
936
937
938tux@acer$ ls /bin /usr/bin | sort | uniq | grep zip
939bunzip2
940bzip2
941gunzip
942gzip
943unzip
944zip
945ziplock
946
947head / tail – Print First / Last Part of Files:
948The head command prints the first ten lines of a file, and the
949
950tail command prints the last ten lines. By default, both commands print ten lines of text, but this can be adjusted with the -n option.
951
952
953
954tux@acer$ head -n 5 ls-output.txt
955total 1234
956
957
958
959
960
961
962
963
964drwxrwxr-x 2 tux tux 4096
965Feb 29
96600:23 2
967drwxrwxr-x 2 tux tux 4096
968Feb 29
96900:23 dir
970drwxrwxr-x 2 tux tux 4096
971Feb 29
97200:24 dir1
973-rw-rw-r-- 3 tux tux
9740 Feb 29
97500:24 fun
976-rw-rw-r-- 3 tux tux
9770 Feb 29
97800:24 fun-hard
979
980tux@acer$ tail -n 5 ls-output.txt
981drwxrwxr-x 1 tux tux 406 Feb 29 00:23 2
982drwxrwxr-x 1 tux tux 96 Feb 29 00:23 dir
983drwxrwxr-x 2 tux tux 4096 Feb 29 00:24 dir1
984-rw-rw-r-- 3 tux tux
9850
986Feb 29
98700:24 fun
988-rw-rw-r-- 3 tux tux
9890
990Feb 29
99100:24 fun-hard
992
993tux@acer$ ls /usr/bin | tail -n 5
994znew
995zonetab2pot.py
996zonetab2pot.pyc
997zonetab2pot.pyo
998zsoelim
999
1000tux@acer$ tail -f /var/log/messages
1001
1002Feb 28 13:40:05 twin4 dhclient: DHCPACK from 192.168.1.1 Feb 28 13:40:05 twin4 dhclient: bound to 192.168.1.4 -- renewal in 1652 seconds.
1003
1004Feb 28 13:55:32 twin4 mountd[3953]: /var/NFSv4/musicbox exported to both 192.168.1.0/24 and twin7.localdomain in 192.168.1.0/24,twin7.localdomain
1005
1006Feb 28 14:07:37 twin4 dhclient: DHCPREQUEST on eth0 to 192.168.1.1 port 67
1007
1008Feb 28 14:07:37 twin4 dhclient: DHCPACK from 192.168.1.1 Feb 28 14:07:37 twin4 dhclient: bound to 192.168.1.4 -- renewal in 1771 seconds.
1009Feb 28 14:09:56 twin4 smartd[3468]: Device: /dev/hda, SMART
1010
1011Prefailure Attribute: 8 Seek_Time_Performance changed from 237 to 236 Feb 28 14:10:37 twin4 mountd[3953]: /var/NFSv4/musicbox exported to both 192.168.1.0/24 and twin7.localdomain in 192.168.1.0/24,twin7.localdomain
1012
1013Feb 28 14:25:07 twin4 sshd(pam_unix)[29234]: session opened for user me by (uid=0)
1014
1015Feb 28 14:25:36 twin4 su(pam_unix)[29279]: session opened for user root by me(uid=500
1016
1017tee – Read from Stdin and Output to Stdout and Files:
1018
1019In keeping with our plumbing metaphor, Linux provides a command called tee which creates a “tee” fitting on our pipe. The tee program reads standard input and copies it to both standard output (allowing the data to continue down the pipeline) and to one or more files. This is useful for capturing a pipeline's contents at an intermediate stage of process-ing. Here we repeat one of our earlier examples, this time including tee to capture the entire directory listing to the file ls.
1020
1021
1022
1023tux@acer$ ls /usr/bin | tee ls.txt | grep zip
1024bunzip2
1025bzip2
1026gunzip
1027gzip
1028unzip
1029zip
1030zipcloak
1031zipgrep
1032zipinfo
1033zipnote
1034zipsplit
1035
1036Practical No. 4
1037
1038Linux Editors Vi
1039
1040
1041is almost always available. This can be a lifesaver if we have a system with no graphical interface, such as a remote server or a local system with a broken X configuration. nano, while increasingly popular, is still not universal. POSIX, a standard for program compatibility on Unix systems, requires that vi be present.
1042
1043is lightweight and fast. For many tasks, it's easier to bring up vi than it is to
1044
1045find the graphical text editor in the menus and wait for its multiple megabytes to load. In addition, vi is designed for typing speed. As we will see, a skilled vi user never has to lift his or her fingers from the keyboard while editing.
1046
1047Starting and Stopping vi
1048To start vi ,we simply enter vi in command line.To exit enter :q.
1049
1050
1051tux@acer$ vi
1052~
1053~
1054~
1055~
1056:q
1057
1058Editing Modes:
1059
1060Let's start vi again, this time passing to it the name of a nonexistent file. This is how we can create a new file with vi:
1061
1062
1063tux@acer$ vi foo.txt
1064The quick brown fox jumped over lazy dog.
1065The linux os are free and fast love to use linux.
1066~
1067~
1068~
1069~
1070~
1071~
1072~
1073~
1074~
1075~
1076~
1077“foo.txt” [New File]
1078:w
1079tux@acer$
1080
1081Enter Insert mode:
1082By pressing i we can insert in the vi editor.
1083Saving:
1084By pressing Esc key and :w saves the inserted work.
1085
1086Moving the Cursor Around:
1087
1088While in command mode, vi offers a large number of movement commands,some of them are listed below:
1089
1090l or right arrow = Right one character.
1091h or left arrow = Left one character.
1092j or down arrow Down = one line.
1093k or up arrow Up = one line.
10940 (zero) = To the beginning of the current line.
1095
1096= To the first non-whitespace character on the current line. $ = To the end of the current line.
1097w = To the beginning of the next word or punctuation character.
1098
1099W = To the beginning of the next word, ignoring punctuation characters. b = To the beginning of the previous word or punctuation character.
1100
1101B = To the beginning of the previous word, ignoring punctuation characters. Ctrl-f or Page Down = Down one page.
1102Ctrl-b or Page Up =Up one page.
1103
1104numberG = To line number. For example, 1G moves to the first line of the file. G = To the last line of the file.
1105
1106Appending Text:
1107
1108For appending text i.e entering something in the same file press “a”.This allow to enter the data.
1109
1110
1111
1112tux@acer$ vi foo.txt
1113The quick brown fox jumped over lazy dog.It was cool
1114The linux os are free and fast love to use linux.
1115Elementaryos
1116PopOS
1117Ubuntu
1118Manjaro
1119Clear Linux
1120~
1121~
1122~
1123~
1124~
1125~
1126~
1127~
1128~
1129~
1130“foo.txt” 26C
1131
1132Opening Line:
1133o = Opens line below current line
1134O = Opens line above the current line
1135
1136cursor at line 3 press o:
1137
1138
1139
1140tux@acer$ vi foo.txt
1141
1142The quick brown fox jumped over lazy dog.It was cool The linux os are free and fast love to use linux. Elementaryos
1143
1144PopOS
1145Ubuntu
1146Manjaro
1147Clear Linux
1148~
1149~
1150~
1151~
1152~
1153~
1154~
1155~
1156~
1157~
1158“foo.txt” 26C
1159
1160Deleting Text:
1161
1162As we might expect, vi offers a variety of ways to delete text, all of which contain one of two keystrokes. First, the x command will delete a character at the cursor location. x may be preceded by a number specifying how many characters are to be deleted. The d command is more general purpose. Like x, it may be preceded by a number specifying the number of times the deletion is to be performed. In addition, d is always followed by a movement command that controls the size of the deletion.
1163
1164x = The current character
11653x = The current character and the next two characters
1166= The current line
11675dd = The current line and the next four lines
1168
1169dW = From the current cursor position to the beginning of the next word d$ = From the current cursor location to the end of the current line d0 = From the current cursor location to the beginning of the line
1170
1171d^ = From the current cursor location to the first non-whitespace character in the line dG = From the current line to the end of the file
1172d20G= From the current line to the twentieth line of the file
1173
1174Place the cursor on the word It on the first text.Press x untill sentence is deleted.
1175Press u for doing the deletion undone.
1176
1177
1178One can delete using d command. Put the cursor on Ubuntu and type dW.
1179
1180
1181
1182tux@acer$ vi look
1183ubuntu
1184~
1185~
1186
1187~
1188~
1189~
1190
1191
1192Type d$ will delete from cursor position to the end of line.
1193
1194The quick brown fox jumped over lazy dog.It was cool
1195The linux os are free and fast love to use linux.
1196Elementaryos
1197PopOS
1198Ubuntu
1199Manjaro
1200Clear Linux
1201~
1202~
1203~
1204~
1205~
1206~
1207~
1208~
1209~
1210~
1211“foo.txt” 26C
1212
1213
1214
1215~
1216~
1217~
1218~
1219~
1220~
1221~
1222~
1223~
1224~
1225~
1226~
1227~
1228~
1229~
1230“foo.txt” 26C
1231
1232Cutting Copying and Pasting Text:
1233
1234The d command not only deletes text, it also “cuts” text.The y command is used to “yank” (copy) text in much the same way the d command is used to cut text.
1235
1236
1237= The current line
12385yy = The current line and the next four lines
1239
1240yW = From the current cursor position to the beginning of the next word y$ = From the current cursor location to the end of the current line y0 = From the current cursor location to the beginning of the line
1241
1242y^ = From the current cursor location to the first non- whitespace character in the line yG = From the current line to the end of the file
1243y20G = From the current line to the twentieth line of the file
1244
1245
1246The quick brown fox jumped over lazy dog.It was cool
1247The linux os are free and fast love to use linux.
1248Elementaryos
1249PopOS
1250Ubuntu
1251Manjaro
1252Clear Linux
1253~
1254~
1255~
1256~
1257~
1258~
1259~
1260~
1261~
1262Clear Linux
1263“foo.txt” 26C
1264
1265
1266
1267Type P to paste above the current line.
1268
1269The quick brown fox jumped over the lazy dog.It was cool.
1270Ubuntu
1271elementaryos
1272Clear Linux
1273miui
1274The quick brown fox jumped over the lazy dog.It was cool.
1275~
1276~
1277~
1278~
1279~
1280~
1281~
1282~
1283~
1284
1285Search and Replace:
1286
1287has the ability to move the cursor to locations based on searches. It can do this either on a single line or over an entire file. It can also perform text replacements with or with-out confirmation from the user.
1288
1289Searching Within a Line:
1290
1291The f command searches a line and moves the cursor to the next instance of a specified character. For example, the command fa would move the cursor to the next occurrence of the character a within the current line. After performing a character search within a line, the search may be repeated by typing a semicolon.
1292
1293Searching the Entire File
1294
1295To move the cursor to the next occurrence of a word or phrase, the / command is used. his works the same way as we learned earlier in the less program. When you type the
1296
1297command, a / will appear at the bottom of the screen. Next, type the word or phrase to be searched for, followed by the Enter key. The cursor will move to the next location containing the search string.
1298
1299
1300The quick brown fox jumped over the lazy dog. It was cool.
1301Line 2
1302Line 3
1303Line 4
1304Line 5
1305
1306
1307Place the cursor on the first line of the file. Type followed by the Enter key.
1308
1309
1310/Line
1311
1312Practical No. 5
1313Practical: 1
1314
1315Write a Shell Script to accept a number and display whether the number is positive, negative or zero.
1316
1317echo -n "Enter the Number : "
1318read x
1319if [ $x -lt 0 ]
1320then
1321echo "$x is Negative"
1322elif [ $x -eq 0 ]
1323then
1324echo "Number entered is Zero"
1325else
1326echo "$x is Positive"
1327fi
1328
1329OUTPUT:
1330
1331[tybsc41@localhost ~]$ sh j02.sh
1332Enter the Number : 7
13337 is Positive
1334[tybsc41@localhost ~]$ sh j02.sh
1335Enter the Number : -9
1336-9 is Negative
1337[tybsc41@localhost ~]$ sh j02.sh
1338Enter the Number : 0
1339Number entered is Zero
1340
1341
1342
1343Practical: 2
1344
1345Write a Shell Script to accept a number from user and check whether the number is even or
1346odd .
1347echo -n "Enter the number : "
1348read n
1349let rem=n%2
1350if test $rem -eq 0
1351then
1352echo "$n is a even number ."
1353else
1354echo "$n is a odd number ."
1355fi
1356
1357OUTPUT :
1358
1359[tybsc41@localhost ~]$ sh j11.sh
1360Enter the number : 7
13617 is a odd number .
1362[tybsc41@localhost ~]$ sh j11.sh
1363Enter the number : 10
136410 is a even number .
1365
1366
1367Practical: 3
1368
1369Write a shell script to accept a number from the user and display even number series till that number.
1370
1371no=0
1372echo "Enter limit"
1373read limit
1374echo "Even number series"
1375while [ $no -le $limit ]
1376do
1377echo $no
1378let no= $no + 2
1379done
1380OUTPUT:
1381
1382[tybsc41@localhost ~]$ sh even.sh
1383Enter limit
138410
1385Even number series
13860
13872
13884
13896
13908
139110
1392
1393
1394
1395Practical: 4
1396Write a Shell to display summation of 1+2+3+4………+n.
1397
1398echo Enter the value for n:
1399read num
1400sum=0
1401iter=1
1402while [ $iter -le $num ]
1403do
1404let sum=$sum+$iter
1405let iter=$iter+1
1406done
1407echo $sum
1408OUTPUT:
1409
1410[tybsc41@localhost ~]$ sh summation.sh
1411Enter the value for n:
141210
141355
1414
1415Practical: 5
1416
1417Write a Shell Script to accept a number n from user and display the squares of all numbers from 1 to n
1418
1419echo -n "Enter the number : "
1420read n
1421c=1
1422a=1
1423while test $c -le $n
1424do
1425let a=$c*$c
1426echo "$c square = $a"
1427let c=c+1
1428done
1429
1430
1431
1432OUTPUT :
1433
1434[tybsc41@localhost ~]$ sh j09.sh
1435Enter the number : 5
14361 square = 1
14372 square = 4
14383 square = 9
14394 square = 16
14405 square = 25
1441
1442
1443
1444Practical: 6
1445
1446Write a shell script to accept two numbers from user and display n^y using while loop.
1447
1448echo Enter value for n:
1449read n
1450echo Enter value for y:
1451read y
1452iter=1
1453sum=1
1454while [ $iter -le $y ]
1455do
1456let sum=sum*n
1457let iter=$iter+1
1458done
1459echo $sum
1460
1461OUTPUT:
1462[tybsc41@localhost ~]$ sh file21.sh
1463Enter value for n:
14642
1465Enter value for y:
14663
14678
1468
1469
1470Practical: 7
1471
1472Write a shell script to accept a number from the user and check the number entered is prime or not.
1473
1474clear
1475echo "Enter the number"
1476read num
1477i=2
1478flag=0
1479while [ $i -lt $num ]
1480do
1481k=`expr $num % $i`
1482if test $k -eq 0
1483then
1484flag=1
1485fi
1486i=`expr $i + 1`
1487done
1488if test $flag -eq 0
1489then
1490echo "Number is prime"
1491else
1492echo "Number is not prime"
1493fi
1494OUTPUT:
1495Enter the number
14962
1497Number is prime
1498
1499Enter the number
15004
1501Number is not prime
1502
1503
1504
1505
1506Practical: 8
1507
1508Write a shell script to accept a number from user and display multiplication table of given number.
1509
1510echo "Multiplication Table:"
1511tab=1
1512i=1
1513a=10
1514
1515echo "Input Number :"
1516read x
1517echo
1518echo "Table of $x"
1519for i in 1 2 3 4 5 6 7 8 9 10
1520do
1521i=`expr $x \* $i`
1522echo $i
1523done
1524
1525OUTPUT:
1526[pritam1@localhost ~]$ sh demofor.sh
1527Multiplication Table:
1528Input Number :
152915
1530
1531Table of 15
153215
153330
153445
153560
153675
153790
1538105
1539120
1540135
1541150
1542
1543
1544
1545Practical No: 9
1546
1547Write a Shell Script to accept a number from user and print the day of the week using case … in
1548
1549echo -n "Enter the day number : "
1550read num
1551case $num in
1552echo "Sunday";;
1553echo "Monday";;
1554
1555echo "Tuesday";;
1556
1557echo "Wednesday";;
1558
1559echo "Thursday";;
1560
1561echo "Friday";;
1562
1563echo "Saturday";;
1564*) echo "Enter any number between 1 to 7."
1565esac
1566
1567OUTPUT :
1568
1569[tybsc41@localhost ~]$ sh j06.sh
1570Enter the day number : 1
1571Enter the day number : 1
1572Sunday
1573[tybsc41@localhost ~]$ sh j06.sh
1574Enter the day number : 3
1575Tuesday
1576[tybsc41@localhost ~]$ sh j06.sh
1577Enter the day number : 2
1578Monday
1579[tybsc41@localhost ~]$ sh j06.sh
1580Enter the day number : 4
1581Wednesday
1582[tybsc41@localhost ~]$ sh j06.sh
1583Enter the day number : 5
1584Thursday
1585[tybsc41@localhost ~]$ sh j06.sh
1586Enter the day number : 6
1587
1588Friday
1589[tybsc41@localhost ~]$ sh j06.sh
1590Enter the day number : 7
1591Saturday
1592
1593Practical: 10
1594
1595Write a Shell Script to accept a file name and check whether the file name exist or not in
1596current directory .
1597echo -n "Enter the Filename : "
1598read fname
1599if [ -f $fname ]
1600then
1601echo "File exists in the current directory. "
1602else
1603echo "File with this name does not exist in the current directory."
1604fi
1605
1606OUTPUT :
1607
1608[tybsc41@localhost ~]$ sh j03.sh
1609Enter the Filename : file5
1610File exists in the current directory.
1611[tybsc41@localhost ~]$ sh j03.sh
1612Enter the Filename : file12
1613File with this name doesnot exist in the current directory.
1614
1615
1616
1617Practical: 11
1618Factorial
1619factorial=1
1620rial=1
1621for (( no=1 ; no <= $1 ; no++ ))
1622do
1623factorial=$[ $factorial * $no ]
1624done
1625echo "the factorial of $1 is $factorial"
1626
1627OUTPUT :
1628
1629[tybsc41@localhost ~]$ sh fact.sh 5
1630The factorial of 5 is 120
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642Practical: 12
1643
1644Basic calculations
1645read n1 n2
1646c=`expr $n1 + $n2`
1647echo "Addition=$c"
1648c=`expr $n1 - $n2`
1649
1650echo "Substraction=$c"
1651c=`expr $n1 \* $n2`
1652echo "multiplication=$c"
1653c=`expr $n1 / $n2`
1654echo "DIvision=$c"
1655
1656OUTPUT :
1657
1658[tybsc41@localhost ~]$ sh calc.sh
16593
1660
1661Addition=8
1662
1663Substraction=2
1664
1665Multiplication=15
1666
1667Division=1
1668
1669
1670
1671Practical: 13
1672
1673Validation
1674echo "enter Your name"
1675read name
1676if [ `expr "$name" : '.*' ` -gt 20 ]
1677then
1678echo "invalid username"
1679else
1680break
1681
1682fi
1683
1684OUTPUT:
1685
1686[tybsc41@localhost ~]$ sh 13.sh
1687Enter your name
1688Abcdefgggggggggggggggggggggggfffffffffffffff
1689Invalid username
1690
1691
1692
1693
1694
1695
1696
1697
1698Practical: 14
1699
1700String equal or not
1701echo "enter two string"
1702read str1 str2
1703if [ $str1 = $str2 ]
1704then
1705echo "String are equal"
1706else
1707echo "String are not equal"
1708fi
1709
1710Output:
1711[pritam1@localhost ~]$ sh string.sh
1712abc abc
1713String are equal
1714
1715
1716Practical: 15
1717
1718Table Limit
1719echo "enter lower limit"
1720read ln
1721echo "enter heigher limit"
1722read hn
1723i=1
1724while [ $ln -le $hn ]
1725do
1726for i in 1 2 3 4 5 6 7 8 9 10
1727do
1728a=`expr $ln \* $i`
1729echo "$ln * $i=$a"
1730done
1731ln=`expr $ln + 1`
1732done
1733
1734Output:
1735
1736[pritam1@localhost ~]$ sh tablelimit.sh
1737Enter lower limit
17382
1739Enter heigher limit
17403
1741
17422*1=2
17432*2=4
17442*3=6
17452*4=8
17462*5=10
17472*6=12
17482*7=14
17492*8=16
17502*9=18
17512*10=20
1752
17533*1=3
17543*2=6
17553*3=9
17563*4=12
17573*5=15
17583*6=18
17593*7=21
17603*8=24
17613*9=27
17623*10=30
1763
1764Practical No. 6
1765Practical: 1
1766
1767Write a shell script to accept a number from the user and display all prime numbers till the number user enters.
1768
1769clear
1770echo Enter limit
1771read limit
1772j=1
1773flag=1
1774echo "Prime Number Series"
1775while [ $j -le $limit ]
1776do
1777i=2
1778while [ $i -lt $j ]
1779do
1780temp=`expr $j % $i`
1781if [ $temp -eq 0 ]
1782then
1783flag=0
1784break;
1785else
1786flag=1
1787fi
1788i=`expr $i + 1`
1789done
1790if test $flag -eq 1
1791then
1792echo $j
1793fi
1794j=`expr $j + 1`
1795done
1796
1797
1798OUTPUT:
1799Enter limit
180015
1801Prime Number Series
18021
18032
18043
18055
18067
180711
180813
1809
1810
1811
1812Practical: 2
1813
1814Write a Shell Script to accept a number n from user and display summation of the following series. 1! + 2! + 3! + … + n!
1815
1816echo -n "Enter the number : "
1817read x
1818
1819i=1
1820sum=0
1821while test $i -le $x
1822do
1823fact=1
1824a=1
1825while test $a -le $i
1826do
1827let fact=$fact*$a
1828let a=$a+1
1829done
1830let sum=$sum+$fact
1831let i=$i+1
1832done
1833
1834let i=2
1835echo -n "1!"
1836while test $i -le $x
1837do
1838echo -n " + $i !"
1839let i=$i+1
1840done
1841echo " = $sum"
1842
1843
1844
1845OUTPUT :
1846
1847[tybsc41@localhost ~]$ sh j08.sh
1848Enter the number : 4
18491!+2!+3!+4!=33
1850
1851
1852
1853
1854
1855
1856Practical: 3
1857
1858Write a shell script to accept basic salary from user and calculate and display the gross salary of an employee.
1859
1860echo Enter the the basic salary
1861read b
1862if [$b lt 1500]
1863
1864then
1865let hra=$b*10/100
1866let da=$b*90/100
1867else
1868let hra=500
1869let da=$b*98/100
1870fi
1871let g=$b+$hra+$da
1872echo gross salary=$g
1873
1874OUTPUT:
1875
1876Enter the the basic salary
18779000
1878gross salary=18320
1879
1880
1881Practical :4
1882
1883Write a Shell Script to accept a character from user and display whether it is alphabet, digit, special character or user has entered more than one character.
1884
1885echo -n "Enter the Character : "
1886read c
1887case $c in
1888[ABCDEFGHIJKLMNOPQRSTUVWXYZ]) echo "Alphabet";;
1889[a-z]) echo "Alphabet";;
1890[0-9]) echo "Digit";;
1891?) echo "special Character";;
1892*) echo "More than two Characters";;
1893esac
1894
1895OUTPUT :
1896
1897[tybsc41@localhost ~]$ sh j12.sh
1898Enter the Character : A
1899Alphabet
1900[tybsc41@localhost ~]$ sh j12.sh
1901Enter the Character : f
1902Alphabet
1903[tybsc41@localhost ~]$ sh j12.sh
1904Enter the Character : 90
1905More than two Characters
1906[tybsc41@localhost ~]$ sh j12.sh
1907Enter the Character : 4
1908Digit
1909[tybsc41@localhost ~]$ sh j12.sh
1910Enter the Character : ^
1911special Character
1912
1913
1914
1915Practical :5
1916
1917Write a shell script to accept a file name from user and check whether file has permission to write. If write permission is there then append to the file.
1918
1919echo Enter the file name
1920read fname
1921if [ -w $fname ]
1922then
1923echo Type text to append. To stop press Ctrl D.
1924cat >> $fname
1925else
1926echo The file has no write permission
1927fi
1928
1929OUTPUT:
1930
1931[shital1@localhost ~]$ sh per.sh
1932Enter the file name
1933b2
1934The file has no write permission
1935
1936[shital1@localhost ~]$ sh per.sh
1937Enter the file name
1938prime.sh
1939Type text to append. To stop press Ctrl D.
1940tybsc
1941
1942
1943
1944
1945
1946
1947
1948Practical : 6
1949
1950Write a shell script to accept a character from the user and check whether the word begins with vowel or begins with digit or ends with digit or is a four lettered word or a consonant.
1951
1952echo Enter the word
1953
1954read str
1955
1956case $str in
1957
1958[aeiou]*) echo The word begins with a vowel;;
1959
1960[0-9]*) echo The word starts with a digit;;
1961
1962*[0-9]) echo The word ends with a digit;;
1963
1964????) echo The word entered is 4 lettered word;; *) echo The word entered is either starts with a
1965consonant or incorrect input;;
1966
1967esac
1968
1969OUTPUT:
1970[shital1@localhost ~]$ sh file12.sh
1971
1972Enter the word
1973a
1974The word begins with a vowel
1975
1976Enter the word
1977tybsc
1978The word entered is either starts with a consonant or incorrect input
1979
1980Enter the word
1981film
1982The word entered is 4 lettered word
1983
1984Enter the word
1985*9
1986The word ends with a digit
1987
1988
1989
1990
1991
1992Practical :7
1993
1994
1995Write a shell script for menu driven program to see the list of files, to see the todays date, to see current month calendar, to see currently logged in users or to exit.
1996
1997echo enter
1998echo "1)List of files"
1999echo "2)Today's date"
2000echo "3)current month calendar"
2001echo "4)User of the system"
2002echo "Quit to unix"
2003echo "Enter a choice"
2004read ch
2005case $ch in
20061)ls;;
20072)date;;
20083)cal 02 2014;;
20094)who;;
20105)exit;;
2011*)echo "enter prpper choice";;
2012esac
2013
2014OUTPUT:
2015
2016[tybsc@localhost ~]$ sh file13.sh
2017
2018enter
2019"1)List of files"
2020"2)Today's date"
2021"3)current month calendar"
2022"4)User of the system"
2023"Quit to unix"
2024"Enter a choice"
2025
20261) List of files
2027
2028
2029a2ps.cfg
2030inittab
2031quotatab
2032a2ps-site.cfg
2033inputrc
2034racoon
2035acpi
2036iproute2
2037raddb
2038
2039
2040
2041
2042
2043
2044
2045
2046Practical :8
2047
2048Write a Shell Script to accept 5 subject marks of a student and display whether the student got first class, second class, third class or Student failed .
2049
2050
2051echo "enter five subject marks"
2052read m1 m2 m3 m4 m5
2053echo "Total marks"
2054a=`expr $m1 + $m2 + $m3 + $m4 + $m5`
2055echo $a
2056echo "The percentage is"
2057per=`expr $a / 5`
2058echo $per
2059if [ $per -ge 60 ]
2060then
2061echo "Grade is A"
2062elif [ $per -lt 60 -a $per -gt 40 ]
2063then
2064
2065echo "Grade is B"
2066else
2067echo "Grade is F"
2068fi
2069OUTPUT :
2070
2071[tybsc41@localhost ~]$ sh j05.sh
2072Enter the marks of five subjects :
207320 15 32 14 34
2074The Percentage is 23 %
2075Grade is F.
2076[tybsc41@localhost ~]$ sh j05.sh
2077Enter the marks of five subjects :
207850 55 45 56 60
2079The Percentage is 53 %
2080Grade is B.
2081
2082
2083
2084
2085Practical :9
2086
2087Write a Shell script to accept a number from the user and display the number of days in that particular month.
2088
2089echo Enter month
2090month=(30 31 29)
2091read mon
2092case "$mon" in
2093
2094[jJ][aA][nN]*) echo "No.of days : ${month[1]} ";; [fF][eE][bB]*) echo enter year
2095read year
2096let rem="$year % 4"
2097if [ $rem -eq 0 ] ; then
2098echo "No. of days : ${month[2]}"
2099else
2100echo "No. of days : 28 "
2101fi;;
2102[mM][aA][rR]*) echo "No. of days : ${month[1]}";;
2103[aA][pP][rR]*) echo "No. of days: ${month[0]}";;
2104[mM][aA][yY]*) echo "No. of days: ${month[1]}";;
2105[jJ[uU][nN]*) echo "No. of days: ${month[0]}";;
2106[jJ][uU][lL]*) echo "No. of days: ${month[1]}";;
2107[aA][uU][gG]*) echo " No. of days: ${month[1]}";;
2108[sS][eE][pP]*) echo " No. of days: ${month[0]}";;
2109[aA][cC][tT]*) echo "No. of days: ${month[1]}";;
2110[nN][oO][vV]*) echo "No. of days: ${month[0]}";;
2111[dD][eE][cC]*) echo "No. of days: ${month[1]}";;
2112esac
2113OUTPUT:
2114
2115[pritam1@localhost ~]$ vi month.sh
2116Enter month
2117feb
2118enter year
21192005
2120No. of days : 28
2121
2122Practical :10
2123
2124
2125#Program to find the type of the file
2126
2127echo Enter the name of the file
2128
2129read fname
2130
2131case $fname in
2132
2133*.c) echo it’s a c prog file;;
2134
2135*.out) echo it is an output file;;
2136
2137*) echo not sure;;
2138
2139esac
2140
2141OUTPUT:
2142
2143[pritam1@localhost ~]$ vi 10.sh
2144Enter the name of the file
2145File1.out
2146It is an output file
2147
2148
2149Practical :11
2150
2151#Program to display the directories and files
2152
2153echo The directories are
2154
2155echo
2156
2157for file in *
2158
2159do
2160
2161if [ -d $file ]
2162
2163then
2164
2165echo $file
2166
2167fi
2168
2169done
2170
2171echo The files are
2172
2173echo
2174
2175for file in *
2176
2177do
2178
2179if [ -f $file ]
2180
2181then
2182
2183echo $file
2184
2185fi
2186
2187done
2188
2189
2190
2191Practical :12
2192
2193Fibonnanci number series
2194echo "enter a no of sequence to display"
2195read n
2196a=1
2197b=1
2198
2199echo "$a"
2200echo "$b"
2201i=2
2202while [ $i -lt $n ]
2203do
2204c=`expr $a + $b`
2205a=`expr $b`
2206b=`expr $c`
2207echo "$c"
2208i=`expr $i + 1`
2209done
2210
2211
2212OUTPUT:
2213
2214[pritam1@localhost ~]$ vi 12.sh
2215
2216enter a no of sequence to display
22175
221811235
2219
2220
2221
2222Practical :13
2223
2224GCD
2225
2226echo "enter two number"
2227read a b
2228r=1
2229if [ $a -gt $b ]
2230then
2231while [ $r -gt 0 ]
2232do
2233r=`expr $a % $b`
2234a=`expr $b`
2235b=`expr $r`
2236done
2237echo "gcd of a number is $a"
2238else
2239while [ $r -gt 0 ]
2240do
2241r=`expr $b % $a`
2242b=`expr $a`
2243a=`expr $r`
2244done
2245echo "gcd of a number is $b"
2246fi
2247
2248
2249OUTPUT:
2250
2251[pritam1@localhost ~]$ vi gcd.sh
2252enter two numbers
22535 25
2254gcd of a number is 5
2255
2256Practical :14
2257
2258Perfect.sh
2259clear
2260i=1
2261sum=0
2262echo "enter a number"
2263read n
2264while [ $i -lt $n ]
2265do
2266k=`expr $n % $i`
2267if [ $k -eq 0 ]
2268then
2269sum=`expr $sum + $i`
2270fi
2271i=`expr $i + 1`
2272done
2273if [ $n -eq $sum ]
2274then
2275echo "Given no is perfect"
2276else
2277echo "Given no is not perfect"
2278fi
2279
2280OUTPUT:
2281
2282[pritam1@localhost ~]$ vi perfect.sh
2283enter a number
22846
2285Given no is perfect
2286
2287
2288
2289Practical :15
2290Search pattern in to file
2291echo "enter pattern"
2292read pname
2293echo "enter file name"
2294read fname
2295if [ -z "$pname" ]
2296then
2297echo "you have not enter the path"
2298elif [ -z "$fname" ]
2299then
2300echo "you have not enterd the file name"
2301else
2302
2303echo "searching for $pname from file $fname"
2304grep "$pname" $fname
2305echo "selected resords shown above"
2306fi
2307
2308OUTPUT:
2309
2310[pritam1@localhost ~]$ vi search.sh
2311enter pattern
2312sales
2313
2314enter file name
2315emp.lst
2316
23171002|a. k. shukla| manager |sales |5000
23182003|r k p | d g m |sales |8000
2319Selected records shown above
2320
2321Practical No. 7
2322Network Commands
2323
2324
2325When it comes to networking, there is probably nothing that cannot be done with Linux. Linux is used to build all sorts of networking systems and appliances, including firewalls, routers, name servers, network-attached storage (NAS) boxes and on and on.
2326
2327ping command:
2328
2329The most basic network command is ping. The ping command sends a special network packet called an ICMP ECHO_REQUEST to a specified host. Most network devices re-ceiving this packet will reply to it, allowing the network connection to be verified.
2330
2331
2332tux@acer$ ping youtube.com
2333PING youtube.com (216.58.203.46) 56(84) bytes of data.
2334
233564 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=1 ttl=56 time=5.02 ms
2336
233764 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=2 ttl=56 time=6.67 ms
2338
233964 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=3 ttl=56 time=9.69 ms
2340
234164 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=4 ttl=56 time=7.27 ms
2342
234364 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=5 ttl=56 time=8.04 ms
2344
234564 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=6 ttl=56 time=7.92 ms
2346
234764 bytes from bom12s05-in-f14.1e100.net (216.58.203.46): icmp_seq=7 ttl=56 time=17.6 ms
2348
2349
2350
2351traceroute command:
2352
2353The traceroute program (some systems use the similar tracepath program instead lists all the “hops” network traffic takes to get from the local system to a specified host.
2354
2355tux@acer$ traceroute slashdot.org
2356
2357traceroute to slashdot.org (216.34.181.45), 30 hops max, 40 byte packets
23581 ipcop.localdomain (192.168.1.1) 1.066 ms 1.366 ms 1.720 ms
23592***
2360
23613 ge-4-13-ur01.rockville.md.bad.comcast.net (68.87.130.9) 14.622 ms 14.885 ms 15.169 ms
2362
23634 po-30-ur02.rockville.md.bad.comcast.net (68.87.129.154) 17.634 ms 17.626 ms 17.899 ms
2364
23655 po-60-ur03.rockville.md.bad.comcast.net (68.87.129.158) 15.992 ms 15.983 ms 16.256 ms
2366
23676 po-30-ar01.howardcounty.md.bad.comcast.net (68.87.136.5) 22.835 ms 14.233 ms 14.405 ms
2368
23697 po-10-ar02.whitemarsh.md.bad.comcast.net (68.87.129.34) 16.154 ms 13.600 ms 18.867 ms
23708 te-0-3-0-1-cr01.philadelphia.pa.ibone.comcast.net (68.86.90.77)
237121.951 ms 21.073 ms 21.557 ms
2372
2373ip command:
2374
2375The ip program is a multi-purpose network configuration tool that makes use of the full range networking of features available in modern Linux kernels. It replaces the earlier and now deprecated ifconfig program. With ip, we can examine a system's network interfaces and routing table.
2376
2377tux@acer$ ip a
2378
2379lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
2380
2381link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo
2382
2383valid_lft forever preferred_lft forever inet6 ::1/128 scope host
2384valid_lft forever preferred_lft forever
2385
2386enp2s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
2387link/ether 40:b0:76:ac:d3:bd brd ff:ff:ff:ff:ff:ff
2388
2389wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
2390link/ether 98:2c:bc:d3:91:6d brd ff:ff:ff:ff:ff:ff
2391
2392inet 192.168.43.160/24 brd 192.168.43.255 scope global dynamic noprefixroute wlp1s0
2393valid_lft 3171sec preferred_lft 3171sec
2394
2395inet6 fe80::829e:bef5:df35:ad94/64 scope link noprefixroute valid_lft forever preferred_lft forever
2396
2397
2398
2399
2400netstat:
2401
2402The netstat program is used to examine various network settings and statistics. Through the use of its many options, we can look at a variety of features in our network setup. Using the -ie option, we can examine the network interfaces in our system.
2403
2404tux@acer$ netstst -ie
2405Kernel Interface table
2406
2407enp2s0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 ether 40:b0:76:ac:d3:bd txqueuelen 1000 (Ethernet)
2408RX packets 0 bytes 0 (0.0 B)
2409RX errors 0 dropped 0 overruns 0 frame 0
2410TX packets 0 bytes 0 (0.0 B)
2411TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2412
2413lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0
2414inet6 ::1 prefixlen 128 scopeid 0x10<host>
2415loop txqueuelen 1000 (Local Loopback)
2416RX packets 5927 bytes 517146 (517.1 KB)
2417RX errors 0 dropped 0 overruns 0 frame 0
2418TX packets 5927 bytes 517146 (517.1 KB)
2419TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2420
2421wlp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.43.160 netmask 255.255.255.0 broadcast 192.168.43.255
2422
2423Transporting Files Over a Network
2424ftp:
2425
2426One of the true “classic” programs, ftp gets its name from the protocol it uses, the File Transfer Protocol. FTP was once the most widely used method of downloading files over the Internet. Most, if not all, web browsers support it, and you often see URIs starting with the protocol ftp://.
2427
2428Before there were web browsers, there was the ftp program. ftp is used to communi-cate with FTP servers, machines that contain files that can be uploaded and downloaded over a network.
2429
2430tux@acer$ ftp fileserver
2431Connected to fileserver.localdomain.
2432220 (vsFTPd 2.0.1)
2433Name (fileserver:me): anonymous
2434331 Please specify the password.
2435Password:
2436230 Login successful.
2437Remote system type is UNIX.
2438Using binary mode to transfer files.
2439ftp> cd pub/cd_images/Ubuntu-18.04
2440250 Directory successfully changed.
2441ftp> ls
2442200 PORT command successful. Consider using PASV.
2443150 Here comes the directory listing.
2444-rw-rw-r--
2445500
2446
2447
2448
2449733079552 Apr 25 03:53 ubuntu-
2450
245118.04-desktop-amd64.iso
2452
2453Directory send OK. ftp> lcd Desktop
2454
2455Local directory now /home/me/Desktop ftp> get ubuntu-18.04-desktop-amd64.iso
2456
2457local: ubuntu-18.04-desktop-amd64.iso remote: ubuntu-18.04-desktop-amd64.iso
2458PORT command successful. Consider using PASV.
2459
2460Opening BINARY mode data connection for ubuntu-18.04-desktop-amd64.iso (733079552 bytes).
2461File send OK.
2462
2463733079552 bytes received in 68.56 secs (10441.5 kB/s) ftp> bye
2464
2465
2466ssh:
2467
2468To address this problem, a new protocol called Secure Shell (SSH) was developed. SSH solves the two basic problems of secure communication with a remote host.
2469
2470It authenticates that the remote host is who it says it is (thus preventing so-called man-in-the-middle attacks).
2471It encrypts all of the communications between the local and remote hosts.
2472
2473
2474tux@acer$ ssh remote-sys
2475
2476The authenticity of host 'remote-sys (192.168.1.4)' can't be established.
2477RSA key fingerprint is
247841:ed:7a:df:23:19:bf:3c:a5:17:bc:61:b3:7f:d9:bb.
2479Are you sure you want to continue connecting (yes/no)?yes
2480Warning: Permanently added 'remote-sys,192.168.1.4' (RSA) to the list
2481of known hosts.
2482me@remote-sys's password:*****
2483ast login: Sat Feb 29 13:00:48 2020
2484[tux@remote-sys ~]$
2485
2486scp and sftp:
2487
2488The OpenSSH package also includes two programs that can make use of an SSH-en-crypted tunnel to copy files across the network. The first, scp (secure copy) is used much like the familiar cp program to copy files. The most notable difference is that the source or destination pathnames may be preceded with the name of a remote host, fol-lowed by a colon character.
2489
2490tux@acer$ scp remote-sys:document.txt .
2491me@remote-sys's password:
2492
2493document.txt 100% 5581 100% 5581 5.5KB/s 00:00 tux@acer$
2494
2495Practical No. 8
2496
2497Advances filters SED
2498
2499
2500Sed command or Stream Editor is very powerful utility offered by Linux /Unix systems.It is mainly used for text substitution,find and replace but it can also perform other text manipulations like insertion,deletion,search etc. With SED, we can edit complete files without actuallly having to open it.
2501
2502Sed also supports to use regular expressions, which makes sed and even more powerful test manipulation tool.
2503
2504i. Display partial text of a file:
2505
2506With sed, we can view only some part of a file rather than seeing whole file. To see some lines of the file, use the following command.
2507
2508
2509tux@acer$ sed -n 13,29p merge.txt
2510Clear Linux
2511Smartphone companies are:
2512Samsung
2513Xiaomi
2514Realme
2515Samsung
2516Huawei
2517Honor
2518
2519
2520Display all expect some lines
2521
2522To display all content of a file except for some portion,use the following command.
2523
2524
2525
2526tux@acer$ sed 13,29p merge.txt
2527Linux is an advance operating system,
2528It is developed by Linus Torvalds
2529The top Linux distribution on disro-watch are:
2530MX Linux
2531Manjaro
2532Ubuntu
2533Debain
2534Elementary OS
2535Pop OS
2536Solous
2537ZorinOS
2538Deepin
2539Clear Linux
2540Clear Linux
2541
2542iii.Display every 3rd line starting with Nth line
2543Do display content of every 3rd line with line number 2 or any other line use the following command.
2544
2545tux@acer$ sed -n '2~3p' merge.txt
2546It is developed by Linus Torvalds
2547Manjaro
2548Elementary OS
2549ZorinOS
2550Smartphone companies are:
2551Realme
2552Honor
2553
2554iv.Search and Replacing a string using sed
2555To search and replace a string from the file, we will use the following example.
2556
2557
2558tux@acer$ sed ’s/honor/phantom/’ merge.sort sed: -e expression #1, char 1: unknown command: ` '
2559
2560
2561This will only replace the character on first occurenconly.
2562
2563
2564v. Running multiple sed commands
2565
2566If we need to perform multiple sed expressions, we can use option ‘e’ to chain the sed command.
2567
2568
2569tux@acer$ sed -e 's/Linux/os/g' -e 's/Linux/os/' merge.txt os is an advance operating system, It is developed by Linus Torvalds
2570The top os distribution on disro-watch are:
2571MX os
2572Manjaro
2573Ubuntu
2574Debain
2575Elementary OS
2576Pop OS
2577Solous
2578ZorinOS
2579Deepin
2580Clear os
2581Smartphone companies are:
2582Samsung
2583Xiaomi
2584Realme
2585Samsung
2586Huawei
2587Honor
2588
2589Making a backup copy before editing file:
2590
2591To create backup copy of a file before we edit, use option ’-i.bak’, This will create backup copy of the file with extension.bak. You can also use other extension if you like.
2592
2593tux@acer$ sed -i.bak -e 's/os/Linux/g' merge.txt
2594Documents merge.txt.bak plaground t wingpanel-indicator-sys-monitor
2595Downloads Music Public Templates
2596merge.txt Pictures SoftMaker Videos
2597
2598Delete a file line starting with & ending with a pattern:
2599To delete a line stsrting with a particular string and ending with another use
2600
2601This will delete the line with ‘Linux’ on starts & it can have any number of words in between, ‘*’ defines that part.
2602
2603
2604tux@acer$ sed -e 's/^Linux.*stops$//g' merge.txt Linux is an advance operating system, It is developed by Linus Torvalds
2605The top Linux distribution on disro-watch are:
2606MX Linux
2607Manjaro
2608Ubuntu
2609Debain
2610Elementary OS
2611Pop OS
2612Solous
2613ZorinOS
2614Deepin
2615Clear Linux
2616Smartphone companies are:
2617Samsung
2618Xiaomi
2619Realme
2620Samsung
2621Huawei
2622Honor
2623
2624
2625viii. Appending lines:
2626
2627To add some content before every line with sed and regex, use so now every line will have ’testing sed’ before it
2628
2629
2630tux@acer$ sed -e 's/.*/testing sed &/' merge.txt testing sed Linux is an advance operating system, testing sed It is developed by Linus Torvalds
2631testing sed The top Linux distribution on disro-watch are:
2632testing sed MX Linux
2633testing sed Ubuntu
2634testing sed Elementary OS
2635testing sed Pop OS
2636testing sed debain
2637
2638Removing all commented lines and empty lines:
2639To remove all commented lines with # and empty lines.
2640
2641
2642tux@acer$cat sort
2643Linux
2644
2645windows
2646#Darwin
2647
2648macos
2649tux@acer$ sed -e 's/#.*//;/^$/d' sort
2650Linux
2651windows
2652Macos
2653
2654x. Get list of all usernames form /etc/passwd
2655
2656To get the list of all usernames from /etc/passwd file, use a complete list all usernames will be generated on screen as output.
2657
2658
2659tux@acer$ sed 's/\([^:]*\).*/\1/' /etc/passwd
2660root
2661daemon
2662bin
2663sys
2664sync
2665games
2666man
2667lp
2668mail
2669news
2670uucp
2671proxy
2672www-data
2673backup
2674list
2675irc
2676gnats
2677nobody
2678systemd-network
2679systemd-resolve
2680syslog
2681messagebus
2682_apt
2683uuidd
2684avahi-autoipd
2685usbmux
2686usbmux
2687dnsmasq
2688rtkit
2689lightdm
2690
2691Practical No. 9
2692
2693Installation and Removing Software
2694
2695
2696Software in linux are installed in the form of package management.Every Linux Distribution has its own package manager were debain and rpm packages are most widely used package manager on linux distribution.
2697The softwares are packed with extensions (.deb) and (.rpm)
2698
2699Debain (.deb):
2700Ubuntu, Linux Mint, Pop OS, Elementary OS
2701Red hat Style (.rpm):
2702Fedora, CentOS, OpenSUSE
2703
2704Repositories:
2705
2706While some software projects choose to perform their own packaging and distribution, most packages today are created by the distribution vendors and interested third parties. Packages are made available to the users of a distribution in central repositories that may contain many thousands of packages, each specially built and maintained for the distribu-tion.
2707
2708
2709Packages are install and removed in the following ways:
2710
2711
2712tux@acer$ sudo apt install g++
2713Reading package lists... Done
2714Building dependency tree
2715Reading state information... Done
2716g++ is already the newest version (4:7.4.0-1ubuntu2.3).
2717g++ set to manually installed.
2718The following packages were automatically installed and are no longer required:
2719fwupdate gir1.2-vte-2.91 libfwup1 libido3-0.1-0
2720
2721tux@acer$ sudo apt remove g++
2722Reading package lists... Done
2723Building dependency tree
2724Reading state information... Done
2725The following packages were automatically installed and are no longer required:
2726fwupdate g++-7 gir1.2-vte-2.91 libfwup1 libido3-0.1-0
2727Use 'sudo apt autoremove' to remove them.
2728The following packages will be REMOVED:
2729build-essential g++
27300 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
2731After this operation, 36.9 kB disk space will be freed.
2732
2733
2734
2735One also install software from ppa:
2736
2737ppa: These are called repositories maintained by developers which are not available in the distribution.
2738
2739tux@acer$ sudo add-apt-repository ppa:software/software-name
2740
2741This connects the software manager to the repository and all the softwares can be downloaded.