-
Notifications
You must be signed in to change notification settings - Fork 2
/
bash-tutorial.html
251 lines (202 loc) · 10.2 KB
/
bash-tutorial.html
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
<!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>Bash Tutorial</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>Generic Page</h1>
<span class="image main"><img src="images/pic13.jpg" alt="" /></span>
<h1>Bash Command Line and Scripting Tutorial</h1>
<h2>1. Download Dataset</h2>
<p>This tutorial will help you learn how to use Bash command line and scripting to work with the <a href="https://openneuro.org/datasets/ds000157/versions/00001" target="_blank">ds000157 dataset</a> available at OpenNeuro. The dataset is in BIDS (Brain Imaging Data Structure) format, used for neuroimaging data.
For more information on Open Science and Data Management, visit our <a href="https://neurohackademy2024.github.io/NeuroNest/open_neuro.html">OpenNeuro</a> and <a href="https://neurohackademy2024.github.io/NeuroNest/bids_data.html">BIDS</a> introduction pages.</p>
<p>Find the <a href="https://openneuro.org/datasets/ds000157/versions/00001">"ds000157" dataset on OpenNeuro.</a> This dataset includes NIfTI files for each subject’s functional and anatomical MRI scans, organized in <a href="bids_data.html">BIDS format</a>.</p>
<p><img width="600" alt="image" src="https://github.com/user-attachments/assets/42300427-c8c2-488c-83b1-49190113f1c0"></p>
<p>You need to download the <em><a href="https://openneuro.org/datasets/ds000157/versions/00001/download">entire OpenNeuro dataset</a></em>, either directly with your browser or using DataLad. Note: this may take a few minutes.</p>
<h4 id="option-1-browser">Option 1: Browser</h4>
<p> <img width="600" alt="image" src="https://github.com/user-attachments/assets/70f82229-58cf-4c32-a9b6-d3c91f724046"></p>
<p>If you download the data directly with your browser, ensure that the files are unzipped correctly in a folder named <code>ds000157</code>.</p>
<h4 id="option-2-datalad">Option 2: DataLad</h4>
<p><img width="773" alt="image" src="https://github.com/user-attachments/assets/4e841224-520b-4313-8d1d-68ef6658321f"></p>
<p>If you do not already have <a href="datalad_data.html">DataLad</a> installed, you will need to install it using your terminal. Then, download the dataset using DataLad and navigate to the dataset directory. You will then download the dataset contents recursively.</p>
<pre><code class="lang-bash"><span class="hljs-meta"># Install datalad</span>
$ pip install datalad
<span class="hljs-meta"># Download the dataset</span>
$ datalad install https:<span class="hljs-comment">//github.com/OpenNeuroDatasets/ds000157.git</span>
$ cd ds000157
datalad <span class="hljs-keyword">get</span> -r .
</code></pre>
<p>For more detailed instructions on using DataLad to download OpenNeuro datasets, refer to the <a href="https://handbook.datalad.org/en/latest/usecases/openneuro.html">DataLad Handbook</a>.</p>
<p>Once the dataset is downloaded, ensure all the files are in a folder named <code>ds000157</code>. For this tutorial, we'll assume you have extracted it into <code>~/Downloads/ds000157</code>.</p>
<p>This OpenNeuro dataset was collected by the <a href="http://www.openfmri.org">OpenfMRI project</a>. For the study, 30 female subjects performed a passive viewing task with blcoks of food andnonfood images.
Cite: Smeets PA, Kroese FM, Evers C, de Ridder DT. Behav Brain Res. 2013 Jul 1;248:41-5. doi: 10.1016/j.bbr.2013.03.041. Epub 2013 Apr 8. http://www.ncbi.nlm.nih.gov/pubmed/23578759</p>
<h2>2. Navigating Directories</h2>
<p>To start, open terminal and navigate to the Downloads directory</p>
<pre><code>$ cd ~/Downloads </code></pre>
<p>Let's take a look inside:</p>
<pre><code>$ ls -tr</code></pre>
<p>In this code, <code>ls</code> lists the files and directories within Downloads, while <code>-t</code> sorts by modification time, newest first and <code>-r OR --reverse</code> prints in reverse order while sorting. Therefore, the output of <code>ls -ltr</code> is a list with the most recently modified files and directories at the bottom!</p>
<p>Now let's see whats inside the </p>
<pre><code># Navigate to the dataset directory
cd ~/datasets/ds000157
# List files and directories
ls
# If you need more details
ls -lh
# To navigate to a specific directory
cd sub-01/func
</code></pre>
<h2>3. Viewing Files and Directories</h2>
<p>You might need to view the contents of files or check the structure.</p>
<pre><code># Display the first few lines of a file
head sub-01_task-rest_bold.nii.gz
# Display the last few lines of a file
tail sub-01_task-rest_bold.nii.gz
# Check the structure of a directory
tree
</code></pre>
<h2>4. File Management</h2>
<p>Copy, move, or remove files as needed.</p>
<pre><code># Copy a file
cp sub-01_task-rest_bold.nii.gz ~/my_data_backup/
# Move a file
mv sub-01_task-rest_bold.nii.gz ~/my_data_backup/
# Remove a file
rm ~/my_data_backup/sub-01_task-rest_bold.nii.gz
# Remove a directory and its contents
rm -r ~/my_data_backup/
</code></pre>
<h2>5. Searching for Files</h2>
<p>Find files based on their names or patterns.</p>
<pre><code># Find all .nii.gz files in the directory
find . -name "*.nii.gz"
# Search for a specific file
find . -name "sub-01_task-rest_bold.nii.gz"
</code></pre>
<h2>6. Viewing and Editing Files</h2>
<p>For BIDS datasets, you might need to inspect metadata or configuration files.</p>
<pre><code># View the content of a text file
cat dataset_description.json
# Use a pager to view large files
less dataset_description.json
# Edit a file using nano text editor
nano dataset_description.json
</code></pre>
<h2>7. Scripting with Bash</h2>
<p>Automate tasks using Bash scripts. Create a file named <code>process_data.sh</code> and make it executable.</p>
<pre><code># Create a new Bash script file
nano process_data.sh
# Add the following script to the file
#!/bin/bash
# Navigate to the dataset directory
cd ~/datasets/ds000157
# Find and list all .nii.gz files
echo "Listing all .nii.gz files:"
find . -name "*.nii.gz"
# Backup all .nii.gz files to a backup directory
mkdir -p ~/my_data_backup
find . -name "*.nii.gz" -exec cp {} ~/my_data_backup/ \;
# Print a completion message
echo "Backup completed."
# Save and exit nano (Ctrl+X, then Y, then Enter)
# Make the script executable
chmod +x process_data.sh
# Run the script
./process_data.sh
</code></pre>
<h2>8. Using Commands in Scripts</h2>
<p>Sometimes you need to include commands in a script for more complex operations.</p>
<pre><code># Example of a more complex script
#!/bin/bash
# Example of a more complex script
echo "Starting data processing..."
# Check disk space
df -h
# Count files in a directory
file_count=$(ls | wc -l)
echo "Number of files in the directory: $file_count"
# Compress all .nii.gz files into a tarball
tar -cvzf fMRI_data_backup.tar.gz *.nii.gz
echo "Data processing completed."
</code></pre>
<h2>9. Advanced Commands</h2>
<p>You might need more advanced processing depending on your needs.</p>
<pre><code># Find files modified in the last 7 days
find . -type f -mtime -7
# Execute a command on each file found
find . -name "*.nii.gz" -exec gzip -d {} \;
</code></pre>
<h2>Conclusion</h2>
<p>This tutorial covers basic Bash commands and scripting for managing and processing the BIDS-format fMRI dataset. As you become more familiar with Bash, you can create more complex scripts to automate and analyze your data.</p>
<p>If you need more specific examples or have questions about particular tasks, feel free to ask!</p>
</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:[email protected]" 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>