Tips and Tricks for Navigating the Linux File-system via the Command Line
Navigating the Linux file system can be a challenge for beginners, especially via the command line interface(CLI). Here are useful some tips and tricks for navigating the Linux file system via the command line.
Page Contents
Understanding Absolute Path and Relative Path
1. On a computer, an absolute path and a relative path are two ways to specify the location of a file or directory in a file system.
An absolute path specifies the complete location of a file or directory, beginning with the file system’s root directory, denoted by the /
symbol. All of the directories and subdirectories that lead to the file or directory are included. In a Linux-based operating system, an absolute path looks like this:
/home/fossman/Documents/myfile.txt
In this example, /
represents the root directory, home
is a subdirectory of the root directory, fossman
is a subdirectory of home
, and so on.
A relative path defines a file or directory’s location relative to the current working directory. It does not start from the root directory and only includes the necessary directories and subdirectories to reach the file or directory from the current location.
Based on the previous example, assuming you are in the /home/fossman/
a relative path to the myfile.txt file would look like this:
Documents/myfile.txt OR ./Documents/myfile.txt
Absolute paths are typically used when referring to files and directories in a different part of the file system or when working with multiple users who have different working directories. Relative paths are often used in scripting and programming to make it easier to refer to files and directories that are located in the same directory or a nearby directory.
Use cd Command
2. Use the cd
command to change directories. For example, the following command will take you to the Documents directory in your home directory, assuming your username is fossman:
$cd /home/fossman/Documents
Use ~ Symbol to Go Home
3. In Linux, the tilde symbol, “~”
is used as a shorthand notation to refer to the home directory of the current user. So if a user is logged in as “fossman”, the tilde “~” symbol represents the directory "/home/fossman"
.
If you are logged in, to go directly to your home directory from the current working directory, use the ~ symbol like this:
$cd ~
Use . and .. Directory Symbols
4. Additionally, in the Linux file system, the dot symbol "."
implies the current directory and two dots ".."
means the parent directory of the current directory. The latter is a special directory entry that is used to reference the directory immediately above the current directory in the file system hierarchy.
For example, if the current directory is /home/fossman/Documents
, then "."
refers to the directory /home/fossman/Documents, and ".."
refers to the directory /home/fossman/
.
To navigate to a parent directory of the current directory, you can use the "cd"
command followed by ".."
as follows:
$cd /home/fossman/Documents $cd ../
You can also use multiple ../
symbols in the cd command. Here is an example:
$cd /home/fossman/Documents/projects/scripts/ $cd ../../../
Use ls Command
5. Use the ls
command to list the contents of a directory. For example, ls /home/username/Documents
will list the files and directories in the Documents directory.
Use pwd Command
6. Use the pwd
command to print the current working directory. This is useful to check where you are in the file system.
Use mkdir Command
7. Use the mkdir
command to create a new directory. The following example will create a new directory called “mytestdir
” in the current directory.
$mkdir mytestdir
Use rmdir Command
8. Use the rmdir
command to remove a directory. For example, rmdir mytestdir
will remove the directory "mytestdir"
from the current directory.
$rmdir mytestdir
Use cp Command
9. Use the cp
command to copy files and directories. For example, this command will copy the file "file.txt"
to the Documents directory.
$cp file.txt /home/username/Documents
Use mv Command
10. Use the mv
command to move files and directories. For example:
$mv file.txt /home/username/Documents OR $mv file.txt /home/username/Documents/myfile.txt
Use rm Command
11. Use the rm
command to remove files from the file system:
$rm file.txt
Or specify the absolute path of the directory where the file to be deleted is located:
$rm /home/username/Documents/myfile.txt
Use touch Command
12. Use the touch
command to create an empty file. To create a file called myfile.txt
in the current directory, run this command:
$touch myfile.txt
Or specify the absolute path of the directory where the file should be created:
$touch /home/username/Documents/myfile.txt
Conclusion
These are just a few of the many commands available for navigating the Linux file system via the command line. With practice, you can become proficient in using these commands and more to efficiently navigate and manipulate files and directories on your Linux system.