Unix answers

From 22126
Jump to navigation Jump to search

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 ))