Abstract
| Goal |
View and manage file organization in a Linux file system. |
| Sections |
|
| Lab |
|
Like most computers, a Linux computer contains data organized into files and folders. A folder is a way to group files together in the same way that you keep papers organized in a physical folder. A computer folder is a listing of files, so on Linux, it is common to use the term directory. The two terms mean the same thing, and can be used interchangeably.
When you log in to a Linux system, you have full access to your home directory. Your home directory contains configuration files and all the data files and directories that you create. Whether or not you have created any data, your home directory is prepopulated with these directories:
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
Linux provides these directories as the default target locations for the applications that you run on your computer.
For example, a file you download from the internet is saved to the Downloads directory.
When you take a screenshot, the image is saved to the Pictures directory.
System directories are situated outside your home directory, and are generally restricted from user interaction. You are not likely to accidentally delete a kernel, a driver, or a configuration file that your system requires because you do not usually have access to those important files.
Your home directory is in the home directory of the system, which itself is in the root directory (/).
The root directory, denoted by a single leading forward slash (/), is the start of the Linux file system.
Everything on your computer is contained in the root directory.
Important locations on a Linux system include the following directories:
The /etc directory contains configuration files.
The /tmp directory stores temporary files that are automatically removed after 30 days.
The /usr directory contains commands and applications.
The /var directory contains system variable data that must persist between boots.
In Linux, the term root is used in three distinct ways.
root userThe root user is the initial user account (usually disabled by default) that exists immediately after installation.
/root directoryThe /root directory is the home directory for the root user.
/ directoryThe / directory is called the root directory to suggest that it is the "root" of the file and directory structure, which resembles a family tree.
In this chapter, the term root refers exclusively to the / directory.
On the GNOME desktop, you can view your home directory with the Files application. After you launch Files, you are viewing your home directory.
On the command line, you can see the directories and files on your computer by using the ls or tree commands.
[user@host ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates VideosThe tree command provides a listing of every file and directory (including the files and directories that each directory contains).
[user@host Documents]$ tree
.
└── Work
├── example.txt
└── Presentation
└── Slides.orgThe tree command is recursive by default, which means that it lists directories within directories until there are no more directories to list.
On the GNOME desktop, you navigate your system by double-clicking on a directory icon in the Files application to change your view to that directory.
On the command line, you use the cd command to change your current working directory.
When run with no arguments, the cd command changes the user's directory to its home directory.
[user@host Documents]$cd Work[user@host Work]$cd[user@host ~]$
When you use the cd command, your prompt changes to display the new directory.
Any commands that you execute consider this new directory to be the current location.
For instance, when you run the tree command after changing to the Documents directory, the output returns the contents of the Documents directory instead of your home directory.
You can express the location of any file or directory on your computer by using the hierarchical design of directories on Linux. Everything exists in the root directory, so by starting with a single forward slash, you can define every step to a file's location by listing each parent directory, separated by a forward slash.
For example, the user home directory in this course has the path /home/user because the user directory is located inside the home directory, which is located inside the root / directory.
If you download a file called file.txt from the internet and your web browser saves it to your hard drive, then its default location is /home/user/Downloads/file.txt.
On the desktop, you can see your current directory in the top bar of the GNOME Files application window.
In a terminal, you can see your current working directory by using the pwd command:
[user@host ~]$pwd/home/user [user@host Documents]$cd Documents/home/user/Documents
You can see the full path of a file by using the realpath command, followed by the file that you want the path to lead to:
[user@host Documents]$ realpath example.txt
/home/user/Documents/example.txtAn absolute path is a file path that starts from the root directory.
The absolute path is an explicit expression of the precise location of a file or directory.
Every file on your computer has an absolute path that starts with a forward slash (/) character.
For example, the absolute path to the Documents directory in your home directory is /home/user/Documents, not just Documents.
A relative path is a file path that starts from your current working directory.
A relative path never starts with a forward slash (/) character.
Instead, a relative path starts with a dot (.), two dots (..), or the name of a file or a directory.
When a relative path starts with a name (of a file or directory) or a dot, the resource is in your current directory.
A name or a dot are instructions to look for the file in the current directory or a subdirectory.
For example, a file called example.txt in the Documents directory can be expressed as ./example.txt, or just example.txt when your current working directory is Documents.
[user@host Documents]$ls example.txtexample.txt [user@host Documents]$ls ./example.txt./example.txt
Two dots indicate a parent directory.
Two dots (..) are an instruction to move "up" in the file tree and to look for a file or directory starting from that location.
For example, the relative path from the Downloads directory to the Documents/example.txt file is ../Documents/example.txt because you must leave the Downloads directory and enter the Documents directory to be in the same location as the example.txt file.
One advantage of knowing a file path is that you can refer to a file or directory from anywhere on your system without having to be in the same directory. For example, to view the contents of a file, you can refer to that file by its full absolute or relative path, regardless of your current working directory.
[user@host ~]$cat ./Documents/example.txthello world [user@host ~]$cd /tmp[user@host tmp]$cat /home/user/Documents/example.txthello world [user@host tmp]$cat ../home/user/Documents/example.txthello world
cd(1), ls(1), pwd(1), realpath(1), and tree(1) man pages
To learn more about absolute and relative paths, refer to What Are the Differences Between Absolute and Relative Paths? at https://learn.spidernet.pl/sysadmin/linux-path-absolute-relative