Skip to content

Instantly share code, notes, and snippets.

@imbstack
Created September 7, 2011 21:52
Show Gist options
  • Save imbstack/1201901 to your computer and use it in GitHub Desktop.
Save imbstack/1201901 to your computer and use it in GitHub Desktop.
Basic Bash Intro

Introduction to Interacting with the Shell

for the CWRU hacker society

  • being able to use the unix shell is an important skill
  • it is also really helpful to know
    • it provides a consistent way to interact with all of your tools
    • it makes automation easy
    • being able to pipe and redirect output (defined later!) is immensely powerful
    • job interviews will ask shell questions
  • best way to be able to use the shell is by using a unix (which includes mac osx) or linux os
    • there are brand new ubuntu computers in Olin 404 on the right
    • alternatively use cygwin from windows

Example Commands

ls – list the contents of a directory
brian@btp:~/Documents/CWRU/classNotes/eecs425/proj1$ ls
data  Makefile  README  src
brian@btp:~/Documents/CWRU/classNotes/eecs425/proj1$ ls -a
.  ..  data  .git  .gitignore  Makefile  README  src

cd – change your working directory

  • use “~” to go back to home directory
  • use “-” to switch back to last directory

mv – move files and directories

  • can be used to move as many files as wanted

cp – copy files and directories

  • similar to mv, except leaves copy of file in place
  • must use “-r” option to recursively move directory

rm – remove files

  • file goes away for good – BE CAREFUL
  • I once removed all of the java files in a project within 3 minutes of its due time
    • fortunately I was using version control, which Fred Hatfull will tell you about later

mkdir – make a directory

  • does exactly what it says it does

top – display running tasks

  • useful for finding which process is taking up all of your cpu, or killing hung processes

grep – pattern matching

  • example use case: leaving “TODO” in your source files while you are working on them, in order to remember what to do later. These can be found by issuing grep -r TODO ./ in your project directory.

Important Concepts

  • man pages
    • think of these as manuals for the commands you can issue in the shell
    • for instance, issuing man ls will show
      LS(1)                                                                 User Commands                                                                
      NAME
             ls - list directory contents
      SYNOPSIS
             ls [OPTION]... [FILE]...
      DESCRIPTION
             List information about the FILEs (the current directory by default).  Sort entries alphabetically if...
             Mandatory arguments to long options are mandatory for short options too.
             -a, --all
                    do not ignore entries starting with .
      ...
      
  • piping and redirection
    • useful for all kinds of tasks
    • exemplary of the Unix philosophy, “Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.”
    • small example: writing a list of all of the cpp files in a directory
      brian@btp:~/Documents/CWRU$ ls resume/ | grep "pdf" > ./res_list.txt
      brian@btp:~/Documents/CWRU$ cat ./res_list.txt 
      res.pdf
      resume.pdf
      
  • Permisssions
    • Only certain users should be allowed to do certain things for security reasons
    • Very good chance you will be managing permissions using sudo
      • This will prompt you for your password, and if you are in the list of users who can sudo the command will be executed
      • Commands are executed as if by another user (which will probably be root)

End

The command shell has an interesting learning curve. At first it will be more difficult to use the shell than the graphical user interface to perform tasks, but persistence will help in learning. Over time, it becomes an indispensable tool in the programmer’s tool box. Keep in mind that there are many people around who want to help you, but only if you try to figure it out first yourself.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment