Updated: 2017-09-21 02:49 EDT

1 Directories: ROOT, /root, HOME, /home, and currentIndexup to index

Some people become confused about these terms:

1.1 The ROOT directory – /Indexup to index

1.2 The directory named /rootIndexup to index

1.3 Your HOME directoryIndexup to index

1.4 The directory named /homeIndexup to index

1.5 The “current directory”Indexup to index

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 ~

2 Review: Absolute vs. Relative pathnamesIndexup to index

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...
Author: 
| 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

Campaign for non-browser-specific HTML   Valid XHTML 1.0 Transitional   Valid CSS!   Creative Commons by nc sa 3.0   Hacker Ideals Emblem   Author Ian! D. Allen