modules

Command Line 2

Interacting with the Filesystem

Intro

Just like in Finder (macOS), Explorer (Windows), or Nautilus (Ubuntu), you can navigate and manipulate files and folders in your filesystem using the command line. Each session in your terminal starts in a particular directory and you can traverse the directory structure starting from there. You can use pwd (print working directory) to find the current working directory.

pwd

This command gives the absolute path to your current working directory.

File paths

Absolute Path

An absolute path specifies the complete path to a file or folder, ignoring your current working directory. For example, if you were to give someone “absolute” directions to your house, you would start by telling them to be on earth, then go to your continent, then go to your country, then go to your region, etc. The root of the filesystem is referred to as /.

Relative Path

A relative path specifies the path to a file or folder, taking into account your current working directory. For example, if you were to give someone “relative” directions to your house, you would give them directions from their current location (the relative path from where they are to where you are).

Special Symbols

A few directories have special symbols:

Example Directory Structure

Below is an example directory structure from macOS.

/
├── Applications
└── Users
    └── mehtad
        ├── Desktop
        ├── Development
        ├── Documents
        ├── Downloads
        ├── Movies
        ├── Music
        └── Pictures
 

In the example above, if I was in the Downloads folder and I wanted the relative path to the Documents folder, that would be ../Documents.

In the same example above, the absolute path to the Documents folder would be /Users/mehtad/Documents.

:raised_hand: Pop Quiz

If you are in the Downloads folder, what folder is:

Notice that those were relative paths, the following are absolute paths that both refer to the same location:

pwd

prints working directory (the directory you are currently in)

cd

To navigate to different directories within your filesystem, you can use the cd (change directory) command. cd takes one positional argument, the absolute or relative path to the destination directory.

cd <target_directory>

cd .. will move you “up” one directory (to the parent directory) and cd ~ will move you back to the “home” directory.

ls

In order to list the contents of the current directory you can use ls.

tree

If you don’t have tree installed, on macOS run brew install tree and on ubuntu run sudo apt-get install tree.

Inspecting Files

cat

You can view the contents of an individual file (or files) by using cat. cat will print the entire content of a file to the screen.

When a file is very long, you can use head to print he beginning of a file.

tail

In the case of long, constantly changing files, like a log file, you can use tail.

less

Last, when reading long files page by page (like when using man), you can use less to paginate the contents.

subl

If you don’t have Sublime Text installed, run brew cask install sublime-text on macOS and on ubuntu follow these instructions.

open (mac only)

wc

:white_check_mark: Try It

  1. Take some time to cd around and explore your filesystem. See what is at the root, see if you can find some of the files you use daily.
  2. Navigate to ~/Desktop and run both ls -a and tree
  3. Navigate to ~/Downloads and run the ls -l command to see information about every item in that folder

hint! - are you lost? don’t know what to type next?

Manipulating Files and Folders

The command line not only lets you navigate the file system but also manipulate it by creating new files (touch), creating directories (mkdir), deleting files and directories (rm -i, rm -ir), copying files and directories (cp -v, cp -vR), move/rename files and directories (mv), and more…

touch

mkdir

rm -i

cp -v

mv

:white_check_mark: Try It

Complete the following assignment: https://classroom.github.com/a/d4WK4RbX

Finding Files and Folders

find

grep

:large_orange_diamond: Example

find all items in the universe folder with the .txt extension.

cd ~/Development/universe
find . -name "*.txt"

:white_check_mark: Try It

find all folders in the universe folder. use man find to figure out how to get folders only.

hint:

The manual for the find command (man find) is super long. The answer is in there, but it might take a while to find. You might want to try google instead.