{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The Unix Shell: File and Directory Management" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Listing files" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "example.fna\tiris.csv\tiris24.csv\n" ] } ], "source": [ "ls data" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "avg.sh\t\t\tcat_if_exists.sh\textract_headers.sh\n" ] } ], "source": [ "ls scripts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Include hidden files" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".\n", "..\n", ".DS_Store\n", ".gitignore\n", ".ipynb_checkpoints\n", "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls -a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exclude current and parent directory" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".DS_Store\n", ".gitignore\n", ".ipynb_checkpoints\n", "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls -A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Show details" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "total 280\n", "-rw-r--r-- 1 cliburn staff 0 Jun 9 08:23 0\n", "-rw-r--r-- 1 cliburn staff 7378 May 2 17:21 01_The_Unix_Shell.ipynb\n", "-rw-r--r-- 1 cliburn staff 11613 Jun 7 18:22 01_The_Unix_Shell_Solutions.ipynb\n", "-rw-r--r-- 1 cliburn staff 27356 Jun 9 09:01 The Unix Shell - File and Directory Management.ipynb\n", "-rw-r--r-- 1 cliburn staff 23128 Jun 8 18:25 The Unix Shell - Finding Stuff.ipynb\n", "-rw-r--r-- 1 cliburn staff 18214 Jun 8 11:29 The Unix Shell - Getting Help.ipynb\n", "-rw-r--r-- 1 cliburn staff 23346 Jun 9 08:50 The Unix Shell - Shell Scripts.ipynb\n", "-rw-r--r-- 1 cliburn staff 8386 Jun 8 14:04 The Unix Shell - Working with Text.ipynb\n", "drwxr-xr-x 6 cliburn staff 204 Jun 9 08:38 data\n", "-rw-r--r-- 1 cliburn staff 45 Jun 8 11:58 hello.txt\n", "-rw-r--r-- 1 cliburn staff 0 Jun 8 12:03 one two three\n", "drwxr-xr-x 6 cliburn staff 204 Jun 9 08:46 scripts\n", "-rw-r--r-- 1 cliburn staff 42 Jun 8 11:58 stderr.txt\n", "-rw-r--r-- 1 cliburn staff 0 Jun 8 11:58 stdout.txt\n", "-rw-r--r-- 1 cliburn staff 50 Jun 8 12:06 test1.txt\n" ] } ], "source": [ "ls -l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sort by last modified time" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "total 280\n", "-rw-r--r-- 1 cliburn staff 27356 Jun 9 09:01 The Unix Shell - File and Directory Management.ipynb\n", "-rw-r--r-- 1 cliburn staff 23346 Jun 9 08:50 The Unix Shell - Shell Scripts.ipynb\n", "drwxr-xr-x 6 cliburn staff 204 Jun 9 08:46 scripts\n", "drwxr-xr-x 6 cliburn staff 204 Jun 9 08:38 data\n", "-rw-r--r-- 1 cliburn staff 0 Jun 9 08:23 0\n", "-rw-r--r-- 1 cliburn staff 23128 Jun 8 18:25 The Unix Shell - Finding Stuff.ipynb\n", "-rw-r--r-- 1 cliburn staff 8386 Jun 8 14:04 The Unix Shell - Working with Text.ipynb\n", "-rw-r--r-- 1 cliburn staff 50 Jun 8 12:06 test1.txt\n", "-rw-r--r-- 1 cliburn staff 0 Jun 8 12:03 one two three\n", "-rw-r--r-- 1 cliburn staff 42 Jun 8 11:58 stderr.txt\n", "-rw-r--r-- 1 cliburn staff 0 Jun 8 11:58 stdout.txt\n", "-rw-r--r-- 1 cliburn staff 45 Jun 8 11:58 hello.txt\n", "-rw-r--r-- 1 cliburn staff 18214 Jun 8 11:29 The Unix Shell - Getting Help.ipynb\n", "-rw-r--r-- 1 cliburn staff 11613 Jun 7 18:22 01_The_Unix_Shell_Solutions.ipynb\n", "-rw-r--r-- 1 cliburn staff 7378 May 2 17:21 01_The_Unix_Shell.ipynb\n" ] } ], "source": [ "ls -lt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Human readable output" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "total 280\n", "-rw-r--r-- 1 cliburn staff 27K Jun 9 09:01 The Unix Shell - File and Directory Management.ipynb\n", "-rw-r--r-- 1 cliburn staff 23K Jun 9 08:50 The Unix Shell - Shell Scripts.ipynb\n", "drwxr-xr-x 6 cliburn staff 204B Jun 9 08:46 scripts\n", "drwxr-xr-x 6 cliburn staff 204B Jun 9 08:38 data\n", "-rw-r--r-- 1 cliburn staff 0B Jun 9 08:23 0\n", "-rw-r--r-- 1 cliburn staff 23K Jun 8 18:25 The Unix Shell - Finding Stuff.ipynb\n", "-rw-r--r-- 1 cliburn staff 8.2K Jun 8 14:04 The Unix Shell - Working with Text.ipynb\n", "-rw-r--r-- 1 cliburn staff 50B Jun 8 12:06 test1.txt\n", "-rw-r--r-- 1 cliburn staff 0B Jun 8 12:03 one two three\n", "-rw-r--r-- 1 cliburn staff 42B Jun 8 11:58 stderr.txt\n", "-rw-r--r-- 1 cliburn staff 0B Jun 8 11:58 stdout.txt\n", "-rw-r--r-- 1 cliburn staff 45B Jun 8 11:58 hello.txt\n", "-rw-r--r-- 1 cliburn staff 18K Jun 8 11:29 The Unix Shell - Getting Help.ipynb\n", "-rw-r--r-- 1 cliburn staff 11K Jun 7 18:22 01_The_Unix_Shell_Solutions.ipynb\n", "-rw-r--r-- 1 cliburn staff 7.2K May 2 17:21 01_The_Unix_Shell.ipynb\n" ] } ], "source": [ "ls -lth" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recursive listing" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n", "\n", "./data:\n", "example.fna\tiris.csv\tiris24.csv\n", "\n", "./scripts:\n", "avg.sh\t\t\tcat_if_exists.sh\textract_headers.sh\n" ] } ], "source": [ "ls -R" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Globbing\n", "\n", "The use of wild cards to specify Unix paths is known as globbing." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `*` represets any number of characters" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n" ] } ], "source": [ "ls *ipynb" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data/example.fna\tdata/iris.csv\t\tdata/iris24.csv\n" ] } ], "source": [ "ls data/*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `?` represents exactly one character" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data/iris24.csv\n" ] } ], "source": [ "ls data/iris??.csv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Character sets\n", "\n", "- `[abc]` represents a or b or c\n", "- [a-z] represents any lower case character\n", "- `!` negates" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\t\t\t\t\t01_The_Unix_Shell_Solutions.ipynb\n", "01_The_Unix_Shell.ipynb\n" ] } ], "source": [ "ls [0-9]*" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "hello.txt\n", "one two three\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n", "\n", "data:\n", "example.fna\tiris.csv\tiris24.csv\n", "\n", "scripts:\n", "avg.sh\t\t\tcat_if_exists.sh\textract_headers.sh\n" ] } ], "source": [ "ls [!0-9]*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Spitting paths" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/cliburn/_teach/bios-821-2017/lessons\n" ] } ], "source": [ "pwd" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/cliburn/_teach/bios-821-2017\n" ] } ], "source": [ "dirname $(pwd)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lessons\n" ] } ], "source": [ "basename $(pwd)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/cliburn/_teach/bios-821\n" ] } ], "source": [ "pwd | xargs dirname" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "lessons\n" ] } ], "source": [ "pwd | xargs basename" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Directory navigation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Show current directory" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/cliburn/_teach/bios-821/lessons\n" ] } ], "source": [ "pwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Move to parent directory" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cd .." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/cliburn/_teach/bios-821\n" ] } ], "source": [ "pwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Move back to last directory" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/cliburn/_teach/bios-821/lessons\n" ] } ], "source": [ "cd -" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Move using relative addressing" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cd data" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/cliburn/_teach/bios-821/lessons/data\n" ] } ], "source": [ "pwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Move using absolute addressing" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cd /Users/cliburn/_teach/bios-821/lessons" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/cliburn/_teach/bios-821/lessons\n" ] } ], "source": [ "pwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Making and removing new directories" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mkdir foo" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "foo\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Making intermediate directories automatically" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mkdir -p a/b/c/d" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b\n", "\n", "a/b:\n", "c\n", "\n", "a/b/c:\n", "d\n", "\n", "a/b/c/d:\n" ] } ], "source": [ "ls -R a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Deleting directories" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rmdir foo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Only works if directory is empty\n", "\n", "The `| cat` part is not necessary on the command line, but is only used here for convenience of Run All Cells as Jupyter stops on non-zero exit codes. The `| cat` syntax \"pipes\" the output of `rmdir data` to a the `cat` program." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "rmdir: data: Directory not empty\n" ] } ], "source": [ "rmdir data | cat" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recursive intermediate directories as well" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rmdir -p a/b/c/d" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working with files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Making an empty file" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "touch foo.txt" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "foo.txt\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Deleting a file" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rm foo.txt" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Viewing a file" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#!/bin/bash \n", "sum=0\n", "n=0\n", "while read line\n", "do\n", " if [ ! -z \"$line\" ]\n", " then\n", " n=$((n+1))\n", " sum=$(echo $sum + $line | bc)\n", " fi\n", "done\n", "echo $sum / $n | bc -l\n" ] } ], "source": [ "cat scripts/avg.sh" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "#!/bin/bash \n", "sum=0\n", "n=0\n" ] } ], "source": [ "head -n 3 scripts/avg.sh" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " fi\n", "done\n", "echo $sum / $n | bc -l\n" ] } ], "source": [ "tail -n 3 scripts/avg.sh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Can start tail form a specified line number with (+)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "while read line\n", "do\n", " if [ ! -z \"$line\" ]\n", " then\n", " n=$((n+1))\n", " sum=$(echo $sum + $line | bc)\n", " fi\n", "done\n", "echo $sum / $n | bc -l\n" ] } ], "source": [ "tail -n +4 scripts/avg.sh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Copying and moving files" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Copying files" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cp \"The Unix Shell - File and Directory Management.ipynb\" The_Unix_Shell.ipynb" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "The_Unix_Shell.ipynb\n", "data\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Copying directories (Recursive copy)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cp -R data data2" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "The_Unix_Shell.ipynb\n", "data\n", "data2\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n", "\n", "./data:\n", "example.fna\tiris.csv\tiris24.csv\n", "\n", "./data2:\n", "example.fna\tiris.csv\tiris24.csv\n", "\n", "./scripts:\n", "avg.sh\t\t\tcat_if_exists.sh\textract_headers.sh\n" ] } ], "source": [ "ls -R" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Renaming a file" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mv The_Unix_Shell.ipynb The_Unix_Shell_Copy.ipynb" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "The_Unix_Shell_Copy.ipynb\n", "data\n", "data2\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n" ] } ], "source": [ "ls" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Move a file to a new location" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mv The_Unix_Shell_Copy.ipynb scripts" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "data2\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n", "\n", "./data:\n", "example.fna\tiris.csv\tiris24.csv\n", "\n", "./data2:\n", "example.fna\tiris.csv\tiris24.csv\n", "\n", "./scripts:\n", "The_Unix_Shell_Copy.ipynb\tcat_if_exists.sh\n", "avg.sh\t\t\t\textract_headers.sh\n" ] } ], "source": [ "ls -R" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## File compression and archival" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Combine multiple files into single file" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "example.fna\tiris.csv\tiris24.csv\n" ] } ], "source": [ "ls data" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "BSDTAR(1) BSD General Commands Manual BSDTAR(1)\n", "\n", "NAME\n", " tar -- manipulate tape archives\n", "\n", "SYNOPSIS\n", " tar [bundled-flags ] [ | ...]\n", " tar {-c} [options] [files | directories]\n", " tar {-r | -u} -f archive-file [options] [files | directories]\n", " tar {-t | -x} [options] [patterns]\n", "\n", "DESCRIPTION\n", " tar creates and manipulates streaming archive files. This implementation\n", " can extract from tar, pax, cpio, zip, jar, ar, and ISO 9660 cdrom images\n", " and can create tar, pax, cpio, ar, and shar archives.\n", "\n", " The first synopsis form shows a ``bundled'' option word. This usage is\n", " provided for compatibility with historical implementations. See COMPATI-\n", " BILITY below for details.\n" ] } ], "source": [ "man tar | head -n 20" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a data\n", "a data/.ipynb_checkpoints\n", "a data/example.fna\n", "a data/iris.csv\n", "a data/iris24.csv\n" ] } ], "source": [ "tar -cvf data.tar data" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rm -rf data/" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data.tar\n", "\n", "data2:\n", "example.fna\tiris.csv\tiris24.csv\n" ] } ], "source": [ "ls data*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compress concatenated file" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": true }, "outputs": [], "source": [ "gzip data.tar" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data.tar.gz\n", "\n", "data2:\n", "example.fna\tiris.csv\tiris24.csv\n" ] } ], "source": [ "ls data*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Uncompress" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": true }, "outputs": [], "source": [ "gunzip data.tar.gz" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data.tar\n", "\n", "data2:\n", "example.fna\tiris.csv\tiris24.csv\n" ] } ], "source": [ "ls data*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recover original files" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x data/\n", "x data/.ipynb_checkpoints/\n", "x data/example.fna\n", "x data/iris.csv\n", "x data/iris24.csv\n" ] } ], "source": [ "tar -xvf data.tar" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data.tar\n", "\n", "data:\n", "example.fna\tiris.csv\tiris24.csv\n", "\n", "data2:\n", "example.fna\tiris.csv\tiris24.csv\n" ] } ], "source": [ "ls data*" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rm data.tar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Concatenate and compress" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a data\n", "a data/.ipynb_checkpoints\n", "a data/example.fna\n", "a data/iris.csv\n", "a data/iris24.csv\n" ] } ], "source": [ "tar -cvzf data.tar.gz data" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rm -rf data/" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data.tar.gz\n", "\n", "data2:\n", "example.fna\tiris.csv\tiris24.csv\n" ] } ], "source": [ "ls data*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Uncompress and recover" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x data/\n", "x data/.ipynb_checkpoints/\n", "x data/example.fna\n", "x data/iris.csv\n", "x data/iris24.csv\n" ] } ], "source": [ "tar -xvzf data.tar.gz" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data.tar.gz\n", "\n", "data:\n", "example.fna\tiris.csv\tiris24.csv\n", "\n", "data2:\n", "example.fna\tiris.csv\tiris24.csv\n" ] } ], "source": [ "ls data*" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rm data.tar.gz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Clean up" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rm scripts/The_Unix_Shell_Copy.ipynb" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rm -r data2" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "01_The_Unix_Shell.ipynb\n", "01_The_Unix_Shell_Solutions.ipynb\n", "The Unix Shell - File and Directory Management.ipynb\n", "The Unix Shell - Finding Stuff.ipynb\n", "The Unix Shell - Getting Help.ipynb\n", "The Unix Shell - Shell Scripts.ipynb\n", "The Unix Shell - Working with Text.ipynb\n", "data\n", "hello.txt\n", "one two three\n", "scripts\n", "stderr.txt\n", "stdout.txt\n", "test1.txt\n", "\n", "./data:\n", "example.fna\tiris.csv\tiris24.csv\n", "\n", "./scripts:\n", "avg.sh\t\t\tcat_if_exists.sh\textract_headers.sh\n" ] } ], "source": [ "ls -R" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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 }