8.3 Working with Files and Directories

To address a certain file or directory, you must specify the path leading to that directory or file. As you may know from MS DOS or Mac OS already, there are two ways to specify a path:

Absolute Path

Enter the entire path from the root directory to the respective file or directory.

Relative Path

Enter a path to the respective file or directory by using the current directory as a starting point. This implies to give the levels you have to move up or down in the file system tree to reach the target directory of file, starting from the current directory.

Paths contain filenames, directories or both, separated by slashes. Absolute paths always start with a slash. Relative paths do not have a slash at the beginning, but can have one or two dots.

When entering commands, you can choose either way to specify a path—depending on your preferences or the amount of typing—both will lead to the same result. To change directories, use the cd command and specify the path to the directory.

NOTE: Handling Blanks in Filenames or Directory Names

If a filename or the name of a directory contains a space, either escape the space using a back slash (\) in front of the blank or enclose the filename in single quotes. Otherwise Bash interprets a filename like My Documents as the names of two files or directories, My and Documents in this case.

When specifying paths, the following shortcuts can save you a lot of typing:

To apply your knowledge, find some examples below. They address basic tasks you may want to execute with files or folders using Bash.

8.3.1 Examples for Working with Files and Directories

Suppose you want to copy a file located somewhere in your home directory to a subdirectory of /tmp.

  1. First, from your home directory create a subdirectory in /tmp:

    1. Enter

      mkdir  /tmp/test

      mkdir stands for make directory. This command creates a new directory named test in the /tmp directory. In this case, you are using an absolute path to create the test directory.

    2. To check what happened, now enter

      ls -l /tmp

      The new directory test should appear in the list of contents of the /tmp directory.

    3. Switch to the newly created directory with

      cd /tmp/test
  2. Now create a new file in a subdirectory of your home directory and copy it to /tmp/test. Use a relative path for this task.

    IMPORTANT: Overwriting of Existing Files

    Before copying, moving or renaming a file, check if your target directory already contains a file with the same name. If yes, consider to change one of the filenames or use cp or mv with options like -i which will prompt before overwriting an existing file. Otherwise Bash will overwrite an existing file without inquiry.

    1. To list the contents of your home directory, enter

      ls -l ~

      It should contain a subdirectory called Documents by default. If not, create this subdirectory with the mkdir command you already know:

      mkdir ~/Documents
    2. Enter

      touch ~/Documents/myfile.txt

      This command creates a new, empty file named myfile.txt in the Documents directory.

      Usually, the touch command updates the modification and access date for an existing file. If you use touch with a filename which does not exist in your target directory, it creates a new file.

    3. Enter

      ls -l ~/Documents

      The new file should appear in the list of contents.

    4. Enter

      cp ~/Documents/myfile.txt .

      Do not forget the dot at the end.

      This command tells Bash to go to your home directory and to copy myfile.txt from the Documents subdirectory to the current directory, /tmp/test, without changing the name of the file.

    5. Check the result by entering

      ls -l

      The file myfile.txt should appear in the list of contents for /tmp/test.

Now suppose you want to rename myfile.txt into tuxfile.txt. Finally you decide to remove the renamed file and the test subdirectory.

  1. To rename the file, enter

    mv myfile.txt tuxfile.txt
  2. To check what happened, enter

    ls -l

    Instead of myfile.txt, tuxfile.txt should appear in the list of contents.

    mv stands for move and is used with two options: the first option specifies the source, the second option specifies the target of the operation. You can use mv either

    • to rename a file or a directory,

    • to move a file or directory to a new location or

    • to do both in one step.

  3. Coming to the conclusion that you do not need the file any longer, you can delete it by entering

    rm tuxfile.txt 

    Bash deletes the file without any inquiry.

  4. Move up one level with cd .. and check with

    ls -l test

    if the test directory is empty now.

  5. If yes, you can remove the test directory by entering

    rmdir test