How to Use Fasd in Linux for Quick Access to Files & Directories

How to Use Fasd in Linux for Quick Access to Files & Directories

Linux users often rely on the command line, but tedious and time-consuming tasks can make it frustrating. Navigating and moving between directories using cd and ls commands, plus typing long path files to access, copy or list files, can be annoying for even advanced users.

To solve the problem of directory navigation and accessing files from other locations in the system using command lines, the fasd tool has been created exactly for this purpose. fasd is a command line utility written in Shell that serves as an advanced version of the autojump and z command utilities; we can call also: Autojump plus z with support for files.

This Linux tool allows the user to quick access a previously visited directories and files from any location in the system.

Fasd can be described as a faster solution to navigate and access the user’s frequent and recent directories and files. This helpful utility saves the frequently visited directories and files (i.e. History) from the command line into a database in order to render filesystem navigation possible and quite easy just by typing small patterns.

In this article you will learn how to use fasd to easily access files and directories without specifying their full path or executing numerous cd commands.

The Fasd Command Syntax

Directories must be visited first or added manually, before they can be accessed to.

fasd [options] [query ...]

fasd commands used in this article

  • -e The utility that will be executed with the resulting path
  • -a match files and directories
  • -d match directories
  • -f match files
  • -A add a path to the database
  • -D delete a path to the database

Getting Started with Fasd

In this section, we will get to know more about fasd and some of its out of the box aliases

How to Install Fasd

To install fasd it is recommended to update the apt database

sudo apt update

The next step is to install fasd, we can do that by using the same apt tool:

sudo apt install fasd

Check if the fasd tool is installed correctly by running the following command:

fasd -help
Output
fasd [options] [query ...] [f|a|s|d|z] [opions] [query ...]

The other way to install fasd is to clone the git repository just as demonstrated below:

git clone https://github.com/clvv/fasd.git

cd to the fasd directory and make the python script executable, then run the script as demonstrated:

cd fasd
sudo make install

Getting Fasd Working in The Shell

To activate the fasd utility we should add the following line to the .bashrc or .zshrc file : {eval "$(fasd --init auto)" }. Simply open it from the home directory using the following:

open .bashrc

Or without complicating things just copy the below mentioned command to your terminal and execute it.

echo 'eval "$(fasd --init auto)"' >> ~/.bashrc
Note: the >> symbol will append the output of the echo command to the .bashrc.

The final step is to source the .bashrc file.

source .bashrc

Now we can move to the interesting stuff about fasd.

Getting Acquainted with Fasd Aliases

As mentioned before, we will get acquainted with some fasd aliases. This tool will add its own aliases as demonstrated below. Additionally, Fasd offers the user to add their own custom aliases in order to expand the role of fasd. For example: interacting with cat, nano or media players… We will get to know more about this, later in this article.

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

We’ve stated before that fasd keeps a history of the frequently visited directories or opened files from the command line. It lists these paths based on their “frecency” (a term invented by Mozilla which means: “frequency” + “recency”)

To understand this concept, we could use the fasd command to show database entries and their key weights/scores. Check the results mentioned below:

~/fasd_lab $ fasd
Output
6          /home/edxd/.bashrc
6          /home/edxd/fasd_lab

Naturally, the database has two paths since we visited a directory called fasd_lab and opened our shell rc file (bashrc).

The name of the fasd tool comes from its right of the box aliases that we will use. Check the following:

f (files), a (files/directories), s (show/search/select), d (directories).

Let’s break it down for you. First, we are going to run the f alias (for files), which will display only the files that have been opened from the terminal.

~/fasd_lab $ f
Output
6 /home/edxd/.bashrc

The a alias will output both directories and files.

~/fasd_lab $ a
Output
6          /home/edxd/.bashrc
12         /home/edxd/fasd_lab

For the s alias, it will list both directories and files previously visited then it will wait for you to choose one of the paths to display it to the terminal.

~/snap $ s
Output
2	12         /home/edxd/.bashrc
1	24    /home/edxd/fasd_lab
> 1

/home/edxd/fasd_lab

The d alias will list only the directories.

~/fasd_lab $ d
Output
24         /home/edxd/fasd_lab

Quick Accessing Files and Directories with Fasd

To understand how to use fasd and its ranking system, it’s better to create multiple directories, subdirectories, and some files.

After that, you should open each of those directories and files from the command line.

Finally, navigate back to your root testing directory when you’ve reached the last subdirectory.

Follow the given example for better understanding:

~/fasd_lab$ mkdir -p zombies/aliens
~/fasd_lab$ mkdir -p zuck/aliens
~/fasd_lab$ cd zombies/
~/fasd_lab/zombies$ cd aliens/
~/fasd_lab/zombies/aliens$ cd ../..
~/fasd_lab$ cd zuck/
~/fasd_lab/zuck$ cd aliens/
~/fasd_lab/zuck/aliens$ touch ship.txt

Now check the fasd added paths and their ranks by executing fasd or a;

~/fasd_lab $ a
Output
6          /home/edxd/fasd_lab/zombies
6          /home/edxd/fasd_lab/zuck
6          /home/edxd/fasd_lab/zuck/aliens/ship.txt
12         /home/edxd/fasd_lab/zombies/aliens
15         /home
15         /home/edxd/.bashrc
15         /home/edxd/fasd_lab/zuck/aliens
24.5715    /home/edxd/snap
35.572     /home/edxd/fasd_lab

You’ll be able to observe all accessed directories and files plus their score number.

How to Navigate to Directories

For jumping to directories, we will use the z alias to achieve that. Let’s navigate to “zombies” and “zuck” directories by typing only the first two words.

~/fasd_lab$ z zo
~/fasd_lab/zombies $ z zu
~/fasd_lab/zuck $
Keep in mind: the z alias is equivalent to fasd_cd -d

Quick Access to Subdirectories

How about navigating to subdirectories by specifying their path patterns? So in case, we want to navigate to the “aliens” child directories from our home directory without typing the full path. We will simply run the mentioned below commands that are based on the directories that we’ve created earlier.

~/fasd_lab $ z zu al
~/fasd_lab/zuck/aliens $ z zo al
~/fasd_lab/zombies/aliens $

Notice that fasd understood the given patterns above flawlessly. “zu al” = “zuck/aliens” & “zo al” = “zombies/aliens”

How about if we specified only “aliens” to fasd? In this case fasd will change the current directory to either this (/zuck/aliens) or this (zombies/aliens) depending on the frequent/recent one. Check the following:

~/fasd_lab$ z aliens
~/fasd_lab/zuck/aliens $

In my case fasd jumped to the (/zuck/aliens). Because this directory has a bigger score/weight than (zombies/aliens); using technical terms: “frecent directory“.

To be sure, execute the d alias to list your fasd path directories and their score.

~/fasd_lab/zuck/aliens $ d
Output
5          /home
6.48966    /home/edxd/fasd_lab/zombies/aliens
8.1905     /home/edxd/snap
17.4       /home/edxd/fasd_lab/zuck
34.5294    /home/edxd/fasd_lab/zombies
34.5294    /home/edxd/fasd_lab/zuck/aliens
49.2872    /home/edxd/fasd_lab
Please remember that: when you have multiple locations/files with the same name, fasd will access the directory or file with the greater score. You should always check the fasd stats to get a good idea about your fasd ranking history.

How to Open Files

We can use fasd to navigate to files in a similar way that we used in the previous section of this article. Let’s create two text files with similar names in the “aliens” directory:

~/fasd_lab/zombies/aliens $ touch space_ship.txt
~/fasd_lab/zombies/aliens $ touch space_ship.txt
~/fasd_lab $ tree
.
├── zombies
│   └── aliens
│       └── space_ship.txt
└── zuck
    └── aliens
        └── space_ship.txt

Execute the f alias to list all file paths recognized by fasd.

~/fasd_lab $ f
Output
5          /home/edxd/.bashrc
6          /home/edxd/fasd_lab/zombies/aliens/space_ship.txt
6          /home/edxd/fasd_lab/zuck/aliens/space_ship.txt

To open the text file located in (zombies/aliens) we will use the similar command that we used earlier with some small tweaks. Instead of the z alias we will use f, then we should specify the program that we’ll be using to open the file by using the -e option. Check the following command for a better understanding:

~/fasd_lab $ f -e nano zo al sp

Note that the “zo al sp” pattern is interpreted by fasd as “zombies/aliens/space_ship.txt”. So basically, the above command is similar as if we navigated to location of the text file and ran nano space_ship.txt:

Let’s try to output the content of the second text file to terminal using the cat command instead:

~/fasd_lab $ f -e cat zu al sp
Output
This is a space battleship

We can use any program to open our files with fasd. In the following command we will open a video file with mplayer.

~/fasd_lab $ f -e mplayer BreakingBad Ep01

Open the File Manager of A Directory

As you might have guessed, we can use the same concept to open the file manager of a directory. We’ll use the z alias and -e xdg-open to achieve this. Try the following to open the “zombies” directory.

~/fasd_lab $ z -e xdg-open zom

Adding Custom Aliases to Open Files and Directories

Since we are using fasd to save time and effort, so it’s probably good idea to add aliases to your shell rc file in order to specify the program you frequently use. For this example, let’s create an alias to open text files with the cat utility.

~/fasd_lab $ z -e xdg-open zom

Managing Fasd Paths

As you might have noticed, fasd will automatically add paths to its database when using the mkdir or touch command. It will also remove an existing path automatically if you delete it with the rm command. Having mentioned that, we could be in a situation where we have to add or remove paths for fasd manually.

To add a new path, we will use the -A fasd option just as so:

~/fasd_lab $ z -e xdg-open zom

We can also remove existing paths from fasd using the -D switch:

~/fasd_lab $ fasd -A /home/edxd/fasd_lab/cypress

Bonus: Advanced Fasd Commands

In this section, we will list some of the cool tricks to efficiently use fasd.

Adding content to a text file using the echo command
~/fasd_lab $ echo "Hello space world" > f `zo al sp`
~/fasd_lab $ cat `zo al sp`
Hello space world

Note: we enclosed the fasd command in double backticks ``:

Copying or moving files to a directory
~/ffmpeg $ cp video.mp4 `d zombies`
~/fasd_lab/zombies $ ls
aliens  video.mp4
Note: we used the d fasd alias to get the path of the “zombies” directory.

Now let’s move a file to a location without being on the file’s current location.

~/fasd_lab $ mv `f ff ou B` `d zu al`
~/fasd_lab/zuck/aliens $ ls
Bbear_EP_01.ac3  space_ship.txt

We use the f fasd alias to specify this file (ffmpeg_bash/output_files/Bbear_EP_01.ac3) in order to move it to our “aliens” directories.

Using the ls command to quickly list the content of a directory.

~/ffmpeg_bash/output_files $ ls `d zu al`
Bbear_EP_01.ac3  space_ship.txt

Conclusion

In this article we’ve seen how to setup fasd, and how to use its aliases, in addition to how to expand the role of fast and pair it with other Linux commands. But most importantly, we showed you how to navigate easily between your favorite directories and also access your frequently uses files without having to type the full path or execute multiple commands.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
How to Use the Kill Command in Linux
Read More

The Kill Command in Linux

Each Linux system runs several processes. Not every process behaves in the manner we want them to. Let’s…