-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shell Command.Rmd
219 lines (183 loc) · 7.96 KB
/
Shell Command.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
---
title: 'Shell Commands'
author: "Nicholas J. Gotelli"
date: "23 April 2024"
output:
html_document:
highlight: tango
theme: united
pdf_document: default
---
# PS1='$ '
*Most of the following commands can be completed in Windows Powershell. For important information on `cmd` and Powershell for Windows users, see the [Windows cmd Glossary](WindowsCmdGlossary.html).*
___
### Shell preliminaries
- 1971 earliest unix shell
- interface keyboard and a teletype
- faster than mouse, better than gui for complicated or repeated activities
- dominant until appearance of personal computers
- still dominant for critical systems (NASA,hospitals, military, air traffic control)
- uses plain-text files
- important for -omics pipelines and working with large data
- bash shell (= bourne again shell)
### System problems
- iOS no problem because underlying unix
- Windows problem because Windows powershell is not comparable; use `Cygwin`
- RStudio has limited shell installation for use with git but not much else
- compatability problems and minor differences very common
### Learning how to navigate the shell
- Windows systems use mouse and have eagle eye view
- Shell uses keyboard and has an ants eye view
- Learning navigation is single most important thing for working with shell
- Always use tab completion for the names of directories and files (will not work on commands, however)
### The organization of a computer
- Hierarchical tree from root to branch tips with forks
- Files and folders versus files and directories
- All files correspond to a directory, but not vice versa
- Keys to the entire factory
- Hard drive or desktop are not the root of your computer
- Be careful on the factory floor
### The `pwd` command
- shell always points to a specific directory
- There is an absolute path to a directory
- Use forward slashes for unix path specification
- Absolute path traces from root of the computer
- Uses forward slashes (in contrast to Windows)
- `/` indicates top level root of entire program
- `pwd` gives /path/to/current/directory
### Open shell
- `$pwd /Users/nickgotelli
- this always tells me where I am
### The `ls` command
- `ls` lists files and lower level directories in current directory
- demonstrate for my system
### Absolute versus relative path statements
- absolute path always starts at root `/Users/nickgotelli/Desktop`
- absolute paths work from anywhere within your system
- relative path does not start with forward slash but with name of directory within your current directory. If I am in my home directory, I can just use `Desktop` or `Desktop/` to indicate this subdirectory, which is relative to my current path
#### Useful options with `ls`
- `ls -a` shows all including hidden files
- `ls -l` long format with lots of detail
- `lf -R` recursively list subdirectories (careful!)
- `ls -1` list one item per line (useful for programming)
- `ls -G` list files and directories in color
- `ls -F` use symbols for different file types
+ no symbol = file
+ `/` = a directory
+ `*` = an executable shell script
#### EXERCISE: Open shell, do a listing with ls -GF
### The `cd` command
- use this to move around in different subdirectories
- most basic form `cd <directory name>` only works for directories that are "below" your current directory (subdirectories)
#### EXERCISE: Open shell `ls cd Desktop`
- now try moving to a subdirectory on your desktop, followed by ls
### Key built in short-cuts
- shell opens in your `home` directory
- `cd ~` always returns you to home in one jump
- `cd` also takes you home with one less symbol to type
- `/` takes you to top level root of your computer (rarely needed)
- `cd DirectoryA/SubdirectoryOfA` jumps you down two directories in a single step
- `cd ..` moves you up to the next highest directory
- `cd ../..` jumps you up two directories
#### Exercise:
- move down to a folder in your desk top one step at a time
- at each step, check your location (pwd) and list the files(ls)
- now move back up one step at a time all the way to the root
- now can you get back to your desktop folder in one step? something like `cd Users/nickgotelli/Desktop/Directory`
- now jump to your home directory in one step (`cd ~` or just `cd`)
### Using paths in combination with shell commands
- Directory structure can also be used to specify locations or file names for which to operate on!
- `cd ~` takes me to my home directory
- `pwd` shows absolute path to my current location (home)
- `pwd ..` shows absolute path to directory one level up
- `ls ..` lists files in directory one level up
- `cd ..` moves me up to that directory
- `ls` shows the list of files from within that directory
### Creating a file or a directory
- `touch path/to/<filename>` creates a file (with an optional path statement before it)
- `mkdir` makes a directory in the current directory
### Removing a file or a directory
- `rm <filename>` removes a file (can't undo this)
- `rmdir <directoryname>` removes a directory (directory must be empty)
- `rm -r <directoryname>` removes directory and recursively removes all of its subdirectories and files!
### Copying or moving files, folders, or directories
- `cp sourceFile destinationFile` copies file, folder or directory from source location to somewhere else
- `mv sourceFile destinationFile` moves file, folder or directory from source location to somewhere else
### Renaming or save as... files, folders, or directories
- `mv oldName newName` in same location renames file
- `cp oldName newName` in same location saves a copy of the file to a new name
### Using wildcards
- `rm *.txt` removes all .txt files
- `rm F*.txt` removes all .txt files that begin with the letter F
### Modifying the shell (in Mac)
- go to home in the shell
- look for file `.bash_profile`
- be sure to show hidden files from finder to see this
- if not there, create it in your home with a text editor
- save it to your home area
### Nick's `.bash_profile`
```
# execute these commands when the shell is opened
echo "--> Executed Nick's .bash_profile file"
export BASH_SILENCE_DEPRECATION_WARNING=1
alias ls='ls -GF'
PS1="$ "
export PATH="$PATH:$HOME/Dropbox/shellScripts"
source myAliases.sh
```
### Nick's aliases
```
#!/bin/bash
# Collection of useful bash aliases
# Add aliases and then source this file
# directory aliases
alias desk="cd ~/Desktop"
alias dropbox="cd ~/Dropbox"
alias repo="cd ~//Desktop/githubRepos"
# alias for editing this file
alias myalias="cd ~;vim myAliases.sh"
# new alias for git log
alias glog="git log --oneline -6"
# function gitgo for all git commands
gitgo(){
git add -A
git commit -m "$1"
git push
git status
}
```
### Creating your own shell scripts
- modify path statement in your `.bash_profile` to point to a directory where your shell scripts will be stored: `export PATH="$PATH:$HOME/Dropbox/shellScripts"`
- go to the directory and create this script called `trialScript.sh`. Use the `sh` suffix to remind you that this is a shell script
- using your plaintext editor, issue these commands inside your shell script:
```
# Minimal shell script
#! /bin/bash
<<<<<<< HEAD
echo '++++++++++++++++++++++++'
echo 'Trial shell script'
echo 'current date and time: '
date
echo 'current directory: '
pwd
echo 'directory contents: '
ls -AFG
echo '++++++++++++++++++++++++'
```
- be sure to include a final carriage return at the end of the last line:
- change the permissions of the file so it can be executed as a script in bash: `chmod u+x trialScript.sh`
- navigate with the shell to different locations and run the script by typing `trialScript.sh`
- any new scripts should be placed in this same location and will need their permissions changed before they will work
```
echo `++++++++++++++++++++++++`
echo `Trial shell script`
echo `current date and time:
date
echo `current directory: `
pwd
echo `directory contents: `
ls -AFG
echo `++++++++++++++++++++++++`
```
- change the permissions of the file so it can be executed as a script in bash: `chmod u+x trialScript.sh`
- navigate with the shell to different locations and run the script by typing `trialScript.sh`