forked from bitvijays/bitvijays.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLFF-ESS-P0B-LinuxEssentials.html
1570 lines (1540 loc) · 87.7 KB
/
LFF-ESS-P0B-LinuxEssentials.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-92365403-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<title>The Essentials : Linux Basics — tech.bitvijays.com</title>
<link rel="stylesheet" href="_static/bizstyle.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/disqus.js"></script>
<script type="text/javascript" src="_static/bizstyle.js"></script>
<link rel="search" title="Search" href="search.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!--[if lt IE 9]>
<script type="text/javascript" src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="nav-item nav-item-0"><a href="index.html">tech.bitvijays.com</a> »</li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">The Essentials : Linux Basics</a><ul>
<li><a class="reference internal" href="#vi-powerful-editor">Vi : Powerful Editor</a><ul>
<li><a class="reference internal" href="#open-file-with-vi">Open file with vi</a></li>
<li><a class="reference internal" href="#vi-modes">Vi Modes</a></li>
<li><a class="reference internal" href="#vi-configuration-files">Vi Configuration Files</a><ul>
<li><a class="reference internal" href="#vimrc">.vimrc</a></li>
<li><a class="reference internal" href="#viminfo">.viminfo</a></li>
</ul>
</li>
<li><a class="reference internal" href="#replace-text-in-vi">Replace text in Vi</a><ul>
<li><a class="reference internal" href="#other-info">Other Info</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#bash-configuration-files-for-debian-ubuntu-based-systems">Bash configuration files - For Debian/Ubuntu based Systems</a><ul>
<li><a class="reference internal" href="#important-files">Important Files</a></li>
<li><a class="reference internal" href="#important-variables">Important variables</a></li>
</ul>
</li>
<li><a class="reference internal" href="#system-administration">System Administration</a><ul>
<li><a class="reference internal" href="#updating-debian-linux-system">Updating Debian Linux System</a><ul>
<li><a class="reference internal" href="#using-apt-get">Using apt-get</a></li>
<li><a class="reference internal" href="#using-debian-package-manager-dpkg">Using Debian Package Manager dpkg</a></li>
</ul>
</li>
<li><a class="reference internal" href="#adding-deleting-modifying-users-groups">Adding/Deleting/Modifying Users/Groups</a></li>
<li><a class="reference internal" href="#changing-group-owner-permission">Changing Group/Owner/Permission</a></li>
<li><a class="reference internal" href="#mounting-unmounting">Mounting/ Unmounting</a><ul>
<li><a class="reference internal" href="#mounting-windows-share-on-linux">Mounting Windows share on Linux</a></li>
</ul>
</li>
<li><a class="reference internal" href="#linux-directories">Linux Directories</a></li>
<li><a class="reference internal" href="#runlevels-and-kernel-configurations">Runlevels and Kernel Configurations</a><ul>
<li><a class="reference internal" href="#linux-boot-process">Linux Boot Process</a></li>
<li><a class="reference internal" href="#sysctl-configure-kernel-parameters">Sysctl - configure kernel parameters</a></li>
<li><a class="reference internal" href="#kernel-modules">Kernel Modules</a></li>
<li><a class="reference internal" href="#manage-runlevels">Manage Runlevels</a></li>
</ul>
</li>
<li><a class="reference internal" href="#screen-multiplexer">Screen Multiplexer</a><ul>
<li><a class="reference internal" href="#tmux">tmux</a></li>
</ul>
</li>
<li><a class="reference internal" href="#file-system-superblock">File System Superblock</a><ul>
<li><a class="reference internal" href="#view-superblock-information">View superblock information</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#programming">Programming</a><ul>
<li><a class="reference internal" href="#git">GIT</a></li>
<li><a class="reference internal" href="#cc-gnu-compile-collection">cc - GNU Compile Collection</a></li>
<li><a class="reference internal" href="#gdb-gnu-debugger">GDB: GNU debugger</a></li>
</ul>
</li>
<li><a class="reference internal" href="#gathering-information">Gathering Information</a><ul>
<li><a class="reference internal" href="#from-files">From Files</a></li>
<li><a class="reference internal" href="#from-commands">From Commands</a></li>
</ul>
</li>
<li><a class="reference internal" href="#useful-utilities-commands">Useful Utilities/ Commands</a><ul>
<li><a class="reference internal" href="#grep-global-regular-expression-print">Grep - Global Regular Expression Print</a><ul>
<li><a class="reference internal" href="#syntax">Syntax</a></li>
<li><a class="reference internal" href="#using-regular-expressions">Using regular expressions</a></li>
<li><a class="reference internal" href="#search-a-specific-string">Search a specific string</a></li>
<li><a class="reference internal" href="#line-and-word-anchors">Line and word anchors</a></li>
<li><a class="reference internal" href="#shell-expansions-input-to-grep">Shell expansions - input to Grep</a></li>
</ul>
</li>
<li><a class="reference internal" href="#copy-copy-files-and-directories">Copy - Copy files and directories</a></li>
<li><a class="reference internal" href="#cut-remove-sections-from-each-line-of-files">cut - remove sections from each line of files</a></li>
<li><a class="reference internal" href="#pipes">Pipes</a></li>
<li><a class="reference internal" href="#tar-archiving-utility">tar - Archiving utility</a></li>
<li><a class="reference internal" href="#find-searching-files">find - Searching files</a><ul>
<li><a class="reference internal" href="#delete-empty-file-and-directories">Delete empty file and directories</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-commands">Other commands</a></li>
<li><a class="reference internal" href="#special-characters">Special Characters</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bash">Bash</a><ul>
<li><a class="reference internal" href="#equality-tests">Equality Tests</a></li>
<li><a class="reference internal" href="#list-of-equality-tests">List of equality tests</a><ul>
<li><a class="reference internal" href="#checks-equality-between-numbers">Checks equality between numbers</a></li>
<li><a class="reference internal" href="#checks-equality-between-strings">Checks equality between strings</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bash-command-substitution">Bash Command Substitution</a></li>
<li><a class="reference internal" href="#bash-case-modification">Bash Case Modification</a><ul>
<li><a class="reference internal" href="#example-parameter">Example: Parameter ^</a></li>
<li><a class="reference internal" href="#id1">Example: Parameter ,</a></li>
<li><a class="reference internal" href="#id2">Example: Parameter ~</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bash-programming">Bash Programming</a><ul>
<li><a class="reference internal" href="#bash-for-loop">Bash For Loop</a></li>
<li><a class="reference internal" href="#bash-if-statement">Bash If Statement</a></li>
<li><a class="reference internal" href="#bash-loop-thru-array-of-strings">Bash loop thru array of strings</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#virtualization">Virtualization?</a><ul>
<li><a class="reference internal" href="#docker">Docker</a></li>
</ul>
</li>
<li><a class="reference internal" href="#important-definitions">Important Definitions</a><ul>
<li><a class="reference internal" href="#information">Information</a><ul>
<li><a class="reference internal" href="#confidentiality-integrity-availability">Confidentiality, Integrity, Availability</a></li>
<li><a class="reference internal" href="#non-repudiation">Non-repudiation</a></li>
</ul>
</li>
<li><a class="reference internal" href="#difference-between-su-and-sudo">Difference between su and sudo</a><ul>
<li><a class="reference internal" href="#su">su</a></li>
<li><a class="reference internal" href="#su-c">su -c</a></li>
<li><a class="reference internal" href="#sudo">sudo</a></li>
</ul>
</li>
<li><a class="reference internal" href="#important-file-formats">Important File Formats</a><ul>
<li><a class="reference internal" href="#etc-passwd">/etc/passwd</a></li>
<li><a class="reference internal" href="#etc-shadow">/etc/shadow</a></li>
<li><a class="reference internal" href="#etc-group">/etc/group</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#tips-and-tricks">Tips and tricks</a><ul>
<li><a class="reference internal" href="#apt-get-error">Apt-get error?</a></li>
<li><a class="reference internal" href="#track-etc-directory">Track /etc directory</a></li>
<li><a class="reference internal" href="#ls-showing-full-path">ls showing full path</a></li>
<li><a class="reference internal" href="#keyboard-shortcuts">Keyboard shortcuts</a><ul>
<li><a class="reference internal" href="#moving">Moving</a></li>
<li><a class="reference internal" href="#erasing">Erasing</a></li>
<li><a class="reference internal" href="#window">Window</a></li>
</ul>
</li>
<li><a class="reference internal" href="#searching-history">Searching History</a></li>
<li><a class="reference internal" href="#awk-converting-to-normal-output-to-csv">Awk converting to normal output to csv</a></li>
<li><a class="reference internal" href="#finding-most-open-ports-in-nmap-scan">Finding most open ports in nmap scan</a></li>
<li><a class="reference internal" href="#cat">cat</a></li>
</ul>
</li>
<li><a class="reference internal" href="#practice">Practice</a><ul>
<li><a class="reference internal" href="#programming-debugging-and-git">Programming, Debugging and Git</a><ul>
<li><a class="reference internal" href="#task-1-git">Task 1 : Git</a></li>
<li><a class="reference internal" href="#task-2-vi-gcc-make">Task 2 : Vi/ gcc/ make</a></li>
<li><a class="reference internal" href="#task-3-gdb">Task 3 : gdb</a></li>
</ul>
</li>
<li><a class="reference internal" href="#id3">System administration</a><ul>
<li><a class="reference internal" href="#task-1-login-logout-messages">Task 1 : Login/ Logout Messages</a></li>
<li><a class="reference internal" href="#task-2-gather-information">Task 2 : Gather Information</a></li>
<li><a class="reference internal" href="#task-3-add-user">Task 3 : Add User</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bash-scripting">Bash Scripting</a><ul>
<li><a class="reference internal" href="#task-1-gather-ip-addresses">Task 1 : Gather IP Addresses</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#interesting-stuff">Interesting Stuff</a></li>
<li><a class="reference internal" href="#changelog">Changelog</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/LFF-ESS-P0B-LinuxEssentials.rst.txt"
rel="nofollow">Show Source</a></li>
<li><a href="https://github.com/bitvijays/bitvijays.github.io-sphinx/blob/master/docs/LFF-ESS-P0B-LinuxEssentials.rst"
rel="nofollow">Show on GitHub</a></li>
<li><a href="https://github.com/bitvijays/bitvijays.github.io-sphinx/edit/master/docs/LFF-ESS-P0B-LinuxEssentials.rst"
rel="nofollow">Edit on GitHub</a></li>
</ul>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="the-essentials-linux-basics">
<h1>The Essentials : Linux Basics<a class="headerlink" href="#the-essentials-linux-basics" title="Permalink to this headline">¶</a></h1>
<p>This post lists essential commands and concepts which would be helpful to a Linux user. We would cover tools required for programming (Vi, git), system administration (Bash configuration files, Updating Debian Linux System, Adding/ Deleting/ Modifying Users/ Groups, Changing Group/ Owner/ Permission, Mounting/ Unmounting, Linux Directories, Runlevels and Kernel Configurations). Also, provide some useful tips, tricks and TODO which would help you learn and practice.</p>
<div class="section" id="vi-powerful-editor">
<h2>Vi : Powerful Editor<a class="headerlink" href="#vi-powerful-editor" title="Permalink to this headline">¶</a></h2>
<div class="section" id="open-file-with-vi">
<h3>Open file with vi<a class="headerlink" href="#open-file-with-vi" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>vi <filename> - Open a file to edit in Vi editor.
</pre></div>
</div>
</div>
<div class="section" id="vi-modes">
<h3>Vi Modes<a class="headerlink" href="#vi-modes" title="Permalink to this headline">¶</a></h3>
<p>Two modes - Command and Insert Mode. All commands below are in command mode.</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>h,l,j,k - Move left, right, down, up
w - Move to the start of the next word.
e - Move to the end of the word.
b - Move to the beginning of the word.
3w - 3w is similar to pressing w 3 times, moves to the start of the third word.
30i-'EscKey' - 30<insert>-<EscapeKey> : Inserts 30 - at once.
f - find and move to the next (or previous) occurrence of a character. fo find next o.
3fo - find third occurrence of o
% - In text that is structured with parentheses or brackets, ( or { or [, use % to jump to the matching parenthesis or bracket.
0 (Zero) - Reach beginning of the line
$ - Reach end of the line.
* - Find the next occurrence of the word under cursor
# - Find the previous occurrence of the word under cursor
gg - Reach beginning of the file
G - Reach end of the file
30G - Reach the 30th line in the file
/<text> - Search for the text. Utilize n, N for next and previous occurrences.
o - Insert a new line below the cursor
O - Insert a new line above the cursor
x - Delete the character
r - replace the character with the next key pressed.
dw - Delete the current word.
dd - Delete the current line.
d$ - Delete the text from where your cursor is to the end of the line.
dnd - Delete n lines.
. - Repeat the last command
:q - Quit.
:wq - Save and close.
:syntax on - Turn on Syntax highlighting for C programming and other languages.
:history - Shows the history of the commands executed
:set number - Turn on the line numbers.
:set nonumber - Turn off the line numbers.
:set spell spelllang=en_us - Turn spell checking on with spell language as "en_us"
:set nospell - Turn spell checking off
:set list - If 'list' is on, whitespace characters are made visible. The default displays "^I" for each tab, and "$" at each EOL (end of line, so trailing whitespace can be seen)
:u - Undo one change.
:!{cmd} - Run the command without exiting the vim. {cmd} can be whoami without external brackets.
z= - If the cursor is on the word ( which is highlighted with spell check), Vim will suggest a list of alternatives that it thinks may be correct.
yy - Yank or copy current line.
y$, yny - Similar to delete lines.
p - Paste the line in the buffer in to text after the currentline.
:%!xxd - to turn it into a hexeditor.
:%!xxd -r - to go back to normal mode (from hexedit mode)
</pre></div>
</div>
</div>
<div class="section" id="vi-configuration-files">
<h3>Vi Configuration Files<a class="headerlink" href="#vi-configuration-files" title="Permalink to this headline">¶</a></h3>
<p>Two configurations files which are important:</p>
<div class="section" id="vimrc">
<h4>.vimrc<a class="headerlink" href="#vimrc" title="Permalink to this headline">¶</a></h4>
<p>Contains optional runtime configuration settings to initialize Vim when it starts. Example: If you want Vim to have syntax on and line numbers on, whenever you open vi, enter syntax on and set number in this file.</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>##Sample contents of .vimrc
syntax on
set number
</pre></div>
</div>
<p>A good details about various options which can be set in vimrc can be found at <a class="reference external" href="https://dougblack.io/words/a-good-vimrc.html">A Good Vimrc</a></p>
</div>
<div class="section" id="viminfo">
<h4>.viminfo<a class="headerlink" href="#viminfo" title="Permalink to this headline">¶</a></h4>
<p>Viminfo file stores command-line, search string, input-line history and other stuff. Useful if you want to find out what user has been doing in vi.</p>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">Both files are present in user home directory.</p>
</div>
</div>
</div>
<div class="section" id="replace-text-in-vi">
<h3>Replace text in Vi<a class="headerlink" href="#replace-text-in-vi" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>:s/test/learn - would replace test to learn in current line but only first instance.
:s/test/learn/g - would replace test to learn in current line all the instance.
:s/test/learn/gi - would replace test (all cases) to learn in current line all the instance.
:%s/test/learn/gi - would replace test to learn in the file (all lines)
</pre></div>
</div>
<div class="section" id="other-info">
<h4>Other Info<a class="headerlink" href="#other-info" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li><a class="reference external" href="https://vimawesome.com/">Vim Awesome</a> provides Awesome VIM plugins from across the universe. Few good one are</li>
</ul>
<blockquote>
<div><ul class="simple">
<li>The NERD tree : Tree explorer plugin for vim</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>:NERDTreeToggle : Toggle the NERD Tree
:NERDTreeFocus : Set the focus to NerdTree
</pre></div>
</div>
</div></blockquote>
<ul class="simple">
<li>Syntastic : Syntax checking hacks for vim</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>SyntasticCheck - Check for the possible syntax issues
</pre></div>
</div>
</div></blockquote>
<ul class="simple">
<li>Youcompleteme : Code-completion engine for Vim</li>
<li><a class="reference external" href="https://github.com/junegunn/fzf.vim">fzf</a> : Bundle of fzf-based commands and mappings</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>GFiles [OPTS] : Git files (git ls-files)
GFiles? : Git files (git status)
History : v:oldfiles and open buffers
History: : Command history
History/ : Search history
Snippets : Snippets (UltiSnips)
Commits : Git commits (requires fugitive.vim)
BCommits : Git commits for the current buffer
Commands : Commands
</pre></div>
</div>
</div></blockquote>
<ul>
<li><p class="first"><a class="reference external" href="https://github.com/SirVer/ultisnips">UltiSnips</a> The ultimate snippet solution for Vim</p>
</li>
<li><p class="first"><a class="reference external" href="https://github.com/godlygeek/tabular">Tabular</a> : Vim script for text filtering and alignment</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>Select the text which you want to align in the visual mode (Do make sure that cursor is also at the same position as visual)
:Tabularize /{pattern to be aligned}
</pre></div>
</div>
</li>
</ul>
</div></blockquote>
<ul class="simple">
<li>Utilize <a class="reference external" href="http://github.com/VundleVim/Vundle.Vim">Vundle, the plug-in manager for Vim</a></li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>:PluginList - lists configured plugins
:PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
:PluginSearch foo - searches for foo; append `!` to refresh local cache
:PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
</pre></div>
</div>
</div></blockquote>
</div>
</div>
</div>
<div class="section" id="bash-configuration-files-for-debian-ubuntu-based-systems">
<h2>Bash configuration files - For Debian/Ubuntu based Systems<a class="headerlink" href="#bash-configuration-files-for-debian-ubuntu-based-systems" title="Permalink to this headline">¶</a></h2>
<div class="section" id="important-files">
<h3>Important Files<a class="headerlink" href="#important-files" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>~/.bash_profile - Stores user environment variables.</li>
<li>~/.bash_history - contains all the history of the commands.</li>
<li>~/.bash_logout - contains the command which are executed when bash is exited.</li>
<li>~/.bashrc - setting of variables for bash.</li>
<li>/etc/profile - Global system configuration for bash which controls the environmental variables and programs that are to be run when bash is executed. Setting of PATH variable and PS1.</li>
<li>/etc/bashrc - Global system configuration for bash which controls the aliases and functions to be run when bash is executed</li>
</ul>
</div>
<div class="section" id="important-variables">
<h3>Important variables<a class="headerlink" href="#important-variables" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>HISTSIZE - Controls the number of commands to remember in the history command. The default value is 500.</li>
<li>HISTFILE - Defines the file in which all commands will be logged to. Normally the value for this variable is set to ~/.bash_history. This means that whatever you type in bash will be stored into the value of HISTFILE. It is advisable to leave it undefined, or pipe the output to /dev/null (For privacy reasons).</li>
<li>HISTFILESIZE - Defines the maximum number of commands in ~/.bash_history.</li>
</ul>
</div>
</div>
<div class="section" id="system-administration">
<h2>System Administration<a class="headerlink" href="#system-administration" title="Permalink to this headline">¶</a></h2>
<div class="section" id="updating-debian-linux-system">
<h3>Updating Debian Linux System<a class="headerlink" href="#updating-debian-linux-system" title="Permalink to this headline">¶</a></h3>
<div class="section" id="using-apt-get">
<h4>Using apt-get<a class="headerlink" href="#using-apt-get" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>apt-get update - Sync with Repositories.
apt-get upgrade - Upgrade installed packages.
apt-get dist-upgrade - Upgrade distribution packages.
apt-get install "Package Name" - Install the package.
apt-get remove "Package Name" - Uninstall the package.
apt-get purge "Package Name" - Removes the package as well as the configuration files.
apt-cache show "Package name" - Shows what package is used for.
apt-cache search "Keywords" - Search package name based on keywords.
</pre></div>
</div>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">As mostly, updating takes time, you can club all the commands like “apt-get update && apt-get upgrade && apt-get dist-upgrade && poweroff”. poweroff would shutdown the system after everything is updated.</p>
</div>
</div>
<div class="section" id="using-debian-package-manager-dpkg">
<h4>Using Debian Package Manager dpkg<a class="headerlink" href="#using-debian-package-manager-dpkg" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>dpkg -i <Package>.deb - Install package.
dpkg -r <Package> - Removes everything except configuration files.
dpkg -P <Package> - Removes configurations files too.
dpkg -l - Shows the list of all installed packages.
dpkg -L "Package name" - Shows a list of files installed by specific packages.
dpkg -S "File path" - Shows the package to which a file belong to.
</pre></div>
</div>
</div>
</div>
<div class="section" id="adding-deleting-modifying-users-groups">
<h3>Adding/Deleting/Modifying Users/Groups<a class="headerlink" href="#adding-deleting-modifying-users-groups" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>adduser <username> : Add a user.
--gecos GECOS : adduser won't ask for finger information.
--system : Create a system user.
--quiet : Suppress informational messages, only show warnings and errors.
--disabled-login : Do not run passwd to set the password.
deluser <username> : Delete a user.
--remove-home : Remove the home directory of the user and its mailspool.
--remove-all-files: Remove all files from the system owned by this user.
--backup : Backup all files contained in the userhome and the mailspool-file to a file named /$user.tar.bz2 or /$user.tar.gz.
usermod : Modify a user account.
-e EXPIREDATE : The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD.
-L, --lock : Lock a user's password.
-U, --unlock : Unlock a user's password
groupadd : Create a new group.
groupdel : Delete a group.
groupmod : Modify a group definition on the system.
</pre></div>
</div>
</div>
<div class="section" id="changing-group-owner-permission">
<h3>Changing Group/Owner/Permission<a class="headerlink" href="#changing-group-owner-permission" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>chown : Change file owner and group.
-reference=RFILE : use RFILE's owner and group rather than specifying OWNER:GROUP values.
-R, --recursive : operate on files and directories recursively.
chmod : change file mode bits.
chgrp : change group ownership.
SUID bit : SetUID bit specifies that an executable should run as its owner instead of the user executing it.
: SUID is mostly commonly used to run an executable as root, allowing users to perform tasks such as changing their passwords.
: If there is a flaw in a SUID root executable, you can run arbitrary code as root.
</pre></div>
</div>
</div>
<div class="section" id="mounting-unmounting">
<h3>Mounting/ Unmounting<a class="headerlink" href="#mounting-unmounting" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>mount <device> <dir> : Mount a filesystem.
-r, --read-only : Mount the filesystem read-only.
unmount {dir|device} : Unmount file systems.
</pre></div>
</div>
<div class="section" id="mounting-windows-share-on-linux">
<h4>Mounting Windows share on Linux<a class="headerlink" href="#mounting-windows-share-on-linux" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>mount -t cifs -o username=<share user>,password=<share password>,domain=example.com //WIN_PC_IP/<share name> /mnt
</pre></div>
</div>
</div>
</div>
<div class="section" id="linux-directories">
<h3>Linux Directories<a class="headerlink" href="#linux-directories" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>/home : users home directories.
/etc : system-wide configuration files.
/bin, /usr/bin, /usr/local/bin : directories with executable files.
/lib, /usr/lib, /usr/local/lib : shared libraries needed to upport the applications.
/sbin, /usr/sbin, /usr/local/sbin : directories with executables supposed to be run by the Superuser.
/tmp, /var/tmp : temporary directories, watch out as /tmp is, by default, cleaned out on each reboot.
/usr/share/doc, /usr/share/man : complete system documentation.
/dev : system device files. In Unix, hardware devices are represented as files.
/proc : "virtual" directory containing files through which you can query or tune Linux kernel settings.
</pre></div>
</div>
</div>
<div class="section" id="runlevels-and-kernel-configurations">
<h3>Runlevels and Kernel Configurations<a class="headerlink" href="#runlevels-and-kernel-configurations" title="Permalink to this headline">¶</a></h3>
<div class="section" id="linux-boot-process">
<h4>Linux Boot Process<a class="headerlink" href="#linux-boot-process" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>1. BIOS start the boot loader.
2. Boot loader loads the kernel into memory.
3. The Kernel mounts disks/partitions and starts the init daemon.
4. The init daemon starts services based on the runlevel.
</pre></div>
</div>
<p>Linux has six runlevels 0-6. Scripts are contained in /etc/rc[0-6,S].d/. Each folder contains the scripts which are followed by either K or S. If the first letter is K that script is not executed. If S, that script is executed. /etc/inittab contains the default run level.</p>
<table border="1" class="docutils">
<colgroup>
<col width="3%" />
<col width="41%" />
<col width="56%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">ID</th>
<th class="head">Name</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>0</td>
<td>Halt</td>
<td>Shuts down the system.</td>
</tr>
<tr class="row-odd"><td>1</td>
<td>Single-user Mode</td>
<td>Mode for administrative tasks.</td>
</tr>
<tr class="row-even"><td>2</td>
<td>Multi-user Mode</td>
<td>Does not configure network interfaces and does not export networks services</td>
</tr>
<tr class="row-odd"><td>3</td>
<td>Multi-user Mode with Networking</td>
<td>Starts the system normally.</td>
</tr>
<tr class="row-even"><td>4</td>
<td>Not used/User-definable</td>
<td>For special purposes.</td>
</tr>
<tr class="row-odd"><td>5</td>
<td>Start system normally with display manager (with GUI).</td>
<td>Same as runlevel 3 + display manager</td>
</tr>
<tr class="row-even"><td>6</td>
<td>Reboot</td>
<td>Reboot the system</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="sysctl-configure-kernel-parameters">
<h4>Sysctl - configure kernel parameters<a class="headerlink" href="#sysctl-configure-kernel-parameters" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>/etc/sysctl.conf : Contains the variables for kernel parameters.
sysctl -a : Display all the kernel parameters
sysctl -w <kernel parameter> : Change a sysctl setting.
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">To make permanent changes to the kernel, edit the /etc/sysctl.conf file.</p>
</div>
</div>
<div class="section" id="kernel-modules">
<h4>Kernel Modules<a class="headerlink" href="#kernel-modules" title="Permalink to this headline">¶</a></h4>
<p>Kernel modules are contained in /lib/modules/$(uname -r)/</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>lsmod : list all loaded modules
modprobe : load kernel modules
lspci : list all pci devices
lsusb : list all usb devices
hal-device : list all the Hardware Abstraction layer devices
</pre></div>
</div>
</div>
<div class="section" id="manage-runlevels">
<h4>Manage Runlevels<a class="headerlink" href="#manage-runlevels" title="Permalink to this headline">¶</a></h4>
<p>Debian GNU provides a convenient tool to manage runlevels (to control when services are started and shut down);</p>
<ul class="simple">
<li>update-rc.d and there are two commonly used invocation methods:</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>update-rc.d -f <service name> remove : Disabling a service.
update-rc.d <service name> defaults : Insert links using defaults, start in runlevel 2-5 and stop in runlevels 0,1 and 6.
</pre></div>
</div>
</div></blockquote>
<ul class="simple">
<li>Systemctl : Control the systemd system and service manager. systemctl may be used to introspect and control the state of the “systemd” system and service manager.</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>systemctl : Present a detailed output about the different services running.
e.g.
systemctl status <service_name> - Status of the service.
systemctl start <service_name> - Start the service
</pre></div>
</div>
</div></blockquote>
</div>
</div>
<div class="section" id="screen-multiplexer">
<h3>Screen Multiplexer<a class="headerlink" href="#screen-multiplexer" title="Permalink to this headline">¶</a></h3>
<div class="section" id="tmux">
<h4>tmux<a class="headerlink" href="#tmux" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>tmux new -s myname : start new with session name:
tmux list-sessions : show sessions
tmux ls : show sessions
tmux list-windows : show windows
tmux attach-session -t myname : Attach to session named "myname"
tmux a -t myname : Attach to session named "myname"
(Prefix) + d : detach
</pre></div>
</div>
<p><strong>Windows (Tabs)</strong></p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>(Prefix Key) +
c create window
w list windows
n next window
p previous window
f find window
, name window
& kill window
" split pane horizontally.
% split pane vertically.
arrow key — switch pane.
Hold Ctrl+b, don’t release it and hold one of the arrow keys — resize pane.
</pre></div>
</div>
<p><strong>tmux.conf</strong></p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span># Enable mouse mode (tmux 2.1 and above)
set -g mouse on
</pre></div>
</div>
<p><strong>Reloading tmux config</strong></p>
<p>If we have made changes to tmux configuration file in the ~/.tmux.conf file, it shouldn’t be necessary to start the server up again from scratch with kill-server. Instead, we can prompt the current tmux session to reload the configuration with the source-file command.
This can be done either from within tmux, by pressing Ctrl+B or Prefix key and then : to bring up a command prompt, and typing:</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>:source-file ~/.tmux.conf
</pre></div>
</div>
<p>Or simply from a shell:</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>$ tmux source-file ~/.tmux.conf
</pre></div>
</div>
<p>This should apply your changes to the running tmux server without affecting the sessions or windows within them.</p>
<p><strong>Copy Paste</strong></p>
<p>For copying, Press the Shift key; i.e., Shift-MouseHighlight properly selects text and - still holding down the shift key</p>
<ul class="simple">
<li>we can right-click and get the standard bash context menu with Copy, Paste, etc.</li>
<li>or Ctrl-Shift-C and Ctrl-Shift-V does work to copy and paste text.</li>
</ul>
</div>
</div>
<div class="section" id="file-system-superblock">
<h3>File System Superblock<a class="headerlink" href="#file-system-superblock" title="Permalink to this headline">¶</a></h3>
<p>Superblock is stores the metadata of the file system such as</p>
<ul class="simple">
<li>Blocks in the file system</li>
<li>No of free blocks in the file system</li>
<li>Inodes per block group</li>
<li>Blocks per block group</li>
<li>No of times the file system was mounted since last fsck.</li>
<li>Mount time</li>
<li>UUID of the file system</li>
<li>Write time</li>
<li>File System State (ie: was it cleanly unounted, errors detected etc)</li>
<li>The file system type etc(ie: whether its ext2,3 or 4).</li>
<li>The operating system in which the file system was formatted</li>
</ul>
<div class="section" id="view-superblock-information">
<h4>View superblock information<a class="headerlink" href="#view-superblock-information" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>dumpe2fs -h /dev/sda4
dumpe2fs 1.42.9 (4-Feb-2014)
Filesystem volume name: cloudimg-rootfs
Last mounted on: /
Filesystem UUID: f75f9307-27dc-xxxx-87b7-xxxxxxxxx
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
.....sniped.......
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="programming">
<h2>Programming<a class="headerlink" href="#programming" title="Permalink to this headline">¶</a></h2>
<div class="section" id="git">
<h3>GIT<a class="headerlink" href="#git" title="Permalink to this headline">¶</a></h3>
<p>Version Control System, really useful for tracking your changes.</p>
<div class="admonition-todo admonition" id="index-0">
<p class="first admonition-title">Todo</p>
<p class="last"><a class="reference external" href="https://try.github.com">try.github.com</a> 15 mins tutorial.</p>
</div>
<p>Command-line substitute for gitk</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>git log --graph --abbrev-commit --pretty=oneline --decorate
</pre></div>
</div>
<p>List all commits for a specific file</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>git log --follow -- filename
</pre></div>
</div>
<p>See the changes in a Git commit?</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>git show <COMMIT>
</pre></div>
</div>
</div>
<div class="section" id="cc-gnu-compile-collection">
<h3>cc - GNU Compile Collection<a class="headerlink" href="#cc-gnu-compile-collection" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>To Compile: gcc -Wall -pedantic -g <C source file> -o <Executable file>
-Wall -pedantic : to check for all the warnings and errors if any.
-g : to create the symbol file to be used by gdb
-o : to create the executable file.
</pre></div>
</div>
</div>
<div class="section" id="gdb-gnu-debugger">
<h3>GDB: GNU debugger<a class="headerlink" href="#gdb-gnu-debugger" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>gdb -tui <Program name>
tui : for listing the source while debugging
<linenumber> : to set the break point
p <variable name> : to print the value of the variable
bt : to print the stack call, mainly useful to find segmentation fault when multiple functions are called.
</pre></div>
</div>
</div>
</div>
<div class="section" id="gathering-information">
<h2>Gathering Information<a class="headerlink" href="#gathering-information" title="Permalink to this headline">¶</a></h2>
<div class="section" id="from-files">
<h3>From Files<a class="headerlink" href="#from-files" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>/etc/issue : Contains the message which is displayed on terminal before login.
/etc/motd : Contains the message which is displayed on terminal after login.
/proc/cpuinfo : provides information about CPU.
/proc/meminfo : provides information about memory/ RAM.
/proc/version : provides information about the version of your system.
</pre></div>
</div>
</div>
<div class="section" id="from-commands">
<h3>From Commands<a class="headerlink" href="#from-commands" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>last : shows all the login attempts and the reboot occurred.
lastb : shows all the bad login attempts.
lastlog : shows the list of all the users and when did they login.
id : print real and effective user and group IDs.
whoami : whoami - print effective userid.
uname : print system information.
-a : print all the information (Kernel name, nodename, kernel-release, kernel-version, machine, processor, hardware-platform)
pstree : display a tree of processes.
hostname : prints out the hostname of the machine which is stored in /etc/hostname.
</pre></div>
</div>
</div>
</div>
<div class="section" id="useful-utilities-commands">
<h2>Useful Utilities/ Commands<a class="headerlink" href="#useful-utilities-commands" title="Permalink to this headline">¶</a></h2>
<div class="section" id="grep-global-regular-expression-print">
<h3>Grep - Global Regular Expression Print<a class="headerlink" href="#grep-global-regular-expression-print" title="Permalink to this headline">¶</a></h3>
<p>Two ways to provide input to Grep:</p>
<ul class="simple">
<li>search a given file or files on a system (including a recursive search through sub-folders).</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep bitvijays /etc/passwd
</pre></div>
</div>
</div></blockquote>
<ul class="simple">
<li>Grep also accepts inputs (usually via a pipe) from another command or series of commands.</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>cat /etc/passwd | grep bitvijays
</pre></div>
</div>
</div></blockquote>
<div class="section" id="syntax">
<h4>Syntax<a class="headerlink" href="#syntax" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep [options] [regexp] [filename]
-i, --ignore-case : 'it DoesNt MatTTer WhaT thE CAse Is'
-v, --invert-match : 'everything , BUT that text'
-A <NUM> : Print NUM lines of trailing context after matching lines.
-B <NUM> : Print NUM lines of trailing context before matching lines.
-C <NUM> : Print additional (leading and trailing) context lines before and after the match.
-a, --text : Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
-w : Whole-word search
-L --files-without-match : which outputs the names of files that do NOT contain matches for your search pattern.
-l --files-with-matches : which prints out (only) the names of files that do contain matches for your search pattern.
-H <pattern> filename : Print the filename for each match.
example: grep -H 'a' testfile
testfile:carry out few cyber-crime investigations
Now, let’s run the search a bit differently:
cat testfile | grep -H 'a'
(standard input):carry out few cyber-crime investigations
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Regular expression should be enclosed in single quotation marks or double quotes (allows environment variables to be used), to prevent the shell (Bash or others) from trying to interpret and expand the expression before launching the grep process.</p>
</div>
</div>
<div class="section" id="using-regular-expressions">
<h4>Using regular expressions<a class="headerlink" href="#using-regular-expressions" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep 'v.r' testfile
thank you very much
</pre></div>
</div>
<p>In the search above, . is used to match any single character - matches “ver” in “very”.</p>
<p>A regular expression may be followed by one of several repetition operators:</p>
<ul class="simple">
<li>The period (.) matches any single character.</li>
<li>? means that the preceding item is optional, and if found, will be matched at the most, once.</li>
<li>* means that the preceding item will be matched zero or more times.</li>
<li>+ means the preceding item will be matched one or more times.</li>
<li>Matching with times</li>
</ul>
<blockquote>
<div><ul class="simple">
<li>{n} means the preceding item is matched exactly n times,</li>
<li>{n,} means the item is matched n or more times.</li>
<li>{n,m} means that the preceding item is matched at least n times, but not more than m times.</li>
<li>{,m} means that the preceding item is matched, at the most, m times.</li>
</ul>
</div></blockquote>
<p><strong>grep -e / grep -E</strong></p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>Matcher Selection
-E, --extended-regexp : Interpret PATTERN as an extended regular expression.
Matching Control
-e PATTERN, --regexp=PATTERN : Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f (--file) option, search for all patterns given. This option can be used to protect a pattern beginning with “-”.
</pre></div>
</div>
<p>Example:</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep -E '^[0-9a-zA-Z]{30,}'
</pre></div>
</div>
<p>Grep anything which is starting with string containing characters from 0-9,a-z or A-Z and has matched 30 or more times</p>
</div>
<div class="section" id="search-a-specific-string">
<h4>Search a specific string<a class="headerlink" href="#search-a-specific-string" title="Permalink to this headline">¶</a></h4>
<p>Scan files for a text present in them Find a way to scan my entire linux system for all files containing a specific string of text. Just to clarify, I’m looking for text within the file, not in the file name.</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep -rnw 'directory' -e "pattern" --include={*.c,*.h} --exclude=*.o
-r : search recursively
-n : print line number
-w : match the whole word.
--include={*.c,*.h} : Only search through the files which have .c or .h extensions.
--exclude=*.o : Exclude searching in files with .o extensions.
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">–exclude or –include parameter could be used for efficient searching.</p>
</div>
</div>
<div class="section" id="line-and-word-anchors">
<h4>Line and word anchors<a class="headerlink" href="#line-and-word-anchors" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>The ^ anchor specifies that the pattern following it should be at the start of the line:</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep '^th' testfile
this
</pre></div>
</div>
</div></blockquote>
<ul class="simple">
<li>The $ anchor specifies that the pattern before it should be at the end of the line.</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep 'i$' testfile
Hi
</pre></div>
</div>
</div></blockquote>
<ul class="simple">
<li>The operator < anchors the pattern to the start of a word.</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep '\<fe' testfile
carry out few cyber-crime investigations
</pre></div>
</div>
</div></blockquote>
<ul class="simple">
<li>> anchors the pattern to the end of a word.</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep 'le\>' testfile
is test file
</pre></div>
</div>
</div></blockquote>
<ul class="simple">
<li>The b (word boundary) anchor can be used in place of < and > to signify the beginning or end of a word:</li>
</ul>
<blockquote>
<div><div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep -e '\binve' testfile
carry out few cyber-crime investigations
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="shell-expansions-input-to-grep">
<h4>Shell expansions - input to Grep<a class="headerlink" href="#shell-expansions-input-to-grep" title="Permalink to this headline">¶</a></h4>
<p>If we don’t single-quote the pattern passed to Grep, the shell could perform shell expansion on the pattern and actually feed a changed pattern to Grep.</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep "$HOME" /etc/passwd
root:x:0:0:root:/root:/bin/bash
</pre></div>
</div>
<p>We used double quotes to make the Bash shell replace the environment variable $HOME with the actual value of the variable (in this case, /root). Thus, Grep searches the /etc/passwd file for the text /root, yielding the two lines that match.</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>grep `whoami` /etc/passwd
root:x:0:0:root:/root:/bin/bash
</pre></div>
</div>
<p>Here, back-tick expansion is done by the shell, replacing <cite>whoami</cite> with the user name (root) that is returned by the whoami command.</p>
</div>
</div>
<div class="section" id="copy-copy-files-and-directories">
<h3>Copy - Copy files and directories<a class="headerlink" href="#copy-copy-files-and-directories" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>cp <SOURCE> <DIRECTORY>
-r : recursive.
-a : similar to preserve,
-p : preserve
-v : verbose.
</pre></div>
</div>
</div>
<div class="section" id="cut-remove-sections-from-each-line-of-files">
<h3>cut - remove sections from each line of files<a class="headerlink" href="#cut-remove-sections-from-each-line-of-files" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>cut OPTION... [FILE]...
-d : use DELIM instead of TAB for field delimiter.
-f : select only these fields.
</pre></div>
</div>
</div>
<div class="section" id="pipes">
<h3>Pipes<a class="headerlink" href="#pipes" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>> : direct normal output.
2> : direct error output.
&> : direct all output.
</pre></div>
</div>
</div>
<div class="section" id="tar-archiving-utility">
<h3>tar - Archiving utility<a class="headerlink" href="#tar-archiving-utility" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>tar
-c : create archive
-t : list the content of the file
-x : extract the files
-j : bzip2 format
-z : gzip format
</pre></div>
</div>
</div>
<div class="section" id="find-searching-files">
<h3>find - Searching files<a class="headerlink" href="#find-searching-files" title="Permalink to this headline">¶</a></h3>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>find / -name somename
-user : File is owned by user uname (numeric user ID allowed).
-group : File belongs to group gname (numeric group ID allowed).
-size : File uses n units of space. c/k/M/G: bytes/Kilobytes/Megabytes/Gigabytes.
-name : Base of file name
</pre></div>
</div>
<div class="section" id="delete-empty-file-and-directories">
<h4>Delete empty file and directories<a class="headerlink" href="#delete-empty-file-and-directories" title="Permalink to this headline">¶</a></h4>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>find -empty -type d -delete
find -empty -type f -delete
</pre></div>
</div>
<p>Find each file in the current directory and tell it’s type and grep JPEG files.</p>
<div class="highlight-None notranslate"><div class="highlight"><pre><span></span>find . -type f -exec file {} + | grep JPEG