How To Navigate Directories Faster In Linux

Navigate Directories Faster In Linux

Today we are going to learn some command line productivity hacks. As you already know, we use “cd” command to move between a stack of directories in Unix-like operating systems. In this guide I am going to teach you how to navigate directories faster without having to use “cd” command often. There could be many ways, but I only know the following three methods right now! I will keep updating this guide when I came across any methods or utilities to achieve this task in the days to come. Let us get started, shall we?

Navigate Directories Faster In Linux

Method 1: Using Pushd, Popd And Dirs Commands

This is the most frequent method that I use everyday to move between a stack of directories. The “Pushd”, “Popd”, and “Dirs” commands comes pre-installed in most Linux distributions, so don’t bother with installation. These trio commands are extremely useful when you’re working in a deep directory structure and scripts. For more details, check our guide in the link given below.

Method 2: Using “bd” utility

The “bd” utility also helps you to quickly go back to a specific parent directory without having to repeatedly typing “cd ../../.” on your Bash.

Bd is also available in the Debian extra and Ubuntu universe repositories. So, you can install it using “apt-get” package manager in Debian, Ubuntu and other DEB based systems as shown below:

sudo apt-get update
sudo apt-get install bd

For other distributions, you can install as shown below.

sudo wget --no-check-certificate -O /usr/local/bin/bd https://raw.github.com/vigneshwaranr/bd/master/bd
sudo chmod +rx /usr/local/bin/bd
echo 'alias bd=". bd -si"' >> ~/.bashrc
source ~/.bashrc

To enable auto completion, run:

sudo wget -O /etc/bash_completion.d/bd https://raw.github.com/vigneshwaranr/bd/master/bash_completion.d/bd
source /etc/bash_completion.d/bd

Bd has been installed. Let use some examples.

Create some directories.

mkdir -p dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10

The above command will create a hierarchy of directories. Let us check directory structure using command:

$ tree dir1/
dir1/
└── dir2
 └── dir3
 └── dir4
 └── dir5
 └── dir6
 └── dir7
 └── dir8
 └── dir9
 └── dir10

9 directories, 0 files

Alright, we have now 10 directories. Let us say you’re in 7th directory i.e dir7.

$ pwd
/home/sk/dir1/dir2/dir3/dir4/dir5/dir6/dir7

You want to move to dir3. Normally you would type,

cd /home/sk/dir1/dir2/dir3

Right? yes! But it not necessary now! To go back to dir3, just type:

bd dir3

Now you will be in dir3.

Easy, isn’t it? It supports auto complete, so you can just type a partial name of a directory and hit the tab key to auto complete the full path.

To check the contents of a specific parent directory, you don’t need to be there. Just type:

ls `bd dir1`

The above command will display the contents of dir1 from your current working directory.

For more details, check out the following GitHub page.

Method 3: Using “Up” Shell script

The “Up” is a shell script that allows you to move quickly to your parent directory. It works well on many popular shells such as Bash, Fish, and Zsh etc. Installation is absolutely easy too!

To install “Up” on Bash, run the following commands one bye:

curl --create-dirs -o ~/.config/up/up.sh https://raw.githubusercontent.com/shannonmoeller/up/master/up.sh
echo 'source ~/.config/up/up.sh' >> ~/.bashrc

The up script registers the “up” function and some completion functions via your “.bashrc” file.

Update the changes using command:

source ~/.bashrc

On zsh:

curl --create-dirs -o ~/.config/up/up.sh https://raw.githubusercontent.com/shannonmoeller/up/master/up.sh
echo 'source ~/.config/up/up.sh' >> ~/.zshrc

The up script registers the “up” function and some completion functions via your “.zshrc” file.

Update the changes using command:

source ~/.zshrc

On fish:

curl --create-dirs -o ~/.config/up/up.fish https://raw.githubusercontent.com/shannonmoeller/up/master/up.fish
source ~/.config/up/up.fish

The up script registers the “up” function and some completion functions via “funcsave”.

Now it is time to see some examples.

Let us create some directories.

mkdir -p dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10

Let us say you’re in 7th directory i.e dir7.

$ pwd
/home/sk/dir1/dir2/dir3/dir4/dir5/dir6/dir7

You want to move to dir3. Using “cd” command, we can do this by typing the following command:

cd /home/sk/dir1/dir2/dir3

But it is really easy to go back to dir3 using “up” script:

up dir3

That’s it. Now you will be in dir3. To go one directory up, just type:

up 1

To go back two directory type:

up 2

It’s that simple. Did I type the full path? Nope. Also it supports tab completion. So just type the partial directory name and hit the tab to complete the full path.

For more details, check out the GitHub page.

Please be mindful that “bd” and “up” can only help you to go backward i.e to the parent directory. You can’t move forward. If you want to switch to dir10 from dir5, you can’t! You need to use “cd” command to switch to dir10. These two utilities are meant for quickly moving you to the parent directory!

In this guide, you have learned the different ways to navigate directory stack faster and easier in Linux. As you can see, it’s not that difficult to browse a pile of directories faster. Now stop typing “cd ../../..” endlessly by using these tools. If you know any other worth trying tool or method to navigate directories faster, feel free to let us know in the comment section below. I will review and add them in this guide.

And, that’s all for now. Hope this helps. More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *