-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-linux.Rmd
1082 lines (739 loc) · 28 KB
/
02-linux.Rmd
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
# Linux
<!--
Name: nise-2406.training.ncgr.org
IP: 172.19.59.20
External ssh: ssh -p2406 [email protected]
-->
**Linux operating system (OS) and Bourne-Again SHell (bash) command-language basics**
## A little shell... aka the $ prompt is the command line interface
+ A shell is a user interface to the operating system.
+ CLI (Command Line Interface)
+ GUI (Graphical User Interface)
+ Bourne-Again SHell (bash) is a Unix shell and command language
+ Each command drives a program or script by talking to the Operating System
(Linux)
## Directory Structure
![](./Figures/DirectoryStructure.png){width=100%}
In a Finder window, you would see this:
![](./Figures/FinderWindow.png){width=60%}
## Find the shell in system you’ll use to log into the NCGR’s server
+ For **Windows**: search for MobaXterm from the start menu.
+ It may be useful to drag the terminal icon to the **desktop** for easier access in the future.
+ For **Mac**: search for "terminal" in the bar located in the Launchpad (rocket icon in the taskbar).
+ It may be useful to drag the terminal icon into the **Dock** for easier access in the future.
## Log on to logrus server
Enter the following command to log on to logrus:
+ substitute **your** personal username in for "user"
```
ssh -p2406 [email protected]
```
Notes:
+ Because we’re logging in remotely, the -p option is required to specify port 44111.
+ If you’re prompted to confirm the connection, say "yes", then enter your password.
## Now that I logged on, where am I?
You’re at the command line interface of the logrus analysis server!
To the left of the command prompt, you should see something like this:
+ [eprice@logrus ~]$
Command **output** is shown after the "##" in this document.
## Part I: Basic Topics
### Understanding Directories
print working directory (pwd), mkdir (make directory), and list contents (ls)
```
pwd
```
```
## /home/eprice
```
+ This is your "home" directory.
Now, create a dir under your home directory for this linux class:
```
mkdir linuxc
ls
```
```
## linuxc
```
### Listing options
using the ls command
+ **l**ong list:
```
ls -l
```
```
## total 4
## drwxrwxr-x 2 eprice eprice 4096 Aug 17 22:50 linuxc
```
+ **l**ong list, by **t**ime, **r**everse order -old to new:
```
ls -ltr
```
```
## total 4
## drwxrwxr-x 2 eprice eprice 4096 Aug 17 22:50 linuxc
```
### Navigation
1) "**c**hange **d**irectory" to the directory you made
+ where ~ is shorthand for your home directory
```
cd ~/linuxc
pwd
```
```
## /home/eprice/linuxc
```
### Files: creating with touch command
1) Create a file
```
touch newfile.txt
ls -l
```
```
## total 0
## -rw-rw-r-- 1 eprice eprice 0 Aug 17 22:50 newfile.txt
```
2) Change to your home dir
```
cd ~
pwd
```
```
## /home/eprice
```
### History command
lists the commands you have entered
```
history
```
```
## 17 ls -ltr
## 18 cd ../linuxc
## 19 ls -ltr
## 20 history
```
Scroll through recent commands with the up and down arrows.
To perform a command from the list by number:
```
!17
```
To perform the last command you made:
```
!!
```
### Files: creating by redirecting standard out
redirect operator **>**
To send output to a file instead of standard out:
+ standard out is just the terminal
```
history > history.txt
ls -l
```
```
## total 4
## -rw-rw-r-- 1 eprice eprice 0 Aug 17 22:50 history.txt
## drwxrwxr-x 2 eprice eprice 4096 Aug 17 22:50 linuxc
```
```
cat history.txt
```
```
## 17 ls -ltr
## 18 cd ../linuxc
## 19 ls -ltr
## 20 history
## 21 ls -ltr
## 20 history > history
```
Now you have a file with your commands!
### File name completion with tab
To autocomplete remainder of file name instead of typing it all in:
+ cat h...(press tab)
+ cat history
+ prevents typos and saves time
### Files: moving files from one filename to another
**m**o**v**ing "mv" command
#### Syntax: mv sourcefilename destinationfilename{-}
```
mv history.txt history_file.txt
ls -l
```
```
## total 4
## -rw-rw-r-- 1 eprice eprice 0 Aug 17 22:50 history_file.txt
## drwxrwxr-x 2 eprice eprice 4096 Aug 17 22:50 linuxc
```
### Files: copying files from one filename to another
**c**o**p**ying "cp" command
#### Syntax: cp sourcefilename destinationfilename{-}
1) Change back to the linuxc directory:
```
cd ~/linuxc
```
2) Make a "back up" copy of a file in your working directory:
```
cp ~/linuxc/newfile.txt newfile_bu.txt
```
3) Check if the newly copied file is there:
```
ls
```
```
## newfile_bu.txt
## newfile.txt
```
### Files: securely copying files between your laptop and logrus
**s**ecure **c**o**p**y "scp" command
+ a secure way to copy files to/from a server while you're working on an outside network
+ like from your home or starbucks to logrus and vice versa
#### Syntax: scp [options] sourcepath destinationpath{-}
1) Create a file to copy
```
touch scp_test.txt
```
2)
+ **Mac** users
+ open a **local** terminal
+ do **not** connect it to logrus
+ **Windows** users
+ open a new MobaXterm session (shell)
3) Run the scp command from your **local** terminal window:
+ to **download** the file **to your computer** from logrus
+ the last period means that the destination is your working directory
```
scp -P 2406 <user>@inbre.ncgr.org:~/linuxc/scp_test.txt .
```
Note: When you designate a port with secure copy (scp), you use a capital P.
You will be prompted for your logrus password if not using MobaXterm.
4) Check to see if the file you copied from logrus is on **your** computer!
5) Now **upload** a file **to logrus** from your computer
+ Again, run the scp command from your **local** terminal window:
```
scp -P 2406 scp_test.txt <user>@inbre.ncgr.org:~/linuxc
```
6) Check to see if the file you copied from your computer is on **logrus**!
### Files and directories: removing files is deleting files
**r**e**m**oving "rm" command
#### Syntax: rm [options] filename{-}
```
rm -i newfile_bu.txt
```
```
## rm: remove regular empty file newfile_bu.txt?
```
Enter "yes" or "y" in response to the question:
```
yes
ls -l
```
```
## total 0
## -rw-rw-r-- 1 eprice eprice 0 Aug 17 22:50 newfile.txt
```
At this point everyone should have the above in their linuxc class directory.
### Tool box: How to abort a command/process
Hold "control" key then hit "c" key, then release.
+ Control-key often referred to as CTRL.
Let’s say you type a command and nothing happens; it hangs.
This can happen when the syntax doesn’t make sense.
Good time for CTRL c
```
cat
```
If you can't execute commands, then CTRL c
```
## ^C
```
You should be returned to your prompt: [username@logrus linuxc]$
## PART II: Advanced Topics
### Files: Symbolic links and the soft link (-s)
#### Syntax: ln -s FileYouWantToLink/PointTo NameYouWantToGiveIt{-}
```
ln -s /home/agomez/covid.fasta covid.fasta
ls -l
```
```
## total 0
## lrwxrwxrwx 1 eprice eprice 26 Aug 17 22:50 covid.fasta -> /home/eprice/covid.fasta
## -rw-rw-r-- 1 eprice Aug 17 22:50 newfile.txt
```
### Understanding a fasta file format
Fasta files (.fasta or .fa) contain one or more sequences, each preceded by a **header** starting with ">".
Show only the **first 10 lines** of a file with "head" command:
```
head covid.fasta
```
```
## >NC_045512.2 |Severe acute respiratory syndrome coronavirus 2 isolate Wuhan-Hu-1, co
## ATTAAAGGTTTATACCTTCCCAGGTAACAAACCAACCAACTTTCGATCTCTTGTAGATCT
## GTTCTCTAAACGAACTTTAAAATCTGTGTGGCTGTCACTCGGCTGCATGCTTAGTGCACT
## CACGCAGTATAATTAATAACTAATTACTGTCGTTGACAGGACACGAGTAACTCGTCTATC
## TTCTGCAGGCTGCTTACGGTTTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAGGTTT
## CGTCCGGGTGTGACCGAAAGGTAAGATGGAGAGCCTTGTCCCTGGTTTCAACGAGAAAAC
## ACACGTCCAACTCAGTTTGCCTGTTTTACAGGTTCGCGACGTGCTCGTACGTGGCTTTGG
## AGACTCCGTGGAGGAGGTCTTATCAGAGGCACGTCAACATCTTAAAGATGGCACTTGTGG
## CTTAGTAGAAGTTGAAAAAGGCGTTTTGCCTCAACTTGAACAGCCCTATGTGTTCATCAA
## ACGTTCGGATGCTCGAACTGCACCTCATGGTCATGTTATGGTTGAGCTGGTAGCAGAACT
```
Now show only the **last 10 lines** of a file using the "tail" command:
```
tail covid.fasta
```
```
## TATTGACGCATACAAAACATTCCCACCAACAGAGCCTAAAAAGGACAAAAAGAAGAAGGC
## TGATGAAACTCAAGCCTTACCGCAGAGACAGAAGAAACAGCAAACTGTGACTCTTCTTCC
## TGCTGCAGATTTGGATGATTTCTCCAAACAATTGCAACAATCCATGAGCAGTGCTGACTC
## AACTCAGGCCTAAACTCATGCAGACCACACAAGGCAGATGGGCTATATAAACGTTTTCGC
## TTTTCCGTTTACGATATATAGTCTACTCTTGTGCAGAATGAATTCTCGTAACTACATAGC
## ACAAGTAGATGTAGTTAACTTTAATCTCACATAGCAATCTTTAATCAGTGTGTAACATTA
## GGGAGGACTTGAAAGAGCCACCACATTTTCACCGAGGCCACGCGGAGTACGATCGAGTGT
## ACAGTGAACAATGCTAGGGAGAGCTGCCTATATGGAAGAGCCCTAATGTGTAAAATTAAT
## TTTAGTAGTGCTATCCCCATGTGATTTTAATAGCTTCTTAGGAGAATGACAAAAAAAAAA
## AAAAAAAAAAAAAAAAAAAAAAA
```
#### The pipe operator will redirect output of a command to another command.{-}
Use the pipe operator to redirect "cat" output to "head":
+ The symbol "|" denotes a pipe
```
cat covid.fasta | head
```
```
## >NC_045512.2 |Severe acute respiratory syndrome coronavirus 2 isolate Wuhan-Hu-1, co
## ATTAAAGGTTTATACCTTCCCAGGTAACAAACCAACCAACTTTCGATCTCTTGTAGATCT
## GTTCTCTAAACGAACTTTAAAATCTGTGTGGCTGTCACTCGGCTGCATGCTTAGTGCACT
## CACGCAGTATAATTAATAACTAATTACTGTCGTTGACAGGACACGAGTAACTCGTCTATC
## TTCTGCAGGCTGCTTACGGTTTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAGGTTT
## CGTCCGGGTGTGACCGAAAGGTAAGATGGAGAGCCTTGTCCCTGGTTTCAACGAGAAAAC
## ACACGTCCAACTCAGTTTGCCTGTTTTACAGGTTCGCGACGTGCTCGTACGTGGCTTTGG
## AGACTCCGTGGAGGAGGTCTTATCAGAGGCACGTCAACATCTTAAAGATGGCACTTGTGG
## CTTAGTAGAAGTTGAAAAAGGCGTTTTGCCTCAACTTGAACAGCCCTATGTGTTCATCAA
## ACGTTCGGATGCTCGAACTGCACCTCATGGTCATGTTATGGTTGAGCTGGTAGCAGAACT
```
```
cat covid.fasta | tail
```
```
## TATTGACGCATACAAAACATTCCCACCAACAGAGCCTAAAAAGGACAAAAAGAAGAAGGC
## TGATGAAACTCAAGCCTTACCGCAGAGACAGAAGAAACAGCAAACTGTGACTCTTCTTCC
## TGCTGCAGATTTGGATGATTTCTCCAAACAATTGCAACAATCCATGAGCAGTGCTGACTC
## AACTCAGGCCTAAACTCATGCAGACCACACAAGGCAGATGGGCTATATAAACGTTTTCGC
## TTTTCCGTTTACGATATATAGTCTACTCTTGTGCAGAATGAATTCTCGTAACTACATAGC
## ACAAGTAGATGTAGTTAACTTTAATCTCACATAGCAATCTTTAATCAGTGTGTAACATTA
## GGGAGGACTTGAAAGAGCCACCACATTTTCACCGAGGCCACGCGGAGTACGATCGAGTGT
## ACAGTGAACAATGCTAGGGAGAGCTGCCTATATGGAAGAGCCCTAATGTGTAAAATTAAT
## TTTAGTAGTGCTATCCCCATGTGATTTTAATAGCTTCTTAGGAGAATGACAAAAAAAAAA
## AAAAAAAAAAAAAAAAAAAAAAA
```
### Understanding fastq (fq) file format
Fastq files contain sequence reads and associated **meta data**.
```
ln -s /home/agomez/SP1.fq SP1.fq
ls -ltr
```
```
## total 0
## -rw-rw-r-- 1 eprice eprice 0 Aug 17 22:50 newfile.txt
## lrwxrwxrwx 1 eprice eprice 26 Aug 17 22:50 covid.fasta -> /home/eprice/covid.fasta
## lrwxrwxrwx 1 eprice eprice Aug 17 22:50 SP1.fq -> /home/fds/unix_basics/SP1.fq
```
Tail the last 4 lines:
```
tail -n 4 SP1.fq
```
```
## @cluster_834:UMI_TTAAGG
## AGGGTGGGGGATCACATTTATTGTATTGAGG
## +
## =A=@AB===>4?A=??EEB?EB@C?ECB=A?
```
A fastq file has 4 lines per record:
+ The header; starts with "@"
+ The sequence
+ Throwaway line; begins with "+"
+ Phred-scaled quality scores
##### What is the difference between this and a fasta file?{-}
### Using grep (global regular expression print) to extract metrics
Grep will output the lines containing a provided expression.
#### Syntax: grep [options] "expression" filename{-}
```
grep ">" covid.fasta
```
```
## >NC_045512.2 |Severe acute respiratory syndrome coronavirus 2 isolate Wuhan-Hu-1, co
## >MT627325.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT622319.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT568634.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT568635.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT568636.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT568637.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT568638.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT568639.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT568640.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT568641.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407649.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407650.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407651.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407652.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407653.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407654.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407655.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407656.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407657.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407658.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT407659.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT534630.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT510727.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT510728.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079843.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079844.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079845.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079846.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079847.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079848.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079849.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079850.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079851.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079852.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079853.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT079854.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT446312.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT412134.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT396241.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT039874.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT281577.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291826.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291827.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291828.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291829.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291830.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291831.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291832.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291833.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291834.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291835.2 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT291836.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT259226.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT259227.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT259228.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT259229.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT259230.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT259231.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253696.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253697.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253698.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253699.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253700.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253701.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253702.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253703.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253704.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253705.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253706.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253707.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253708.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253709.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT253710.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT226610.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT121215.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT135041.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT135042.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT135043.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT135044.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT123290.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT123291.2 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT123292.2 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT123293.2 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT093631.2 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT049951.1 |Severe acute respiratory syndrome coronavirus 2 isolate SARS-CoV-2/huma
## >MT039873.1 |Severe acute respiratory syndrome coronavirus 2 isolate HZ-1, complete
## >MT019529.1 |Severe acute respiratory syndrome coronavirus 2 isolate BetaCoV/Wuhan/I
## >MT019530.1 |Severe acute respiratory syndrome coronavirus 2 isolate BetaCoV/Wuhan/I
## >MT019531.1 |Severe acute respiratory syndrome coronavirus 2 isolate BetaCoV/Wuhan/I
## >MT019532.1 |Severe acute respiratory syndrome coronavirus 2 isolate BetaCoV/Wuhan/I
## >MT019533.1 |Severe acute respiratory syndrome coronavirus 2 isolate BetaCoV/Wuhan/I
## >MN996527.1 |Severe acute respiratory syndrome coronavirus 2 isolate WIV02, complete
## >MN996528.1 |Severe acute respiratory syndrome coronavirus 2 isolate WIV04, complete
## >MN996529.1 |Severe acute respiratory syndrome coronavirus 2 isolate WIV05, complete
## >MN996530.1 |Severe acute respiratory syndrome coronavirus 2 isolate WIV06, complete
## >MN996531.1 |Severe acute respiratory syndrome coronavirus 2 isolate WIV07, complete
## >MN988668.1 |Severe acute respiratory syndrome coronavirus 2 isolate 2019-nCoV WHU01
## >MN988669.1 |Severe acute respiratory syndrome coronavirus 2 isolate 2019-nCoV WHU02
## >MN938384.1 |Severe acute respiratory syndrome coronavirus 2 isolate 2019-nCoV_HKU-S
## >MN975262.1 |Severe acute respiratory syndrome coronavirus 2 isolate 2019-nCoV_HKU-S
## >MN908947.3 |Severe acute respiratory syndrome coronavirus 2 isolate Wuhan-Hu-1, com
```
Adding the "-c" option counts the number of lines containing a match
+ **not** the number of matches!
```
grep -c ">" covid.fasta
```
```
## 102
```
The "-v" option reverses the grep search, which is the first step towards finding the total length of sequences
```
grep -v ">" covid.fasta
```
Use <CTRL> c to halt the overflow of output.
We can add in the pipe operator to redirect our output to the "wc" command.
The output shows the number of newline characters, followed by line count and total character count.
```
grep -v ">" covid.fasta | wc
```
wc: prints newline, word, and byte counts for each file:
```
## 50812 50812 3096018
```
Let’s trim out the newline characters with "tr -d" before doing the word count:
```
grep -v ">" covid.fasta | tr -d ’\n’ | wc
```
```
## 0 1 3045206
```
### Working with compressed files
Powerful Z commands (zcat)
+ p. 1 of 2
Let’s copy another file:
```
cp /home/agomez/table1.txt.gz /home/<user>/linuxc
zcat table1.txt.gz
```
```
## 1, Justin Timberlake, Title 545, Price $7.30
## 2, Taylor Swift, Title 723, Price $7.90
## 3, Mick Jagger, Title 610, Price $7.90
## 4, Lady Gaga, Title 118, Price $7.30
## 5, Johnny Cash, Title 482, Price $6.50
## 6, Elvis Presley, Title 335, Price $7.30
## 7, John Lennon, Title 271, Price $7.90
## 8, Michael Jackson, Title 373, Price $5.50
```
```
zgrep "Jagger" table1.txt.gz
```
```
## 3, Mick Jagger, Title 610, Price $7.90
```
!! = last command
:s = substitute /word1/with word2
```
!!:s/Jagger/John
```
```
## 5, Johnny Cash, Title 482, Price $6.50
## 7, John Lennon, Title 271, Price $7.90
```
### Start ^ and end $ symbols
Powerful Z commands (zgrep)
+ p. 2 of 2
Display all the lines that start with 8:
```
zgrep "^8" table1.txt.gz
```
```
## 8, Michael Jackson, Title 373, Price $5.50
```
Display all the lines that end with 50:
```
zgrep "50$" table1.txt.gz
```
```
## 5, Johnny Cash, Title 482, Price $6.50
## 8, Michael Jackson, Title 373, Price $5.50
```
### Files: parsing and creating data-subsets
p.1 of 5
Be sure you are in linuxc directory (as usual)
AWK command
+ **A**ho, **W**einberger and **K**ernighan
+ the authors of the language
General syntax:
+ awk ’pattern {action}’ input-file
+ output goes to standard out=terminal
+ awk ’pattern {action}’ input-file > output-file
+ or send to an output file
#### Exercise:{-}
How many fields does the 1st row of table1.txt have?
Let’s look at the 1st row of table1.txt:
1, Justin Timberlake, Title 545, Price $7.30
How many fields does it have?
### Files: parsing and creating data-subsets
p. 2 of 5
```
gunzip table1.txt.gz
cat table1.txt
```
```
## 1, Justin Timberlake, Title 545, Price $7.30
## 2, Taylor Swift, Title 723, Price $7.90
## 3, Mick Jagger, Title 610, Price $7.90
## 4, Lady Gaga, Title 118, Price $7.30
## 5, Johnny Cash, Title 482, Price $6.50
## 6, Elvis Presley, Title 335, Price $7.30
## 7, John Lennon, Title 271, Price $7.90
## 8, Michael Jackson, Title 373, Price $5.50
```
```
awk '{print $1 $3 $5}' table1.txt
```
```
## 1,Timberlake,545,
## 2,Swift,723,
## 3,Jagger,610,
## 4,Gaga,118,
## 5,Cash,482,
## 6,Presley,335,
## 7,Lennon,271,
## 8,Jackson,373,
```
### Files: parsing and creating data-subsets
p. 3 of 5
Using field separator command -F
```
cat table1.txt
```
```
## 1, Justin Timberlake, Title 545, Price $7.30
## 2, Taylor Swift, Title 723, Price $7.90
## 3, Mick Jagger, Title 610, Price $7.90
## 4, Lady Gaga, Title 118, Price $7.30
## 5, Johnny Cash, Title 482, Price $6.50
## 6, Elvis Presley, Title 335, Price $7.30
## 7, John Lennon, Title 271, Price $7.90
## 8, Michael Jackson, Title 373, Price $5.50
```
```
awk -F, '{print $3}' table1.txt
```
```
## Title 545
## Title 723
## Title 610
## Title 118
## Title 482
## Title 335
## Title 271
## Title 373
```
### Files: parsing and creating data-subsets
p. 4 of 5: Conditional awk
Time to really watch for syntax errors!
1) Statements inside the curly brackets {statement} are called a block.
2) If you put a conditional expression in front of a block with with ==, the statement inside the block will be executed **only if** the condition is **true**.
3) The whole awk command is inside ' '.
+ For example:
+ awk '$7=="$7.30"{print $3}' table1.txt
+ The condition is:
+ if $7=="$7.30"
+ meaning if the element at column 7 is equal to "$7.30", then execute statement(s) in the block {print $3}.
### Revisiting table1 and *previous* awk command
p. 5 of 5
```
awk '$7=="$7.30" {print $3}' table1.txt
```
```
## Timberlake,
## Gaga,
## Presley,
```
![](./Figures/awk.png){width=60%}
### Files: **S**tream **ED**itor (sed)
text substitution
#### Syntax: sed s/pattern/replacement{-}
Examples:
```
echo "it’s a trap" | sed s/ra/ar/
```
```
## it’s a tarp
```
Say you want to change all price occurrences of $7.90 to $8.90 from table1.txt, and save the changes to a new file.
You can do this with sed.
```
sed 's/7.90/8.90/' table1.txt > table2.txt
```
Use cat to display the contens of the new file:
```
cat table2.txt
```
```
## 1, Justin Timberlake, Title 545, Price $7.30
## 2, Taylor Swift, Title 723, Price $8.90
## 3, Mick Jagger, Title 610, Price $8.90
## 4, Lady Gaga, Title 118, Price $7.30
## 5, Johnny Cash, Title 482, Price $6.50
## 6, Elvis Presley, Title 335, Price $7.30
## 7, John Lennon, Title 271, Price $8.90
## 8, Michael Jackson, Title 373, Price $5.50
```
### The Bash "for" Loop
Suppose we want to run a command for a **group of files** in a directory. We can use a for loop to target all of them at once.
#### Syntax: for variablename in filenameexpression; do command ${variablename}; done {-}
```
for file in *; do echo ${file}; done
```
```
## covid.fasta
## newfile.txt
## SP1.fq
## table1.txt
## table2.txt
```
+ The part up to the first semicolon targets every file in the working directory with the "*" wildcard
+ The second part will sequentially echo each file in the working directory
+ The third part is required to terminate the loop
### Help with command syntax
If you forget details of a certain command, documentation can easily be found with a web search.
There is also cheat sheet on the weebly site under Supplemental Documents.
## Exercises
1. Using awk, print to output the first names of artists with album prices over $7.50 from ta- ble1.txt. Then redirect this output to a file named homework_1.txt
2. Using sed, replace all commas with semicolons in table1.txt. Save this to a file named home- work_2.txt
3. Piping history to grep, show all commands you’ve used with the expression "ls". Save this to a file named homework_3.txt
4. Piping cat to wc -l on the history text file made during the tutorial, count the number of lines in it.
5. Using scp, download table1.txt to your own machine. Check it’s there, then upload it back to logrus.
## PART III
1) Log on to logrus server
2) Enter the following command to log on to the server:
```
ssh -p2406 [email protected]
```
+ Don't forget to substitute **your** personal username in
3) Make a new directory under **your home directory**:
```
mkdir fastq_files
```
4) Enter into the new directory:
```
cd fastq_files
```
5) Move the fastq file from yesterday to the present working directory:
```
mv ~/linuxc/SP1.fq .
ls -ltr
```
```
## total 0
## lrwxrwxrwx 1 elavelle elavelle 28 Aug 17 22:50 SP1.fq -> /home/fds/unix_basics/SP1.f
```
6) How can we count the number of records in a fastq file?