Command Line Interface

Helpful reminders for working with your Bash, Terminal and command line.

Process status, see what processes are running on the machine

ps -ax

Confirm Apache is running

ps -ax | grep http

Confirm MySQL is running

ps -ax | grep mysql

list files and directories.

ls

Change directory.

cd

Create a new directory.

mkdir

Copy files or directories.

cp

Move (rename) files or directories.

mv myfile.txt destination-directory

Command Line Interface / Terminal Commands

  • {control}+{c}
    Abort the current line or running script.
  • {command}+{k}
    Clear the interface.
  • {control}+{a}
    Put the cursor at the beginning of the current line.
  • {control}+{e}
    Put the cursor at the end of the current line.
  • cd -
    Switch to the last directory you were in.
  • pwd
    Print the current working directory path.

Symbolic Links

ln -s /path/to/original /path/to/symlink
# ┌── ln(1) link, ln -- make links
# │   ┌── Create a symbolic link.
# │   │                         ┌── the path to the intended symlink
# │   │                         │   can use . or ~ or other relative paths
# │   │                   ┌─────┴────────┐
# ln -s /path/to/original /path/to/symlink
#      └───────┬───────┘
#              └── the path to the original file/folder
#                  can use . or ~ or other relative paths

Bash and Terminal Configs

Open the .bashrc file located at /Users/{ account name }/.bashrc

# prints the current user in the prompt and makes the prompt green.
export PS1='\[\e[0;32m\]\u >\[\e[0m\]'

# Unix style color coding.
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

# Global PATH
export PATH=/opt/local/bin:/opt/local/sbin:/Applications/MAMP/bin/php/php5.3.28/bin:$PATH

# Alias the ls command for listing directory content.
alias l='ls -l -a'

# Alias show and hide hidden file commands:
alias showfiles='defaults write com.apple.finder AppleShowAllFiles YES; killall Finder /System/Library/CoreServices/Finder.app'
alias hidefiles='defaults write com.apple.finder AppleShowAllFiles NO; killall Finder /System/Library/CoreServices/Finder.app'

# Alias 'php' to the MAMP PHP path.
alias php=/Applications/MAMP/bin/php/php5.3.28/bin/php

# Alias 'mysql' to the MAMP mysql path.
alias mysql=/Applications/MAMP/Library/bin/mysql

# Alias 'pear' to the MAMP pear path.
alias pear="/Applications/MAMP/bin/php/php5.3.28/bin/pear"

# Alias common locations for quick access.
alias cdhtdocs='cd /Applications/MAMP/htdocs'

MySQL

Export a Database

You should not be logged-in to your MySQL server, simply run this command from the directory where you want the backup file put.

mysqldump -u [username] -p [database name] > [filename].sql

If you use MAMP, the mysqldump script is located in the MAMP application directory. You can make an alias to MAMPs version of mysqldump in your bash profile, or call it directly with something like:

/Applications/MAMP/Library/bin/mysqldump -u [username] -p [database name] > [filename].sql

Import a Database

You can import an .sql file from the command line, and from the MySQL shell.

Command Line

You must have an empty database ready to import data to, so if you don’t, login to the MySQL shell and create the database with CREATE DATABASE [database name];, then log-out of the MySQL shell.

To import, on the command line navigate to the directory where your .sql import file is located and enter the command:

mysql -u [username] -p [database_name] < [dumpfilename.sql]

You will be prompted for your password, then the import will proceed. When it is finished, your command prompt will return to normal.

MySQL Shell

This method will allow you to monitor the import progress. To import a .sql file into your local MySQL database, first login to the MySQL shell:

mysql -u [username] -p

Then run these commands in the MySQL shell to create a new database, switch to using it, then import a .sql file:

CREATE DATABASE [database name];
use [database name];
source [path to your sql file to import];