View on GitHub

Commandline Tools Workshop

Course site for PiCSciE/RC bootcamp workshop

Commands

Commands in a POSIX (Unix, Linux) world are just small executables that live in folders that the shell knows to search. (In Bash, this is your $PATH variable).

Even basic builtins like cp or mv are in exactly the same category.

Flags and Args

Most commands take a pattern of using position arguments (args) and flags denoted by a - for their short forms and sometimes by -- for longer spelled out forms.

Most builtins have man pages that discuss how to use them and their flags/args.

To view it, just type man command where command is the one you’re interested in. Inside the man page you can scroll up and down, use / and type in a search for a word or phrase and pretty q to leave.

As an example of how to read these, the first part of cp’s man page:

CP(1)                            User Commands                           CP(1)

NAME
       cp - copy files and directories

SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
       Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       -a, --archive
              same as -dR --preserve=all

      ... (etc. etc.)

From this, you can see the basic patterns of cp, most of which revolved around option flags, followed by a SOURCE and a DIRECTORY (or DEST) in which to copy them. (The -t/T flag is a way to make explicit behavior that just happens if you pass args in the usual way.)

Typically, short flags can be combined after one -, i.e. cp -rf (recurse and force). Long flags must always be written out, and depending on the utility, may need = after them.

$PATH

As mentioned before, $PATH dictates what directories are searched for binaries. It is one of many special Bash environment variables, which you can see if you echo $PATH or printenv (for everything). My path on adroit4 is:

/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/puppetlabs/bin:/opt/dell/srvadmin/bin:/home/bhicks/bin

Directories are searched left to right, as divided by :, with the first match being the one that is used.

If you need to adjust your $PATH or other environmental variables, a few words of warning:

1) Always include the current path when resetting it. A good strategy is to temporarily set it for a single login shell via export to test first:

export PATH=/new/folder:$PATH

If you just do export PATH=/new/folder you will remove all of the default directories from your path and suddenly calling vi or cp or ls or any utility will fail.

(If you just used export in the shell, you can merrily relog in and the problem goes away.)

2) Once you’re confident you have it right, you can add this $PATH adjustment to your ~/.bashrc, then just type . ~/.bashrc to reload. If you broke your PATH, you’ll either need a sysadmin to help our, or to use the full path to your text editor of choice to remove the offending line (/usr/bin/vi ~/.bashrc, for example)

The module system for the cluster handles much of this for you if you use those, and you will frequently want to do so.