Another post about symbolic links, XDG utils and easy filesystem management

:question: Haven't you written about this like, multiple times already??

Yes, I have. Except this time, this post is beautified and puts everything I've learned together. Also that information is buried and I like keeping this stuff near the surface for new users because it's actually useful stuff to know.

So right, let's get it on.

:evergreen_tree: A foreword about filesystem management

While this post will explain advanced methods of filesystem management, it does not mean the directories shown are to be used as intended. Depending on how you use your machine, you may not even use the directories shown, and that's alright because it's your personal machine (usually) and no one has the right to criticize your filesystem management so long it helps you to function.

After implementing what follows, you may be able to access your personal effects more quickly, but your personal effects could also be put in a single directory so long that's what works for you. If software expects a user- or system-configured path, then be careful, adjust preferences as necessary and you should not encounter any issues following this advice.

Everything below is intended to affect what the system determines are native directories for expedited access by an end user. It does not mean that's how an end-user will see it.

:telescope: What this post's scope is

This will not teach you about mounting your own disks. This assumes you already configured what disks you want mounted by opening gnome-disks and managing your partitions there, or you have configured it by hand editing /etc/fstab.

This will also not explain about different partition formats. There's enough information about that on the Internet. The most common formats are NTFS, Ext3, Ext4, FAT32 and ExFAT, the latter two of which can't have symlinks going from them. More on that later.

The whole point about this post is to present, in somewhat great detail, the idea of simplifying your local filesystem using tools in your open-source OS to create the means of manipulating your existing directories on media available to the system as if they were native to it. This will involve a bit of tinkering, but when finished you'll find it easier to manage your files from either a shared folder they are moved to, or at an existing folder where your files presently exist.

:man_shrugging: So why bother with this at all? :woman_shrugging:

So said everybody. The reason you want to bother with this is if you are dual-booting, or want to have your files accessible outside of the system; away from an Ext4 partition where they are otherwise rendered inaccessible without special software to mount said partitions on systems not using the Linux kernel and / or are otherwise incapable by design to mount Ext4. (Crazy thought right? Linux systems which can't mount Ext4 by default? Outrageous! But it's in your pocket if you use Android with your OEM's firmware.)

:thinking: Symbolic links or XDG directories?

:link: Symbolic links

Symbolic links refer to the absolute path of an existing directory, and will not work if the directory does not exist. One can also use hard links, but for a variety of reasons they are not recommended because of directory looping which can lead to some very fun times browsing your directory tree. (Meant to be read sarcastically.)

The advantage of these is that they can be on anything and still work so long they exist. They do not rely upon a configuration file which defines directories of specific purpose, so for people who habitually distro-hop (which you shouldn't, but people do that) they get to preserve access of their files between installations so long the directories those links refer to exist.

There can also be multiple copies for people who use multiple open-source systems on the same disk, for those crazy people who want to do things that way, and be copied into their respective home partitions so they are usable everywhere without contributing much except for the few inodes they'll take up. (Which is a reason why you might want hard links, but again that isn't recommended.)

:file_folder: XDG directories

xdg-utils gives a fair bit to the end-user for filesystem management, namely for the scope of this presentation the means to define within the system what directories refer to which items. By default, every item in your home directory with an icon in it, at least for the big five plus Public, Desktop and Templates is managed by XDG, all of which is stored in ~/.config/user-dirs.dirs file.

For Windows users, it's the effective equivalent of user library directories defined in the registry. So much, that supporting file managers will display the directories in the side pane once assigned. Except where users could define library paths using Explorer in Windows, users in open-source systems with XDG have to modify user-dirs.dirs or use the other method explained later in this guide.

:star: Which one is best?

Preferably, symbolic links. There are some applications which do not respect XDG directory locations, or expect a hard-coded path and this is where symlinks reign supreme. If on the other hand you are using a partition which does not support symbolic links, but still want to have quick access to those directories and use them as your "Big five" then XDG directories are where it's at.

:brain: Applying the knowledge

:mag_right: Why you would want to do any of this

In a dual-boot scenario or in an instance where you are using an open-source system as a means of editing files which exist on an externally-connected or hot-swappable device, using one of the two methods explained below can help to make it so access of your data is maintained when you are not using the system. Not only would this centralize data often worked upon using multiple devices, you can also always have the data on-hand, when you need it the most.

:terminal: How to begin

When tinkering with common directories other programs rely upon in your home directory, it is usually a good idea to perform this stuff in a TTY instance of a user shell. By default, this is what you have when you drop to shell as it is often called, to sign in as a user (by username) so one can perform disk operations without summoning every other GUI-based application for any user which may become provoked into declaring errors during the course of your work when used normally.

To do this:eye:, hold Ctrl and Alt while pressing any one of the first six function keys (F1 through F6) on your keyboard:dagger:.

:eye: The moment you do this, you will not be able to see this guide. Finish reading before doing anything to prevent system inaccessibility the first time you do this.
:dagger: You may have to hold an additional key, depending on your keyboard and / or BIOS settings. Usually this would be Fn.

You will be given a prompt for $HOST login, sign in with your account's username (whatever $USER may be) and you will have a shell prompt:pencil2: .

:pencil2: By default, this is Bourne Again shell (/bin/bash) as defined in /etc/passwd which can be configured for the current user in future via chsh as user preference.

To return into your X graphical session, hold Ctrl and Alt while pressing F7:dagger:. This will return you into lightdm if you are logged out, or your current session if logged in. If I didn't make it quite clear earlier, you should perform operations on directories other programs may produce errors if they do not exist while not logged in at your X session.

:link: Symbolic links

For this, we are using the scenario of wanting to manage directories on an NTFS partition which contains system files for Microsoft Windows.

:fast_forward: For brevity, the following is assumed:

  • The username on both instances of Windows and the other system uses the same name (thus, $USER will mean the same username on both systems). Not always the case.
  • The disk is already mounted as per definitions in /etc/fstab and is labelled Windows, mounted as /mnt/Windows. This may differ, adjust as necessary.
  • Files exist on the target directories already. (often not the case on new installations.)
  • You don't care if something is overwritten. If this isn't you, do mv -i instead.

Preparation
Assumes you already have files in these directories. If not, skip to committal.
:heavy_dollar_sign: In a TTY instance using a shell of preference:

mv -f $HOME/Documents/* /mnt/Windows/Users/$USER/Documents
mv -f $HOME/Downloads/* /mnt/Windows/Users/$USER/Downloads
mv -f $HOME/Music/* /mnt/Windows/Users/$USER/Music
mv -f $HOME/Pictures/* /mnt/Windows/Users/$USER/Pictures
mv -f $HOME/Videos/* /mnt/Windows/Users/$USER/Videos

Committal
:heavy_dollar_sign: In a TTY instance using a shell of preference:

rmdir $HOME/Documents $HOME/Downloads $HOME/Music $HOME/Pictures $HOME/Videos
ln -s /mnt/Windows/Users/$USER/Documents $HOME
ln -s /mnt/Windows/Users/$USER/Downloads $HOME
ln -s /mnt/Windows/Users/$USER/Music $HOME
ln -s /mnt/Windows/Users/$USER/Pictures $HOME
ln -s /mnt/Windows/Users/$USER/Videos $HOME

:file_folder: XDG directories

For this, we are using the scenario of a Linux desktop at home, but wanting to manage files on an Android device which accepts externally-connected storage media via USB, where you don't prefer to use symbolic links. (However, for some directories links may still apply.)

:fast_forward: For brevity, the following is assumed:

  • The disk is already mounted as per definitions in /etc/fstab and is labelled AndUSB, mounted as /mnt/AndUSB. This may differ, adjust as necessary.
  • Files exist on the target directories already. (often not the case on new installations.)
  • You don't care if something is overwritten. If this isn't you, do mv -i instead.

Preparation
Assumes you already have files in these directories. If not, skip to committal.
:heavy_dollar_sign: In a TTY instance using a shell of preference:

mv -f $HOME/Documents/* /mnt/AndUSB/Documents
mv -f $HOME/Downloads/* /mnt/AndUSB/Downloads
mv -f $HOME/Music/* /mnt/AndUSB/Music
mv -f $HOME/Pictures/* /mnt/AndUSB/Pictures
mv -f $HOME/Videos/* /mnt/AndUSB/Videos

Committal
:heavy_dollar_sign: In a TTY instance using a shell of preference:

rmdir $HOME/Documents $HOME/Downloads $HOME/Music $HOME/Pictures $HOME/Videos
xdg-user-dirs-update --set DOCUMENTS /mnt/AndUSB/Documents
xdg-user-dirs-update --set DOWNLOAD /mnt/AndUSB/Downloads
xdg-user-dirs-update --set MUSIC /mnt/AndUSB/Music
xdg-user-dirs-update --set PICTURES /mnt/AndUSB/Pictures
xdg-user-dirs-update --set VIDEOS /mnt/AndUSB/Videos

That is not a typo, it is DOWNLOAD, not DOWNLOADS as some might expect.

The above also implies you will always have your external device connected, at all times. For convenience sake, you may want to format the media so there are Ext4 partitions which contain the system and bootloader. You only really need a minimum of 16GB to host the system comfortably but of course the more you allocate, the more software and files can exist on the system partition.

If you're in a bind for space and really need something to exist on the system itself, you may still find yourself using symbolic links which is entirely possible going to an ExFAT directory, but not coming from.

:laughing: Just For fun; Flexing one's ego

Preparation
:heavy_dollar_sign: In a TTY instance using a shell of preference:

mv $HOME/Documents/ $HOME/My\ Documents
mv $HOME/Downloads/ $HOME/My\ Downloads
mv $HOME/Music/ $HOME/My\ Music
mv $HOME/Pictures/ $HOME/My\ Pictures
mv $HOME/Videos/ $HOME/My\ Videos

Committal
:heavy_dollar_sign: In a TTY instance using a shell of preference:

xdg-user-dirs-update --set DOCUMENTS $HOME/My\ Documents
xdg-user-dirs-update --set DOWNLOAD $HOME/My\ Downloads
xdg-user-dirs-update --set MUSIC $HOME/My\ Music
xdg-user-dirs-update --set PICTURES $HOME/My\ /Pictures
xdg-user-dirs-update --set VIDEOS $HOME/My\ Videos

And just like that, it's all yours. Because you said so.

2 Likes