pamela.fox | Edit Slideshow | Copy Slideshow Download PDF | Sign out

Press key to advance. Zoom in/out: Ctrl or Command + +/-.

Command-Line

Shells

A text-based command interpreter.

The most common shell is "bash".

On Mac, use a shell via the "Terminal" app.

Shell prompt

The prompt usually shows you your username and computer name:

username@hostname:

To see what directory you're in, use the pwd command:

username@hostname: pwd
/Users/username/Desktop

File Paths

A sequence of nested directories with a file or directory at the end, separated by "/":

  • Relative: documents/fun/microsoft_jokes.html
  • Absolute: /usr/bin/local
  • Root: /

Shortcuts:

  • Current directory: .
  • Parent directory: ..
  • Home directory: ~

Change directory

Use the cd command to change directories:

cd mydirectory/
cd ../

List directory

Use the ls command to list the contents of a directory:

username@hostname: ls
mydir/
myfile.txt

For more details, use -l, and to see hidden files, use -a:

username@hostname: ls -al
-rw-r--r--    16 pamelafox  staff      544 19 Mar 11:40 .dropbox
drwxr-xr-x    16 pamelafox  staff      544 19 Mar 11:40 mydir/
-rw-r--r--    16 pamelafox  staff      544 19 Mar 11:40 myfile.txt

File Names

  • case sensitive
  • any character (besides "/")
  • file extensions are for user convenience

Examples:

README   .bashrc   index.html    index.html.old    Bugs List.txt

Making & deleting directories

To make a new directory, use the mkdir command:

mkdir myproject

To remove a (empty) directory, use the rmdir command:

rmdir myproject

Filesystem structure

Varies from system, but some common folders:

  • /bin: Basic commands
  • /etc: System config files
  • /tmp: Temporary files
  • /usr: Regular user tools
  • /usr/local: Installed user software
  • /var: Data used by system

Working with files

To make a file, use the touch command:

touch newfile.txt

To copy a file, use the cp command:

cp newfile.txt newerfile.txt

To move a file, use the mv command:

mv newerfile.txt betterfilename.txt

To remove a file, use the rm command:

rm betterfilename.txt

Editing files

You can use various editors built into bash:

  • vi myfile.txt
  • emacs myfile.txt
  • pico myfile.txt

Or (on Mac), you can open with any desktop app:

open -a TextEdit myfile.txt

With default editor:

open -t myfile.txt

With TextEdit:

open -e myfile.txt

Viewing files

You can view files in an editor, or for read-only viewing, try:

  • cat myfile.txt
  • more myfile.txt
  • less myfile.txt

You can pass multiple files into any of them:

 cat myfile.txt myfile2.txt myfile3.txt

Searching files

Use the grep command to search in files:

Find the phrase "hello" in a specific file:

grep hello myfile.txt

Find the phrase "hello" in all txt files:

grep hello *.txt

Finding files

Use the find command to find files according to name/metadata:

Find all txt files under the current directory:

find . -name '*.txt' -print

Redirecting output

By default, input comes from the screen and output goes to the screen.

You can use < and > to redirect input/output.

Save results of search in a file:

grep hello *.txt > results.txt

Using commands

  • The left/right arrow keys let you edit within a command
  • The up/down arrow keys let you select previous commands
  • CTRL+a goes to beginning of command, CTRL+e to the end.
  • The tab auto-completes filenames

Command shortcuts

Use history to see your previous commands:

  564  grep 'jquery' *.txt > results.txt
  565  vi results.txt 
  566* open -a TextEdit myfile.txt
  568  history

Also useful:

  • !!: Latest command
  • !568: Command by #
  • !open: Command matching string

Exercise Time!

  • Navigate to your "Dropbox" or "Documents" folder, wherever you've been putting projects.
  • Create a new folder called "myproject".
  • Change to that directory.
  • Create a new file called "index.html".
  • Edit that file so it's a basic webpage with a title.
  • Copy a file from another project into that folder, rename it, and use it in the webpage.
  • Open that file in Chrome and check it works.