Today’s LCoTD isn’t so much a single command as an introduction to the filesystem. Today, I’ll cover the cd command, the HOME variable, and the PWD and echo commands.
To make use of any Linux system, you need to be comfortable with Linux files and their layout. Every Linux file is contained in a collection called a directory. Directories form a hierarchy or tree. That is, one directory can contain any number of other directories, called subdirectories, which may also contain other files or subdirectories, and so on, and so on, and…you get the point. The topmost directory is called the root directory and is denoted by a slash (/). In Linux, all files and directories descend from the root. This is unlike Windows or DOS, where different devices are accessed by drive letters (C:, D:, etc.).
We refer to files and directories using a “names and slashes” syntax called a path. For instance, this path:
/one/two/three/fourrefers to the root directory /, which contains a directory called one, which contains a directory called two, which contains a directory called three, which contains the final file or directory, four. If a path begins with the root directory, it’s called an absolute path, and if not, it’s a relative path.
Whenever you are running a shell, that shell is “in” some directory (in an abstract sense). More technically, your shell has a current working directory (or CWD), and when you run commands in that shell, they operate relative to the directory. More specifically, if you refer to a relative file path in that shell, it is relative to your CWD. For example, if your shell is “in” the directory /one/two/three, and you run a command that refers to a file myfile, then it’s really /one/two/three/myfile. Likewise, a relative path a/b/c would imply the true path /one/two/three/a/b/c.
Two special directories are denoted . (a single period) and .. (two periods in a row). The former means your current directory, and the latter means your parent directory, on level above. So if your current directory is /one/two/three, then . refers to this directory and .. refers to /one/two.
You “move” your shell from one directory to another using the cd command:
$ cd /one/two/threeMore technically, this command changes your shell’s CWD to be /one/two/three. This is an absolute change (since the directory begins with “/”). You can make relative moves as well:
$ cd d
$cd ../mydirThe first example enters subdirectory d. The second goes up to the parent directory, and then into the directory mydir. File and directory names may contain most characters you expect: capital and small letters (which are not equivalent, as Linux filenames are case-sensitive), numbers, periods, dashes, underscores, and most other symbols (except “/” since it is reserved for separating directories). In general, however, avoid using spaces, asterisks, parentheses, and other characters that have special meaning to the shell. Otherwise, you’ll need to quote or escape these characters all the time.
Home Directories
Users’ personal files are often found in /home (for ordinary users) or /root (for superusers). Your home directory is typically /home/your-username: /home/george, /home/jones, etc. There are several ways to locate or refer to your home directory.
cd
With no arguments, the cd command returns you (i.e., sets the shell’s CWD) to your home directory.
HOME variable
The environment variable HOME contains the name of your home directory.
$ echo $HOME
/home/georgeWhen used in place of a directory, a lone tilde (~) is expanded by the shell to the name of your home directory.
$ echo ~
/home/george(echo tells the shell to print the arguments).
When followed by a username (as in ~george), the shell expands this string to be the user’s home directory:
$ cd ~smith
$ pwd
/home/smith(PWD stands for “Print Working Directory”).
That about covers it for today. Tomorrow, I’ll cover System Directories, something any Linux user needs to know to hit the ground running. Until then, Ta!
Related posts:




