Navigating Directories

In a typical graphical interface (desktop environment), you can browse directories using a mouse, but on the Linux terminal, you need to use various commands to navigate, interact with files, and directories.

This section is designed to teach the basics of Linux navigation, including navigating directories, listing files, managing files, and using shortcuts for efficiency.

Linux File System Structure

The Linux file system is hierarchically organized, starting with the root directory /. Understanding this structure is key to effectively navigating in Linux.

  • /: The root directory, the base of the file system.

  • /bin, /sbin: Contains essential user and system binaries.

  • /etc: Contains system configuration files.

  • /home: Contains personal directories for users.

  • /var: Contains variable files like logs and databases.

  • /usr: Secondary hierarchy for user data; contains most user programs and utilities.

Getting Started with Linux Navigation

First, it's important to know which directory/location you're in. The pwd (print working directory) command will show you your current location in the file system.

user@hackerbox:~$ pwd
/home/user

In the example above, the user is in the /home/user directory. The home folder contains individual home directories for users. Each user typically has a home directory, and the terminal starts in the user's home directory when it is first opened.

To list the files and folders in your current directory, you can use the ls command.

user@hackerbox:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Videos

The above example shows the output of the ls command. As seen, there are 6 folders in the current location. You can detail the output of the ls command using the -l option, which shows details such as permissions, ownership, and modification times.

The output with the -l option includes columns with the following structure:

Column Content
Description

drwxr-xr-x

File type and permissions

2

Number of hard links to the file/directory

user

Owner of the file/directory

users

Group owner of the file/directory

4096

Size of the file or blocks used to store the directory information

Jul 29 08:24

Creation or last modification date of the file/directory

Desktop

Name of the file/directory

We have listed the contents of the directory, but there might be hidden files/directories we don't see. In Linux, files and directories starting with . are considered hidden files (e.g., .bashrc file). These won't be listed by default with the ls command. To include hidden files in the list, use the -a option with ls.

As seen, the previously hidden .bashrc file is now listed in the output of the ls command.

You can combine the -la nd -a options with ls -la.

As seen, combining the -l and -a options allows for both detailed listing and inclusion of hidden files.

Navigating to Other Directories

You don't need to be in a directory to list its contents. You can provide the path to a directory as a parameter to the ls command:

To change your current directory, use the cd (change directory) command:

If you wish to go back to the previous directory, simply type cd -.

Another feature you should know about while navigating directories is auto-completion. It speeds up your navigation and prevents typos.

Type

cd /usr/sand press the Tab key twice, which will suggest directories starting with "s" in the /usr/location, allowing you to effortlessly write the path you want to navigate to.

Last updated