forked from NeuroHackademy2024/NeuroNest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_commands.html
More file actions
228 lines (211 loc) · 12.8 KB
/
bash_commands.html
File metadata and controls
228 lines (211 loc) · 12.8 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
<!DOCTYPE HTML>
<!--
Phantom by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
<head>
<title>Command Line</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="assets/css/main.css" />
<noscript><link rel="stylesheet" href="assets/css/noscript.css" /></noscript>
</head>
<body class="is-preload">
<!-- Wrapper -->
<div id="wrapper">
<!-- Header -->
<header id="header">
<div class="inner">
<!-- Logo -->
<a href="index.html" class="logo">
<span class="symbol"><img src="images/NeuroNestLogo.png" alt="NeuroNest Logo" /></span><span class="title">NeuroNest</span>
</a>
<!-- Nav -->
<nav>
<ul>
<li><a href="#menu">Menu</a></li>
</ul>
</nav>
</div>
</header>
<!-- Menu -->
<nav id="menu">
<h2>Menu</h2>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="resource_menu.html">Resources</a></li>
<li><a href="https://sopkoc.wixsite.com/neuronest/forum">Ask a Question</a></li>
<li><a href="https://sopkoc.wixsite.com/neuronest/about">About NeuroNest</a></li>
<li><a href="https://sopkoc.wixsite.com/neuronest/contact">Contact</a></li>
</ul>
</nav>
<!-- Main -->
<div id="main">
<div class="inner">
<h1>Introduction to Command Line</h1>
<h2 id="learning-commands">1. Learning Commands</h2>
<p>Most commands follow the same format: command, then flags/options, then arguments.</p>
<pre><code>$ command [flags/options] [arguments]</code></pre>
<p><strong>Without Arguments or Options</strong>: Simply type the command and hit <code>return</code>. For example:<p>
<pre><code>$ ls</code></pre>
<p>Because there is no argument, this command would print the names of the files and directories within our the current working directory.</p>
<p><strong>With Arguments</strong>: Commands can accept arguments that modify their behavior. For example, the <code>cd</code> command changes the directory, requiring a path as an argument:<p>
<pre><code>$ ls ~/path/to/directory </code></pre>
<p>By including the file path in the argument, we are specifying what directory we would like to see. This code will print the contents of "directory", even though it is not our current working directory.</p>
<p><strong>With Flags </strong>: Flags modify the way a command runs. They start with <code>-</code> followed by a letter, or <code>--</code> followed by a word. For example:<p>
<pre><code>$ ls -l </code></pre>
<p> The <code>-l</code> flag makes <code>ls</code> display detailed information about the contents of the current working directory.
<p><strong>With Arguments and Flags </strong>: You can combine arguments and flags. Flags generally precede any arguments passed to a UNIX command. For example, to <i>list all files</i>, including hidden ones (<code>-a flag</code>), in the <i>Desktop</i> directory:<p>
<pre><code>$ ls -a Desktop </code></pre>
<p><strong>Combing Flags</strong>: We can combine multiple flags with a single - (dash), with no spaces between flags. For example:</p>
<pre><code>$ ls -Fa </code></pre>
<p>In this example, <code>ls -F</code> prints each type of file, where directories are indicated with a <code>/</code> before the name. In addition, <code>ls -a</code> prints all files, including the hidden ones.</p>
<h2 id="command-examples">2.Basic Examples</h2>
<p><strong>Example #1.</strong>Move into the 'Desktop' directory.</p>
<pre><code>$ cd Desktop </code> or <code>cd /Users/name/Desktop</code></pre>
<p><strong>Example #2.</strong> Make a new directory. Note: avoid using spaces in file names; underscores are also not recommended.</p>
<pre><code>$ mkdir -p ~/Desktop/practice/example</code></pre>
<p>In this example, we are attempting to create a directory named "example". By adding the <code>-p</code> flag, we are able to create a path of directories. Instead of only making one directory, "example", we are able to create a parent directory "practice" <i>and</i> the subdirectory "example" in one line of code. Otherwise, our could would look like this (which is also an acceptable way to make directories):
<pre><code>$ cd ~/Desktop
$ mkdir practice
$ cd practice
$ mkdir example
$ </code></pre>
<p><strong>Example #3.</strong>Copy a file. In the first argument, put the original name of the file. In the second argument, put the name of the new duplicate file (and new file path if desired).</p>
<pre><code>$ cp [original] [new]</code></pre>
<p><strong>Example #4</strong> Copy a directory and all its contents.</p>
<pre><code>$ cp -r [source] [destination]</code></pre>
<p><strong>Example #5.</strong> Rename or copy a file or directory to a new location. In the first argument, put the original file location and name. In the second argument, put the new location (and new name, if you want).</p>
<pre><code>mv [old].txt ../[new].txt</code></pre>
<p><strong>Example #6.</strong> Remove a file permanently</p>
<pre><code>$ rm [path]</code></pre>
<p><strong>Example #7.</strong> Remove a file or directory permanently. The <code>-r</code> flag signals "recursive", resulting in permanently deleting the directory and its contents.</p>
<pre><code>$ rm -rf [path]</code></pre>
<p>Important Note: Deleted files are gone forever! There is no trash or recycle bin. The <code>-i</code> flag initiates <i>interactive mode</i>, which forces confirmation before permanent deletion. </p>
<pre><code>$ rm -i [path]</code></pre>
<li><code>$ touch [filename]</code>: create an empty file
<ul>
<p>Useful for generating placeholder files required by some programs.</p>
</ul></li>
<li><code>$ nano [filename]</code>: Create or edit a file. If the file does not exist, it will be created. Type your content, then save and exit.
Shortcut Keys in Nano:
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Ctrl + O</code></td>
<td>Save the file (into a current or new name).</td>
</tr>
<tr>
<td><code>Ctrl + X</code></td>
<td>Exit the editor. If unsaved, nano will prompt to save.</td>
</tr>
<tr>
<td><code>Ctrl + K</code></td>
<td>Cut (“kill”) a line of text. Repeated use cuts multiple lines.</td>
</tr>
<tr>
<td><code>Ctrl + U</code></td>
<td>Paste the cut text.</td>
</tr>
</tbody>
</table>
</li></li></ul>
<ul>Wildcard Characters
<li> <code>*</code>: Matches zero or more characters. Example: <code>*.txt</code> matches all <code>.txt</code> files.</li>
<li><code>?</code>: Matches any single character. Example: <code>?.txt</code> matches <code>a.txt</code> but not <code>any.txt</code>.</li>
</ul>
<p></p>
<ul>
<li><code>$ cat</code>: print the contents of files.
<ul><li>Example: <code>cat file1.txt file2.txt</code></li></ul></li>
<li><code>$ more </code>: similar to cat, but displays one page at a time, use spacebar to continue </li>
<li><code>$ less -N [file]</code>: displays content page by page with line numbers. Use q to exit.</li>
<li><code>$ sort [file]</code>: sorts file contents alphanumerically.
<ul>Use -n for numerical sorting.</ul></li>
<li><code>$ head [file]</code>: displays the first 10 lines.
<ul>Use -n [#] to specify the number of lines.</ul></li>
<li><code>$ tail [file]</code>: displays the last 10 lines.
<ul>Use -n [#] to specify the number of lines.</ul></li>
<p></p><h2 id="variables">3 Variables</h2>
<p>A variable in the shell is a reference to actual data. You can create, assign, and delete variables without specifying a data type. Variable names can contain letters (a-z, A-Z), numbers (0-9), and underscores (_). However, a variable name must start with a letter or an underscore, not a number.</p>
<p><strong>Declaring Variables</strong>: to create a variable, use the following syntax:</p>
<pre><code>$ [variable_name]=value</code><p>Note: No spaces are allowed betwen the variable name, the equals sign, and the value. Use consistent and simple filenames to facilitate looping.</p></pre>
<p><strong>Accessing Values</strong>: to retrieve the value of a variable, prefix its name with a dollar sign <code>$</code>, as seen below </p>
<pre><code>$ echo $variable_name</code></pre>
<h2>4. Pipes and Filters</h2>
<ul>
<li>Pipe Operator: <code>$ [command] | [command]</code>: Redirect the output of one command into another.
<ul><li>Example: <code>ls | wc</code></li></ul>
<ul>Counts lines, words, and characters in the output of ls.</ul>
</li>
<li><code>$ command > [file]</code>: Redirect output into a file. Note: this will overwrite the file.</li>
<li><code>$ command >> [file]</code>: Append output to a file</li>
<li><code>$ wc [file]</code>: Count lines, words, and characters.
<lu>Example: <code>$ wc -l *.txt</code> Output the line count for every file.</lu></li>
<li><code>grep [pattern] [file]</code>: Search for a pattern in files.
<lu>Use -v to exclude lines with the pattern, <p></p>-i for case-insensitivity.</p></lu></li>
</ul>
<p>Examples:
<lu>
<li><code>ls | grep "D" | wc</code>: counts the number of words containing "D" in the ls output.</li>
<li><code>$ ls -l > filename.list</code>: Redirect output to a file:</li>
<li><code>$ cat filename.list | grep keyword > filefound.list</code>: Pipe output to another command</li>
<li><code>$ cut -d , -f 2 [file].csv | sort | uniq</code>: Remove duplicates</li>
</lu></p>
<h2 id="for loops">5. For Loops</h2>
<p>For Loops allow us to execute a series of commands repeatedly for each item in a list. For loops are often used in scripts. Below is a for loop syntax template. In this example, <code>'variable'</code> represents each item in the <code>'list'</code> during each iteration</p>
<pre><code>for variable in list
do
[commands]
done
</code></pre>
<p><strong>Variable Expansion</strong>: Use <code>$variable</code> or <code>${variable}</code> to access the value of a variable. For example:</p>
<pre><code>for filename in file1 file2 file3
do
echo $filename
head -n 2 $filename | tail -n 1 >> $filename.txt
done
</code></pre>
<ul>
<li>For each file ('file 1', 'file 2', 'file 3'), this loop prints the file name. Then, it extracts the second line of the file (by getting the first 2 lines and then taking the last line) and appends it to a new file, <code>$filename.txt</code>.</li>
<li>Avoid using spaces, quotes, or wildcard characters, <code>*, ?</code>, in filenames to simplify variable expansion.</li>
</ul>
</div>
</div>
<!-- Footer -->
<footer id="footer">
<div class="inner">
<section>
<h2>Funding</h2>
<p> We would like to express our heartfelt gratitude to <strong>Neurohackademy</strong> at the <strong>University of Washington eScience Institute</strong> for providing invaluable training and support. This experience has significantly enriched our understanding of neuroimaging and data science. We also acknowledge the support of the National Institute of Mental Health (NIMH) grant number <strong>5R25MH112480-08</strong>, which made this opportunity possible.</p>
</section>
<section>
<h2>Follow</h2>
<ul class="icons">
<li><a href="https://x.com/Neuro_Nest" class="icon brands style2 fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="https://github.com/NeuroHackademy2024/NeuroNest" class="icon brands style2 fa-github"><span class="label">GitHub</span></a></li>
<li><a href="mailto:sopkoc@umich.edu" class="icon solid style2 fa-envelope"><span class="label">Email</span></a></li>
</ul>
</section>
<ul class="copyright">
<li>© Untitled. All rights reserved</li><li>Design: <a href="http://html5up.net">HTML5 UP</a></li>
</ul>
</div>
</footer>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/browser.min.js"></script>
<script src="assets/js/breakpoints.min.js"></script>
<script src="assets/js/util.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>