TranslateProject/sources/tech/20190802 Understanding file paths and how to use them in Linux.md
DarkSun fe275afd98 选题: 20190802 Understanding file paths and how to use them in Linux
sources/tech/20190802 Understanding file paths and how to use them in Linux.md
2019-08-03 01:14:59 +08:00

8.9 KiB
Raw Blame History

Understanding file paths and how to use them in Linux

If you're used to drag-and-drop environments, then file paths may be frustrating. Learn here how they work, and some basic tricks to make using them easier. open source button on keyboard

A file path is the human-readable representation of a file or folders location on a computer system. Youve seen file paths, although you may not realize it, on the internet: An internet URL, despite ancient battles fought by proprietary companies like AOL and CompuServe, is actually just a path to a (sometimes dynamically created) file on someone elses computer. For instance, when you navigate to example.com/index.html, you are actually viewing the HTML file index.html, probably located in the var directory on the example.com server. Files on your computer have file paths, too, and this article explains how to understand them, and why theyre important.

When computers became a household item, they took on increasingly 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. Its 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 invented computers or operating systems. You would probably have 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 these units of organization directories or folders. All files on your computers drive are in the systems base (root) directory. Even external drives 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 the usual components like the letters, numbers, and other characters on a keyboard. But when a file is inside a folder, or a folder is inside another folder, the / character shows the relationship between them. Thats why you often see files listed in the format /usr/bin/python3 or /etc/os-release. The forward slashes indicate that one item is stored inside of the item preceding it.

Every file and folder on a POSIX system can be expressed as a path. If I have the file penguin.jpg in the photos folder within my home directory, and my username is seth, then the file path can be expressed as /home/seth/Pictures/penguin.jpg.

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

Practice makes perfect

Computers use file paths whether youre thinking of what that path is or not. Theres not necessarily a 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 if youre looking to become a developer (you need to understand the paths to support libraries), a web designer (file paths ensure youre pointing your HTML to the appropriate CSS), a system administrator, or just a power user.

When in doubt, drag and drop

If youre 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 have the option to) the full file path to where you are, which helps reinforce the concept on a daily basis:

Dolphin file manager

opensource.com

If youre using a terminal, it might help to know that modern terminals, unlike the teletype machines they emulate, can accept files by way of drag-and-drop. When you copying a file to a server over SSH, for instance, and youre not certain of how to express the file path, try dragging the file from your GUI file manager into your terminal. The GUI object representing the file gets translated into a text file path in the terminal:

Terminals accept drag and drop actions

opensource.com

Dont waste time typing in guesses. 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, rest assured that no seasoned POSIX user ever types out everything. In the Bash shell, the Tab key means autocomplete, and autocomplete never lies. For instance, to type the example penguin.jpg files location, you can start with:

$ ~/Pi

and then press the Tab key. As long as there is only one item starting with Pi, the folder Pictures autocompletes for you.

If there are two or more items starting with the letters you attempt to autocomplete, then Bash displays what those items are. You manually type more until you reach a unique string that the shell can safely autocomplete. The best thing about this process isnt necessarily that it saves you from typing (though thats definitely a selling point), but that autocomplete is never wrong. No matter how much you fight the computer to autocomplete something that isnt there, in the end, youll 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 youve 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. The file you want simply does not exist in the location where you think it exists. That feature can be helpful when youre trying to point your web page to a font or CSS file you were sure youd uploaded, or when youre pointing a compiler to a library youre 100% positive you already compiled.

This isnt your grandmas autocompletion

If you like Bashs autocompletion, youll come to scoff at it once you try the autocomplete in Zsh. The Z shell, along with the Oh My Zsh 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 modest Zsh configuration.

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

Practice more

File paths are important on any system. You might be a visual thinker who prefers to think of files as literal documents inside of literal folders, but the computer sees files and folders as named tags in a pool of data. The way it identifies one collection of data from another is by following its designated path. If you understand these paths, you can also come to visualize them, and you can speak the same language as your OS, making file operations much, much faster.


via: https://opensource.com/article/19/8/understanding-file-paths-linux

作者:Seth Kenlon 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出