MOVED TO CONFLUENCE 2020/7/1
https://confluence.morningside.edu/display/OPS/CommandLineFu
CommandLineFu
CommandLineFu is the use of the command-line to expedite and automate otherwise tedious processes.
It can refer to any CommandLine interface, but in this article it will initially be limited to BASH (Linux & MacOS).
Environment
No CLI operation occurs in a vacuum. It has an environment which contains myriad information for a process such as:
- Current User / Login Name
- Current Working Directory
How to Read This (And Other) Documentation
There are a few common symbols you should know:
Symbol |
Meaning |
FixedWidth |
Something that is typed or printed at the command line |
[Your Name] |
Something optional that you should substitute your name into (so here I might type meyersh.) |
<Your Name> |
Something mandatory that I need to substitute. (Here I must type meyersh.) |
italic-FixedWidth |
Equivalent to <italic-FixedWidth>. |
| |
The pipe, in syntax, is equivalent to "this or that": echo hi shaun | mike implies either echo hi shaun OR echo hi mike. Not to be confused with | (the UNIX Pipe operator) |
$ foo |
The dollar sign implies the user-level command (foo) is to be entered at the BASH shell. |
# foo |
The hash sign implies that the root-level commend (foo) is to be entered at the BASH shell. |
These are common to most CLI documentation.
Getting more help
This document only contains the highest level overview of these utilities, each of which is meant to do one thing and do one thing well.
To get more help from the BASH command line, you may use the man (short for manual) program to review its documentation:
$ man <program>
For example, to see the complete documentation for the ssh command you would run man ssh.
= bash = bash (Borne-Again SHell) is an interpreter and "runs" the CLI on a GNU/Linux or MacOS system. It provides an interface between the user and the commands.
Bash Operators:
Operator |
Name |
Description |
| |
Pipe |
The pipe operator takes the stdout of one command and attaches is to the stdin of a second command. For example: ls -l | grep root |
> |
Redirect |
The redirect operator takes the stdout of a command and writes to a file. For example: ls -l > ls.txt |
< |
Redirect (from file) |
The redirect operator reads the contents of a file and writes it to the stdin of a program. Example: mysql -p test < test.sql |
SSH
SSH stands for Secure Shell and is the most common way to access remote server resources. It is an extremely powerful method that allows remote access to CLI and Files.
Basic Usage:
$ ssh [username@]<hostname> [remote command]
rsync
Rsync stands for Remote Syncronizer and is a great way to synchronize collection of local files to a remote collection. Rsync works over the ssh protocol; any options configured for ssh will work for rsync by default.
Basic Usage:
$ rsync [-r] [-v] [username@]<hostname>:<pathSpec> <localPathSpec>
Suppose the server remote-svr has the directory /some/dir which you want to copy to a local machine into /my/dir.
$ rsync -r remote-svr:/some/dir/ /my/
mysqldump
mysqldump is a program which exports a given database to text.
Basic Usage:
$ mysqldump [-h<hostname>] [-u<username>] [-p[password]] <database> [table]
Suppose you want to save the database test to the file test.sql:
$ mysqldump -umyuser -p test > test.sql
mysql
mysql is the command line interface to the MySQL server. It provides a place to easily test and use SQL queries and shares basic usage with mysqldump:
$ mysql [-h<hostname>] [-u<username>] [-p[password]] [database]
Supposing you wanted to read in the test.sql file created by mysqldump:
$ mysql -umyuser -p test < test.sql