This chapter is about navigating the file system in Linux using the command line.
Understanding the File System Tree
- Linux organizes files in a hierarchical directory structure, like a tree.
- The top of the tree is the root directory, represented by a single slash (
/). - Unlike Windows, Linux has only one single file system tree, even if you have multiple hard drives. All storage devices are attached to this single tree.
The Current Working Directory
- When you are in the shell, you are always “inside” a specific directory. This is called the current working directory.
- To see which directory you are in, use the
pwdcommand, which stands for “print working directory.” - When you first start a terminal session, you are in your home directory, represented by (
~). This is the only place a regular user can create files.
Commands for Navigation
ls: This command lists the contents (files and subdirectories) of the current directory. You can also use it to list the contents of any other directory by giving it a specific path.cd: This command is used to change the current working directory. You must follow it with the pathname of the directory you want to go to.
Absolute and Relative Pathnames
There are two ways to describe a path to a directory:
- Absolute Pathnames: These paths start from the root directory (
/). For example,/usr/binis an absolute path that starts at the root, goes into theusrdirectory, and then into thebindirectory. - Relative Pathnames: These paths start from your current working directory. They use special symbols:
.(a single dot) refers to the current directory...(two dots) refers to the parent directory (the directory one level above the current one).- For example,
cd ..moves you up one directory, andcd bin(which is the same ascd ./bin) moves you into thebinsubdirectory from your current location.
Helpful Shortcuts and Important Filename Facts
cd: Typingcdby itself will take you to your home directory.cd -: This command changes the directory back to the previous directory you were in.cd ~user_name: This changes the directory to the home directory of a specific user.cd ~:- This changes the directory to the home directory of the user (if there is only one user).
- This changes the directory to the root directory if you’re logged as root.
Important facts about filenames
- Hidden files: Filenames that start with a period (
.) are hidden. You won’t see them with a simplelscommand unless you use thels -aoption. - Case sensitive: Linux treats
File1andfile1as two different files. - No file extensions: Linux does not require file extensions (like
.txtor.jpg) to know what a file is. Many programs still use them, but they are not mandatory for the operating system. - Spaces in filenames: Avoid using spaces in filenames. Instead, use underscores (
_) or dashes (-) to separate words.