Command Line

Basic Commands

clear

  • Description: Clear the terminal screen.
  • Form: clear

echo

  • Description: Print arguments. Useful for piping and bash scripting.
  • Form: echo <string>
  • Example: echo "some text"

exit

  • Description: Quit the shell.
  • Form: exit

history

  • Description: Show previous commands.
  • Form: history

File System Commands

ls

  • Description: Display contents of the working directory.
  • Form: ls [options]
  • Options:
    • -l
      • List contents with extra information.
    • -a
      • Show all files/folders (even hidden ones).
    • -t
      • Sort by date modified.

pwd

  • Description: Show the working directory path.
  • Form: pwd

cd

  • Description: Change the working directory.
  • Forms:
    • cd: Move to the home directory.
    • cd ..: Move up the directory tree.
    • cd -: Move to the previous working directory.
    • cd <path>: Move to a specific location.
  • Example: cd ~/.emacs.d/

mkdir

  • Description: Create a directory.
  • Form: mkdir <name>

cp

  • Description: Copy a file/folder.
  • Form: cp <source> <target>
  • Options:
    • -r
      • Recursively copy a folder.
  • Examples:
    • cp file.lisp folder/file.lisp
    • cp folder/ destination/folder/

mv

  • Description: Move or rename a file/folder.
  • Form: mv <file> <file>
  • Examples:
    • mv file.lisp folder/file.lisp
    • mv file.lisp new-name.lisp
    • mv folder renamed-folder

rm

  • Description: Delete a file/folder.
  • Form: rm <file>
  • Options:
    • -r
      • Recursively delete a directory.
    • -f
      • Force removal without confirmation.
  • Examples:
    • rm file.lisp
    • rm -r some/folder

cat

  • Description: Print the entire contents of a file.
  • Form: cat <file>

less

  • Description: View the contents of a file.
  • Form: less <file>
  • Usage:
    • Type q to quit, / to search, and h for help

tail

  • Description: Display the end of a file.
  • Form: tail <file>
  • Options:
    • -f
      • Follow the file, updating as new output appears.
    • -n
      • Number of lines to show.
  • Examples: tail -f solr.log

sed

  • Description: Modify lines in a file.
  • Form: sed <command> <file>
  • Options:
    • -i
      • Perform edits in-place. This will cause sed to modify the source file instead of just printing the results.
  • Examples:
    • sed -i 's/red/blue' file.txt
      • Replace the first occurrence of 'red' with 'blue' on each line.
    • sed -i 's/red/blue/g' file.txt
      • Replace all occurrences of 'red' with 'blue' on each line.
    • sed -i 's/unix/{&}/' file.txt
      • Replace all occurrences of 'unix' with '{unix}'. & represents the matched string.
    • sed -i '/HOST/ s/localhost/ds-dev01/' file.txt
      • Find lines that contain 'HOST'. In those lines, replace all occurrences of 'localhost' with 'ds-dev01'.

Network Commands

curl

  • Description: Transfer data from or to a server. Commonly used for HTTP and FTP.
  • Form: curl [options] <URL>
  • Options:
    • -X, --request <command>
      • HTTP method (e.g. POST, PUT). Defaults to GET.
    • -H <header>
      • HTTP request header (e.g. 'Content-Type: application/json').
    • -d <data>
      • Send data in an HTTP POST request. Similar to a web form.
    • -u <user:pass>
      • Username and password for authentication.
    • -o <file>
      • Write output to <file> instead of stdout.
  • Examples:
    • curl http://www.google.com
    • curl -d '{"name":"elliot"}' -H 'Content-Type: application/json' http://penson.io

ping

  • Description: Send an echo request to test a network connection.
  • Form: ping <host>
  • Example: ping google.com

ssh

  • Description: Secure SHell enables remote machine login. The command provides secure, encrypted communication.
  • Form: ssh <user@host>
  • Options:
    • -i <key>
      • Specify a private key file.
  • Example:
    • ssh john.cabmin@penson.io
    • ssh -i key.pem ec2-user@216.3.128.12

scp

  • Description: Secure CoPy allows files/folders to be moved to, from, or between different hosts. It uses ssh for data transfer and provides the same authentication and security.
  • Form: scp <[user@host:]file> <[user@host:]file>
  • Options
    • -r
      • Recursively move a directory
  • Examples:
    • scp elliot@penson.io:/homes/elliot/file .
    • scp -r elliot@penson.io:/homes/elliot/folder/ .

Search Commands

grep

Information in this section is taken from http://www.uccs.edu/~ahitchco/grep/.

  • Description: Global Regular Expression Print. Searches input files for a search string and prints matching lines.
  • Form: grep [options] <regex> <filename(s)>
  • Options:
    • -n
      • Explains which lines match the search string.
    • -v
      • Prints the negative result (all non-matching lines).
    • -c
      • Suppresses the line printing, displays the number of matching lines.
    • -l
      • Only prints the filenames with matching lines.
    • -i
      • Ignore case.
    • -x
      • Search for eXact matches only.
    • -f
      • Allows specification of a file containing the search string.
    • -r
      • Directory search. grep -r "test" . searches all files in the current directory

Sibling grep Commands

The egrep command stands for "extended grep" and supports certain useful sequences such as the + and ? operators. It's equivalent to grep -E. The fgrep command gives a performance boost as it doesn't interpret regular expressions. It's equivalent to grep -F.

find

  • Description: Search for files in a directory hierarchy.
  • Form: find <path> <expression>
  • Examples:
    • find . -name 'foo'
      • Find a file called foo.
    • find . -name 'foo*'
      • Find a file beginning with foo.
    • find . -name '*.txt'
      • Find a file with the txt extension.
    • find . -type d -name 'bar'
      • Find a folder called bar.

Pipes and Redirects

Pipe operator

The pipe operator | passes the output from one command to another. For example: ls | grep ".org" will display all org-mode files in the current directory.

Redirects

The less-than > symbol is used to redirect the output from a command to a file. The greater-than < symbol causes a command to read its input from a file. Double less-than >> will append output to a file.

Symbolic Links

Unix filesystems make use of aliases for files known as symbolic links (symlinks). A symbolic link is treaded in a similar fashion to the actual file. The ln command can be used to create symlinks. The ls -l will reveal where a symlink points. For example:

$ ls
bar foo
$ ln -s bar baz
$ ls -l
total 8
-rw-r--r-- 1 elliot staff 0 Jan 8 09:50 bar
lrwxr-xr-x 1 elliot staff 3 Jan 8 09:58 baz -> bar
-rw-r--r-- 1 elliot staff 0 Jan 8 09:57 foo

ln

  • Description: Create a link.
  • Form: ln <file> <alias-file>
  • Options:
    • -s
      • Make a symbolic link.

Processes

A process is an instance of a program. Processes are identified with a process ID (PID). In the command line, programs are run either in the foreground or the background. The shell waits for foreground commands to finish. Most programs run in the foreground by default. The shell doesn't wait for background processes to end and other commands can be executed in the meantime. Include an ampersand (&) at the end of a command to run it in the background. Programs currently running in the foreground may be changed into a background process with Control-Z.

ps

  • Description: Process Status. Display a list of running processes.
  • Form: ps [options]
  • Options:
    • -e
      • Show all user processes (even those without a controlling terminal). This option is useful for finding the PID of a command executed with nohup (see below).
    • -f
      • Display extra information including user ID, CPU percentage, process start time, and arguments used.

fg

  • Description: Move a background process to the foreground.
  • Form: fg [%job-number]

kill

  • Description: Stop a given process.
  • Form: kill [PID]

nohup

  • Description: Causes a command to ignore the hangup (HUP) signal. When one exits the shell, background commands are usually stopped. nohup allows users to prevent this stop signal on logout.
  • Form: nohup <command> <arguments> &
  • Example: nohup python program.by &

top

  • Description: Display dynamic information about running processes.
  • Form: top
  • Usage:
    • Get help with ?. Type o to specify a sort key. Options include pid, cpu, and mem. Quit with q.

Documentation

man

  • Description: Open a command's manual.
  • Form: man <command>

Usage Statement (Loose) Guidelines

Anything in angle brackets (<>) means the argument is required (e.g. <foo>). Anything in square brackets ([]) means the argument is optional (e.g. [bar]). Options separated by the pipe (|) are choices (e.g. –baz=one|two|three). Note that this mirrors the or operator. Single-letter options start with one dash (e.g. -a). Multi-letter options start with two dashes (e.g. –foo-bar).