Research Bazaar Arizona 2021
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: ResBaz2021 --- # Introduction to Unix Shell: Automating the boring parts ## Monday, May 17th, 2021 3\:00-5\:00 [Back to Resbaz HackMD Directory](https://hackmd.io/@ResBaz21/directory) ## Workshop Summary In this workshop participants will be introduced to; * the Unix shell command-line, * some essential command-line operations, * navigating the file system using the shell, * writing shell scripts to automate some of those command-line operations. --- No prior command-line experience is required. Mac and Linux users will already have a Unix shell terminal installed. If you have a Windows machine, we ask you to download Git-Bash (https://git-scm.com/downloads, a Unix Shell for Windows) ahead of the workshop. ## Getting Started ### Introductions - Patrick Bunn, UArizona PhD student in Hydrology and Atmospheric Science, ptwbunn@email.arizona.edu - Izzy Viney, UArizona MS student in Microbiology, iav@email.arizona.edu - Emilie Lefoulon, UArizona Postdoc in Microbiology, elefoulon@email.arizona.edu. - Jung Mee Park, University of Arizona, MLIS student library and information science at UA, PhD in sociology from Cornell, jmpark@email.arizona.edu, @jpark1917 (https://twitter.com/jpark1917) - Rene Dario Herrera (they/them), University of Arizona Cancer Center, Data Analyst, renedherrera@email.arizona.edu, [twitter@reneherrera](https://twitter.com/reneherrera), [github@renedarioherrera](https://github.com/renedarioherrera), [linkedin.com@renedarioherrera/](https://www.linkedin.com/in/renedarioherrera/) ### What is the Unix Shell? * Most often we use a graphical user interface (GUI) to interact with our personal computer. Think of saving a document, you move the mouse to the save icon, or select save from the File tab. We use the mouse to navigate the GUI and then upon clicking, the GUI tells the computer to save. * The Unix shell is different as we can use it to interact directly with the computer (it came before GUIs). The Unix shell is a command-line interface (CLI) allows you to control your computer using a set of specific commands. On the command-line you would type a command to tell the computer to save a document, essentially bypassing the mouse-navigation/click/GUI in the saving a document example above. * The Unix shell is both a CLI and a scripting language. Creating scripts of these commands allows repetitive tasks to be done automatically and fast. * It's one of the most underrated, yet useful, tools in computing (not many people include Unix shell commands in their acknowledgements unlike R, Python, or Matlab Packages) ### Why learn how to use the Unix shell? * Quickly perform operations on large files (without reading into memory) * Automation of repetitive tasks, with the correct commands this lessens chance of human error (need to rename 100 files? a Unix shell script makes light work of this task and do this later in this workshop) * Enables the use of high-powered computers elsewhere, server or cloud-based (interacting remotely with using a Unix shell uses less bandwidth than when using a GUI) # Opening a Unix Shell **Windows**: Press the ⊞ Windows Key on your keyboard, then find Git Bash and open it. **Mac**: Press ⌘ Command + Space Bar on your Mac Keyboard. Type in “Terminal” when you see Terminal in the Spotlight search list, click it to open the Terminal app. **Linux**: Use Ctrl+Alt+T to open a Terminal or search for it in applications. When the shell is first opened, you are presented with a prompt $, indicating that the shell is waiting for input: ``` [username]$ ``` --- # Workshop Activities We'll run through the most useful commands first, then depending on time create a shell script together to rename some files ## Essential commands ```bash # Print username whoami # What time/date is it? date # Where in the computers files am I? # print working directory (the folder you a currently in) pwd # What kind of Unix shell am I using? (Bash is most common) # echo is the command to print something on the commnad line. # In this instance we print the default Unix variable SHELL and # the $ and {} brackets are used to call a Unix variable echo ${SHELL} # We can create our own Unix variables using these commands, # but we need to use backtick `` to substitute in the date command # as the variable learning_unix_since . # Also, notice no spaces around = # this ensure it will be read as a unix variable learning_unix_since=`date` echo ${learning_unix_since} # Use the up arrow ↑ on your keyboard to cycle back through the # commands used so far (really useful to go back and try again # when something doesn't work) ``` ___ ## Some more essential commands How is the pace so far...? ```bash # Show the contents of a directory ls # Show the contents of a directory with details ls -l # See the contents of a subfolder # (the TAB key autofills, and is an essential tool!) ls <subfolder>/ # Make a new folder with the mkdir command called: unix_workshop mkdir <name> # Change directory to specified pathway cd <pathway> # touch is a command that can be used to create files # if they don't already exist in the directory. # In the directory we just made unix_workshop, # create a text file using touch. touch random_file_00.txt # Change directory, up one in the pathway. cd ../ # Wildcards * # one or more of any character # Example use of * (list all files ending in .txt) ls *.txt # Make a copy of a file cp <existing filename> <destination path/filename> # Move file, but also used for renaming mv <filename> <destination> # Remove a file; # use with caution # use with caution # use with caution # use with caution # use with caution # use with caution # use with caution # there's no going back, it's gone forever! rm <filename> # Remove a directory; # can only remove an empty directory rmdir <name> # Move to a directory with many different file types # Get the line, word, character count of a file wc <filename> # Get just the line count wc -l <filename> # Pipe command; join commands together # The pipe key is near Enter, # Shift+| (on US-keyboard) | # E.g. count of items in the directory ls <directory> | wc - l # Get number of txt files in a directory ls *.txt | wc -l # Make a file with the output of a command using > # will overwrite existing file ls > <filename> # Global regular expression # searches for a string in a file # good for quickly searching through # scripts to re-use bits of code grep <"string"> <file> # finding a file in your directory tree find -name "<filename>" # get help for any command <command> --help # cancel incomplete command CTRL + C ``` ___ ## View and change files ```bash # Viewing the head (start) or tail (end) files head <filename> tail <filename> # Let's be more specific about looking at the head of a file head -n <no. rows> <filename> # Open vi, a text editor (can use another text editor) # this will create a file if it doesn't exist vi <filename> # To edit the file, go to EDIT mode by # pressing Insert key, should see INSERT on the # bottom bar Ins # To go back to COMMAND mode, press Esc key # bottom bar should go blank Esc # Exit vi, and save changes: in COMMAND mode :wq # Exit vi, without saving: in COMMAND mode :q! ``` ___ ## Creating a shell script to change the names of some files If we've covered enough for the day, we can skip to [Workshop Close Out](#Workshop-Close-Out) and those interested in creating a shell script can come back to here... ```bash # create some files to eventually change the name of touch random_file_{01..99}.txt # the {01..99} creates a sequence from 1 to 99, try it after # the echo command. # We should have just created 99 files empty text files, # using this sequence. ``` First we need to open a text file to write the script in. ```bash # We can use vi to create the script vi <filename>.sh ``` This is the shell script that we'll go through together; ```bash #!/bin/bash # This first line ensures that your Unix shell knows # this is a bash shell script (not another shell type) # and can interpret it accordingly # First we set the pathway to the files wanting to be renamed # we can use the present working directory path_to_files=`pwd` # Start a loop that looks for .txt files in the directory ${path_to_files} # then loop over each file in that directory. for file in ${path_to_files}/*.txt do # Take the extension off the filename using % # the % sign cuts a string of characters, like the filename # file in this instance prefix=${file%.txt} # use the prefix to create the new name for the file, # by adding _modified to each filename outfile=${prefix}_modified.txt # print out the filename, the prefix and the new filename echo ${file} ${outfile} # perform the name change using the mv command, # and the variable we just set up outfile # mv <filename> <new filename> mv ${file} ${outfile} done ``` ___ ## Running a shell script ```bash # to execute the shell script we just made bash <filename>.sh ``` ## Workshop Close Out ```bash # see all commands since the start of the session history # save history to a text file history > tutorial_history.txt # clear your shell screen of previous commands clear ``` > If you run commands with the shell, you can view your history of inputs: so you KNOW what you did. Whereas, a graphical interface does not leave behind a record of what happened. --- ## Questions and Answers In this section, you can post your questions and feel free to answer if you have it. Questions will be answered during or after the workshop. 1. Ask your question. - We'll answer it here 2. Your --- ### Links to additional resources - This site is useful if you want to decode a command-line operation into its different components: https://explainshell.com/ :::info **Session Feedback :mega:** Use the link below to provide your feedback on the session: [**Session Feedback Form**](https://forms.gle/TrnJpr9qRBEKdnVVA) :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully