Using Fasd in Linux for Quick Access to Files & Directories: 5 Easy Steps

Posted on

Using Fasd in Linux for Quick Access to Files & Directories: 5 Easy Steps

Using Fasd in Linux for Quick Access to Files & Directories: 5 Easy Steps

In this guide, we want to show you an ultimate guide for Using Fasd in Linux for Quick Access to Files & Directories. Most Linux users employ various Linux commands to navigate between files and directories from the command line, often needing to specify the full path to access desired locations. To streamline this process, a command-line utility called Fasd provides a more efficient way to access files and directories. Written in Shell, Fasd simplifies file access significantly.

Now you can follow the rest of the article to install and use the Fasd command that can help you access your files much easier than other Linux Commands such as cd.

To complete this guide, you must have access to your server as a root or non-root user with sudo privileges. For this purpose, you can visit the Orcacore website and check our initial server setup guides for Linux distros.

Step 1 – Install Fasd Command in Linux

The Fasd command utility is readily available in most Linux distributions. You can easily install and use it through your Linux package manager.

Install Fasd in Ubuntu / Debian

In Debian-based distributions, begin by updating the system’s package list and then install Fasd using the following commands:

# sudo add-apt-repository ppa:aacebedo/fasd
# sudo apt update
# sudo apt install fasd

Install Fasd in Centos / RHEL / AlmaLinux / Rocky Linux

To install Fasd in CentOS 7 and RHEL 7, you can use the commands below:

# sudo yum install epel-release
# sudo yum install fasd

For installing Fasd in CentOS 8, RHEL 8, and later versions such as AlmaLinux 9, you can either install Fasd from the Snap Store or clone it from GitHub. Here, we demonstrate the installation using Git:

# sudo dnf install git
# sudo git clone https://github.com/clvv/fasd.git
# cd fasd
# sudo make install

Enable Fasd Command in Linux

After the installation is complete, you need to add the following line to your ~/.bashrc file to enable Fasd:

vi ~/.bashrc

Add this line:

eval "$(fasd --init auto)"

Save and close the file. Then, source the file to apply the changes:

source ~/.bashrc

Now you can start Using Fasd in Linux for Quick Access to Files & Directories.

Step 2 – Fasd Aliases in Linux

Now, let’s explore the default Fasd aliases. These aliases provide shortcuts for common Fasd actions. Here are the default Fasd aliases:

1) alias a='fasd -a'        # any
2) alias s='fasd -si'       # show, search, select
3) alias d='fasd -d'        # directory
4) alias f='fasd -f'        # file
5) alias sd='fasd -sid'     # interactive directory selection
6) alias sf='fasd -sif'     # interactive file selection
7) alias z='fasd_cd -d'     # cd, same functionality as j in autojump
8) alias zz='fasd_cd -d -i' # cd with interactive selection

Each alias serves a specific purpose, making file and directory access more efficient. Let’s delve into Using Fasd in Linux for Quick Access to Files & Directories with these aliases.

Step 3 – Fasd Syntax Command

The general syntax for using Fasd is as follows:

fasd [options] [query ...]

Fasd maintains a history of frequently visited directories and opened files from the command line. It ranks these paths based on their "frecency" (frequency + recency). To illustrate this, run the Fasd command in your desired directory, and it will display the paths you’ve used, ordered by their frecency:

fasd

As demonstrated in the previous step, Fasd utilizes default aliases. You can use the f alias to list files in your current directory:

f

The a alias displays both files and directories:

a

The s alias lists previously visited directories and files, prompting you to select a path for display in the terminal.

And the d alias lists only directories:

d

Step 4 – Quick Access Files and Directories with Fasd in Linux

As you saw, the alias a=’fasd -a’ can be used for showing both files and directories. This will help you to easily find your files and directories. For example:

a
Quick Access Files and Directories with Fasd in Linux

Step 5 – Quick Navigate To Directories with Fasd in Linux

The Fasd utility uses alias z=’fasd_cd -d’ for switching between directories.

For example, we want to navigate to our core directory with the command below:

z co

Then, from there, we navigate to our test directory:

z te

As you can see, you can use the first two words of your directory for quick navigation.

Also, you can navigate to the subdirectories with one command. For example:

z co te

This will switch us to our core directory and the subdirectory test directly.

Note: If you have multiple locations and files with the same name, Fasd will access the file with a greater score which you have visited the most.

As you can see, the Fasd utility will help you to save time and access and list your files in an easier way.

For more options, you can use the Fasd help command:

fasd -h
Fasd Command options in Linux for Quick Access to Files & Directories

Conclusion

At this point, you have learned Using Fasd in Linux for Quick Access to Files & Directories. It has default aliases that can help you save time and you don’t need to type the full path of your directories in the command.

Hope you enjoy it. You may also interested in these articles:

Tail Command in Linux for Logs

Counting Files in a Linux Directory with Examples

Alternative Solutions for Quick File and Directory Access

While Fasd offers a convenient way to navigate files and directories, other approaches can achieve similar results. Here are two alternative solutions:

1. Utilizing zsh with the z plugin (z.lua)

zsh (Z Shell) is a powerful shell that offers extensive customization options. The z plugin provides a similar "jump around" functionality to Fasd, but it integrates seamlessly with zsh.

  • Explanation: The z plugin tracks your most frequently used directories based on your navigation history. You can then use the z command followed by a part of the directory name to quickly jump to the best matching directory. It’s very similar in concept to how Fasd works with the z alias, but it’s built specifically for zsh and often considered more performant and reliable by zsh users. z.lua is a modern re-write of the popular z.sh plugin, known for its speed and efficiency, written in Lua.

  • Installation (using a zsh plugin manager like oh-my-zsh):

    First, install zsh if you don’t have it:

    sudo apt install zsh  # Debian/Ubuntu
    sudo yum install zsh  # CentOS/RHEL

    Then, configure zsh to be your default shell:

    chsh -s $(which zsh)

    Install oh-my-zsh:

    sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

    Finally, install the z.lua plugin. You can do this by cloning the repository into your oh-my-zsh custom plugins directory:

    git clone https://github.com/skywind3000/z.lua ~/.oh-my-zsh/custom/plugins/z.lua

    Edit your ~/.zshrc file and add z.lua to the plugins line:

    plugins=(git z.lua)

    Source your .zshrc file:

    source ~/.zshrc
  • Usage:

    z <partial_directory_name>

    For example, after navigating to /home/user/projects/my_project/src multiple times, you can simply type z src to jump back to that directory.

2. Creating Custom Bash Aliases and Functions

You can create your own set of aliases and functions tailored to your specific needs. This gives you more control over how you navigate your file system.

  • Explanation: This approach involves creating aliases or functions that leverage find and cd to quickly locate and navigate to directories based on partial names. It’s a more manual approach but allows for very specific and customized behavior.

  • Code Example (in ~/.bashrc or ~/.zshrc):

    # Function to find a directory and cd into it
    cdf() {
      local dir=$(find . -type d -name "*$1*" -print -quit)
      if [ -n "$dir" ]; then
        cd "$dir"
      else
        echo "Directory not found."
      fi
    }
    
    alias c='cdf' # Short alias for the function
  • Usage:

    c <partial_directory_name>

    For example, if you have a directory named long_project_name, you can type c proj to navigate to it. The find command will search for the first directory matching the pattern and cd into it.

These alternative solutions offer different approaches to achieving quick file and directory access in Linux. Choosing the right method depends on your preferred shell, desired level of customization, and performance requirements. Using Fasd in Linux for Quick Access to Files & Directories is a great start, but exploring these alternatives can enhance your command-line workflow even further.

Leave a Reply

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