Command Line NavigationIn previous posts, I have touched on both paths and navigation, without going into all of the details.

Since the two are so closely intertwined, I decided today to go into greater detail about both subjects at once, with an emphasis on how to navigate via the Linux command line, and what shortcuts are available to streamline the navigation process.

Definitions

Navigation is the process of planning and executing the movement of something(s) or someone(s) from one place to another, via a specific route that is determined during the process.

Smart navigation involves utilizing the best (shortest, smoothest) path to accomplish the objective.

A path is the visible means by which navigation can be carried out, such as a trail, a track, etc.

In computing terms, a path or path name is the means of reaching an object, or a complete description of its location.

Common paths include file paths, directory paths, network paths, and internet paths (URLs).

Absolute Vs. Relative Paths

There are two distinct types of paths: absolute and relative.

An absolute path is a very specific method of describing a location. It starts at the beginning of the file system (the root directory) and continues on until the path is complete.

Absolute paths can be used from any location in the file system without being modified, because they are not dependent on their location in the file system, but rather upon the structure of the file system itself.

Examples:
/home/gwen/
http://takingnothingforgranted.com/

A relative path is a very general method of describing a location. It starts at the current directory and describes a location based on its relation to the current directory.

Relative paths cannot be used from any location in the file system without being modified, because they are dependent on the location of the current directory as it relates (is relative) to another directory or file’s location.

Examples:
Documents/
index.html

Basic Navigation Commands

Most often, there are three Linux commands brought up when navigation is discussed.

Together, those three commands allow you to see where you are in the file system, change where you are in the file system, and list the contents of a directory in the file system.

pwd

The pwd command prints the name of the current/working directory that you are located in.

In comes in very handy when/if you ever get lost in the file system.

cd

The cd command stands for “change directory”. It allows you to move around the file system.

The command, followed by a path name, will change your current working directory, or move you into the specified directory. Either absolute or relative path names can be used.

The default directory is the current user’s home directory; typing in cd without specifying a directory to change to will return the user to their home directory.

Some basic examples of the cd command are:

cd

cd /home/otheruser/ or cd /home/otheruser

cd Documents/ or cd Documents

There are many useful “shortcuts” to navigation — useful both with and far beyond the cd command — that we will explore after first learning about the third command.

ls

The ls command will list the contents of a directory.

By default, ls will list the contents of the current directory, but it can be told to list the contents of another directory simply by specifying that directory location: ls /other/directory/

Many options exist to modify the results listed. My favorites are:

-a will list all files/directories, including hidden files that begin with a period.

-h will print human-readable file sizes (used with -l).

-l will list files/directories in a long list format & files details.

-t will sort by modification date/time, latest first.

-1 will list one file/directory per line, without details.

As with most commands, the options can be combined: ls -lt

The ll command, which is Fedora-specific, is a shortcut for ls -l.

Navigation Shortcuts

Now we get to go through a list of cool navigation shortcuts that are useful with many types of commands. These shortcuts can save a lot of typing and/or repetition.

Don’t forgot that the Tab key can be used to finish commands, path names, file names, etc. Pressing the Tab key twice, will show the available options.

A dash can be used (only with the cd command) to toggle back and forth between the last two directories.

Example: cd -

~

The tilde is used to reference the current user’s home directory.

Examples: cd ~ and mv ~/FolderName ~/NewFolderName

.

A single period/dot is used to reference the current directory.

Example: mv ~/FolderName .

..

Two periods/dots together are used to reference your current directory’s parent directory. It can be used multiple times in a row.

Examples: cd ../../ and mv ../filename.txt .

Drag-and-Drop

One easy way to reference a file that is multiple-directories-deep in the file system — or one that has a long file name — is to drag and drop it from your GUI file manager (Dolphin, Konquerer, Etc.) into your terminal window and choose “Paste Location” from among the options that appear. The full path name and file name will be pasted into the command line for you.

Conclusion

These shortcuts will get you started; what others did I overlook here?

Leave your suggestions in the comments.