Directories

Navigating Directories

A directory contains a set of files. Directories can contain other directories, allowing files to be organized in a hierarchy.  
Each directory's name must be unique within the context of its parent directory.

Moving Around

At any time, you are inside of a directory. The pwd command displays your current directory.
$ pwd
/home/myuser/code/kubernetes
 
Changing to a new directory is done through the cd command.
$ cd /home/myuser/code/openshift
$ pwd
/home/myuser/code/openshift
 
The parent directory is referenced through the .. shortcut.
$ pwd
/home/myuser/code/openshift
$ cd ..
$ pwd
/home/myuser/code
 
Instead of specifying the full directory path, you can indicate the path relative to your current directory.
$ pwd
/home/myuser/code
$ cd kubernetes
$ pwd
/home/myuser/code/kubernetes

Listing Files in a Directory

The ls command, by default, will list both files and directories in the current directory.
$ ls
existing-dir existing-file
 
Notice how the output contains multiple files on each line. This can get difficult to read when there are a number of files in the directory.
 
The -l flag (which stands for "long") is commonly used to view more information about each file/directory. This extra information includes permissions, user and group ownership, file size, and timestamps.
$ ls -l
drwxr-xr-x. 2 myuser myuser 40 May 19 10:52 existing-dir
-rw-r--r--. 1 myuser myuser 12 May 19 10:52 existing-file
 
In the above example, note the first character on each line. If it is set to d, the entry is a directory. Otherwise, it is a file.
 
Another commonly used flag is -a, used to indicate that all files (including the current directory and the parent) should be listed.
$ ls -a
. .. existing-dir existing-file

The above example introduced two shortcuts that can be used at the command line. The shortcut . can be used to refer to the current directory. The other shortcut, .., refers to the parent of the current directory.

These two flags are frequently combined to give a reasonably detailed view of the current directory.

$ ls -al
total 4
drwxr-xr-x. 3 myuser myuser 80 May 19 10:52 .
drwxr-xr-x. 3 myuser myuser 60 May 19 10:45 ..
drwxr-xr-x. 2 myuser myuser 40 May 19 10:52 existing-dir
-rw-r--r--. 1 myuser myuser 12 May 19 10:52 existing-file
So far, the examples have all simply used the ls command by itself, which defaults to the current directory. The full path to another directory can be specified to list its contents instead.
$ cd /
$ ls /home/myuser
total 4
drwxr-xr-x. 3 myuser myuser 80 May 19 10:52 .
drwxr-xr-x. 3 myuser myuser 60 May 19 10:45 ..
drwxr-xr-x. 2 myuser myuser 40 May 19 10:52 existing-dir
-rw-r--r--. 1 myuser myuser 12 May 19 10:52 existing-file

Creating & Deleting Directories

New directories are created using the mkdir command:
 
$ ls
existing-dir

$ mkdir my-new-dir

$ ls
existing-dir
my-new-dir
 
Both files and directories are deleted through the rm command. Directories, however, require a special flag (-r for "recursive") when being deleted.
 
Warning: This is, as you can imagine, a destructive action. Take care that the directory you specify is actually the one you intend to delete, as it cannot easily be recovered.
$ ls
existing-dir
my-new-dir

$ rm my-new-dir
rm: cannot remove 'my-new-dir/': Is a directory

$ rm -r my-new-dir

$ ls
existing-dir
 

Copying & Moving (& Renaming)

 
Files (directories are slightly different and covered later) are copied using the cp command and specifying the file to be copied and its destination path and name.
 
For example, to make a copy of a file in the same directory:
$ ls
existing-dir existing-file

$ cp existing-file copy-file

$ ls
copy-file existing-dir existing-file
 
If no path information is explicitly specified, the copy will be made in the same directory. For either argument, the full path of the file may be specified.
$ cp /home/myuser/existing-file /tmp/temp-copy
Just like when deleting directories, the -r flag must be used when copying a directory.
$ cp existing-dir copy-dir
cp: -r not specified; omitting directory 'existing-dir'

$ cp -r existing-dir copy-dir

$ ls
copy-dir copy-file existing-dir existing-file
 
Moving files between directories is done through the mv command, specifying (in order) the file to be moved and the directory into which to move it.
$ ls
copy-dir copy-file existing-dir existing-file

$ mv copy-file /tmp

$ ls
copy-dir existing-dir existing-file

$ ls /tmp/copy-file
/tmp/copy-file
Unlike with cp and rm, the move command does not require a special flag when dealing with directories.
$ mv copy-dir /tmp

$ ls /tmp/copy-dir
/tmp/copy-dir
There is no specific "rename" command. Renaming a file or directory is done by simply moving (using the mv command) the file to its new name.
$ ls
existing-dir existing-file

$ mv existing-file my-file
existing-dir my-file