How to hide snap packages from lsblk on Linux
Snap packages are an excellent Linux technology that the community is embracing, as it offers a lot of features and benefits. However, sometimes Snap packages can cloud out your command-line output when the lsblk command runs in the terminal, and it can be incredibly annoying.
In this guide, we’ll go over how you can take steps to hide Snap packages from the lsblk command-output via a command. We’ll even go over how to make the filter permanent, so you never have to worry about it again! Here’s how to do it.
Why do Snap packages ruin LSBLK output?
Snap packages are file systems of sorts, with the program sandboxed (AKA isolated) from the rest of the system. Snaps being in a Sandbox has many positives, like the ability to have things come pre-configured, stay in a confined space away from the user, etc. However, they can also introduce some serious annoyances for Linux users.
Despite how excellent they can be, the problem is that because Snaps present to Linux users as filesystems (although isolated), they appear when the Linux user runs the lsblk filesystem command. It effectively clouds out real hard drives with tons and tons of Snap package information, and it can be incredibly annoying.
Run LSBLK without Snap packages in output
It is possible to filter out Snap packages from the LSBLK tool when running the lsblk command to view your computer’s file-systems. To do it, open up a terminal window and execute the lsblk command with the -e7 command-line switch. This switch will remove SquashFS filesystems (Snap packages) from the output.
lsblk -e7
If you see Snaps appear in the lsblk command output on the root account, this command will also work there. To filter out Snaps while using the root account (or logged in as sudo -s), take the sudo command and place it in front of lsblk -e7.
sudo lsblk -e7
If you dislike running the sudo command, it is also possible to log into root and then use the lsblk -e7 command. For example, I can quickly log into root with su and then execute the filter command.
su - lsblk -e7
The “e” (AKA exclude) command-line switch for the lsblk command is potent and has many excellent features and options. For more information on the “e” command-line switch and how you can use it to make a filter out various things, please check out the lsblk manual.
To access the lsblk manual, open up a terminal window and enter the man lsblk command. Or, save the manual to a readable text file with:
man lsblk > ~/Documents/lsblk-manual.txt
Making the Snap filter permanent
While it is handy to know that the “e7” command-line switch, when run with the lsblk command, will filter out Snap packages from the command output, it’s rather tedious having to remember to enter it each time. If you’d like to make this filter permanent, you can set up what is known as a Bash alias.
A Bash “alias” is essentially a “shortcut” command that tells the command-line that you want to execute a specific command when entered in the terminal. We can use this to make the lsblk -e7 command work when you enter the lsblk -e7 command.
Backing up your Bashrc
Before we go over how to set up the Bash alias that will automatically filter out Snaps, a backup of your Bashrc must be made. This backup will make it easy to revert the changes if need be.
To make a backup of your Bashrc, enter the following command in a terminal window.
cp ~/.bashrc ~/bashrc-backup
Once your Bashrc is backed up, keep it in your home directory. Or place it somewhere for safekeeping.
Setting up the alias
To set up the Bash alias for lsblk, start by opening your Bashrc file for editing purposes using the Nano text editor.
nano ~/.bashrc
Once inside of the Nano text editor, find a blank space. If your Bashrc file is filled with text, scroll through it, and find somewhere to type and enter the code below. Or, place your new alias directly below existing aliases if you have some already set up.
alias lsblk='lsblk -e7'
After writing out the new alias in the Nano text editor, press the Ctrl + O button on the keyboard. Once this button is pressed, press the Enter key to tell the Nano text editor to save your edits. Then, exit the text editor by pressing the Ctrl + X command.
Now that the alias is set up, close your terminal window and re-open it. Then, enter the lsblk command. It should automatically filter out Snaps from the lsblk output.
How to undo the Snap filter
Decided you don’t mind having Snaps show up as devices when running the lsblk command on your computer? Run these commands to undo it.
rm ~/.bashrc mv ~/bashrc-backup ~/.bashrc