class: center, middle, inverse, title-slide # MKT615: Data Storytelling for Marketers ## Lecture 3: The Shell ### Davide Proserpio ### Marshall School of Business --- # Table of contents 1. [Introduction](#intro) 2. [Bash shell basics](#basics) 3. [SSH](#remote) 3. [Files and directories](#files) 4. [Working with text files](#text) 5. [Redirecting and pipes](#pipes) 6. [Scripting](#scripting) 7. [User roles and file permissions](#permissions) 8. [Changing permissions](#permissions2) 9. [Vim, running scripts, and monitor resources](#vim) 10. [Appendix (Windows users only)](#windows) --- name:start # Checklist ☑ Have you cloned the [course repo](https://github.com/dadepro/mkt-615) to your local machine? ☑ Do you have Bash-compatible shell? You should be ok with Mac and Linux, **Windows** users: see [here](#windows) before continuing (or you can decide to use **mshresearch** and [RStudio Server](http://mshresearch.marshall.usc.edu:8787/) ready for your to use) </br> (This lecture is the last detour before we get back to data analyis with R and RStudio.) --- class: inverse, center, middle name: intro # Introduction <!-- <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --> --- # Definitions Don't be thrown off by terminology: *shell*, *terminal*, *tty*, *command prompt*, etc. - These are all basically just different names for the same thing.<sup>1</sup> - They are all referring to a **Command Line Interface** (CLI). .footnote[ <sup>1</sup> [Truth be told](https://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con), there are some subtle and sometimes important differences, as well as some interesting history behind the names. But we can safely ignore these here. ] -- There are many shell variants, but we're going to focus on [**Bash**](https://www.gnu.org/software/bash/) (i.e. **B**ourne **a**gain **sh**ell). - Included by default on Linux and MacOS. - Windows users need to install a Bash-compatible shell first (again, see [here](#windows)). -- (For the record, MacOS default shell is now [zsh](https://ohmyz.sh/) (i.e. the "Z shell"). As a result, my shell might look slightly different to yours during live coding sessions. The commands will stay the same, though.) --- # Why even bother with the shell? 1. Power - Both for executing commands and for fixing problems. There are some things you just can't do in an IDE or GUI. <!-- - It also avoids memory complications associated with certain applications and/or IDEs. We'll get to this issue later in the course. --> 2. Reproducibility - Scripting is reproducible, while clicking is not. 3. Interacting with servers and high-performance computers - The shell is often the only option for high performance computing. 4. Automating workflow and analysis pipelines - Easily track and reproduce an entire project (e.g. use a Makefile to combine multiple programs, scripts, etc.) --- # What do I use the shell for? - Git/SVN - Running scripts (Python, Bash scripts, R, etc.) - Managing mshresearch (creating users, monitoring resources, etc.) - Installing software - Schedule tasks ([Crontab](https://crontab.guru/)) - File management (renaming, moving, etc.) - Probably more things I am forgetting about --- class: inverse, center, middle name: basics # Bash shell basics <!-- <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --> --- # First look Let's open up our Bash shell. - [Linux](https://www.wikihow.com/Open-a-Terminal-Window-in-Ubuntu) - [Mac](https://www.techwalla.com/articles/how-to-open-terminal-on-a-macbook) - [Windows](https://www.howtogeek.com/265900/everything-you-can-do-with-windows-10s-new-bash-shell/) You can also use RStudio if you prefer: - [RStudio's built-in Terminal](https://support.rstudio.com/hc/en-us/articles/115010737148-Using-the-RStudio-Terminal). --- # First look (cont.) You should see something like: ```bash username@hostname:~$ ``` This is shell-speak for: "Who am I and where am I?"0 -- - `username` denotes a specific user (one of potentially many on this computer). -- - `@hostname` denotes the name of the computer or server. -- - `:~` denotes the directory path (where `~` signifies the user's home directory). -- - `$` denotes the start of the command prompt. - We'll get to this later, but for a special "superuser" called root, the dollar sign will change to a `#`. --- # Useful keyboard shortcuts - `Tab` completion. - Use the `↑` (and `↓`) keys to scroll through previous commands. - `Ctrl`+`→` (and `Ctrl`+`←`) to skip whole words at a time. - `Ctrl`+`a` moves the cursor to the beginning of the line. - `Ctrl`+`e` moves the cursor to the end of the line. - `Ctrl`+`k` deletes everything to the right of the cursor. - `Ctrl`+`u` deletes everything to the left of the cursor. - `Alt`+`Del` delete a word to the left (`option` for Mac) - `Ctrl`+`Shift`+`c` to copy and `Ctrl`+`Shift`+`v` to paste (I think `shift` is optional if you are not using RStudio shell) - `clear` to clear your terminal. --- # Syntax All Bash commands have the same basic syntax: **<center>command option(s) argument(s)</center>** Examples: ```bash $ ls -lh ~/Documents/ ``` ```bash $ sort -u myfile.txt ``` --- count: false # Syntax All Bash commands have the same basic syntax: **<center><span style='background: #ffff88;'>command</span> option(s) argument(s)</center>** Examples: ```bash $ `ls` -lh ~/Documents/ ``` ```bash $ `sort` -u myfile.txt ``` --- count: false # Syntax All Bash commands have the same basic syntax: **<center>command <span style='background: #ffff88;'>option(s)</span> argument(s)</center>** Examples: ```bash $ ls `-lh` ~/Documents/ ``` ```bash $ sort `-u` myfile.txt ``` --- count: false # Syntax All Bash commands have the same basic syntax: **<center>command option(s) <span style='background: #ffff88;'>argument(s)</span></center>** Examples: ```bash $ ls -lh `~/Documents/` ``` ```bash $ sort -u `myfile.txt` ``` --- # Syntax All Bash commands have the same basic syntax: **<center>command option(s) argument(s)</center>** Examples: ```bash $ ls -lh ~/Documents/ ``` ```bash $ sort -u myfile.txt ``` </br> **Commands** - You don't always need options or arguments. (E.g. `$ ls ~/Documents/` and `$ ls -lh` are both valid commands that will yield output.) - However, you always need a command. --- # Syntax (cont.) **options** (also called **flags**) - Start with a dash. - Usually one letter. - Multiple options can be chained together under a single dash. ```bash $ ls -l -a -h /var/log ## This works $ ls -lah /var/log ## So does this ``` - An exception is with (rarer) options requiring two dashes. ```bash $ ls --group-directories-first --human-readable /var/log ``` **arguments** - Tell the command *what* to operate on. - Usually a file, path, or a set of files and folders. --- # Help: man The `man` command ("manual pages") is your friend if you ever need help. - Tip: Hit spacebar to scroll down a page at a time, "h" to see the help notes of the `man` command itself and "q" to quit. ```bash man ls ``` ``` ## ## LS(1) BSD General Commands Manual LS(1) ## ## NNAAMMEE ## llss -- list directory contents ## ## SSYYNNOOPPSSIISS ## llss [--AABBCCFFGGHHLLOOPPRRSSTTUUWW@@aabbccddeeffgghhiikkllmmnnooppqqrrssttuuwwxx11%%] [_f_i_l_e _._._.] ## ## DDEESSCCRRIIPPTTIIOONN ## For each operand that names a _f_i_l_e of a type other than directory, llss ## displays its name as well as any requested, associated information. For ## each operand that names a _f_i_l_e of type directory, llss displays the names ## of files contained within that directory, as well as any requested, asso- ## ciated information. ## ## If no operands are given, the contents of the current directory are dis- ## played. If more than one operand is given, non-directory operands are ## displayed first; directory and non-directory operands are sorted sepa- ## rately and in lexicographical order. ## ## The following options are available: ## ## --@@ Display extended attribute keys and sizes in long (--ll) output. ## ## --11 (The numeric digit ``one''.) Force output to be one entry per ## line. This is the default when output is not to a terminal. ## ## --AA List all entries except for _. and _._.. Always set for the super- ## user. ## ## --aa Include directory entries whose names begin with a dot (_.). ## ## --BB Force printing of non-printable characters (as defined by ## ctype(3) and current locale settings) in file names as \_x_x_x, ## where _x_x_x is the numeric value of the character in octal. ## ## --bb As --BB, but use C escape codes whenever possible. ## ## --CC Force multi-column output; this is the default when output is to ## a terminal. ## ## --cc Use time when file status was last changed for sorting (--tt) or ## long printing (--ll). ## ## --dd Directories are listed as plain files (not searched recursively). ## ## --ee Print the Access Control List (ACL) associated with the file, if ## present, in long (--ll) output. ## ## --FF Display a slash (`/') immediately after each pathname that is a ## directory, an asterisk (`*') after each that is executable, an at ## sign (`@') after each symbolic link, an equals sign (`=') after ## each socket, a percent sign (`%') after each whiteout, and a ver- ## tical bar (`|') after each that is a FIFO. ## ## --ff Output is not sorted. This option turns on the --aa option. ## ## --GG Enable colorized output. This option is equivalent to defining ## CLICOLOR in the environment. (See below.) ## ## --gg This option is only available for compatibility with POSIX; it is ## used to display the group name in the long (--ll) format output ## (the owner name is suppressed). ## ## --HH Symbolic links on the command line are followed. This option is ## assumed if none of the --FF, --dd, or --ll options are specified. ## ## --hh When used with the --ll option, use unit suffixes: Byte, Kilobyte, ## Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the ## number of digits to three or less using base 2 for sizes. ## ## --ii For each file, print the file's file serial number (inode num- ## ber). ## ## --kk If the --ss option is specified, print the file size allocation in ## kilobytes, not blocks. This option overrides the environment ## variable BLOCKSIZE. ## ## --LL Follow all symbolic links to final target and list the file or ## directory the link references rather than the link itself. This ## option cancels the --PP option. ## ## --ll (The lowercase letter ``ell''.) List in long format. (See ## below.) A total sum for all the file sizes is output on a line ## before the long listing. ## ## --mm Stream output format; list files across the page, separated by ## commas. ## ## --nn Display user and group IDs numerically, rather than converting to ## a user or group name in a long (--ll) output. This option turns on ## the --ll option. ## ## --OO Include the file flags in a long (--ll) output. ## ## --oo List in long format, but omit the group id. ## ## --PP If argument is a symbolic link, list the link itself rather than ## the object the link references. This option cancels the --HH and ## --LL options. ## ## --pp Write a slash (`/') after each filename if that file is a direc- ## tory. ## ## --qq Force printing of non-graphic characters in file names as the ## character `?'; this is the default when output is to a terminal. ## ## --RR Recursively list subdirectories encountered. ## ## --rr Reverse the order of the sort to get reverse lexicographical ## order or the oldest entries first (or largest files last, if com- ## bined with sort by size ## ## --SS Sort files by size ## ## --ss Display the number of file system blocks actually used by each ## file, in units of 512 bytes, where partial units are rounded up ## to the next integer value. If the output is to a terminal, a ## total sum for all the file sizes is output on a line before the ## listing. The environment variable BLOCKSIZE overrides the unit ## size of 512 bytes. ## ## --TT When used with the --ll (lowercase letter ``ell'') option, display ## complete time information for the file, including month, day, ## hour, minute, second, and year. ## ## --tt Sort by time modified (most recently modified first) before sort- ## ing the operands by lexicographical order. ## ## --uu Use time of last access, instead of last modification of the file ## for sorting (--tt) or long printing (--ll). ## ## --UU Use time of file creation, instead of last modification for sort- ## ing (--tt) or long output (--ll). ## ## --vv Force unedited printing of non-graphic characters; this is the ## default when output is not to a terminal. ## ## --WW Display whiteouts when scanning directories. (--SS) flag). ## ## --ww Force raw printing of non-printable characters. This is the ## default when output is not to a terminal. ## ## --xx The same as --CC, except that the multi-column output is produced ## with entries sorted across, rather than down, the columns. ## ## --%% Distinguish dataless files and directories with a '%' character ## in long (--ll) output, and don't materialize dataless directories ## when listing them. ## ## The --11, --CC, --xx, and --ll options all override each other; the last one ## specified determines the format used. ## ## The --cc and --uu options override each other; the last one specified deter- ## mines the file time used. ## ## The --BB, --bb, --ww, and --qq options all override each other; the last one ## specified determines the format used for non-printable characters. ## ## The --HH, --LL and --PP options all override each other (either partially or ## fully); they are applied in the order specified. ## ## By default, llss lists one entry per line to standard output; the excep- ## tions are to terminals or when the --CC or --xx options are specified. ## ## File information is displayed with one or more <blank>s separating the ## information associated with the --ii, --ss, and --ll options. ## ## TThhee LLoonngg FFoorrmmaatt ## If the --ll option is given, the following information is displayed for ## each file: file mode, number of links, owner name, group name, number of ## bytes in the file, abbreviated month, day-of-month file was last modi- ## fied, hour file last modified, minute file last modified, and the path- ## name. In addition, for each directory whose contents are displayed, the ## total number of 512-byte blocks used by the files in the directory is ## displayed on a line by itself, immediately before the information for the ## files in the directory. If the file or directory has extended ## attributes, the permissions field printed by the --ll option is followed by ## a '@' character. Otherwise, if the file or directory has extended secu- ## rity information (such as an access control list), the permissions field ## printed by the --ll option is followed by a '+' character. If the --%% ## option is given, a '%' character follows the permissions field for data- ## less files and directories, possibly replacing the '@' or '+' character. ## ## If the modification time of the file is more than 6 months in the past or ## future, then the year of the last modification is displayed in place of ## the hour and minute fields. ## ## If the owner or group names are not a known user or group name, or the --nn ## option is given, the numeric ID's are displayed. ## ## If the file is a character special or block special file, the major and ## minor device numbers for the file are displayed in the size field. If ## the file is a symbolic link, the pathname of the linked-to file is pre- ## ceded by ``->''. ## ## The file mode printed under the --ll option consists of the entry type, ## owner permissions, and group permissions. The entry type character ## describes the type of file, as follows: ## ## bb Block special file. ## cc Character special file. ## dd Directory. ## ll Symbolic link. ## ss Socket link. ## pp FIFO. ## -- Regular file. ## ## The next three fields are three characters each: owner permissions, group ## permissions, and other permissions. Each field has three character posi- ## tions: ## ## 1. If rr, the file is readable; if --, it is not readable. ## ## 2. If ww, the file is writable; if --, it is not writable. ## ## 3. The first of the following that applies: ## ## SS If in the owner permissions, the file is not exe- ## cutable and set-user-ID mode is set. If in the ## group permissions, the file is not executable and ## set-group-ID mode is set. ## ## ss If in the owner permissions, the file is exe- ## cutable and set-user-ID mode is set. If in the ## group permissions, the file is executable and set- ## group-ID mode is set. ## ## xx The file is executable or the directory is search- ## able. ## ## -- The file is neither readable, writable, exe- ## cutable, nor set-user-ID nor set-group-ID mode, ## nor sticky. (See below.) ## ## These next two apply only to the third character in the last ## group (other permissions). ## ## TT The sticky bit is set (mode 1000), but not execute ## or search permission. (See chmod(1) or ## sticky(8).) ## ## tt The sticky bit is set (mode 1000), and is search- ## able or executable. (See chmod(1) or sticky(8).) ## ## EEXXAAMMPPLLEESS ## The following is how to do an llss listing sorted by increasing size ## ## ls -lrS ## ## DDIIAAGGNNOOSSTTIICCSS ## The llss utility exits 0 on success, and >0 if an error occurs. ## ## EENNVVIIRROONNMMEENNTT ## The following environment variables affect the execution of llss: ## ## BLOCKSIZE If the environment variable BLOCKSIZE is set, the block ## counts (see --ss) will be displayed in units of that size ## block. ## ## CLICOLOR Use ANSI color sequences to distinguish file types. See ## LSCOLORS below. In addition to the file types mentioned ## in the --FF option some extra attributes (setuid bit set, ## etc.) are also displayed. The colorization is dependent ## on a terminal type with the proper termcap(5) capabili- ## ties. The default ``cons25'' console has the proper ## capabilities, but to display the colors in an xterm(1), ## for example, the TERM variable must be set to ## ``xterm-color''. Other terminal types may require simi- ## lar adjustments. Colorization is silently disabled if ## the output isn't directed to a terminal unless the ## CLICOLOR_FORCE variable is defined. ## ## CLICOLOR_FORCE Color sequences are normally disabled if the output isn't ## directed to a terminal. This can be overridden by set- ## ting this flag. The TERM variable still needs to refer- ## ence a color capable terminal however otherwise it is not ## possible to determine which color sequences to use. ## ## COLUMNS If this variable contains a string representing a decimal ## integer, it is used as the column position width for dis- ## playing multiple-text-column output. The llss utility cal- ## culates how many pathname text columns to display based ## on the width provided. (See --CC and --xx.) ## ## LANG The locale to use when determining the order of day and ## month in the long --ll format output. See environ(7) for ## more information. ## ## LSCOLORS The value of this variable describes what color to use ## for which attribute when colors are enabled with ## CLICOLOR. This string is a concatenation of pairs of the ## format _f_b, where _f is the foreground color and _b is the ## background color. ## ## The color designators are as follows: ## ## aa black ## bb red ## cc green ## dd brown ## ee blue ## ff magenta ## gg cyan ## hh light grey ## AA bold black, usually shows up as dark grey ## BB bold red ## CC bold green ## DD bold brown, usually shows up as yellow ## EE bold blue ## FF bold magenta ## GG bold cyan ## HH bold light grey; looks like bright white ## xx default foreground or background ## ## Note that the above are standard ANSI colors. The actual ## display may differ depending on the color capabilities of ## the terminal in use. ## ## The order of the attributes are as follows: ## ## 1. directory ## 2. symbolic link ## 3. socket ## 4. pipe ## 5. executable ## 6. block special ## 7. character special ## 8. executable with setuid bit set ## 9. executable with setgid bit set ## 10. directory writable to others, with sticky bit ## 11. directory writable to others, without sticky ## bit ## ## The default is "exfxcxdxbxegedabagacad", i.e. blue fore- ## ground and default background for regular directories, ## black foreground and red background for setuid executa- ## bles, etc. ## ## LS_COLWIDTHS If this variable is set, it is considered to be a colon- ## delimited list of minimum column widths. Unreasonable ## and insufficient widths are ignored (thus zero signifies ## a dynamically sized column). Not all columns have ## changeable widths. The fields are, in order: inode, ## block count, number of links, user name, group name, ## flags, file size, file name. ## ## TERM The CLICOLOR functionality depends on a terminal type ## with color capabilities. ## ## TZ The timezone to use when displaying dates. See ## environ(7) for more information. ## ## CCOOMMPPAATTIIBBIILLIITTYY ## The group field is now automatically included in the long listing for ## files in order to be compatible with the IEEE Std 1003.2 (``POSIX.2'') ## specification. ## ## LLEEGGAACCYY DDEESSCCRRIIPPTTIIOONN ## In legacy mode, the --ff option does not turn on the --aa option and the --gg, ## --nn, and --oo options do not turn on the --ll option. ## ## Also, the --oo option causes the file flags to be included in a long (-l) ## output; there is no --OO option. ## ## When --HH is specified (and not overridden by --LL or --PP) and a file argument ## is a symlink that resolves to a non-directory file, the output will ## reflect the nature of the link, rather than that of the file. In legacy ## operation, the output will describe the file. ## ## For more information about legacy mode, see compat(5). ## ## SSEEEE AALLSSOO ## chflags(1), chmod(1), sort(1), xterm(1), compat(5), termcap(5), ## symlink(7), sticky(8) ## ## SSTTAANNDDAARRDDSS ## The llss utility conforms to IEEE Std 1003.1-2001 (``POSIX.1''). ## ## HHIISSTTOORRYY ## An llss command appeared in Version 1 AT&T UNIX. ## ## BBUUGGSS ## To maintain backward compatibility, the relationships between the many ## options are quite complex. ## ## BSD May 19, 2002 BSD ``` --- # Help: man (cont.) A useful feature of `man` is quick pattern searching with "/pattern". - Try this now by running `$man ls` again and then typing "/time" and hitting the return key. - To continue on to the next case, hit `n`. -- </br> Again, this and other `man` tricks are detailed in the help pages (hit "h"). --- # Help: cheat I also like the [cheat](https://github.com/cheat/cheat) utility, which provides a more readable summary / cheatsheet of various command. You'll need to install it first. (Linux and MacOS only.) ``` $ cheat ls ## # Displays everything in the target directory ## ls path/to/the/target/directory ## ## # Displays everything including hidden files ## ls -a ## ## # Displays all files, along with the size (with unit suffixes) and timestamp ## ls -lh ## ## # Display files, sorted by size ## ls -S ## ## # Display directories only ## ls -d */ ## ## # Display directories only, include hidden ## ls -d .*/ */ ``` --- class: inverse, center, middle name: remote # Connect to a remote machine <!-- <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --> --- # SSH (Secure shell) ### Definition Cryptographic network protocol for operating network services securely over an unsecured network You will be using it to connect to computing resources (e.g., `mshresearch`), let's try: ``` # Note that you need to be on a USC network or USC VPN ssh username@mshreserch.marshall.usc.edu ``` Once you are logged in, you are in your home, check it with `pwd` You should not save research projects in your home, instead use: 1. `/data/research/ (868G available) 2. `/data-2/research/ (6.9TB available) As you don't have superuser permission (more on this later), you need to tell me to create a project dir for you --- class: inverse, center, middle name: files # Files and directories <!-- <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --> --- # Navigation Key navigation commands: - `pwd` to print (the current) working directory. ```bash pwd ``` ``` ## /Users/dproserp/phd/teaching/mkt-615/lectures/03-shell ``` --- # Navigation (cont.) - `cd` to change directory. You can use absolute paths, but it's better to use relative paths and invoke special symbols for a user's home folder (`~`), current directory (`.`), and parent directory (`..`) as needed. ```bash pwd cd examples ## Move into the "examples" sub-directory of this lecture directory. pwd cd .. ## Now go back up one directories. pwd cd ../.. ## Now go back up two directories. pwd ``` ``` ## /Users/dproserp/phd/teaching/mkt-615/lectures/03-shell ## /Users/dproserp/phd/teaching/mkt-615/lectures/03-shell/examples ## /Users/dproserp/phd/teaching/mkt-615/lectures/03-shell ## /Users/dproserp/phd/teaching/mkt-615 ``` --- # Navigation (cont.) Beware of directory names that contain spaces. Say you have a directory called "My Documents". (I'm looking at you, Windows.) - Why won't `$ cd My Documents` work? -- **Answer:** Bash syntax is super pedantic about spaces and ordering. Here it thinks that "My" and "Documents" are separate arguments. -- Solutions: - Use quotation marks: `$ cd "My Documents"`. - Use Tab completion to automatically "escape" the space: `$ cd My\ Documents`. - **BEST PRACTICE: Don't use spaces in file and folder names!** --- # Listing files and their properties We're about to go into more depth about the `ls` command. - To do this effectively, it will be helpful if we're all working off the same group of files and folders. - Navigate to the directory containing these lecture notes (i.e. `03-shell`). Now list the contents of the `examples/` sub-directory with the `-lh` option ("long format", "human readable"). ```bash # cd PathWhereYouClonedThisRepo/lectures/03-shell ## change as needed ls -lh examples ``` ``` ## total 288 ## drwxr-xr-x@ 3 dproserp staff 96B Nov 13 15:22 ABC ## drwxr-xr-x 3 dproserp staff 96B Nov 13 15:22 copies ## -rwxrwxr-x@ 1 dproserp staff 154B Aug 29 14:37 hello.R ## -rwxr-xr-x@ 1 dproserp staff 30B Aug 24 17:53 hello.sh ## -rwxr-xr-x 1 dproserp staff 73B Sep 24 07:33 loop.sh ## drwxr-xr-x@ 9 dproserp staff 288B Aug 2 09:45 meals ## -rw-r--r--@ 1 dproserp staff 32B Aug 2 09:45 nursery.txt ## -rw-r--r-- 1 dproserp staff 1.8K Aug 11 10:51 output.txt ## -rwxr-xr-x@ 1 dproserp staff 153B Aug 2 09:45 reps.txt ## -rw-r--r--@ 1 dproserp staff 117K Aug 2 09:45 sonnets.txt ``` --- # Listing files and their properties (cont.) We're about to go into more depth about the `ls` command. - To do this effectively, it will be helpful if we're all working off the same group of files and folders. - Navigate to the directory containing these lecture notes (i.e. `03-shell`). Now list the contents of the `examples/` sub-directory with the `-lh` option ("long format", "human readable"). ```bash # cd PathWhereYouClonedThisRepo/lectures/03-shell ## change as needed cd examples ls -lh ``` ``` ## total 288 ## drwxr-xr-x@ 3 dproserp staff 96B Nov 13 15:22 ABC ## drwxr-xr-x 3 dproserp staff 96B Nov 13 15:22 copies ## -rwxrwxr-x@ 1 dproserp staff 154B Aug 29 14:37 hello.R ## -rwxr-xr-x@ 1 dproserp staff 30B Aug 24 17:53 hello.sh ## -rwxr-xr-x 1 dproserp staff 73B Sep 24 07:33 loop.sh ## drwxr-xr-x@ 9 dproserp staff 288B Aug 2 09:45 meals ## -rw-r--r--@ 1 dproserp staff 32B Aug 2 09:45 nursery.txt ## -rw-r--r-- 1 dproserp staff 1.8K Aug 11 10:51 output.txt ## -rwxr-xr-x@ 1 dproserp staff 153B Aug 2 09:45 reps.txt ## -rw-r--r--@ 1 dproserp staff 117K Aug 2 09:45 sonnets.txt ``` --- # Listing files and their properties (cont.) What does this all mean? Let's focus on the top line. ```bash drwxr-xr-x 2 dproserp staff 4.0K Jan 12 22:12 ABC ``` --- count:false # Listing files and their properties (cont.) What does this all mean? Let's focus on the top line. <p span style="font-family:Fira Code; font-size:80%; color: #333; background: #f8f8f8; padding: 0.5em;";> <span style='background: #ffff88;'>d</span>rwxr-xr-x 3 dproserp staff 96B Aug 24 17:05 ABC</span> - The first column denotes the object type: - `d` (directory or folder), `l` (link), or `-` (file) --- count:false # Listing files and their properties (cont.) What does this all mean? Let's focus on the top line. <p span style="font-family:Fira Code; font-size:80%; color: #333; background: #f8f8f8; padding: 0.5em;";>d<span style='background: #ffff88;'><span style='color: #e41a1c;'>rwx</span><span style='color: #377eb8;'>r-x</span><span style='color: #4daf4a;'>r-x</span></span> 3 dproserp staff 96B Aug 24 17:05 ABC</span> - <span style='color: #A9A9A9;'>The first column denotes the object type: - `d` (directory or folder), `l` (link), or `-` (file)</span> - Next, we see the permissions associated with the object's three possible user types: 1) <span style='color: #e41a1c;'>owner</span>, 2) <span style='color: #377eb8;'>the owner's group</span>, and 3) <span style='color: #4daf4a;'>all other users</span>. - Permissions reflect `r` (read), `w` (write), or `x` (execute) access. - <b>`-`</b> denotes missing permissions for a class of operations. --- count:false # Listing files and their properties (cont.) What does this all mean? Let's focus on the top line. <p span style="font-family:Fira Code; font-size:80%; color: #333; background: #f8f8f8; padding: 0.5em;";>drwxr-xr-x <span style='background: #ffff88;'>3</span> dproserp staff 96B Aug 24 17:05 ABC</span> - <span style='color: #A9A9A9;'>The first column denotes the object type: - `d` (directory or folder), `l` (link), or `-` (file)</span> - <span style='color: #A9A9A9;'>Next, we see the permissions associated with the object's three possible user types: 1) owner, 2) the owner's group, and 3) all other users. - Permissions reflect `r` (read), `w` (write), or `x` (execute) access. - <b>`-`</b> denotes missing permissions for a class of operations.</span> - The number of [hard links](https://www.redhat.com/sysadmin/linking-linux-explained) to the object. --- count:false # Listing files and their properties (cont.) What does this all mean? Let's focus on the top line. <p span style="font-family:Fira Code; font-size:80%; color: #333; background: #f8f8f8; padding: 0.5em;";>drwxr-xr-x 3 <span style='background: #ffff88;'><span style='color: #e41a1c;'>dproserp</span> <span style='color: #377eb8;'>staff</span></span> 96B Aug 24 17:05 ABC</span> - <span style='color: #A9A9A9;'>The first column denotes the object type: - `d` (directory or folder), `l` (link), or `-` (file)</span> - <span style='color: #A9A9A9;'>Next, we see the permissions associated with the object's three possible user types: 1) owner, 2) the owner's group, and 3) all other users. - Permissions reflect `r` (read), `w` (write), or `x` (execute) access. - <b>`-`</b> denotes missing permissions for a class of operations.</span> - <span style='color: #A9A9A9;'>The number of hard links to the object.</span> - We also see the identity of the object's <span style='color: #e41a1c;'>owner</span> and their <span style='color: #377eb8;'>group</span>. --- count:false # Listing files and their properties (cont.) What does this all mean? Let's focus on the top line. <p span style="font-family:Fira Code; font-size:80%; color: #333; background: #f8f8f8; padding: 0.5em;";>drwxr-xr-x 3 dproserp staff <span style='background: #ffff88;'>96B Aug 24 17:05 ABC</span></span> - <span style='color: #A9A9A9;'>The first column denotes the object type: - `d` (directory or folder), `l` (link), or `-` (file)</span> - <span style='color: #A9A9A9;'>Next, we see the permissions associated with the object's three possible user types: 1) owner, 2) the owner's group, and 3) all other users. - Permissions reflect `r` (read), `w` (write), or `x` (execute) access. - <b>`-`</b> denotes missing permissions for a class of operations.</span> - <span style='color: #A9A9A9;'>The number of hard links to the object.</span> - <span style='color: #A9A9A9;'>We also see the identity of the object's owner and their group.</span> - Finally, we see some descriptive elements about the object: - Size, date and time of creation, and the object name. --- # Create: touch and mkdir One of the most common shell tasks is object creation (files, directories, etc.) We use `mkdir` to create directories. E.g. To create a new "testing" directory: ```bash # remove directory if exists before creating rm -fR testing mkdir testing ``` We use `touch` to create (empty) files. E.g. To add some files to our new directory: ```bash # remove if file exist before creating it rm -f testing/test1.txt testing/test2.txt testing/test3.txt touch testing/test1.txt testing/test2.txt testing/test3.txt ``` -- Check that it worked: ```bash ls testing ``` ``` ## test1.txt ## test2.txt ## test3.txt ``` --- # Copy: cp The syntax for copying is `$ cp object path/copyname` - If you don't provide a new name for the copied object, it will just take the old name. - However, if there is already an object with the same name in the target destination, then you'll have to use `-f` to force an overwrite. ```bash ## Create new "copies" sub-directory rm -fR examples/copies mkdir examples/copies ## Now copy across a file (with a new name) cp examples/reps.txt examples/copies/reps-copy.txt ## Show that we were successful ls examples/copies ``` ``` ## reps-copy.txt ``` -- You can use `cp` to copy directories, although you'll need the `-r` (or `-R`) flag if you want to recursively copy over everything inside of it to. - Try this by copying over the `meals/` sub-directory to `copies/`. --- # Move (and rename): mv The syntax for moving is `$ mv object path/newobjectname` ```bash ## Move the abc.txt file and show that it worked mv examples/ABC/abc.txt examples ls examples/ABC ## empty ``` ```bash ## Move it back again mv examples/abc.txt examples/ABC ls examples/ABC ## not empty ``` ``` ## abc.txt ``` -- Note that "moving" an object within the same directory, but with the (newobjectname) option, is effectively the same as renaming it. ```bash ## Rename reps-copy to reps2 by "moving" it with a new name mv examples/copies/reps-copy.txt examples/copies/reps2.txt ls examples/copies ``` ``` ## reps2.txt ``` --- # Find (files) The last command that I want to mention w.r.t. navigation is `find`. - This can be used to locate files and directories based on a variety of criteria; from pattern matching to object properties. ```bash find examples -iname "monday.csv" ## will automatically do recursive ``` ``` ## examples/meals/monday.csv ``` ```bash find . -iname "*.txt" ## must use "." to indicate current directory! ``` ``` ## ./testing/test1.txt ## ./testing/test2.txt ## ./testing/test3.txt ## ./examples/ABC/abc.txt ## ./examples/sonnets.txt ## ./examples/reps.txt ## ./examples/nursery.txt ## ./examples/copies/reps2.txt ## ./examples/output.txt ``` --- class: inverse, center, middle name: text # Working with text files <!-- <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --> --- # Counting text: wc You can use the `wc` command to count: 1. lines of text 2. the number of words 3. the number of characters. Let's demonstrate with a text file containing all of Shakespeare's Sonnets.<sup>1</sup> ```bash wc examples/sonnets.txt ``` ``` ## 3029 20701 119751 examples/sonnets.txt ``` .footnote[ <sup>1</sup> Courtesy of [Project Gutenburg](http://www.gutenberg.org/cache/epub/1041/pg1041.txt). ] --- # Find patterns: grep To find patterns in text, we can use regular expression-type matching with `grep`. For example, say we want to find the famous opening line to Shakespeare's [Sonnet 18](https://en.wikipedia.org/wiki/Sonnet_18). - I'm going to include the `-n` ("number") flag to get the line that it occurs on. ```bash grep -n "Shall I compare thee" examples/sonnets.txt ``` ``` ## 336: Shall I compare thee to a summer's day? ``` -- By default, `grep` returns all matching patterns. - What happens if you run `$ grep -n "summer" examples/sonnets.txt`? - Or, for that matter, `$ grep -n "the" examples/sonnets.txt`? --- # Find patterns: grep (cont.) Note that `grep` can be used to identify patterns in a group files (e.g. within a directory) too. - This is particularly useful if you are trying to identify a file that contain, say, a function name. Here's a simple example: Which days will I eat pasta this week? - I'm using the `R` (recursive) and `l` (just list the files; don't print the output) flags. ```bash grep -Rl "pasta" examples/meals ``` ``` ## examples/meals/monday.csv ``` --- class: inverse, center, middle name: pipes # Redirecting and pipes <!-- <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --> --- # Redirect: > You can send output from the shell to a file using the redirect operator `>` For example, let's print a message to the shell using the `echo` command. ```bash echo "At first, I was afraid, I was petrified" ``` ``` ## At first, I was afraid, I was petrified ``` -- If you wanted to save this output to a file, you need simply redirect it to the filename of choice. ```bash echo "At first, I was afraid, I was petrified" > survive.txt find survive.txt ## Show that it now exists ``` ``` ## survive.txt ``` --- # Redirect: > (cont.) If you want to *append* text to an existing file, then you should use `>>`. - Using `>` will try to overwrite the existing file contents. ```bash echo "'Kept thinking I could never live without you by my side" >> survive.txt cat survive.txt ``` ``` ## At first, I was afraid, I was petrified ## 'Kept thinking I could never live without you by my side ``` --- # Pipes: | The pipe operator `|` lets you send (i.e. "pipe") intermediate output to another command. - In other words, it allows us to chain together a sequence of simple operations and thereby implement a more complex operation. (Remember the Unix philosophy!) Let me demonstrate using a very simple example: ```bash find . -type f | wc -l ``` ``` ## 37 ``` --- class: inverse, center, middle name: scripting # Scripting <!-- <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --> --- # Hello World! Writing code and commands interactively in the shell makes a lot of sense when you are exploring data, file structures, etc. However, it's also possible (and often desirable) to write reproducible shell scripts that combine a sequence of commands. - These scripts are demarcated by their `.sh` file extension. -- Let's look at the contents of a short shell script that I've included in the examples folder. ```bash cat examples/hello.sh ``` ``` ## #!/bin/sh ## echo "Hello World!" ``` -- I'm sure that you already have a good idea of what this script is meant to do, but it will prove useful to quickly go through some things together. --- # Hello World! (cont.) ```bash #!/bin/sh echo "Hello World!" ``` - `#!/bin/sh` is a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix), indicating which program to run the command with (here: any Bash-compatible shell). However, it is typically ignored (note that it begins with the hash comment character.) --- count: false # Hello World! (cont.) ```bash #!/bin/sh echo "Hello World!" ``` - `#!/bin/sh` is a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix), indicating which program to run the command with (here: any Bash-compatible shell). However, it is typically ignored (note that it begins with the hash comment character.) - `echo "Hello World!"` is the actual command that we want to run. --- count: false # Hello World! (cont.) ```bash #!/bin/sh echo "Hello World!" ``` - `#!/bin/sh` is a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix), indicating which program to run the command with (here: any Bash-compatible shell). However, it is typically ignored (note that it begins with the hash comment character.) - `echo -e "\nHello World!\n"` is the actual command that we want to run. The `-e` flag tells bash that we want to evaluate an expression rather than a file. To run this simple script, you can just type in the file name and press enter. ```bash examples/hello.sh # bash examples/hello.sh ## Also works ``` ``` ## Hello World! ``` --- name: rscript # Rscript It's important to realize that we aren't limited to running shell scripts in the shell. The exact same principles carry over to other programs and files. The most relevant case for this class is the [`Rscript`](https://stat.ethz.ch/R-manual/R-devel/library/utils/html/Rscript.html) command for (you guessed it) executing R scripts and expressions. For example: ```bash Rscript -e "cat('Hello World, from R')" ``` ``` ## Hello World, from R ``` -- Of course, the more typical `Rscript` use case is to execute full length R scripts. An optional, but very useful feature here is the ability to pass extra arguments from the shell to your R script. Consider the `hello.R` script that I've bundled in the examples folder. ```bash cat examples/hello.R ``` ``` ## args = commandArgs(trailingOnly = TRUE) ## i = args[1]; j = args[2] ## ## cat('Hello World, from R!\n', ## i, '+', j, '=', as.integer(i) + as.integer(j),'\n') ``` --- # Rscript (cont.) The key step for using additional `Rscript` arguments is held within the top two lines. ```r args = commandArgs(trailingOnly = TRUE) i = args[1]; j = args[2] ``` These tell Rscript to capture any trailing arguments (i.e. after the file name) and then pass them on as objects that can be used within R. -- Let's run the script to see it in action. ```bash Rscript examples/hello.R 12 9 ``` ``` ## Hello World, from R! ## 12 + 9 = 21 ``` PS: Add `#!/usr/bin/env Rscript` as the first line to run the script w/o using `Rscript` --- class: inverse, center, middle name: permissions2 # Changing permission <!-- <html><div style='float:left'></div><hr color='#EB811B' size=1px width=796px></html> --> --- # Disclaimer This next section is tailored towards Unix-based operating systems, like Linux or MacOS, which is why I have saved it for the end. <b>Windows users:</b> Don't be surprised if some commands don't work, especially if you haven't installed the [WSL](#windows)... <br> Regardless, the things we learn here will become relevant to everyone (even Windows users) once you start interacting with Linux servers (e.g., mshresearch) --- # The superuser: root, sudo, etc. There are two main user roles on a Linux system: 1. Normal users 2. A superuser (AKA "root") Difference is one of priviledge. - Superusers can make system changes, install software, browse through different users' home folders, etc. Normal users are much more restricted in what they can do. - Explains why Unix-based OS's are much more resilient to security threats like viruses. Need superuser priviledges to install (potentially malicious) software. --- # The superuser: root, sudo, etc. (cont.) - On `mshresearch` you don't have superuser privileges - You **cannot** install packages/software *globally* but you can do so *locally* - e.g., you can install your own Python version - You should have root access on your laptop and you can become superuser by typing: `sudo -s` - We are going to see how to change files and directories permissions. Note that on `mshresearch`, you can do so only for files that you own - If you are working on *mshresearch* you don't need to worry too much about permission (at best you might need to set a script as executable) - Still, it is very useful to see how permissions work --- # Changing permissions and ownership Let's think back to the `ABC/` directory that we saw previously while exploring the ls command. ```bash drwxr-xr-x@ 3 dproserp staff 96B Aug 10 18:57 ABC ``` We can change the permissions and ownership of this folder with the `chmod` and `chown` commands, respectively. We'll now review these in turn. - Note that I'm going to use the "recursive" option (i.e. `-R`) in the examples that follow, but only because `ABC/` is a directory. You can drop that when modifying individual files. -- PS — I'm going to skip writing out the full path for this next section to save on typing. Now is a a good time to move into the `examples/` directory if you want to follow along with my commands literally. ```bash cd examples ``` --- # chmod Changing permissions using `chmod` depends on how those permissions are represented. There are two options: 1) Octal notation and 2) Symbolic notation. - We'll go into more detail on the next slide, but let's see some examples first. - (Test the results yourself using the `ls -lh` command afterwards.) -- **Example 1: rwxrwxrwx.** Read, write and execute permission for all users. - Octal: `$ chmod -R 777 ABC` - Symbolic: `$ chmod -R a=rwx ABC` -- **Example 2: rwxr-xr-x.** Read, write and execute permission for the main user (i.e. owner) of the file. For all other users, read and execute permission only. - Octal: `$ chmod -R 755 ABC` - Symbolic: `$ chmod -R u=rwx,g=rx,o=rx ABC` -- Now that we've seen some examples, let's get into the logic behind them. --- # chmod (cont.) ### Octal notation Takes advantage of the fact that `4` (for "read"), `2` (for "write"), and `1` (for "execute") can be combined in unambiguous ways. - 7 (= 4 + 2 + 1) means read, write and execute permission. - 5 (= 4 + 0 + 1) means read and execute permission, but not write permission. - etc. - Note that Octal notation requires a number for each of the three user types: owner, owner's group, and all others. E.g. `$ chmod 777 myfile.txt` --- # chmod (cont.) ### Symbolic notation Links permissions to different symbols (i.e. abbreviations). - Users: `u` ("User/owner"), `g` ("Group"), `o` ("Others""), `a` ("All") - Permissions: `r` ("read"), `w` ("write"), `x` ("execute") - Changes: `+` ("add permissions"), `-` ("remove permissions"), `=` ("set new permissions") --- # chmod (cont.) Here's a quick comparison table with some common permission levels. | Octal value | Symbolic value | Permission level | |:-----------:|:-----------------:|:----------------:| | 777 | a+rwx | rwxrwxrwx | | 770 | u+rwx,g+rwx,o-rwx | rwxrwx--- | | 755 | a+rwx,g=rw,o=rw | rwxrwxrwx | | 700 | u+rwx,g-rwx,o-rwx | rwx------ | | 644 | u=rw,g=r,o=r | rw-r--r-- | -- </br> PS — Note the Symbolic method allows for relative changes, which means that you don't necessarily need to write out the whole entry in the table above. E.g. To go from the first line to the second line, you'd only need `$ chmod o-rwx myfile`. --- # chown Changing file ownership is somewhat easier than changing permissions, because you don't have to remember the different Octal and Symbolic notation mappings. - E.g. Say there is another user on your computer called "alice", then you could just assign her ownership of the ABC subfolder using: ```bash $ chown -R alice ABC ``` Things get a little more interesting when we want to add new users and groups, or change an existing users group (but this is out of the scope of this course) --- # More resources - [The Unix Shell](http://swcarpentry.github.io/shell-novice/) (Software Carpentery) - [The Unix Workbench](https://seankross.com/the-unix-workbench/) (Sean Kross) - [Data Science at the Command Line](https://www.datascienceatthecommandline.com/) (Jeroen Janssens) <!-- - [Using AWK and R to parse 25tb](https://livefreeordichotomize.com/2019/06/04/using_awk_and_r_to_parse_25tb/) (Nick Strayer) --> --- class: inverse, center, middle name:vim # Vim, running scripts, and monitor resources --- # [Vim](https://www.vim.org/) - Text editor that works on the terminal - Steep curve to learn (I will give you just a brief intro here) Open a file: ```bash vim examples/hello.sh ``` - Exit: `q` (exit w/o saving changes: `q!`) - Save: `w` (save and exit: `wq`) - To start writing (insert): `i` - Exit from current status: Escape - Search: `/pattern` (`n` for next occurrence) - Delete line: `dd` - Copy and paste line: `yyp` - Commands start with colons and can by quite complicated, e.g.: `%s/Hello/Ciao/g` --- name: running #(More on) Running Scripts - Let create a new script called `loop.sh` under the `examples` folder that runs a for loop from 1 to 100, printing `Loop iteration i` at each iteration and wait (sleep) a second between each iteration ```bash vim examples/loop.sh ``` -- ```bash #!/bin/bash for i in {1..100} do echo "Loop iteration $i" sleep 1 done ``` - Now, try to run the script: ```bash dadeusc:examples dproserp$ ./loop.sh ``` --- #(More on) Running Scripts - Does the script run? Why? -- - Let's set the right permissions, e.g.: ```bash chmod 755 loop.sh ``` -- - Now run the script. -- - Now, let's stop it: `crtl c` (it literally kills the script) -- - Start it again -- - **Pause** the script: `ctrl z` - Restart it in the background: `bg 1` - Can you kill it using `ctrl c`? -- (No, you need to bring it to the foreground first: `fg 1` ) --- #(More on) Running Scripts - Let's start `loop.sh` in background: add `&` after the script name (with a space in between) and redirect the output to a files called output.txt: ```bash # Note that I redicrect the output to a file loop.sh > output.txt & ``` - You should see something like this: ```bash dadeusc:examples dproserp$ ./loop.sh > output.txt & [1] 43073 ``` 43073 is the process id (PID) of the script you just run. Very useful to **kill** and **disown** processes: `kill -9 pid` (-9 is a sort of *force kill*) `disown pid` (very useful in cases in which you run a script on the server and close the connection) --- #(More on) Running Scripts How do you check your script is running? Command `ps`: - Run the script in the background again (`./loop.sh > output.txt &`) - Check it is running: `ps x | grep loop.sh`: ```bash (base) dadeusc:examples dproserp$ ps x | grep loop.sh 43678 s000 S 0:00.01 /bin/bash ./loop.sh 43690 s000 S+ 0:00.00 grep loop.sh ``` - If you want learn more abou ps: `man ps` --- # Crontab: Scheduling tasks What if you want to run a script every hour, or every other week, or on a specific schedule? (I use it a lot to setup scrapers) [Crontab](https://man7.org/linux/man-pages/man5/crontab.5.html) is your friend! <div align="center"> <img src="03-shell-files/figures/crontab.png" height=400> </div> --- # Crontab: Scheduling tasks (cont) Open crontab: `crontab -e` (and use vim to edit it) Example (run everyday at 5 o'clock): ``` 0 5 * * * /Users/dproserp/phd/teaching/mkt-615/lectures/03-shell/examples/loop.sh > output.txt ``` PS: Note that I used the absolute path of the script! --- name: monitor # Monitor resources ### Top <div align="center"> <img src="03-shell-files/figures/top.png" height=400> </div> --- name: windows # Bash on Windows Windows users have two options: ### 1. [Git Bash](https://gitforwindows.org/) - **Pros:** You should already have installed this as part of the previous lecture. - **Cons:** Functionality is limited to Git-related commands, so various things that we're going to practice today won't work. ### 2. [Windows Subsystem for Linux (WSL)](https://docs.microsoft.com/en-us/windows/wsl/install-win10) - **Pros:** A self-contained Linux image (terminal) that allows full Bash functionality. - **Cons:** Must be installed first and only available to Windows 10 users. I recommend option 2 (WSL) if available to you. It's more overhead, but I think worth it. See the next two slides for instructions and tips .footnote[(Back to [start](#start).)] --- name:wsl # WSL The basic WSL installation guide is [**here**](https://docs.microsoft.com/en-us/windows/wsl/install-win10). - Follow the guide to install your preferred Linux distro. [Ubuntu](https://www.microsoft.com/en-us/p/ubuntu/9nblggh4msv6) is a good choice. - Then, once you've restarted your PC, come back to these slides. After installing your chosen WSL, you need to navigate to today's lecture directory to run the examples. You have two options: #### Option (i) Access WSL through RStudio (*recommended*) If you access WSL through RStudio, then it will conveniently configure your path to the present working directory. So, here's how to make WSL your default [RStudio Terminal](https://support.rstudio.com/hc/en-us/articles/115010737148-Using-the-RStudio-Terminal): - In RStudio, navigate to: *Tools > Terminal > Terminal Options...*. \[[Screenshot](03-shell-files/figureswsl-rstudio-1.png).\] - Click on the dropdown menu for *New terminals open with* and select "Bash (Windows Subsystem for Linux)", Then click *OK*. \[[Screenshot](03-shell-files/figureswsl-rstudio-2.png).\] - Refresh your RStudio terminal (`Alt+Shift+R`). \[[Screenshot](03-shell-files/figureswsl-rstudio-3.png).\] - You should see the WSL Bash environment with the path automatically configured to the present working director, mount point and all. \[[Screenshot](03-shell-files/figureswsl-rstudio-4.png).\] --- name: wsl-rstudio # WSL (cont.) #### Option (ii) Go directly through the WSL Presumably, you've cloned the course repo somewhere on your C drive. - The way this work is that Windows drives are mounted on the WSL's `mnt` directory. (More [here](https://www.howtogeek.com/261383/how-to-access-your-ubuntu-bash-files-in-windows-and-your-windows-system-drive-in-bash/).) - Say you cloned the repo to "C:\\Users\\Davide\\Desktop\\mkt-615". - The WSL equivalent is "/mnt/c/Users/Davide/Desktop\\mkt-615". - So, then you can navigate to today's lecture through the WSL with: `$ cd /mnt/c/Users/Davide/Desktop/mkt-615/03-shell`. Adjust as needed. .footnote[(Back to [start](#start).)] -- ### Which option to choose? Both are fine, but I recommend option **(i)**. As a Windows user, being able to access a true Bash shell (i.e. terminal) conveniently from RStudio will make things *much* easier for you in my class. You can always revert back to a different shell later if you want.