1. Overview

On a Linux system, navigating directories and accessing online resources are considered fundamental tasks. For instance, there are scenarios when we may prefer to interact with the file system or explore URLs in the web browser using a quick command-line approach.

In this tutorial, we’ll check out methods for opening a directory or a URL through the command line on a Ubuntu 22.04 GNOME desktop.

Particularly, we use the nautilus and xdg-open commands and edit the ~./bashrc file as well.

2. Using the nautilus Command

The nautilus command is used to launch Nautilus, the default file manager of GNOME directly from the command line. Specifically, we can use it to open directories with a graphical interface.

Now, let’s run the nautilus command to open a directory through the terminal:

$ nautilus /home/author/Documents/folder

Here, we passed /home/author/Documents/folder as the directory path to open in Nautilus:

Opening a directory using nautilus command in Linux

As a result, we can now access and use the opened directory in Nautilus.

3. Using the xdg-open Command

The xdg-open command is used to open files, directories, and URLs using the default applications associated with them.

For instance, let’s run the xdg-open command to open the /home/author/Documents/folder directory through the command line:

$ xdg-open /home/author/Documents/folder

The above command opens the given path in the default file manager of our Linux system:

Opening a directory using xdg-open command in Linux

In addition, we can also use the xdg-open command to launch the default web browser and navigate to a URL:

$ xdg-open http://baeldung.com &

Here, the ampersand & symbol at the end runs the xdg-open command in the background and enables us to continue using the terminal.

Let’s check the result:

Opening a URL using xdg-open command in Linux

Thus, we navigate to the URL in Google Chrome, the default web browser of our system.

4. Editing the ~/.bashrc File

Moreover, we can also edit the ~./bashrc file of the system to create aliases for opening directories and URLs through the command line.

For this, we first open the ~./bashrc file using a text editor like nano:

$ sudo nano ~/.bashrc

Then, we define aliases for opening directories and URLs:

alias opendir=nautilus
alias openurl=xdg-open

In the above script, the first line creates an alias named opendir equivalent to the nautilus command. Specifically, when we type opendir, the terminal interprets it as nautilus.

Likewise, the second line creates an alias named openurl which directly spawns the xdg-open command. This states that when we execute openurl, the terminal treats it as xdg-open.

After adding the code, we press CTRL+O and Return to save the ~./bashrc file and CTRL+X to exit the nano editor.

Adding aliases in bashrc file in Linux

Next, we run the source command to apply the changes made to the ~./bashrc file:

$ source ~/.bashrc

Now, we can use the opendir alias to open the /home/author/Documents/folder directory from the command line:

$ opendir /home/author/Documents/folder

Let’s check the result:

Opening a directory using alias in Linux terminal

Consequently, the given alias opens the /home/author/Documents/folder directory in Nautilus.

Now, let’s use the openurl alias to navigate to a URL through the terminal:

$ openurl http://baeldung.com

Here, we passed http://baeldung.com as a URL that we want to open in our default web browser:

Opening a URL using alias in Linux terminal

Thus, we successfully navigate to the URL using the alias in the terminal.

5. Conclusion

In this article, we learned different methods for opening a directory and a URL through the Linux command line.

Particularly, we used the nautilus command to only open the directory in the default file manager. Further, the xdg-open command can open both a directory and a URL as per our requirements.

On the other hand, we can edit the ~./bashrc file and define aliases for the same purpose, so it’s more readily available when needed.

Ultimately, we can select any of these methods based on our preferences for using the simple commands or their relevant aliases in the terminal.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.