-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-Shell_programming.qmd
More file actions
1475 lines (1059 loc) · 50.5 KB
/
01-Shell_programming.qmd
File metadata and controls
1475 lines (1059 loc) · 50.5 KB
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
---
title: Introducing the Shell
code-copy: true
---
## Learning Objectives
- Describe key reasons for learning shell.
- Navigate your file system using the Bash command line.
- Access and read help files for `bash` programs and use help files to identify useful command options.
- Demonstrate the use of tab completion, and explain its advantages.
- Translate an absolute path into a relative path and vice versa.
- Learn to use the `nano` text editor.
- Understand how to move, create, and delete files.
- Write a basic shell script.
- Use the `bash` command to execute a shell script.
- Use `chmod` to make a script an executable program.
**Questions to be answered in this lesson**
- What is a command shell and why would I use one?
- How can I move around on my computers file system?
- How can I see what files and directories I have?
- How can I specify the location of a file or directory on my computer?
## What is a shell?
A *shell* is a program that provides a command-line interface (CLI), allowing you to control your computer by typing commands instead of using graphical user interfaces (GUIs) with a mouse or touchscreen. Think of it as conversing directly with your computer in its native language, granting you more power and flexibility—no point-and-click needed!
There are various types of shells, each with its own flair. On macOS and Linux systems, you’ll often encounter *Bash* (Bourne Again Shell) and *Zsh* (Z Shell). For Windows users, there’s the *Command Prompt* and *PowerShell*, which is like Command Prompt’s more sophisticated sibling. Mastering the shell can help you work more efficiently, automate tasks, and unlock your computer’s full potential!
For this workshop, we will be using *Bash* in a Ubuntu virtual machine specifically set up for this class. The commands we explore will also work in *zsh* (available in the Mac Terminal) with minimal adjustments. While *PowerShell* (the Windows shell) has a different syntax, many of the fundamental commands and concepts are similar and can be adapted with little or no modification.
### Why should I learn the Shell?
There are many reasons that may benefit you from learning about about the shell:
- *Automate repetitive tasks.* If you often need to repeat tasks with a large number of files, with the shell, you can automate those.
- *Make your work less error-prone.* The shell makes your work less error-prone. When humans do the same thing a hundred different times (or even ten times), they're likely to make a mistake. Your computer can do the same thing a thousand times with no mistakes.
- *Make your work reproductible.* When using the command-line your computer keeps a record of every command that you've run, which you can use to re-do your work when you need to. It also gives you a way to communicate unambiguously what you've done, so that others can inspect or apply your process to new data.
- *Save on computing capacity.* Sometimes, when we work with data some tasks may require large amounts of computing power and can't realistically be run on your own machine. These tasks are best performed using remote computers or cloud computing, which can only be accessed through a shell.
- *Get advantage of the command line tools.* Many bioinformatics tools can only be used through the shell. Many offer features and parameter options which are not available in the GUI. BLAST is an example. Many of the advanced functions are only accessible to users who know how to use a shell.
### The *Shell* Window
As you open your *Terminal* Window, you may see something like the following:
```bash
ComputerUserName-####ABC MINGW64 ~
$
```
The dollar sign is a **prompt**, which shows us that the shell is waiting for input; your shell may use a different character as a prompt and may add information before the prompt. When typing commands, either from these lessons or from other sources, do not type the prompt, only the commands that follow it.
This symbol may be different if you are using a Linux or Mac computer.
## Navigating your file system
The **file system** manages files and directories in an operating system, organizing data into files (which store information) and directories or folders (which contain files or other directories(sub-directories)).
Common commands allow you to create, inspect, rename, and delete files and directories. To determine your location, use the `pwd` ("print working directory") command.
Your **current working directory** is the default location where commands are executed unless you specify a different directory.
Command:
```bash
$ pwd
```
Let's look at how our file system is organized. We can see what files and subdirectories are in this directory by running `ls`, which stands for "listing":
Command:
```bash
$ ls
```
`ls` prints the names of the files and directories in the current directory in alphabetical order, arranged neatly into columns. We'll be working within the `shell_data` subdirectory, and creating new subdirectories, throughout this workshop.
The command to change locations in our file system is `cd`, followed by a directory name to change our working directory. `cd` stands for "change directory".
Let's say we want to navigate to the `shell_data` directory we saw above. We can use the following command to get there:
Command:
```bash
$ cd shell_data
```
Let's look at what is in this directory:
Command:
```bash
$ ls
```
Output:
```output
sra_metadata untrimmed_fastq
```
We can make the `ls` output more comprehensible by using the **flag** `-F`, which tells `ls` to add a trailing `/` to the names of directories:
Command:
```bash
$ ls -F
```
Output:
```output
sra_metadata/ untrimmed_fastq/
```
Anything with a "/" after it is a directory. Things with a "\*" after them are programs. If there are no decorations, it's a file. `ls` has lots of other options.
To find out what they are, we can type:
```bash
$ man ls
```
`man ls` displays detailed documentation for commands in `Bash`. It is a powerful resource to explore `bash` commands, understand their usage and flags. Some manual files are very long. You can scroll through the file using your keyboard's down arrow or use the <kbd>Space</kbd> key to go forward one page and the <kbd>b</kbd> key to go backwards one page. When you are done reading, hit <kbd>q</kbd> to quit.
### Try it Yourself!
Use the `-l` option for the `ls` command to display more information for each item in the directory.
What is one piece of additional information this long format gives you that you don't see with the bare `ls` command?
::: {.callout-tip collapse="true"}
### Solution
Command:
```bash
$ ls -l
```
Output:
*Note: This is an example*
```output
total 8
drwxr-x--- user user 4096 Jul 30 2015 sra_metadata
drwxr-xr-x user user 4096 Nov 15 2017 untrimmed_fastq
```
:::
The additional information given includes the name of the owner of the file, when the file was last modified, and whether the current user has permission to read and write to the file.
::: {.callout-tip}
No one can possibly learn all of these arguments, that's what the `man` page is for. You can (and should) refer to the `man` page or other help files as needed.
:::
Let's go into the `untrimmed_fastq` directory and see what is in there.
Command:
```bash
$ cd untrimmed_fastq
$ ls -F
```
Output:
```output
SRR097977.fastq SRR098026.fastq
```
This directory contains two files with `.fastq` extensions. FASTQ is a format for storing information about sequencing reads and their quality. We will be learning more about FASTQ files in a later lesson.
### Shortcut: Tab Completion
Typing out file or directory names can waste a lot of time and it's easy to make typing mistakes.Instead we can use tab complete as a shortcut. When you start typing out the name of a directory or file, then hit the <kbd>Tab</kbd> key, the shell will try to fill in the rest of the directory or file name.
Return to your home directory and navigate to your desktop.
Command:
```bash
$ cd
$ cd Desktop
$ cd shell_data
```
Use the tab shorcut to return to shell_data directory.
```bash
$ cd she<tab>
```
The shell will fill in the rest of the directory name for `shell_data`.
Now, change directories to `untrimmed_fastq`
```bash
$ cd un<tab><tab>
```
Using tab complete can be very helpful. However, it will only autocomplete a file or directory name if you've typed enough characters to provide a unique identifier for the file or directory you are trying to access.
For example, if we now try to list the files which names start with `SR` by using tab complete:
```bash
$ ls SR<tab>
```
The shell auto-completes your command to `SRR09`, because all file names in the directory begin with this prefix. When you hit <kbd>Tab</kbd> again, the shell will list the possible choices.
Command:
```bash
$ ls SRR09<tab><tab>
```
Output:
```output
SRR097977.fastq SRR098026.fastq
```
Tab completion can also fill in the names of programs, which can be useful if you remember the beginning of a program name.
Command:
```bash
$ pw<tab><tab>
```
Output:
```output
pwck pwconv pwd pwdx pwunconv
```
Displays the name of every program that starts with `pw`.
### Shortcut: Moving around the file system
We've learned how to use `pwd` to find our current location within our file system. We've also learned how to use `cd` to change locations and `ls` to list the contents of a directory. Now we're going to learn some additional commands for moving around within our file system.
Use the commands we've learned so far to navigate to the `shell_data/untrimmed_fastq` directory, if you're not already there.
```bash
$ cd
$ cd shell_data
$ cd untrimmed_fastq
```
What if we want to move back up and out of this directory and to our top level directory? Can we type `cd shell_data`? Try it and see what happens.
Command:
```bash
$ cd shell_data
```
Output:
*Note: This is an example*
```output
$ cd cd: shell_data: No such file or directory
```
Your computer looked for a directory or file called `shell_data` within the directory you were already in. It didn't know you wanted to look at a directory level above the one you were located in.
We have a special command to tell the computer to move us back or up one directory level.
```bash
$ cd ..
```
Now we can use `pwd` to make sure that we are in the directory we intended to navigate to, and `ls` to check that the contents of the directory are correct.
Command:
```bash
$ pwd
```
Command:
```bash
$ ls
```
Output:
```output
sra_metadata untrimmed_fastq
```
From this output, we can see that `..` did indeed take us back one level in our file system.
Command:
```bash
$ ls ../../
```
### Try it Yourself!
**Finding hidden directories**
First navigate to the `shell_data` directory. There is a hidden directory within this directory. Explore the options for `ls` to find out how to see hidden directories. List the contents of the directory and identify the name of the text file in that directory.
Hint: hidden files and folders in Unix start with `.`, for example `.my_hidden_directory`
First use the `man` page to look at the options for `ls`.
```bash
$ man ls
```
::: {.callout-tip collapse="true"}
### Solution
The `-a` option is short for `all` and says that it causes `ls` to "not ignore entries starting with ."
This is the option we want.
Command:
```bash
$ ls -a
```
Output:
```output
. .. .hidden sra_metadata untrimmed_fastq
```
:::
The name of the hidden directory is `.hidden`. We can navigate to that directory using `cd`.
```bash
$ cd .hidden
```
And then list the contents of the directory using `ls`.
Command:
```bash
$ ls
```
Output:
```output
youfoundit.txt
```
The name of the text file is `youfoundit.txt`.
In most commands the flags can be combined together in no particular order to obtain the desired results/output.
```
$ ls -Fa
$ ls -laF
```
### Examining the contents of other directories
By default, the `ls` commands lists the contents of the working directory (i.e. the directory you are in). You can always find the directory you are in using the `pwd` command. However, you can also give `ls` the names of other directories to view Navigate to your home directory if you are not already there.
```bash
$ cd
```
Then enter the command:
```bash
$ ls shell_data
```
Output:
```output
sra_metadata untrimmed_fastq
```
This will list the contents of the `shell_data` directory without you needing to navigate there.
The `cd` command works in a similar way.
Try entering:
```bash
$ cd
$ cd shell_data/untrimmed_fastq
```
This will take you to the `untrimmed_fastq` directory without having to go through the intermediate directory.
### Exercice
**Navigating practice**
Navigate to your home directory. From there, list the contents of the `untrimmed_fastq` directory.
::: {.callout-tip collapse="true"}
### Solution
Command:
```bash
$ cd
$ ls shell_data/untrimmed_fastq/
```
Output:
```output
SRR097977.fastq SRR098026.fastq
```
:::
### Summary
We now know how to move around our file system using the command line. This gives us an advantage over interacting with the file system through a GUI as it allows us to work on a remote server, carry out the same set of operations on a large number of files quickly, and opens up many opportunities for using bioinformatic software that is only available in command line versions of software.
## Mastering Absolute vs. Relative Paths with the `cd` Command
Navigating the file system efficiently is crucial for managing files and directories. The `cd` (**c**hange **d**irectory) command is a fundamental tool that allows you to move between directories using **absolute** and **relative** paths. Mastering these path types will enhance your command-line proficiency.
### Understanding Path Types
#### Absolute Path
- **Definition**: An absolute path specifies the complete address of a directory from the root of the file system.
- **Starts With**: A forward slash `/`.
- **Example**: `/home/user/shell_data/untrimmed_fastq`
#### Relative Path
- **Definition**: A relative path specifies the location of a directory relative to your current directory.
- **Does Not Start With**: A forward slash `/`.
- **Example**: `untrimmed_fastq` (if you are already in `/home/user/shell_data`)
### Directory Hierarchy Overview
Visualizing the directory structure helps in understanding absolute and relative paths.
```
/
├── home
│ └── user
│ ├── shell_data
│ │ └── sra_metadata
│ │ └── untrimmed_fastq
│ └── Documents
└── etc
```
- **Root Directory (`/`)**: The top-level directory.
- **Subdirectories**: Branch out from the root or other directories.
### Using the `cd` Command
Navigate to the home directory, then enter the `pwd` command.
Command:
```bash
$ cd
$ pwd
```
Output:
*Note: This is an example*
```output
$ /usr/home/⟨username⟩
```
Here `pwd` displays the full name of your home directory. The very top of the hierarchy is a directory called `/` which is usually referred to as the *root directory*.
Now lets navigate directly to the `.hidden` folder using the full path.
```bash
$ cd /usr/home/⟨username⟩/shell_data/.hidden
```
This jumps forward multiple levels to the `.hidden` directory. Now go back to the home directory.
```bash
$ cd
```
You can also navigate to the `.hidden` directory using a relative path (relative to our working directory):
```bash
$ cd shell_data/.hidden
```
Both commands navigate to the `.hidden` directory, but one uses an absolute path from the home directory (`~/shell_data/.hidden`), while the other employs a relative path from the current working directory (`.hidden`). Absolute paths always begin with a `/` or `~`, providing the complete address regardless of your location. In contrast, relative paths are shorter and depend on your current directory, making them quicker to type when navigating nearby folders.
Think of a relative path as receiving local directions, effective when you’re already nearby, whereas an absolute path is like GPS coordinates that work from anywhere. Depending on where you are in the directory hierarchy, you can choose the most convenient path type: use absolute paths when starting from the home directory and relative paths when working within a specific folder. As you become more familiar with your directory structure, navigating between directories using both path types will become easier and more intuitive.
### Navigational Shortcuts
The root directory is the highest level directory in your file system and contains files that are important for your computer to perform its daily work. While you will be using the root (`/`) at the beginning of your absolute paths, it is important that you avoid working with data in these higher-level directories, as your commands can permanently alter files that the operating system needs to function. Dealing with the `home` directory is very common. The tilde character, `~`, is a shortcut for your home directory.
Now Navigate to the `shell_data` directory from `.hidden` directory:
```bash
$ cd ..
```
Then enter the command:
```bash
$ ls ~
```
This prints the contents of your home directory, without you needing to type the full path.
### Summary
- **Absolute Paths** provide the full directory address from the root (or home directory using `~`), ensuring you can navigate to any directory from anywhere.
- **Relative Paths** are based on your current location, allowing quicker navigation within nearby directories.
- Mastering both path types with the `cd` command enhances your ability to efficiently manage and navigate the file system.
- The commands `cd`, and `cd ~` are very useful for quickly navigating back to your home directory.
By practicing these commands and understanding when to use each path type, you'll become proficient in navigating and managing directories in any operating system.
## Writing and Reading Files
Now that we know how to move around and look at things, let’s learn how to read, write, and handle files! We’ll start by moving back to our home directory and creating a scratch directory:
```bash
$ cd ~
$ mkdir shell_test
$ cd shell_test
```
### Creating and Editing Text Files
When working in a shell, we will frequently need to create or edit text
files. Text is one of the simplest computer file formats, defined as a simple
sequence of text lines.
What if we want to make a file? There are a few ways of doing this, the easiest
of which is simply using a text editor. For this lesson, we are going to us
`nano`, since it's more intuitive than many other terminal text editors.
To create or edit a file, type `nano <filename>`, on the terminal, where
`<filename>` is the name of the file. If the file does not already exist, it
will be created. Let's make a new file now, type whatever you want in it, and
save it.
```bash
$ nano draft.txt
```

Nano defines a number of *shortcut keys* (prefixed by the <kbd>Control</kbd> or
<kbd>Ctrl</kbd> key) to perform actions such as saving the file or exiting the
editor. Here are the shortcut keys for a few common actions:
* <kbd>Ctrl</kbd>+<kbd>O</kbd> — save the file (into a current name or a
new name).
* <kbd>Ctrl</kbd>+<kbd>X</kbd> — exit the editor. If you have not saved
your file upon exiting, `nano` will ask you if you want to save.
* <kbd>Ctrl</kbd>+<kbd>K</kbd> — cut ("kill") a text line. This command
deletes a line and saves it on a clipboard. If repeated multiple times
without any interruption (key typing or cursor movement), it will cut a chunk
of text lines.
* <kbd>Ctrl</kbd>+<kbd>U</kbd> — paste the cut text line (or lines). This
command can be repeated to paste the same text elsewhere.
### Examining Files
Next, let’s explore how to view the contents of files.
One way to examine a file is by using the cat command, which prints the entire contents of the file to the terminal.
Enter the following command from within the `untrimmed_fastq` directory:
```bash
$ cat SRR098026.fastq
```
This will print out all of the contents of the `SRR098026.fastq` to the screen.
### Try it Yourself!
1. Print out the contents of the `~/shell_data/untrimmed_fastq/SRR097977.fastq` file. What is the last line of the file?
2. From your home directory, and without changing directories, use one short command to print the contents of all of the files in the `~/shell_data/untrimmed_fastq` directory.
::: {.callout-tip collapse="true"}
### Solution
1. The last line of the file is `C:CCC::CCCCCCCC<8?6A:C28C<608'&&&,'$`.
2. `cat Desktop/unix_lesson/shell_data/untrimmed_fastq/*`
:::
While `cat` is a fantastic tool for displaying the contents of a file, it can become cumbersome when dealing with very large files. In such cases, the `less` command is more efficient and user-friendly. `less` opens the file in read-only mode and allows you to navigate through its contents interactively.
Enter the following command:
```bash
$ less SRR097977.fastq
```
Some navigation commands in `less`:
| key | action |
| ----- |---------------- |
| <kbd>Space</kbd> | to go forward |
| <kbd>b</kbd> | to go backward |
| <kbd>g</kbd> | to go to the beginning |
| <kbd>G</kbd> | to go to the end |
| <kbd>q</kbd> | to quit |
`less` also gives you a way of searching through files. Use the "/" key to begin a search. Enter the word you would like to search for and press `enter`. The screen will jump to the next location where that word is found.
**Shortcut:**
If you hit "/" then "enter", `less` will repeat the previous search. `less` searches from the current location and works its way forward. Scroll up a couple lines on your terminal to verify you are at the beginning of the file. Note, if you are at the end of the file and search for the sequence "CAA", `less` will not find it. You either need to go to the beginning of the file (by typing `g`) and search again using `/` or you can use `?` to search backwards in the same way you used `/` previously.
For instance, let's search forward for the sequence `TTTTT` in our file. You can see that we go right to that sequence, what it looks like, and where it is in the file. If you continue to type `/` and hit return, you will move forward to the next instance of this sequence motif. If you instead type `?` and hit return, you will search backwards and move up the file to previous examples of this motif.
### Try it Yourself!
What are the next three nucleotides (characters) after the first instance of the sequence quoted above?
::: {.callout-tip collapse="true"}
### Solution
`CAC`
:::
Remember, the `man` program actually uses `less` internally and therefore uses the same commands, so you can search documentation using "/" as well!
### Viewing Parts of Files with head and tail
Another method to inspect files is to view only a portion of their contents, which is especially useful when you want to see the beginning or the end of a file or examine its formatting without loading the entire file. The `head` and `tail` commands facilitate this by allowing you to display the first few lines or the last few lines of a file, respectively. These commands are invaluable for quickly assessing large files, checking recent entries in log files, or verifying the structure of a document without the need to scroll through the entire content.
Command:
```bash
$ head SRR098026.fastq
```
Output:
```output
@SRR098026.1 HWUSI-EAS1599_1:2:1:0:968 length=35
NNNNNNNNNNNNNNNNCNNNNNNNNNNNNNNNNNN
+SRR098026.1 HWUSI-EAS1599_1:2:1:0:968 length=35
!!!!!!!!!!!!!!!!#!!!!!!!!!!!!!!!!!!
@SRR098026.2 HWUSI-EAS1599_1:2:1:0:312 length=35
NNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNN
+SRR098026.2 HWUSI-EAS1599_1:2:1:0:312 length=35
!!!!!!!!!!!!!!!!#!!!!!!!!!!!!!!!!!!
@SRR098026.3 HWUSI-EAS1599_1:2:1:0:570 length=35
NNNNNNNNNNNNNNNNANNNNNNNNNNNNNNNNNN
```
Command:
```bash
$ tail SRR098026.fastq
```
Output:
```output
+SRR098026.247 HWUSI-EAS1599_1:2:1:2:1311 length=35
#!##!#################!!!!!!!######
@SRR098026.248 HWUSI-EAS1599_1:2:1:2:118 length=35
GNTGNGGTCATCATACGCGCCCNNNNNNNGGCATG
+SRR098026.248 HWUSI-EAS1599_1:2:1:2:118 length=35
B!;?!A=5922:##########!!!!!!!######
@SRR098026.249 HWUSI-EAS1599_1:2:1:2:1057 length=35
CNCTNTATGCGTACGGCAGTGANNNNNNNGGAGAT
+SRR098026.249 HWUSI-EAS1599_1:2:1:2:1057 length=35
A!@B!BBB@ABAB#########!!!!!!!######
```
The `-n` option to either of these commands can be used to print the first or last `n` lines of a file.
Command:
```bash
$ head -n 1 SRR098026.fastq
```
Output:
```output
@SRR098026.1 HWUSI-EAS1599_1:2:1:0:968 length=35
```
Command:
```bash
$ tail -n 1 SRR098026.fastq
```
Output:
```output
A!@B!BBB@ABAB#########!!!!!!!######
```
### Summary
In this section, you learned how to view the contents of files using several `Bash` commands. You can display the entire file with `cat`, navigate through large files interactively with `less`, view the beginning of a file using `head`, and examine the end of a file with `tail.` These tools offer flexible options for accessing and inspecting file data efficiently.
## Creating, moving, copying, and removing
Now we can move around in the file structure, look at files, and search files. But what if we want to copy files or move
them around or get rid of them? Most of the time, you can do these sorts of file manipulations without the command line,
but there will be some cases (like when you're working with a remote computer) where it will be
impossible. You'll also find that you may be working with hundreds of files and want to do similar manipulations to all
of those files. In cases like this, it's much faster to do these operations at the command line.
### Copying Files
While navigating the file structure, viewing files, and searching through them are essential tasks, there are times when you need to create, move, copy, or delete files. Although many of these file manipulations can be performed using graphical interfaces, there are scenarios—such as working on a remote computer or handling large batches of files—where the command line becomes indispensable. When dealing with hundreds of files that require similar operations, executing these tasks via the command line is significantly faster and more efficient.
First, let's make a copy of one of our FASTQ files using the `cp` command.
Navigate to the `shell_data/untrimmed_fastq` directory and enter:
```bash
$ cp SRR098026.fastq SRR098026-copy.fastq
$ ls
```
```output
SRR097977.fastq SRR098026-copy.fastq SRR098026.fastq
```
We now have two copies of the `SRR098026.fastq` file, one of them named `SRR098026-copy.fastq`. We'll move this file to a new directory
called `backup` where we'll store our backup data files.
### Creating Directories
The `mkdir` command is used to make a directory. Enter `mkdir`
followed by a space, then the directory name you want to create:
```bash
$ mkdir backup
```
### Moving / Renaming
We can now move our backup file to this directory. We can
move files around using the command `mv`:
```bash
$ mv SRR098026-copy.fastq backup
$ ls backup
```
```output
SRR098026-copy.fastq
```
The `mv` command is also how you rename files. Let's rename this file to make it clear that this is a backup:
```bash
$ cd backup
$ mv SRR098026-copy.fastq SRR098026-backup.fastq
$ ls
```
```output
SRR098026-backup.fastq
```
### File Permissions
We've now made a backup copy of our file, but just because we have two copies, it doesn't make us safe. We can still accidentally delete or overwrite both copies. To make sure we can't accidentally mess up this backup file, we're going to change the permissions on the file so that we're only allowed to read (i.e. view) the file, not write to it (i.e. make new changes).
View the current permissions on a file using the `-l` (long) flag for the `ls` command:
```bash
$ ls -l
```
```output
-rw-r--r-- 1 <username> 43332 <last modified date time> SRR098026-backup.fastq
```
The first part of the output for the `-l` flag gives you information about the file's current permissions. There are ten slots in the
permissions list. The first character in this list is related to file type, not permissions, so we'll ignore it for now. The next three
characters relate to the permissions that the file owner has, the next three relate to the permissions for group members, and the final
three characters specify what other users outside of your group can do with the file. We're going to concentrate on the three positions
that deal with your permissions (as the file owner).
{alt='Permissions breakdown'}
Here the three positions that relate to the file owner are `rw-`. The `r` means that you have permission to read the file, the `w`
indicates that you have permission to write to (i.e. make changes to) the file, and the third position is a `-`, indicating that you
don't have permission to carry out the ability encoded by that space (this is the space where `x` or executable ability is stored. This controls your ability to run files that are programs or `cd` into a directory).
Our goal for now is to change permissions on this file so that you no longer have `w` or write permissions. We can do this using the `chmod` (change mode) command and subtracting (`-`) the write permission `-w`.
```bash
$ chmod -w SRR098026-backup.fastq
$ ls -l
```
```output
-r--r--r-- 1 <username> 43332 <last modified date time> SRR098026-backup.fastq
```
### Removing
To prove to ourselves that you no longer have the ability to modify this file, try deleting it with the `rm` command:
```bash
$ rm SRR098026-backup.fastq
```
You'll be asked if you want to override your file permissions:
```output
rm: remove write-protected regular file ‘SRR098026-backup.fastq'?
```
You should enter `n` for no. If you enter `n` (for no), the file will not be deleted. If you enter `y`, you will delete the file. This gives us an extra
measure of security, as there is one more step between us and deleting our data files.
**Important**: The `rm` command permanently removes the file. Be careful with this command. It doesn't
just nicely put the files in the Trash. They're really gone.
By default, `rm` will not delete directories. You can tell `rm` to
delete a directory using the `-r` (recursive) option. Let's delete the backup directory
we just made.
Enter the following command:
```bash
$ cd ..
$ rm -r backup
```
This will delete not only the directory, but all files within the directory. If you have write-protected files in the directory,
you will be asked whether you want to override your permission settings.
::: {.callout-tip}
### Try it Yourself!
Starting in the `shell_data/untrimmed_fastq/` directory, do the following:
1. Make sure that you have deleted your backup directory and all files it contains.
2. Create a backup of each of your FASTQ files using `cp`. (Note: You'll need to do this individually for each of the two FASTQ files. We haven't
learned yet how to do this
with a wildcard.)
3. Use a wildcard to move all of your backup files to a new backup directory.
4. Change the permissions on all of your backup files to be write-protected.
:::: {.callout-caution collapse="true" icon="false"}
### Solution
1. `rm -r backup`
2. `cp SRR098026.fastq SRR098026-backup.fastq` and `cp SRR097977.fastq SRR097977-backup.fastq`
3. `mkdir backup` and `mv *-backup.fastq backup`
4. `chmod -w backup/*-backup.fastq`
It's always a good idea to check your work with `ls -l backup`. You should see something like:
```output
-r--r--r-- 1 <username> 47552 <modified date time> SRR097977-backup.fastq
-r--r--r-- 1 <username> 43332 <username> SRR098026-backup.fastq
```
::::
:::
### Summary
In this section, you learned essential commands for managing your file system efficiently. The `cp`, `mv`, and `mkdir` commands allow you to copy and move existing files as well as create new directories, enabling effective organization and manipulation of your files. Additionally, you can view file permissions using `ls -l`, which provides detailed information about each file’s access rights, and modify these permissions with `chmod` to control who can read, write, or execute your files.
## Downloading and Extracting Files
When you're working in the shell, especially on remote computers, its not possible to access a web browser to download files from the internet. Fortunately there are commands we can use to do just this! Also since bioinformatics files tend to be huge we typically have to compress them so they travel faster over the internet. This means we also need tools to extract the compressed files. Lets check out these tools.
Let's grab and unpack a set of demo files for use later. To do this, we'll use
[`wget`](https://www.gnu.org/software/wget/) (`wget link` downloads a file from a link).
```bash
$ cd
$ cd shell_data
$ wget http://www.hpc-carpentry.org/hpc-shell/files/bash-lesson.tar.gz
$ ls
```
You'll commonly encounter `.tar.gz` archives while working in UNIX. To extract the files from a `.tar.gz` file, we run the command `tar -xvf filename.tar.gz`:
```bash
$ tar -xvf bash-lesson.tar.gz
```
```output
dmel-all-r6.19.gtf
dmel_unique_protein_isoforms_fb_2016_01.tsv
gene_association.fb
SRR307023_1.fastq
SRR307023_2.fastq
SRR307024_1.fastq
SRR307024_2.fastq
SRR307025_1.fastq
SRR307025_2.fastq
SRR307026_1.fastq
SRR307026_2.fastq
SRR307027_1.fastq
SRR307027_2.fastq
SRR307028_1.fastq
SRR307028_2.fastq
SRR307029_1.fastq
SRR307029_2.fastq
SRR307030_1.fastq
SRR307030_2.fastq
```
::: {.callout-tip title="Unzipping files"}
We just unzipped a .tar.gz file for this example. What if we run into other file formats that we need to unzip? Just use the handy reference below:
* `gunzip` extracts the contents of .gz files
* `unzip` extracts the contents of .zip files
* `tar -xvf` extracts the contents of .tar.gz and .tar.bz2 files
:::
### Summary
In this section we explored how to download files from the internet using the `wget` command and extract compressed .tar.gz archives with the `tar` command. By navigating to the appropriate directory, downloading the necessary demo files, and unpacking them, you can efficiently manage and access large datasets. These command-line tools are essential for handling file transfers and extractions, especially when working in environments where graphical interfaces are unavailable or when dealing with extensive data sets in UNIX-based systems.
## Wildcards and Pipes
Now that we know some of the basic UNIX commands, we are going to explore some more advanced features. The first of these features is the wildcard `*`. In our examples before, we've done things to files one at a time and otherwise had to specify things explicitly. The `*` character lets us speed things up and do things across multiple files.
Ever wanted to move, delete, or just do "something" to all files of a certain type in a directory? `*` lets you do that, by taking the place of one or more characters in a piece of text. So `*.txt` would be equivalent to all `.txt`
files in a directory for instance. `*` by itself means all files. Let's use our example data to see what I mean.
### Wildcards
Navigate to your `shell_data` directory:
```bash
$ cd shell_data
```
We are interested in looking at the FASTQ files in this directory. We can list all files with the .fastq extension using the command:
```bash
$ ls *.fastq
```
Output:
```output
SRR307023_1.fastq SRR307025_1.fastq SRR307027_1.fastq SRR307029_1.fastq
SRR307023_2.fastq SRR307025_2.fastq SRR307027_2.fastq SRR307029_2.fastq
SRR307024_1.fastq SRR307026_1.fastq SRR307028_1.fastq SRR307030_1.fastq
SRR307024_2.fastq SRR307026_2.fastq SRR307028_2.fastq SRR307030_2.fastq
```
The `*` character is a special type of character called a wildcard, which can be used to represent any number of any type of character. Thus, `*.fastq` matches every file that ends with `.fastq`.
### Try it Yourself!
Now let's try cleaning up our working directory a bit. Create a folder called "fastq" and move all of our .fastq files there in one `mv` command.
::: {.callout-tip collapse="true"}
### Solution
```bash
mkdir fastq
mv *.fastq fastq/
```
:::
### Redirecting output
Each of the commands we've used so far does only a very small amount of work.
However, we can chain these small UNIX commands together to perform otherwise
complicated actions!
For our first foray into *piping*, or redirecting output, we are going to use
the `>` operator to write output to a file. When using `>`, whatever is on the
left of the `>` is written to the filename you specify on the right of the
arrow. The actual syntax looks like `command > filename`.
Let's try using `>`. `echo` simply prints back, or echoes
whatever you type after it.
Command:
```bash
$ echo "this is a test"
$ echo "this is a test" > test.txt
$ cat test.txt
```
Output:
```output
this is a test
this is a test
```
There are 3
input/output streams for every UNIX program you will run: `stdin`, `stdout`, and `stderr`.
Let's dissect these three streams of input/output in the command we just ran:
`echo "this is a test" > test.txt`
* `stdin` is the input to a program. In the command we just ran, `stdin` is
represented by `"this is a test"`, which is simply every filename in our current directory.
* `stdout` contains the actual, expected output. In this case, `>` redirected
`stdout` to the file `test.txt`.
* `stderr` typically contains error messages and other information that doesn't quite fit into the category of "output". If we insist on redirecting both `stdout` and `stderr` to the same file we would use `&>` instead of `>`. (We can redirect just `stderr` using `2>`.)
Now we will add another line of text to our file, but how do we do that without overwriting our original file? For this there is a different redirect `>>`.
Command:
```bash
$ echo "adding more text to the end of the file" >> test.txt
$ cat test.txt
```
Output:
```output
this is a test
adding more text to the end of the file
```
### Try it Yourself!
Let me introduce you to a new command that tells us how long a file is: `wc`. `wc -l file` tells us the length of a file in lines.
Command:
```bash
$ wc -l dmel-all-r6.19.gtf
```