{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# File I/O" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using an editor" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using redirection" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\u001b[01;34mdata\u001b[0m\n", "\u001b[01;34mfigs\u001b[0m\n", "R00_Review_Basics.ipynb\n", "R00_Review_Basics_Scratch.ipynb\n", "R01_Data_Manipulation.ipynb\n", "R01_Data_Manipulation_Scratch.ipynb\n", "R01_Data_Manipulation_Solutions.ipynb\n", "R01_Manipulating_Data_In_R.ipynb\n", "R02_Tidying_Data_In_R.ipynb\n", "R02_Tidying_Data.ipynb\n", "R02_Tidying_Data_Solutions.ipynb\n", "R03_FileIO.ipynb\n", "R04_Unsupervised_Learning.ipynb\n", "R04_Unsupervised_Learning_Scratch.ipynb\n", "R05_Unsupervised_Learning_More_Examples.ipynb\n", "R06_Graphics_Overview.ipynb\n", "R07_Graphics_Base.ipynb\n", "R08_Graphics_ggplot2.ipynb\n", "R09_Graphics_Exercise.ipynb\n", "R09_Graphics_Exercise_Solutions.ipynb\n", "\u001b[01;34mseqs\u001b[0m\n", "Unix01_File_And_Directory.ipynb\n", "Unix01_File_And_Directory_Solutions.ipynb\n", "Unix02_FileIO.ipynb\n", "Unix02_FileIO_Solutions.ipynb\n", "Unix03_File_Storage.ipynb\n", "Unix03_File_Storage_Solutions.ipynb\n", "Unix04_Text_Manipulation.ipynb\n", "Unix04_Text_Manipulation_Solutinos.ipynb\n", "Unix05_Variables.ipynb\n", "Unix05_Variables_Solutions.ipynb\n", "Unix06_Bash_Bioinformatics.ipynb\n", "Unix07_Capstone_Exercise.ipynb\n", "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n" ] } ], "source": [ "ls" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Unix\n" ] } ], "source": [ "echo \"Hello Unix\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "echo \"Hello Unix\" > foo.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using `heredocs`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "cat > bar.txt << EOF\n", "This old man he played one\n", "He played knick knack on my thumb\n", "EOF" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## View file contents" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Unix\n" ] } ], "source": [ "cat foo.txt" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This old man he played one\n", "He played knick knack on my thumb\n" ] } ], "source": [ "cat bar.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More redirection" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "echo \"I love bash\" >> foo.txt" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "cat >> bar.txt << EOF\n", "This old man he played two\n", "He played knick knack on my shoe\n", "EOF" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## View file contents" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Unix\n", "I love bash\n" ] } ], "source": [ "cat foo.txt" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This old man he played one\n", "He played knick knack on my thumb\n", "This old man he played two\n", "He played knick knack on my shoe\n" ] } ], "source": [ "cat bar.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More redirection" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "echo \"Many people hate bash\" > foo.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oops, you've over-written the original contents of foo.txt and they are lost forever." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Many people hate bash\n" ] } ], "source": [ "cat foo.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Safety\n", "\n", "If you file contents are precious and should not be changed (e..g raw data), you can change permisions to read-only." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-rw-r--r-- 1 jovyan users 22 Jun 26 09:15 foo.txt\n" ] } ], "source": [ "ls -l foo.txt" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "chmod -w foo.txt" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-r--r--r-- 1 jovyan users 22 Jun 26 09:15 foo.txt\n" ] } ], "source": [ "ls -l foo.txt" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bash: foo.txt: Permission denied\n" ] }, { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "echo \"Try to overwrite\" > foo.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Redirecting command output" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bar.txt\n", "\u001b[0m\u001b[01;34mdata\u001b[0m\n", "\u001b[01;34mfigs\u001b[0m\n", "foo.txt\n", "R00_Review_Basics.ipynb\n", "R00_Review_Basics_Scratch.ipynb\n", "R01_Data_Manipulation.ipynb\n", "R01_Data_Manipulation_Scratch.ipynb\n", "R01_Data_Manipulation_Solutions.ipynb\n", "R01_Manipulating_Data_In_R.ipynb\n", "R02_Tidying_Data_In_R.ipynb\n", "R02_Tidying_Data.ipynb\n", "R02_Tidying_Data_Solutions.ipynb\n", "R03_FileIO.ipynb\n", "R04_Unsupervised_Learning.ipynb\n", "R04_Unsupervised_Learning_Scratch.ipynb\n", "R05_Unsupervised_Learning_More_Examples.ipynb\n", "R06_Graphics_Overview.ipynb\n", "R07_Graphics_Base.ipynb\n", "R08_Graphics_ggplot2.ipynb\n", "R09_Graphics_Exercise.ipynb\n", "R09_Graphics_Exercise_Solutions.ipynb\n", "\u001b[01;34mseqs\u001b[0m\n", "Unix01_File_And_Directory.ipynb\n", "Unix01_File_And_Directory_Solutions.ipynb\n", "Unix02_FileIO.ipynb\n", "Unix02_FileIO_Solutions.ipynb\n", "Unix03_File_Storage.ipynb\n", "Unix03_File_Storage_Solutions.ipynb\n", "Unix04_Text_Manipulation.ipynb\n", "Unix04_Text_Manipulation_Solutinos.ipynb\n", "Unix05_Variables.ipynb\n", "Unix05_Variables_Solutions.ipynb\n", "Unix06_Bash_Bioinformatics.ipynb\n", "Unix07_Capstone_Exercise.ipynb\n", "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n" ] } ], "source": [ "ls" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "ls > contents.txt" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bar.txt\n", "contents.txt\n", "data\n", "figs\n", "foo.txt\n", "R00_Review_Basics.ipynb\n", "R00_Review_Basics_Scratch.ipynb\n", "R01_Data_Manipulation.ipynb\n", "R01_Data_Manipulation_Scratch.ipynb\n", "R01_Data_Manipulation_Solutions.ipynb\n", "R01_Manipulating_Data_In_R.ipynb\n", "R02_Tidying_Data_In_R.ipynb\n", "R02_Tidying_Data.ipynb\n", "R02_Tidying_Data_Solutions.ipynb\n", "R03_FileIO.ipynb\n", "R04_Unsupervised_Learning.ipynb\n", "R04_Unsupervised_Learning_Scratch.ipynb\n", "R05_Unsupervised_Learning_More_Examples.ipynb\n", "R06_Graphics_Overview.ipynb\n", "R07_Graphics_Base.ipynb\n", "R08_Graphics_ggplot2.ipynb\n", "R09_Graphics_Exercise.ipynb\n", "R09_Graphics_Exercise_Solutions.ipynb\n", "seqs\n", "Unix01_File_And_Directory.ipynb\n", "Unix01_File_And_Directory_Solutions.ipynb\n", "Unix02_FileIO.ipynb\n", "Unix02_FileIO_Solutions.ipynb\n", "Unix03_File_Storage.ipynb\n", "Unix03_File_Storage_Solutions.ipynb\n", "Unix04_Text_Manipulation.ipynb\n", "Unix04_Text_Manipulation_Solutinos.ipynb\n", "Unix05_Variables.ipynb\n", "Unix05_Variables_Solutions.ipynb\n", "Unix06_Bash_Bioinformatics.ipynb\n", "Unix07_Capstone_Exercise.ipynb\n", "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n" ] } ], "source": [ "cat contents.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Redirecting error messages" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat: foobar.txt: No such file or directory\n" ] }, { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "cat foobar.txt" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat: foobar.txt: No such file or directory\n" ] }, { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "cat foobar.txt > error.txt" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "error.txt\n" ] } ], "source": [ "ls e*txt" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "cat error.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Why is error.txt it empty?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The standard `>` is short for `1>` which means re-direct the standard output stream." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat: foobar.txt: No such file or directory\n" ] }, { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "cat foobar.txt 1> stdout.txt" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "cat stdout.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To redirect the standard error stream, you need `2>``." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "cat foobar.txt 2> stderr.txt" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat: foobar.txt: No such file or directory\n" ] } ], "source": [ "cat stderr.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can redirect to both." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "cat foobar.txt > results.txt 2> errors.txt" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "cat results.txt" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat: foobar.txt: No such file or directory\n" ] } ], "source": [ "cat errors.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also redirect both streams to the same file with `&>`." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "cat foobar.txt &> combined.txt" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cat: foobar.txt: No such file or directory\n" ] } ], "source": [ "cat combined.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple text operations" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bar.txt\n", "contents.txt\n", "data\n", "figs\n", "foo.txt\n", "R00_Review_Basics.ipynb\n", "R00_Review_Basics_Scratch.ipynb\n", "R01_Data_Manipulation.ipynb\n", "R01_Data_Manipulation_Scratch.ipynb\n", "R01_Data_Manipulation_Solutions.ipynb\n", "R01_Manipulating_Data_In_R.ipynb\n", "R02_Tidying_Data_In_R.ipynb\n", "R02_Tidying_Data.ipynb\n", "R02_Tidying_Data_Solutions.ipynb\n", "R03_FileIO.ipynb\n", "R04_Unsupervised_Learning.ipynb\n", "R04_Unsupervised_Learning_Scratch.ipynb\n", "R05_Unsupervised_Learning_More_Examples.ipynb\n", "R06_Graphics_Overview.ipynb\n", "R07_Graphics_Base.ipynb\n", "R08_Graphics_ggplot2.ipynb\n", "R09_Graphics_Exercise.ipynb\n", "R09_Graphics_Exercise_Solutions.ipynb\n", "seqs\n", "Unix01_File_And_Directory.ipynb\n", "Unix01_File_And_Directory_Solutions.ipynb\n", "Unix02_FileIO.ipynb\n", "Unix02_FileIO_Solutions.ipynb\n", "Unix03_File_Storage.ipynb\n", "Unix03_File_Storage_Solutions.ipynb\n", "Unix04_Text_Manipulation.ipynb\n", "Unix04_Text_Manipulation_Solutinos.ipynb\n", "Unix05_Variables.ipynb\n", "Unix05_Variables_Solutions.ipynb\n", "Unix06_Bash_Bioinformatics.ipynb\n", "Unix07_Capstone_Exercise.ipynb\n", "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n" ] } ], "source": [ "cat contents.txt" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bar.txt\n", "contents.txt\n", "data\n" ] } ], "source": [ "head -3 contents.txt" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n" ] } ], "source": [ "tail -3 contents.txt" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 39 39 1089 contents.txt\n" ] } ], "source": [ "wc contents.txt" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "39 contents.txt\n" ] } ], "source": [ "wc -l contents.txt" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "39 contents.txt\n" ] } ], "source": [ "wc -w contents.txt" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1089 contents.txt\n" ] } ], "source": [ "wc -c contents.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you don't want the filename, force it to read from standard input." ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1089\n" ] } ], "source": [ "wc -c < contents.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Piping" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "26\n" ] } ], "source": [ "head -3 contents.txt | wc -c" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "head -3 contents.txt | wc -c > word_count.txt" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "26\n" ] } ], "source": [ "cat word_count.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Clean up" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bar.txt\n", "combined.txt\n", "contents.txt\n", "\u001b[0m\u001b[01;34mdata\u001b[0m\n", "errors.txt\n", "error.txt\n", "\u001b[01;34mfigs\u001b[0m\n", "foo.txt\n", "R00_Review_Basics.ipynb\n", "R00_Review_Basics_Scratch.ipynb\n", "R01_Data_Manipulation.ipynb\n", "R01_Data_Manipulation_Scratch.ipynb\n", "R01_Data_Manipulation_Solutions.ipynb\n", "R01_Manipulating_Data_In_R.ipynb\n", "R02_Tidying_Data_In_R.ipynb\n", "R02_Tidying_Data.ipynb\n", "R02_Tidying_Data_Solutions.ipynb\n", "R03_FileIO.ipynb\n", "R04_Unsupervised_Learning.ipynb\n", "R04_Unsupervised_Learning_Scratch.ipynb\n", "R05_Unsupervised_Learning_More_Examples.ipynb\n", "R06_Graphics_Overview.ipynb\n", "R07_Graphics_Base.ipynb\n", "R08_Graphics_ggplot2.ipynb\n", "R09_Graphics_Exercise.ipynb\n", "R09_Graphics_Exercise_Solutions.ipynb\n", "results.txt\n", "\u001b[01;34mseqs\u001b[0m\n", "stderr.txt\n", "stdout.txt\n", "Unix01_File_And_Directory.ipynb\n", "Unix01_File_And_Directory_Solutions.ipynb\n", "Unix02_FileIO.ipynb\n", "Unix02_FileIO_Solutions.ipynb\n", "Unix03_File_Storage.ipynb\n", "Unix03_File_Storage_Solutions.ipynb\n", "Unix04_Text_Manipulation.ipynb\n", "Unix04_Text_Manipulation_Solutinos.ipynb\n", "Unix05_Variables.ipynb\n", "Unix05_Variables_Solutions.ipynb\n", "Unix06_Bash_Bioinformatics.ipynb\n", "Unix07_Capstone_Exercise.ipynb\n", "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n", "word_count.txt\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fancy globbing" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "shopt -s extglob" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Specifying what NOT to list." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bar.txt contents.txt error.txt results.txt stdout.txt\n", "combined.txt errors.txt foo.txt stderr.txt word_count.txt\n", "\n", "data:\n", "gene_counts_raw.txt gene_counts.txt\n", "\n", "figs:\n", "\u001b[0m\u001b[01;35mbox.png\u001b[0m \u001b[01;35mfig1.png\u001b[0m \u001b[01;35mfig2.png\u001b[0m \u001b[01;35mfig3.png\u001b[0m \u001b[01;35mfig4.png\u001b[0m \u001b[01;35mfig5.png\u001b[0m g3.pdf \u001b[01;35mlm.png\u001b[0m\n", "\n", "seqs:\n", "CM000046.1.fa CM000047.1.fa\n" ] } ], "source": [ "ls !(*.ipynb)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "R00_Review_Basics.ipynb\n", "R00_Review_Basics_Scratch.ipynb\n", "R01_Data_Manipulation.ipynb\n", "R01_Data_Manipulation_Scratch.ipynb\n", "R01_Data_Manipulation_Solutions.ipynb\n", "R01_Manipulating_Data_In_R.ipynb\n", "R02_Tidying_Data_In_R.ipynb\n", "R02_Tidying_Data.ipynb\n", "R02_Tidying_Data_Solutions.ipynb\n", "R03_FileIO.ipynb\n", "R04_Unsupervised_Learning.ipynb\n", "R04_Unsupervised_Learning_Scratch.ipynb\n", "R05_Unsupervised_Learning_More_Examples.ipynb\n", "R06_Graphics_Overview.ipynb\n", "R07_Graphics_Base.ipynb\n", "R08_Graphics_ggplot2.ipynb\n", "R09_Graphics_Exercise.ipynb\n", "R09_Graphics_Exercise_Solutions.ipynb\n", "Unix01_File_And_Directory.ipynb\n", "Unix01_File_And_Directory_Solutions.ipynb\n", "Unix02_FileIO.ipynb\n", "Unix02_FileIO_Solutions.ipynb\n", "Unix03_File_Storage.ipynb\n", "Unix03_File_Storage_Solutions.ipynb\n", "Unix04_Text_Manipulation.ipynb\n", "Unix04_Text_Manipulation_Solutinos.ipynb\n", "Unix05_Variables.ipynb\n", "Unix05_Variables_Solutions.ipynb\n", "Unix06_Bash_Bioinformatics.ipynb\n", "Unix07_Capstone_Exercise.ipynb\n", "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n", "\n", "data:\n", "gene_counts_raw.txt gene_counts.txt\n", "\n", "figs:\n", "\u001b[0m\u001b[01;35mbox.png\u001b[0m \u001b[01;35mfig1.png\u001b[0m \u001b[01;35mfig2.png\u001b[0m \u001b[01;35mfig3.png\u001b[0m \u001b[01;35mfig4.png\u001b[0m \u001b[01;35mfig5.png\u001b[0m g3.pdf \u001b[01;35mlm.png\u001b[0m\n", "\n", "seqs:\n", "CM000046.1.fa CM000047.1.fa\n" ] } ], "source": [ "ls !(*.txt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Specifying alternatives." ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bar.txt\n", "combined.txt\n", "contents.txt\n", "errors.txt\n", "error.txt\n", "foo.txt\n", "R00_Review_Basics.ipynb\n", "R00_Review_Basics_Scratch.ipynb\n", "R01_Data_Manipulation.ipynb\n", "R01_Data_Manipulation_Scratch.ipynb\n", "R01_Data_Manipulation_Solutions.ipynb\n", "R01_Manipulating_Data_In_R.ipynb\n", "R02_Tidying_Data_In_R.ipynb\n", "R02_Tidying_Data.ipynb\n", "R02_Tidying_Data_Solutions.ipynb\n", "R03_FileIO.ipynb\n", "R04_Unsupervised_Learning.ipynb\n", "R04_Unsupervised_Learning_Scratch.ipynb\n", "R05_Unsupervised_Learning_More_Examples.ipynb\n", "R06_Graphics_Overview.ipynb\n", "R07_Graphics_Base.ipynb\n", "R08_Graphics_ggplot2.ipynb\n", "R09_Graphics_Exercise.ipynb\n", "R09_Graphics_Exercise_Solutions.ipynb\n", "results.txt\n", "stderr.txt\n", "stdout.txt\n", "Unix01_File_And_Directory.ipynb\n", "Unix01_File_And_Directory_Solutions.ipynb\n", "Unix02_FileIO.ipynb\n", "Unix02_FileIO_Solutions.ipynb\n", "Unix03_File_Storage.ipynb\n", "Unix03_File_Storage_Solutions.ipynb\n", "Unix04_Text_Manipulation.ipynb\n", "Unix04_Text_Manipulation_Solutinos.ipynb\n", "Unix05_Variables.ipynb\n", "Unix05_Variables_Solutions.ipynb\n", "Unix06_Bash_Bioinformatics.ipynb\n", "Unix07_Capstone_Exercise.ipynb\n", "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n", "word_count.txt\n" ] } ], "source": [ "ls *{.txt,.ipynb}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Clean up" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rm: cannot remove 'data': Is a directory\n", "rm: cannot remove 'figs': Is a directory\n", "rm: cannot remove 'seqs': Is a directory\n" ] }, { "ename": "", "evalue": "1", "output_type": "error", "traceback": [] } ], "source": [ "rm -f !(*.ipynb)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\u001b[01;34mdata\u001b[0m\n", "\u001b[01;34mfigs\u001b[0m\n", "R00_Review_Basics.ipynb\n", "R00_Review_Basics_Scratch.ipynb\n", "R01_Data_Manipulation.ipynb\n", "R01_Data_Manipulation_Scratch.ipynb\n", "R01_Data_Manipulation_Solutions.ipynb\n", "R01_Manipulating_Data_In_R.ipynb\n", "R02_Tidying_Data_In_R.ipynb\n", "R02_Tidying_Data.ipynb\n", "R02_Tidying_Data_Solutions.ipynb\n", "R03_FileIO.ipynb\n", "R04_Unsupervised_Learning.ipynb\n", "R04_Unsupervised_Learning_Scratch.ipynb\n", "R05_Unsupervised_Learning_More_Examples.ipynb\n", "R06_Graphics_Overview.ipynb\n", "R07_Graphics_Base.ipynb\n", "R08_Graphics_ggplot2.ipynb\n", "R09_Graphics_Exercise.ipynb\n", "R09_Graphics_Exercise_Solutions.ipynb\n", "\u001b[01;34mseqs\u001b[0m\n", "Unix01_File_And_Directory.ipynb\n", "Unix01_File_And_Directory_Solutions.ipynb\n", "Unix02_FileIO.ipynb\n", "Unix02_FileIO_Solutions.ipynb\n", "Unix03_File_Storage.ipynb\n", "Unix03_File_Storage_Solutions.ipynb\n", "Unix04_Text_Manipulation.ipynb\n", "Unix04_Text_Manipulation_Solutinos.ipynb\n", "Unix05_Variables.ipynb\n", "Unix05_Variables_Solutions.ipynb\n", "Unix06_Bash_Bioinformatics.ipynb\n", "Unix07_Capstone_Exercise.ipynb\n", "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Create 3 files with the same contents (say `lsd1.txt`, `lsd2.txt`, `lsd3.txt`)\n", "\n", "```\n", "Two of the most famous products of Berkeley are LSD and Unix. \n", "I don’t think that this is a coincidence.\n", "```\n", "\n", "using \n", "\n", "- A text editor\n", "- Echo\n", "- A HereDoc" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "cat > lsd1.txt << EOF\n", "Two of the most famous products of Berkeley are LSD and Unix. \n", "I don’t think that this is a coincidence.\n", "EOF" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "cp lsd1.txt lsd2.txt \n", "cp lsd1.txt lsd3.txt" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[0m\u001b[01;34mdata\u001b[0m\n", "\u001b[01;34mfigs\u001b[0m\n", "lsd1.txt\n", "lsd2.txt\n", "lsd3.txt\n", "R00_Review_Basics.ipynb\n", "R00_Review_Basics_Scratch.ipynb\n", "R01_Data_Manipulation.ipynb\n", "R01_Data_Manipulation_Scratch.ipynb\n", "R01_Data_Manipulation_Solutions.ipynb\n", "R01_Manipulating_Data_In_R.ipynb\n", "R02_Tidying_Data_In_R.ipynb\n", "R02_Tidying_Data.ipynb\n", "R02_Tidying_Data_Solutions.ipynb\n", "R03_FileIO.ipynb\n", "R04_Unsupervised_Learning.ipynb\n", "R04_Unsupervised_Learning_Scratch.ipynb\n", "R05_Unsupervised_Learning_More_Examples.ipynb\n", "R06_Graphics_Overview.ipynb\n", "R07_Graphics_Base.ipynb\n", "R08_Graphics_ggplot2.ipynb\n", "R09_Graphics_Exercise.ipynb\n", "R09_Graphics_Exercise_Solutions.ipynb\n", "\u001b[01;34mseqs\u001b[0m\n", "Unix01_File_And_Directory.ipynb\n", "Unix01_File_And_Directory_Solutions.ipynb\n", "Unix02_FileIO.ipynb\n", "Unix02_FileIO_Solutions.ipynb\n", "Unix03_File_Storage.ipynb\n", "Unix03_File_Storage_Solutions.ipynb\n", "Unix04_Text_Manipulation.ipynb\n", "Unix04_Text_Manipulation_Solutinos.ipynb\n", "Unix05_Variables.ipynb\n", "Unix05_Variables_Solutions.ipynb\n", "Unix06_Bash_Bioinformatics.ipynb\n", "Unix07_Capstone_Exercise.ipynb\n", "Unix07_Capstone_Exercise_Solutions.ipynb\n", "Unix_Appendix01_Regular_Expressions.ipynb\n", "Unix_Appendix02_Review.ipynb\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Count the number of words in the second sentence only of lsd1.txt" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "tail -1 lsd1.txt |\n", "wc -w" ] } ], "metadata": { "kernelspec": { "display_name": "Bash", "language": "bash", "name": "bash" }, "language_info": { "codemirror_mode": "shell", "file_extension": ".sh", "mimetype": "text/x-sh", "name": "bash" } }, "nbformat": 4, "nbformat_minor": 2 }