Each user has a login (aka home) directory, for example /home/alice for the user alice. Each file that alice owns inside her home directory has an unambiguous name, starting with the prefix /home/alice. Each running program, known as a process, has a current directory, and all filenames are implicitly assumed to start with the name of that directory unless they begin directly with a slash. The command pwd (print working directory) identifies the current directory.
[alice@localhost ~]$ pwd /home/alice [alice@localhost ~]$
The notion of directories is organizational. Related files belong together in a directory. For example, a project in a course, or a set of recipes. For example, the following creates a set of directories for this course.
[alice@localhost ~] mkdir cshu-153 [alice@localhost ~] cd cshu-153 [alice@localhost ~] mkdir module1 module2 module3 module4 module5 module6 [alice@localhost ~] cd module1 [alice@localhost ~] touch README.md [alice@localhost ~] cd .. [alice@localhost ~] cd module2 [alice@localhost ~] mkdir test1 [alice@localhost ~] cd test1 [alice@localhost ~] echo "I am a test string" > test1
The echo command shown above prints the string "I am a test string" that gets redirected to the file named test1. Note that the ls command only shows the files in the current directory. The -R option shows the directories and files recursively. For example (recall that the dot represents the current directory):
[alice@localhost ~]$ ls -R cshu-153/ cshu-153/: module1 module2 module3 module4 module5 module6 cshu-153/module1: README.md cshu-153/module2: test1 cshu-153/module2/test1: message cshu-153/module3: cshu-153/module4: cshu-153/module5: cshu-153/module6:
The structures of directories and files looks like an upside down tree. You can see a more visual representation of the tree structure of a directory with the tree command. Try tree cs-hu153 (if the command isn't found, install it using your package manager yum).
A directory is just a file in Linux as well. However, the system does not let users directly read/write directories. Rather we call the system to do operations on our behalf (for integrity of the file system and for security). This can be done with commands such as ls, mkdir etc or it can done from inside a program.
Examine the following Java program that recursively lists the size of each file in a directory. Note that the method listFiles calls itself recursively!
style=javanonum
Here is its output on the same folder:
[alice@localhost ~]$ java ListFiles cs-hu153/ /home/alice/cs-hu153: 4096 bytes /home/alice/cs-hu153/module4: 4096 bytes /home/alice/cs-hu153/module2: 4096 bytes /home/alice/cs-hu153/module2/test1: 4096 bytes /home/alice/cs-hu153/module2/test1/message: 19 bytes /home/alice/cs-hu153/module6: 4096 bytes /home/alice/cs-hu153/module1: 4096 bytes /home/alice/cs-hu153/module1/README.md: 0 bytes /home/alice/cs-hu153/module5: 4096 bytes /home/alice/cs-hu153/module3: 4096 bytes [alice@localhost ~]$
What happens if we keep walking up the file system tree using the cd command?
[alice@localhost cshu-153]$ cd ..; pwd /home/alice [alice@localhost ~]$ cd ..; pwd /home [alice@localhost home]$ cd ..; pwd / [alice@localhost /]$ cd ..; pwd / [alice@localhost /]$
The directory / is called the root directory of the file system. Every file in the system is in the root directory or one of its subdirectories, and the root is its own parent directory.