What is the command-line?

The command-line, also known as a “terminal” or “shell”, is a way of interacting with your local computer or a remote server by means of typing commands or scripts, without using a graphical user interface (GUI).

Why do I want to be doing this?

At first, the command-line can seem a little intimidating. But after you get used to typing instead of pointing and clicking to issue your commands, you’ll realize how powerful it is. For example, it’s quite easy to copy, move, edit, and search within thousands of files in multiple directories with some simple command-line code. It would take forever to do this by dragging/dropping with a mouse. The command-line also allows you to work with very large data files without uncompressing them fully, or loading the entire file’s contents into memory…something that standard GUI type applications aren’t good at.

So, let’s get started…

ip0af52fbf:papers srkeller$ ssh srkeller@pbio381.uvm.edu
srkeller@pbio381.uvm.edu's password: 
Last login: Tue Jan 31 10:51:15 2017 from ip040027.uvm.edu
[srkeller@pbio381 ~]$ 

bash [srkeller@pbio381 ~]$ pwd /users/s/r/srkeller [srkeller@pbio381 ~]$

[srkeller@pbio381 ~]$ mkdir mydata
[srkeller@pbio381 ~]$ ll
total 0
drwxr-xr-x. 6 srkeller users 82 Jan 31 17:21 archive
drwxr-xr-x. 2 srkeller users  6 Jan 31 17:21 mydata
drwxr-xr-x. 2 srkeller users  6 Jan 31 17:15 scripts
[srkeller@pbio381 ~]$ 
[srkeller@pbio381 ~]$ cd mydata/
[srkeller@pbio381 mydata]$ ll
total 0
[srkeller@pbio381 mydata]$ 
drwxr-xr-x.  5 root root       73 Jan 31 17:35 archive
drwxrwxr-x.  2 root pb381adm   40 Nov 30  2015 packages
drwxrwxr-x. 33 root pb381adm 4096 Nov 30  2015 popgen
drwxrwxr-x.  3 root pb381adm   42 Jan 30 09:08 project_data
drwxrwxr-x.  2 root pb381adm    6 Oct  2  2015 scripts
drwxr-xr-x. 18 root root     4096 Sep  2  2015 users
[srkeller@pbio381 data]$ 
[srkeller@pbio381 data]$ cd project_data/
[srkeller@pbio381 project_data]$ ll
total 8
drwxr-xr-x. 12 srkeller users 4096 Jan 30 09:06 archive
-rw-r--r--.  1 srkeller users 1255 Jan 30 09:08 ssw_samples.txt
[srkeller@pbio381 project_data]$ 
[srkeller@pbio381 project_data]$ cp ssw_samples.txt ~/mydata/
[srkeller@pbio381 project_data]$ cd ~/mydata/
[srkeller@pbio381 mydata]$ ll
total 4
-rw-r--r--. 1 srkeller users 1255 Jan 31 17:42 ssw_samples.txt
[srkeller@pbio381 mydata]$ 
[srkeller@pbio381 mydata]$ head ssw_samples.txt 
Individual  Trajectory  Location    Day3    Day6    Day9    Day12   Day15
10  HH  INT 10_5-08_H   10_5-11_H   10_5-14_H   10_5-17_H   10_5-20_H
24  HH  INT 24_5-08_H   24_5-11_H   24_5-14_H   24_5-17_H   24_5-20_H
27  HH  INT 27_5-08_H   27_5-11_H   27_5-14_H   27_5-17_H   27_5-20_H
08  HS  INT 08_5-08_H   08_5-11_S   08_5-14_S   08_5-17_S   08_5-20_S
09  HS  INT 09_5-08_H       09_5-14_S   09_5-17_S   09_5-20_S
15  HS  INT 15_5-08_H   15_5-11_H   15_5-14_H   15_5-17_S   15_5-20_S
19  HS  INT     19_5-11_H   19_5-14_H   19_5-17_H   19_5-20_S
20  HS  INT 20_5-08_H   20_5-11_H   20_5-14_H   20_5-17_H   20_5-20_S
03  SS  INT 03_5-08_S   03_5-11_S
[srkeller@pbio381 mydata]$ grep 'HH' ssw_samples.txt 
10  HH  INT 10_5-08_H   10_5-11_H   10_5-14_H   10_5-17_H   10_5-20_H
24  HH  INT 24_5-08_H   24_5-11_H   24_5-14_H   24_5-17_H   24_5-20_H
27  HH  INT 27_5-08_H   27_5-11_H   27_5-14_H   27_5-17_H   27_5-20_H
31  HH  SUB 31_6-12_H   31_6-15_H   31_6-18_H   31_6-21_H   31_6-24_H
32  HH  SUB 32_6-12_H   32_6-15_H   32_6-18_H   32_6-21_H   
33  HH  SUB 33_6-12_H   33_6-15_H   33_6-18_H   33_6-21_H   33_6-24_H
34  HH  SUB 34_6-12_H   34_6-15_H   34_6-18_H   34_6-21_H   34_6-24_H
35  HH  SUB 35_6-12_H   35_6-15_H   35_6-18_H   35_6-21_H   
[srkeller@pbio381 mydata]$
[srkeller@pbio381 mydata]$ grep 'HH' ssw_samples.txt >ssw_HHonly.txt
[srkeller@pbio381 mydata]$ ll
total 8
-rw-r--r--. 1 srkeller users  462 Jan 31 20:46 ssw_HHonly.txt
-rwxrwxr-x. 1 srkeller users 1255 Jan 31 17:42 ssw_samples.txt
[srkeller@pbio381 mydata]$ 
[srkeller@pbio381 mydata]$ grep 'SS' ssw_samples.txt >ssw_SSonly.txt
[srkeller@pbio381 mydata]$ ll
total 12
-rw-r--r--. 1 srkeller users  462 Jan 31 20:46 ssw_HHonly.txt
-rwxrwxr-x. 1 srkeller users 1255 Jan 31 17:42 ssw_samples.txt
-rw-r--r--. 1 srkeller users  342 Jan 31 20:48 ssw_SSonly.txt
[srkeller@pbio381 mydata]$ 
[srkeller@pbio381 mydata]$ man grep


GREP(1)                            General Commands Manual                           GREP(1)

NAME
       grep, egrep, fgrep - print lines matching a pattern

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

DESCRIPTION
       grep searches the named input FILEs (or standard input if no files are named, or if a
       single hyphen-minus (-) is given as file name) for lines containing a  match  to  the
       given PATTERN.  By default, grep prints the matching lines.

       In  addition,  two variant programs egrep and fgrep are available.  egrep is the same
       as grep -E.  fgrep is the same as grep -F.  Direct  invocation  as  either  egrep  or
       fgrep  is  deprecated,  but is provided to allow historical applications that rely on
       them to run unmodified.

OPTIONS
   Generic Program Information
       --help Print a usage message briefly summarizing these command-line options  and  the
              bug-reporting address, then exit.

       -V, --version
              Print  the version number of grep to the standard output stream.  This version
              number should be included in all bug reports (see below).

   Matcher Selection
       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).  (-E  is
              specified by POSIX.)

       -F, --fixed-strings, --fixed-regexp
              Interpret  PATTERN  as  a list of fixed strings, separated by newlines, any of
              which is to be matched.  (-F is  specified  by  POSIX,  --fixed-regexp  is  an
              obsoleted alias, please do not use it in new scripts.)

       -G, --basic-regexp
              Interpret PATTERN as a basic regular expression (BRE, see below).  This is the
              default.

       -P, --perl-regexp
              Interpret PATTERN as a Perl regular expression.  This is  highly  experimental
              and grep -P may warn of unimplemented features.
[srkeller@pbio381 mydata]$ grep 'INT' ssw_samples.txt | wc
     16     106     762
[srkeller@pbio381 mydata]$ 
[srkeller@pbio381 mydata]$ mkdir sample_by_disease/
[srkeller@pbio381 mydata]$ ll
total 12
drwxr-xr-x. 2 srkeller users   10 Jan 31 21:12 sample_by_disease
-rw-r--r--. 1 srkeller users  462 Jan 31 20:46 ssw_HHonly.txt
-rwxrwxr-x. 1 srkeller users 1255 Jan 31 17:42 ssw_samples.txt
-rw-r--r--. 1 srkeller users  342 Jan 31 20:48 ssw_SSonly.txt
[srkeller@pbio381 mydata]$ mv *only* sample_by_disease/
[srkeller@pbio381 mydata]$ ll
total 4
drwxr-xr-x. 2 srkeller users   60 Jan 31 21:12 sample_by_disease
-rwxrwxr-x. 1 srkeller users 1255 Jan 31 17:42 ssw_samples.txt
[srkeller@pbio381 mydata]$ cd sample_by_disease/
[srkeller@pbio381 sample_by_disease]$ ll
total 8
-rw-r--r--. 1 srkeller users 462 Jan 31 20:46 ssw_HHonly.txt
-rw-r--r--. 1 srkeller users 342 Jan 31 20:48 ssw_SSonly.txt
[srkeller@pbio381 sample_by_disease]$ 
[srkeller@pbio381 mydata]$ ll
total 8
drwxr-xr-x. 2 srkeller users   60 Jan 31 21:12 sample_by_disease
-rw-r--r--. 1 srkeller users  282 Feb  1 05:35 ssw_HSonly.txt
-rwxrwxr-x. 1 srkeller users 1255 Jan 31 17:42 ssw_samples.txt
[srkeller@pbio381 mydata]$ rm ssw_HSonly.txt 
[srkeller@pbio381 mydata]$ ll
total 4
drwxr-xr-x. 2 srkeller users   60 Jan 31 21:12 sample_by_disease
-rwxrwxr-x. 1 srkeller users 1255 Jan 31 17:42 ssw_samples.txt
[srkeller@pbio381 mydata]$
  1. cd to your home directory (~/) 2. list all the files, including “hidden” ones that aren’t usually shown. To do this, use ll -a. 3. Look for a file called “.bashrc” — this contains your settings for how you interact with the server when you log in. 4. We’re going to open this file and edit it to add a setting to request that rm confirms deletion with us. To edit text files on the fly in UNIX, you can use the built-in text editor, “vim”: vim .bashrc 5. You should see something that looks like this:
  # .bashrc

  # Source global definitions
  if [ -f /etc/bashrc ]; then
          . /etc/bashrc
  fi

  # Uncomment the following line if you don't like systemctl's auto-paging feature:
  # export SYSTEMD_PAGER=

  # User specific aliases and functions
  1. Use your arrow key to move your cursor down to the last line, below “”# User specific aliases and functions" — this is where we’re going to insert our new function.

  2. By defauly, vim is in read-only mode when it opens files. To go into edit mode, press your “i” key (for “insert”). You are now able to make changes to the file.

  3. Add the following text on a new line directly below the “# User specific…” line:

    alias rm='rm -i'

  4. Your file should now look like this:

  # .bashrc

  # Source global definitions
  if [ -f /etc/bashrc ]; then
          . /etc/bashrc
  fi

  # Uncomment the following line if you don't like systemctl's auto-paging feature:
  # export SYSTEMD_PAGER=

  # User specific aliases and functions

  alias rm='rm -i'
  1. You’re now ready to get out of edit mode (hit the escape key), save your changes (type :w), and exit vim (type :q).

  2. These changes won’t take effect until you log out (type exit to log out of the server). But from now on, every time you log in, the server will remember that you want a reminder before deleting any of your work.

Let’s review what we’ve learned so far…

[srkeller@pbio381 ~]$ cd /data/
[srkeller@pbio381 data]$ ll
total 8
drwxr-xr-x.  5 root root       73 Jan 31 17:35 archive
drwxrwxr-x.  2 root pb381adm   40 Nov 30  2015 packages
drwxrwxr-x. 33 root pb381adm 4096 Nov 30  2015 popgen
drwxrwxr-x.  3 root pb381adm   42 Jan 30 09:08 project_data
drwxrwxr-x.  2 root pb381adm    6 Oct  2  2015 scripts
drwxr-xr-x. 18 root root     4096 Sep  2  2015 users
[srkeller@pbio381 data]$ 

Handy UNIX cheat sheet for helping to remember some of these commonly used commands (and others)

Here’s another useful UNIX cheatsheet