What Are Dotfiles?
In my deep-dive write-up of how RBENV works, my first step was to find out where (i.e. in which file) the command's logic lived. It turned out that it lived in a shim file, which in turn lived inside the ~/.rbenv
directory. But I didn't explain what the ~/.rbenv
directory is, and more specifically, why it starts with a .
character.
This post will try to answer that question, i.e. why do some files and directories start with a dot?
Dotfiles and dot folders
In UNIX, there's a convention whereby the names of files and folders which are meant to be hidden from view by default are prefixed with a dot. Unsurprisingly, these are called "dotfiles" or "dot folders".When I say "hidden", I mean hidden from the ls
command. For example, the following is the contents of my laptop's home directory, according to the regular ls
command (passing the -l
flag to list one item per line):
$ ls -l
total 0
drwxr-xr-x 3 richiethomas staff 96 Dec 4 14:18 Applications
drwx------+ 35 richiethomas staff 1120 Jan 10 12:08 Desktop
drwx------+ 4 richiethomas staff 128 Dec 27 22:02 Documents
drwx------+ 50 richiethomas staff 1600 Jan 8 19:23 Downloads
drwx------@ 92 richiethomas staff 2944 Jan 7 11:56 Library
drwx------ 4 richiethomas staff 128 Dec 4 15:42 Movies
drwx------+ 4 richiethomas staff 128 Dec 30 21:11 Music
drwx------+ 4 richiethomas staff 128 Dec 4 14:02 Pictures
drwxr-xr-x+ 4 richiethomas staff 128 Dec 4 14:02 Public
$
But there are actually many more files in this folder. To view them all, we have to add the -a
flag to our previously-run command:
$ ls -la
total 240
drwxr-x---+ 36 richiethomas staff 1152 Jan 10 12:23 .
drwxr-xr-x 5 root admin 160 Dec 4 14:02 ..
-r-------- 1 richiethomas staff 7 Dec 4 14:02 .CFUserTextEncoding
-rw-r--r--@ 1 richiethomas staff 10244 Jan 5 22:24 .DS_Store
drwx------+ 33 richiethomas staff 1056 Jan 9 13:52 .Trash
-rw-r--r-- 1 richiethomas staff 348 Dec 4 15:04 .aliases.sh
drwxr-xr-x 3 richiethomas staff 96 Dec 23 11:48 .bundle
drwx------ 3 richiethomas staff 96 Dec 19 18:15 .cups
-rw-r--r-- 1 richiethomas staff 36 Dec 4 15:07 .editor.sh
-rw-r--r-- 1 richiethomas staff 373 Dec 4 15:07 .format_terminal_prompt.sh
drwxr-xr-x@ 4 richiethomas staff 128 Dec 6 08:47 .gem
-rw-r--r-- 1 richiethomas staff 203 Jan 9 14:16 .gitconfig
-rw------- 1 richiethomas staff 5082 Jan 8 17:36 .irb_history
-rw------- 1 richiethomas staff 32 Jan 9 14:38 .lesshst
drwxr-xr-x 4 richiethomas staff 128 Dec 13 14:24 .local
drwxr-xr-x 5 richiethomas staff 160 Dec 29 16:59 .npm
-rw------- 1 richiethomas staff 1082 Jan 7 21:17 .python_history
drwxr-xr-x 18 root staff 576 Jan 9 14:11 .rbenv
drwx------ 7 richiethomas staff 224 Jan 1 17:40 .ssh
drwxr-xr-x 3 richiethomas staff 96 Jan 8 19:21 .texlive2023
-rw------- 1 richiethomas staff 23757 Jan 10 12:23 .viminfo
-rw-r--r-- 1 richiethomas staff 440 Dec 4 15:09 .vimrc
drwxr-xr-x@ 5 richiethomas staff 160 Dec 4 14:54 .vscode
-rw-r--r-- 1 richiethomas staff 43 Dec 4 15:11 .zprofile
-rw------- 1 richiethomas staff 36003 Jan 9 14:20 .zsh_history
drwx------ 48 richiethomas staff 1536 Jan 9 14:20 .zsh_sessions
-rw-r--r-- 1 richiethomas staff 1018 Jan 7 12:18 .zshrc
drwxr-xr-x 3 richiethomas staff 96 Dec 4 14:18 Applications
drwx------+ 35 richiethomas staff 1120 Jan 10 12:08 Desktop
drwx------+ 4 richiethomas staff 128 Dec 27 22:02 Documents
drwx------+ 50 richiethomas staff 1600 Jan 8 19:23 Downloads
drwx------@ 92 richiethomas staff 2944 Jan 7 11:56 Library
drwx------ 4 richiethomas staff 128 Dec 4 15:42 Movies
drwx------+ 4 richiethomas staff 128 Dec 30 21:11 Music
drwx------+ 4 richiethomas staff 128 Dec 4 14:02 Pictures
drwxr-xr-x+ 4 richiethomas staff 128 Dec 4 14:02 Public
$
Ignoring the fact that my dotfiles are a lot less organized than they could be lol, we now see all the dotfiles and dot folders in my home directory.
What is an rc
dotfile?
When you install RBENV from source, one of the steps you have to take is "(c)onfigure your shell to load RBENV". You do this by updating a certain file in your home directory to include a certain command. For the Bash shell, this file is ~/.bashrc
. For the Zsh shell, this is the ~/.zshrc
file.
We see from the filenames that both of these files are dotfiles. But what is the significance of the rc
at the end of these filenams?
When you open a new terminal tab or window in Zsh (the now-default shell for Macs), one of the first things that happens is that Zsh runs a few setup scripts. One of these scripts is that ~/.zshrc
file. This file is where you'd put configuration options that you'd want to run on every new terminal tab or window. The same is true for Bash's ~/.bashrc
file.
There are other setup scripts which get loaded as well, such as ~/.zshenv
. But ~/.zshrc
is the one I interact with most. Zsh will run these scripts in a specific order, and there are conventions around what you should put in each file.
Other shells have similar rc
files (ex.- Bash has ~/.bashrc
). The rc
in .zshrc
stands for "run commands" or "run control", depending on who you ask.
Experiment- preventing files from being deleted
Hidden files are helpful in preventing accidental deletion of important data. Let's test that out now.
I create a temporary directory named foo/
, and create two files inside of it:
- a hidden file named
.bar
- a regular file named
baz
$ mkdir foo
$ touch foo/.bar foo/baz
$ ls foo
baz
$ ls -a foo
. .. .bar baz
$
Then I run rm ./foo/*
and hit the y
key when prompted for confirmation. Lastly, I re-run ls -a foo
:
$ rm ./foo/*
zsh: sure you want to delete the only file in /Users/richiethomas/Desktop/Workspace/temp/./foo [yn]? y
$ ls -a foo
. .. .bar
$
We see that the file baz
is now gone, but the hidden dotfile .bar
is still there. So yes, it appears that adding a .
prefix to a file can prevent it from being accidentally deleted.
But what if we really do want to delete our dotfiles? We simply add a dot before the *
symbol. To demonstrate this, I'll create a 2nd dotfile named .baz
, and then delete all the dotfiles in my foo/
folder at once:
$ rm foo/.*
$ ls -a foo
. ..
$
As you can see, the foo/
folder is now truly empty- no more dotfiles.
Wrapping Up
That's a very basic introduction to dotfiles. For a much deeper dive into how dotfiles are used in a professional environment, check out this guide from thoughtbot.
Photo Attribution
Title of Photo: Unknown
Description: n/a
Author: Unknown
Source: PickPik
License: CC0 1.0 Public Domain