% Directories: ROOT, /root, HOME, /home, and current % Ian! D. Allen -- -- [www.idallen.com] % Winter 2015 - January to Apil 2015 - Updated 2017-09-21 02:49 EDT - [Course Home Page] - [Course Outline] - [All Weeks] - [Plain Text] Directories: ROOT, `/root`, HOME, `/home`, and current ====================================================== Some people become confused about these terms: The ROOT directory -- `/` ------------------------- - The nameless directory at the top of the Unix/Linux/BSD/OSX file system tree. - The nameless directory to the left of the leftmost slash in an absolute pathname. - Often named `/` (slash), but since slashes *separate* pathname components, this is wrong. But you can't write nothing ("") easily, so slash `/` is often used to mean the nameless system ROOT directory. - Example: The file pathname `/etc/passwd` contains two directories -- the nameless ROOT directory to the left of the first slash, and the directory `etc` to the left of the second slash. The directory named `/root` --------------------------- - The personal HOME directory of the `root` account on many systems. - This is the HOME directory where `root` is placed when `root` logs in to the system as a user. - Note that this one `root` account HOME directory is not under the usual directory named `/home`, where most other account HOME directories are. - Not all systems give the system `root` account a separate `/root` directory. (Some systems may even use the system ROOT `/` directory as the HOME directory of the `root` account.) Your HOME directory ------------------- - The directory given to you by the system administrator and into which you are placed when you first log in, e.g: `/home/idallen` - Your HOME directory is the usual place to store your files. - This is the directory you go to when you type `cd` with no arguments. - You own this directory, and therefore you can change its permissions. - The name of your HOME directory is available in the shell keyword variable named `$HOME`. The variable name `$HOME` is **UPPER CASE**. - The name of your HOME directory is also available in some shells as a leading tilde on a pathname, e.g. `~/assignments`, `~/letters`, etc. - On many Unix systems, your HOME directory is located under the system directory named `/home` e.g. `/home/abcd0001`, `/home/zyxw0002`, etc., but this is not always true, or may not be true for all accounts The directory named `/home` --------------------------- - This is a pathname (probably and usually a directory, not a file) under the ROOT directory, named `home`. - `/home` is *NOT* your HOME directory; it is usually a system directory named `/home`. - Some other names (directories) under ROOT are `/etc`, `/bin`, and `/usr`. - `/home` is a system directory; you do not own it and you cannot change it - Usually the directory named `/home` contains all the user HOME directories, e.g. `/home/idallen`, `/home/abcd0001`, etc. The "current directory" ----------------------- - Also called the "current working directory" or "working directory". - This is the directory that your shell (or any Unix/Linux process) is currently in. - The current directory of a shell can be changed by using the `cd` command. - All *relative* pathnames in this process are relative to this current directory. (Absolute pathnames are not affected by current directory.) - Every process has its own current directory; changing current directory in one process does not change it for other processes. - The current directory is passed on to child processes when they are first started (e.g. from your shell process to shell scripts that you invoke). - Changing directories inside a shell script will not affect the parent process that called the shell script (different processes). - Since you can use the `cd` command to change the current directory of the shell, you may or may not have permissions to list, read, or modify the filenames in the "current directory"; it depends where your current directory is! - A name for the current directory is always `.` (dot) Problem: "Put the date into a file out.txt in the current directory." Solution: date >out.txt -or- date >./out.txt # unnecessary ./ Problem: "Put the date into a file out.txt in your HOME directory." Solution: date >"$HOME"/out.txt -or- date >~/out.txt Problem: "List the names (including hidden names) in the /home directory." Solution: ls -a /home Problem: "List the names (including hidden names) in the /home directory and append the output to file foobar.txt in your HOME directory." Solution: ls -a /home >>"$HOME"/foobar.txt -or- ls -a /home >>~/foobar.txt Problem: "Move the item named foo.txt from your HOME directory to the item named bar.txt in the current directory." Solution: mv "$HOME"/foo.txt bar.txt -or- mv ~/foo.txt bar.txt Problem: "Copy the file foo.txt from the current directory to your HOME directory." Solution1: cp foo.txt "$HOME"/foo.txt Solution2: cp foo.txt "$HOME"/ Solution3: cp foo.txt "$HOME" # In most shells, you can use a leading tilde ~ in place of a leading "$HOME": Solution1: cp foo.txt ~/foo.txt Solution2: cp foo.txt ~/ Solution3: cp foo.txt ~ Review: Absolute vs. Relative pathnames ======================================= Pathnames that resolve to begin with a slash (the ROOT directory) are absolute pathnames. Shell variables may contain slashes, and a shell tilde expansion usually expands to start with a slash: /home # absolute path starts with slash /bin/bash # absolute path starts with slash /etc/passwd # absolute path starts with slash /home/idallen/.bashrc # absolute path starts with slash $HOME/.bashrc # absolute because $HOME expands to /home/userid "$HOME"/.bashrc # absolute because "$HOME" expands to /home/userid ~/.bashrc # absolute because leading ~ is the same as $HOME ~idallen/.bashrc # absolute because leading ~user is HOME of user Adding quotes around an absolute pathname simply turns off the shell's GLOB and space processing for the name; the name is still absolute (because the shell strips the quotes away before using the name). All other pathnames (that do not start with a slash after being expanded) are relative pathnames. Relative pathnames behave as if the current directory were added to the front of all relative pathnames. For example, if the current directory is `/foo/bar`, then: In /foo/bar relative pathname "file" is actually "/foo/bar/file" In /foo/bar relative pathname "./file" is actually "/foo/bar/./file" In /foo/bar relative pathname "." is actually "/foo/bar/." In /foo/bar relative pathname ".." is actually "/foo/bar/.." In /foo/bar relative pathname "dir/xxx" is actually "/foo/bar/dir/xxx" ...etc... -- | Ian! D. Allen, BA, MMath - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/ [Plain Text] - plain text version of this page in [Pandoc Markdown] format [www.idallen.com]: http://www.idallen.com/ [Course Home Page]: .. [Course Outline]: course_outline.pdf [All Weeks]: indexcgi.cgi [Plain Text]: 170_home_and_HOME.txt [Pandoc Markdown]: http://johnmacfarlane.net/pandoc/