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:
Enter the entire path from the root directory to the respective file or directory.
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.
![]() | 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 ( | |
When specifying paths, the following “shortcuts” can save you a lot of typing:
The tilde symbol (~) is a shortcut for home
directories. For example, to list the contents of your home directory, use
ls
~. To list the contents of another user's home directory,
enter ls ~ (or course, this will only work if you have permission
to view the contents, see Section 7.3, “File Access Permissions”).
For example, entering ls ~tux would list the contents of
the home directory of a user named username
tux. You can use the tilde symbol as shortcut for home
directories also if you are working in a network environment and where your
home directory may not be called /home but can be
mapped to any directory in the file system.
From anywhere in the file system, you can reach your home directory by
entering cd ~ or even shorter, by
simply entering cd without any options.
When using relative paths, refer to the current directory with a dot
(.). This is mainly useful for commands such as
cp or mv by which you can copy or
move files or directories.
The next higher level in the tree is represented by two dots
(..). In order to switch to the parent directory of
your current directory, enter cd .., to go up two levels
from the current directory enter cd ../.. etc.
To apply your knowledge, find some examples below. They address basic tasks you may want to execute with files or folders using Bash.
Suppose you want to copy a file located somewhere in your home
directory to a subdirectory of /tmp.
First, from your home directory create a subdirectory in
/tmp:
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.
To check what happened, now enter
ls -l /tmp
The new directory test should appear in the list of contents of
the /tmp directory.
Switch to the newly created directory with
cd /tmp/test
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.
![]() | 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 | |
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
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.
Enter
ls -l ~/Documents
The new file should appear in the list of contents.
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.
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.
To rename the file, enter
mv myfile.txt tuxfile.txt
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.
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.
Move up one level with cd .. and check with
ls -l test
if the test directory is empty now.
If yes, you can remove the test directory by
entering
rmdir test