# Understanding file paths and how to use them

A file path is the human-readable representation of a file or folder's location on a computer system.
You've seen file paths, although you may not realise it, on the Internet: an Internet URL, despite ancient battles fought by proprietary companies like AOL and CompuServ, is actually just a path to a (sometimes dynamically created) file on someone else's computer.
Files on your computer have file paths, too, and this article explains how to understand file paths, and why they're important.

When computers became a household item, they started to take on stronger and stronger analogies to real-world models.
For instance, instead of accounts and directories, personal computers were said to have "desktops" and "folders", and eventually people developed the latent impression that the computer was a window into a virtual version of the real world.
It's a useful analogy because everyone is familiar with the concept of desktops and file cabinets, while fewer people understand digital storage and memory addresses. 

Imagine for a moment that you had invented computers or operating systems.
You'd probably have instinctively created a way to group common files together, because humans love to classify and organize things.
Since all files on a computer are on the hard drive, the biggest container you probably would have designated is the drive itself; that is, all files on a drive are in the drive.

As it turns out, the creators of UNIX had the same instinct, only they called the units of organization *directories* or *folders*. 
All files on your computer's drive are in the root directory of the system.

Even external drives you attach to your computer are brought into this root directory, just as you might place important related items into one container if you were organizing your office space or hobby room. 

Files and folders on Linux are given names containing all the usual things like letters and numbers and other characters on a keyboard.
But when a file is inside a folder, or a folder is inside another folder, the ``/`` character is used to show the relationship between them.
That's why you often see files listed in the format ``/usr/bin/python3`` or ``/etc/os-release``.
The forward-slashes indicate that one item is being stored inside of the item preceding it.

Every file and folder on a [POSIX](https://opensource.com/article/19/7/what-posix-richard-stallman-explains) system can be expressed as a path.
If I have a file called **penguin.jpg** in my home directory, and my username is ``seth``, then the file path can be expressed as ``/home/seth/penguin.jpg``.

Most users interact primarily with their home directory, so the tilde (``~``) character is used as shorthand for your home directory.
That means that I can express my example penguin picture as either ``/home/seth/penguin.jpg`` or as ``~/penguin.jpg``.


## Practice makes perfect

Computers use file paths whether you're thinking of what that path is or not.
And there's not necessarily any reason for you to have to think of files in terms of a path.
However, file paths are part of a useful framework for understanding how computers work, and learning to think of files in a path can be useful to you if you're looking to become a developer (you need to understand the paths of support libraries), a web designer (file paths ensure you're pointing your HTML to the appropriate CSS), or just a power user.


### When in doubt, drag and drop 

But if you're not used to thinking of the structure of your hard drive as a *path*, then it can be difficult to construct a full path for an arbitrary file.
On Linux, most file managers either natively display (or else have the option to activate it) the full file path to any location you go.

![Dolphin file manager](dolphin-file-path.jpg)

Seeing the full file path can help reenforce the concept for you on a daily basis.

And if you are using a terminal for something, it might help you to know that modern terminals, unlike the teletype machines they are emulating, can accept files by way of a drag-and-drop.
If you're trying to copy a file to a server over SSH, for instance, and you're not certain of how to express the file path, try dragging the file from your file manager into your terminal.
The GUI object representing the file gets translated into a text file path.

![Modern terminals accept files like file managers](terminal-drag-drop.jpg)

Don't waste time typing in guesses to your files. 
Just drag and drop.

### Tab is your friend

On a system famous for eschewing three-letter commands when two or even one-letter commands will do, you can rest assured that no seasoned POSIX user *ever* types out everything they are doing.
In the Bash shell, the **Tab** key means *autocomplete*, and autocomplete never lies.

For instance, to type the location of the example **penguin.jpg** file, you can just type:

```
$ ~/ph
```

and then press the **Tab** key on your keyboard.
As long as there is onle one item starting with ``ph``, the folder ``Pictures`` get autocompleted for you. 
If there are two or more items starting with the letters you are attempting to autocomplete, then Bash displays what those are so that you can manually type more until you reach a unique string that can safely be autocompleted by the shell.
The best thing about this isn't necessarily that it saves you from typing (though that's definitely a selling point), but that autocomplete is never wrong.
No matter how much you fight the computer to autocomplete something that isn't there, in the end you'll find that autocomplete understands paths better than anyone.
Assume that you, in a fit of late night reorganization, move ``penguin.jpg`` from your ``~/Pictures`` folder to your ``~/Spheniscidae`` directory.
You fall asleep and wake up refreshed but with no memory that you've reorganized, so you try to copy ``~/Pictures/penguin.jpg`` to your web server, in the terminal, using autocomplete.
No matter how much you pound on the **Tab** key, Bash refuses to autocomplete because the file you want simply does not exist in the location you want it to exist.
That can be helpful when you're trying to point your web page to a font or CSS file *you were just sure* you'd uploaded, or when you're pointing a compiler to a library you're *100% positive* you already compiled, and so on.

### This isn't your grandma's autocompletion

If you like Bash's autocompletion, you'll come to scoff at it once you try the autocomplete in [Zsh](https://opensource.com/article/18/9/tips-productivity-zsh).
The Z-shell, along with the [Oh My Zsh](https://ohmyz.sh/) site, provides a dynamic experience filled with plugins for specific programming languages and environments, visual themes packed with useful feedback, and a vibrant community of passionate shell users.

![A very modest Zsh configuration](zsh-simple.jpg)

If you're a visual thinker and you find the display of most terminals stagnant and numbing, Zsh may well change the way you interact with your computer.


## File paths

File paths are important on any system because even though you might be a visual thinker who prefers to think of files as literal documents inside of literal folders, the computer sees files and folders as named tags in a big pool of data.
The way it identifies one collection of data from another is by following its designated file path.
If you understand file paths, then you can talk the same language as your OS, and that can make file operations much much faster.