Unix answers: Difference between revisions
(Created page with " 1. Use a text editor to (nedit/gedit/komodo/textwrangler) to create a file mycommands.txt where you write all commands and observations you do in the following exercises. Use copy/paste to copy the commands. Note: There are more standard text editors than nedit, etc. Examples are emacs, xemacs, vi, vim, and pico. Make sure that we can easily see which exercise you attempt to solve. 2. First list the files in the directory. <pre> ls </pre> 3. Copy ex1.acc to myfile.ac...") |
No edit summary |
||
| Line 1: | Line 1: | ||
== Possible Answers to UNIX Exercises == | |||
1. Use | 1. Use any plain-text editor (nano, vim, emacs, Notepad++, TextEdit in plain text mode, etc.) to create ''mycommands.txt'' and paste all commands you run. | ||
Make sure it is clear which exercise each section belongs to. | |||
Make sure | |||
2. First list the files in the directory. | 2. First list the files in the directory. | ||
| Line 19: | Line 19: | ||
<pre> | <pre> | ||
cat ex1.acc | cat ex1.acc | ||
cat myfile.acc | cat myfile.acc | ||
diff ex1.acc myfile.acc | |||
# or: | |||
md5sum | md5sum ex1.acc myfile.acc | ||
</pre> | </pre> | ||
5. Copy ex1.dat to myfile.acc (overwrite). | |||
5. Copy ex1.dat to myfile.acc. | |||
<pre> | <pre> | ||
cp ex1.dat myfile.acc | cp ex1.dat myfile.acc | ||
</pre> | </pre> | ||
6. Check that the content of myfile.acc changed. | 6. Check that the content of myfile.acc changed. | ||
<pre> | |||
diff ex1.dat myfile.acc | |||
# or view: | |||
head myfile.acc | |||
</pre> | |||
7. Delete myfile.acc. | 7. Delete myfile.acc. | ||
| Line 44: | Line 46: | ||
</pre> | </pre> | ||
8. Make a directory test and move the three files | 8. Make a directory ''test'' and move the three files into it. | ||
<pre> | <pre> | ||
mkdir test | mkdir test | ||
mv ex1.acc ex1.dat orphans.sp test/ | |||
mv ex1.acc | |||
</pre> | </pre> | ||
9. Make a directory ''data'' and move the three files to that instead. | |||
9. Make a directory data and move the three files to that instead. | |||
<pre> | <pre> | ||
mkdir data | mkdir data | ||
mv test/* data/ | mv test/* data/ | ||
</pre> | </pre> | ||
| Line 67: | Line 63: | ||
<pre> | <pre> | ||
rmdir test | rmdir test | ||
</pre> | </pre> | ||
11. Change directory to data and confirm that you succeeded. Then go back. | |||
11. Change directory to data and confirm that you | |||
<pre> | <pre> | ||
cd data | cd data | ||
pwd | pwd | ||
ls | |||
cd - | cd - | ||
#or | # or: | ||
cd ~ | cd ~ | ||
</pre> | </pre> | ||
12. Make three | 12. Make three nested directories “newtest” like Russian dolls. | ||
<pre> | <pre> | ||
mkdir newtest | mkdir -p newtest/one/two | ||
</pre> | </pre> | ||
13. Move the data directory to the innermost newtest directory. | 13. Move the ''data'' directory to the innermost newtest directory. | ||
<pre> | <pre> | ||
mv data newtest/one/two/ | |||
mv data | |||
</pre> | </pre> | ||
14. Confirm the files are inside newtest/one/two/data. | |||
14. Confirm | |||
<pre> | <pre> | ||
ls newtest/ | ls newtest/one/two/data | ||
</pre> | </pre> | ||
15. Copy the three files to your home | 15. Copy the three files back to your home directory. | ||
<pre> | <pre> | ||
cp newtest/ | cp newtest/one/two/data/* . | ||
</pre> | </pre> | ||
16. Remove all newtest directories and data | 16. Remove all newtest directories and data with a single safe command. | ||
<pre> | <pre> | ||
rm - | rm -r newtest | ||
</pre> | </pre> | ||
| Line 132: | Line 110: | ||
<pre> | <pre> | ||
wc -l ex1.acc ex1.dat | |||
wc -l ex1.acc | |||
</pre> | </pre> | ||
18. Concatenate | 18. Concatenate acc and dat files into ex1.tot. | ||
<pre> | <pre> | ||
| Line 144: | Line 119: | ||
</pre> | </pre> | ||
19. Merge/ | 19. Merge/paste acc and dat side-by-side. | ||
<pre> | <pre> | ||
paste ex1.acc ex1.dat > ex1.tot | paste ex1.acc ex1.dat > ex1.tot | ||
</pre> | |||
Verify: | |||
<pre> | |||
head ex1.acc | head ex1.acc | ||
head ex1.dat | head ex1.dat | ||
head ex1.tot | head ex1.tot | ||
</pre> | |||
20. Extract column 1 and 5 from ex1.tot into ex1.res. | |||
<pre> | |||
cut -f1,5 ex1.tot > ex1.res | |||
</pre> | </pre> | ||
21. Find the 3 highest values (column 2) from ex1.res. | |||
<pre> | <pre> | ||
sort -k2,2nr ex1.res | head -3 | |||
</pre> | </pre> | ||
22. Count GenBank accessions in orphans.sp (should be 85). | |||
A more accurate regex: | |||
<pre> | <pre> | ||
grep -Eo "[A-Z]{1,2}[0-9]{5,6}" orphans.sp | wc -l | |||
</pre> | </pre> | ||
23. Count human genes with SwissProt IDs; count how many are hypothetical. | |||
<pre> | <pre> | ||
grep | grep "_HUMAN" orphans.sp | wc -l | ||
grep "_HUMAN" orphans.sp | grep -i "HYPOTHETICAL" | wc -l | |||
grep | |||
grep | |||
</pre> | </pre> | ||
24. Count rat genes and precursors. | |||
<pre> | <pre> | ||
grep | grep "_RAT" orphans.sp | wc -l | ||
grep "_RAT" orphans.sp | grep -i PRECURSOR | wc -l | |||
grep | |||
</pre> | </pre> | ||
25. Split ex1.res into positive and negative values. | |||
Cleaner version than cat | grep: | |||
<pre> | <pre> | ||
grep | grep "-" ex1.res > ex1.neg | ||
grep -v "-" ex1.res > ex1.pos | |||
grep | |||
</pre> | </pre> | ||
26. Arithmetic directly in the shell. | |||
<pre> | <pre> | ||
echo $(( (356+51)*123 - 12765 )) | |||
echo $(( ((356+51)*123 - 12765) / 56 )) | |||
</pre> | </pre> | ||
Latest revision as of 15:46, 19 November 2025
Possible Answers to UNIX Exercises
1. Use any plain-text editor (nano, vim, emacs, Notepad++, TextEdit in plain text mode, etc.) to create mycommands.txt and paste all commands you run. Make sure it is clear which exercise each section belongs to.
2. First list the files in the directory.
ls
3. Copy ex1.acc to myfile.acc.
cp ex1.acc myfile.acc
4. Look at the content of both files to ensure they are identical.
cat ex1.acc cat myfile.acc diff ex1.acc myfile.acc # or: md5sum ex1.acc myfile.acc
5. Copy ex1.dat to myfile.acc (overwrite).
cp ex1.dat myfile.acc
6. Check that the content of myfile.acc changed.
diff ex1.dat myfile.acc # or view: head myfile.acc
7. Delete myfile.acc.
rm myfile.acc
8. Make a directory test and move the three files into it.
mkdir test mv ex1.acc ex1.dat orphans.sp test/
9. Make a directory data and move the three files to that instead.
mkdir data mv test/* data/
10. Remove test directory.
rmdir test
11. Change directory to data and confirm that you succeeded. Then go back.
cd data pwd ls cd - # or: cd ~
12. Make three nested directories “newtest” like Russian dolls.
mkdir -p newtest/one/two
13. Move the data directory to the innermost newtest directory.
mv data newtest/one/two/
14. Confirm the files are inside newtest/one/two/data.
ls newtest/one/two/data
15. Copy the three files back to your home directory.
cp newtest/one/two/data/* .
16. Remove all newtest directories and data with a single safe command.
rm -r newtest
17. Count the lines in ex1.acc and ex1.dat.
wc -l ex1.acc ex1.dat
18. Concatenate acc and dat files into ex1.tot.
cat ex1.acc ex1.dat > ex1.tot
19. Merge/paste acc and dat side-by-side.
paste ex1.acc ex1.dat > ex1.tot
Verify:
head ex1.acc head ex1.dat head ex1.tot
20. Extract column 1 and 5 from ex1.tot into ex1.res.
cut -f1,5 ex1.tot > ex1.res
21. Find the 3 highest values (column 2) from ex1.res.
sort -k2,2nr ex1.res | head -3
22. Count GenBank accessions in orphans.sp (should be 85).
A more accurate regex:
grep -Eo "[A-Z]{1,2}[0-9]{5,6}" orphans.sp | wc -l
23. Count human genes with SwissProt IDs; count how many are hypothetical.
grep "_HUMAN" orphans.sp | wc -l grep "_HUMAN" orphans.sp | grep -i "HYPOTHETICAL" | wc -l
24. Count rat genes and precursors.
grep "_RAT" orphans.sp | wc -l grep "_RAT" orphans.sp | grep -i PRECURSOR | wc -l
25. Split ex1.res into positive and negative values.
Cleaner version than cat | grep:
grep "-" ex1.res > ex1.neg grep -v "-" ex1.res > ex1.pos
26. Arithmetic directly in the shell.
echo $(( (356+51)*123 - 12765 )) echo $(( ((356+51)*123 - 12765) / 56 ))