File System Navigation

Description

This guide outlines the basics of navigating a *nix directory structure. Everything is a file in Linux, from directories to physical disk mounting points. All files consist of a filename, file contents, permissions and other properties. Files can be symbolically linked to other files via hard and soft links. We’ll use commands to list directory contents and to move around the file system. The commands used in this guide don’t represent their complete abilities.

Finding a Bearing

In order to navigate a file system, you must first determine what your current working directory is. A working directory is the directory you’re working out of and serves as a point for which relational references are made. Depending on the system you’re using, it’s likely that your current user name, hostname and working directory are printed at your prompt. This is handy for quick reference, but there are ways of determining these values via built-in Bash commands and there’s also ways of modifying your user’s shell prompt, though this is outside of the scope of this guide.

  • pwd – Prints your working directory
  • hostname – Prints the hostname of the host commands are being executed on
  • whoami – Prints the $USER variable of the entity executing the command

ls

The ls command lists the contents of the specified directory. When issued without any options, the ls command lists the contents of your working directory with the files being sorted alphabetically and with files prefixed with a period being hidden from the output.

Issued without options
The -a option lists all files
The -l option lists files in a long list format w/ file sizes in bytes
The -h option displays file sizes in a human readable format
The -R option lists files recursively
Options can be tacked together

tree

The tree command is a great way of visually representing a directory structure in a terminal window. This is particularly useful to obtain a high level overview of a directory structure and is easier to read than ls -R. When issued without options, it’ll print directories, files and subdirectories recursively.

Issued without options
tree -d displays only directories

The -L n option, where n is any positive integer, displays files recursively with a depth of n levels. Here’s a couple of examples.

tree -dL 1
tree -dL 2

Traversal

When working in a *nix file system, it’s not always necessary to change your working directory as you can always specify a file path with commands. It is most useful to change your working directory if you will be performing many duties in a particular directory, as you can make use of relative paths, like .(current directory) ./(current directory) ../(parent directory).

cd

This command is short for change directory and allows you to change your working directory. In order to use it, you issue the command followed by your target directory. You can use either an absolute or relative pathname.

An absolute pathname starts at the root of the system. A relative pathname starts at your working directory. A single period denotes the working directory but its inclusion is not always necessary to have this effect. It’s also possible to specify parent directories with a double period.

Changed to /var then back one level and demonstrated relative path with the exclusion of leading forward slash
Changed two levels back with cd ../../ and changed to same folder with an absolute path
The option changes your working directory to the one last used

This article’s content is incomplete



Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

  1. […] Once installed, let’s first allow TCP traffic over your SSH port so that you don’t get locked out of…

  2. […] have the option to choose between Password and SSH key selection. Learn more about SSH keys here. It’s recommended…