Giter Club home page Giter Club logo

unix-cli's Introduction

General Assembly Logo

The Unix Command Line

Summary

Using the Command Line to manipulate the filesystem.

Prerequisites

Objectives

  • View files within a directory.
  • Create a directory (folder) in which to store a project.
  • Create subdirectories to organize a project.
  • Create files to contain project information.
  • Edit and delete files.
  • Navigate within a project.
  • Get help from user manuals.

Exploring the File System

You're probably used to creating files and folders using your operating system's "explorer" program. The common explorer programs found on each OS are:

  • Mac: Finder
  • Windows: Explorer
  • Ubuntu: Nautilus

On unix-like systems (sometimes refered to as *nix or POSIX), all your files are stored in your home folder. On Windows, your files are usually stored in "My Documents".

You might see ~ or $HOME used to refer to your home folder on unix-like systems.

Code Along: Explore the Home Directory

Open your file explorer application. By default, new windows open in your home directory. Note the files you see there.

Now, open your terminal emulator. By default, new sessions start in your home directory as well. How can we tell? Let's inspect the files in our current directory with the ls command (mnemonic: "list"). You should see the same files and directories in your explorer and your terminal.

Directories and Paths

Directories (and subdirectories) are usually represented as "folders" in your explorer application. They're just convenient ways of grouping files together. Paths are string (textual) representations of your current place in the file hiearchy (or "tree").

Examine this path: /Users/JWeeks. The leading slash (/) is the root of the file system. The next part Users indicates that there is a directory, or folder, with the name "Users" inside the root of the file system. Within the "Users" directory is another directory named JWeeks. The string /Users/JWeeks is an absolute path to the home directory for the current user on my machine.

Absolute Paths

An absolute path shows the unique location of the files and directories within the system's file system. No other files can have this path.

Note: It sometimes helps to think of the path as the actual full name of the file. This is especially true when we are moving or renaming files from the command line.

Absolute paths:

  • Always start with a leading slash, '/'
  • Are relative to the root directory of the file system.

The root directory the top level directory in the tree structure that makes up your project or file system. When we talk about absolute paths, the paths are relative to the topmost path of the file system.

Code Along: Explore the Root Path

In your terminal sessions, figure out what directory you are in by using the pwd command (mnemonic: "print working directory"). What is the topmost directory in this path? What is the root of the file system?

Open up the Finder and go to the root directory of your filesystem. There are several ways to accomplish this, but I prefer the ⌘ + ⇧ + g shortcut in Finder. Examine the contents of the root directory.

Now, back in your terminal, change your current working directory to the root of the file system using the cd command (mnemonic: "change directory"). What happens if we type cd and then press the "Return" or "Enter" key? Let's wait before we answer that question.

In order to change to the root directory, we type cd /. In this case, we're giving the cd command an absolute path. How do we know it's an absolute path? Confirm you're in the root directory by using the pwd command.

In order to get back to your home directory, you can type cd ~, or cd $HOME, or even just cd. Try it!

Relative Paths

Relative paths are paths described with respect to the current working directory. What command do we use to figure out our current working directory?

So if I'm in my home directory and I see a folder projects (how would I see that?), I can guess the full path to projects is /Users/JWeeks/projects. However, since I'm already in the home folder, the ls command tells me only unique part of the name needed to distinguish it from sibling directories. Sibling directories are directories that exist "beside" other directories, instead of within them ("child directories") or above them ("parent directories").

This "partial path" is a relative path. It's pretty useful, since I can use it to change into the directory by using cd projects. So, in addition to taking absolute paths, cd can take relative paths to navigate the file system. Neat!

There are two special relative directories.

  • .. represents the parent directory.
  • . represents the current directory.

So, in order to navigate to a grandparent (parent of a parent) directory from the current directory, what command would we use?

Lab: Make a Subdirectory

Now that we're back in the home directory, let's create a place to store all the work we're going to do in this WDI. Naming can often be difficult, but simple names are best. Let's create a subdirectory named "wdi". Always use lowercase names for your directories and files.

Before issuing the command in the terminal, bring your explorer window into focus, the one with the home directory open. Resize and position it so that it is visible along with the terminal. Now, in the terminal, execute mkdir wdi to create the directory (mnemonic: "make directory").

Now, what folder are you in? Did it change by making the new directory?

Create the following directories as well. They should be subdirectories of wdi.

  • training
  • diagnostics
  • projects
  • tmp
  • challenges
  • practice
  • study

Verify that your directory structure resembles the following:

$HOME/wdi
├── challenges
├── diagnostics
├── practice
├── projects
├── study
├── tmp
└── training

Files

Let's use the tmp directory to play around. tmp directories are conventionally used to store files that can be safely deleted. You should never put anything in them that you want to keep.

Code Along: Create a File

Move into the tmp directory inside wdi. Create a new file using the touch command. Let's make a blank text file: touch name.txt.

Next open that file in atom: atom name.txt. Write your name inside this file and save it. On a Mac, the shortcut to save a file is ⌘ + s. You can also search for the save command (indeed, any command) using the command palette. Try it by bringing up the palette with ⌘ + ⇧ + p. Then search for "Save".

Lets quickly take a peek inside the file we jsut created in our terminal. Type cat name.text.

Now type open name.txt. See what happened? The file name.txt opens in whatever default application your computer uses for that type of file.

Deleting a File

How do we get rid of this file? We'll use the rm command. But first: a warning. This is a very dangerous command. Do not ever press enter after typing rm without being certain you're targeting the right file. There's no way to recover files deleted with rm. The same advice goes double with r and f flags. Flags are switches that occur after dashes when issuing commands.

rm -rf <file_or_directory> # VERY DANGEROUS!

Code Along: User Manuals

Sometimes I forget how to use certain commands on the command line. My first step, even before Googling, is to read the command's user manual. You can access the user manual for a command using man <name_of_command> in your terminal. So, to figure out how to use rm, you would executre man rm. Do so now. You should see something like this:

NAME
     rm, unlink -- remove directory entries

SYNOPSIS
     rm [-dfiPRrvW] file ...
     unlink file

...

Lab: Delete a File

Read the manual entry for rm. When you're done reading, you can quit by pressing q. Delete the name.txt file you created in the previous exercise. Type the command, then check with a colleague before pressing issuing the command.

Lab: Cheatsheet

Let's use your new skills to create a reference sheet.

Create a cheat sheet of the unix commands that we've used so far. I like to these kinds of files for reference. Describe each command in your own words. Don't just cut and paste from some definition on the web!

References

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.

unix-cli's People

Contributors

srhoulam avatar jrhorn424 avatar realweeks avatar tdyer avatar payne-chris-r avatar

Watchers

James Cloos avatar Natasa Peic avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.