用 rcm 管理隐藏文件
配置文件 隐藏文件
![](/data/attachment/album/201901/22/104627m4y4jfa8z5ny6kzk.jpg)


许多 GNU/Linux 程序的一个特点是有个易于编辑的配置文件。几乎所有常见的自由软件都将配置设置保存在纯文本文件中，通常采用结构化格式，如 JSON、YAML 或[“类似 ini”](https://en.wikipedia.org/wiki/INI_file) 的文件中。这些配置文件经常隐藏在用户的主目录中。但是，基本的 `ls` 不会显示它们。UNIX 标准要求以点开头的任何文件或目录名称都被视为“隐藏”，除非用户特意要求，否则不会列在目录列表中。例如，要使用 `ls` 列出所有文件，要传递 `-a` 选项。


随着时间的推移，这些配置文件会有很多定制配置，管理它们变得越来越具有挑战性。不仅如此，在多台计算机之间保持同步是大型组织所面临的共同挑战。最后，许多用户也对其独特的配置感到自豪，并希望以简单的方式与朋友分享。这就是用到 rcm 介入的地方。


rcm 是一个 “rc” 文件管理套件（“rc” 是命名配置文件的另一种约定，它已被某些 GNU/Linux 程序采用，如 `screen` 或 `bash`）。 rcm 提供了一套命令来管理和列出它跟踪的文件。使用 `dnf` 安装 rcm。


### 开始使用


默认情况下，rcm 使用 `~/.dotfiles` 来存储它管理的所有隐藏文件。一个被管理的隐藏文件实际保存在 `~/.dotfiles` 目录中，而它的符号链接会放在文件原本的位置。例如，如果 `~/.bashrc` 由 rcm 所管理，那么详细列表将如下所示。



```
[link@localhost ~]$ ls -l ~/.bashrc
lrwxrwxrwx. 1 link link 27 Dec 16 05:19 .bashrc -> /home/link/.dotfiles/bashrc
[link@localhost ~]$
```

rcm 包含 4 个命令：


* `mkrc` – 将文件转换为由 rcm 管理的隐藏文件
* `lsrc` – 列出由 rcm 管理的文件
* `rcup` – 同步由 rcm 管理的隐藏文件
* `rcdn` – 删除 rcm 管理的所有符号链接


### 在两台计算机上共享 bashrc


如今用户在多台计算机上拥有 shell 帐户并不罕见。在这些计算机之间同步隐藏文件可能是一个挑战。这里将提供一种可能的解决方案，仅使用 rcm 和 git。


首先使用 `mkrc` 将文件转换成由 rcm 管理的文件。



```
[link@localhost ~]$ mkrc -v ~/.bashrc
Moving...
'/home/link/.bashrc' -> '/home/link/.dotfiles/bashrc'
Linking...
'/home/link/.dotfiles/bashrc' -> '/home/link/.bashrc'
[link@localhost ~]$
```

接下来使用 `lsrc` 验证列表是否正确。



```
[link@localhost ~]$ lsrc
/home/link/.bashrc:/home/link/.dotfiles/bashrc
[link@localhost ~]$
```

现在在 `~/.dotfiles` 中创建一个 git 仓库，并使用你选择的 git 仓库托管设置一个远程仓库。提交 `bashrc` 文件并推送一个新分支。



```
[link@localhost ~]$ cd ~/.dotfiles
[link@localhost .dotfiles]$ git init
Initialized empty Git repository in /home/link/.dotfiles/.git/
[link@localhost .dotfiles]$ git remote add origin git@github.com:linkdupont/dotfiles.git
[link@localhost .dotfiles]$ git add bashrc
[link@localhost .dotfiles]$ git commit -m "initial commit"
[master (root-commit) b54406b] initial commit
1 file changed, 15 insertions(+)
create mode 100644 bashrc
[link@localhost .dotfiles]$ git push -u origin master
...
[link@localhost .dotfiles]$
```

在第二台机器上，克隆这个仓库到 `~/.dotfiles` 中。



```
[link@remotehost ~]$ git clone git@github.com:linkdupont/dotfiles.git ~/.dotfiles
...
[link@remotehost ~]$
```

现在使用 `rcup` 更新受 rcm 管理的符号链接。



```
[link@remotehost ~]$ rcup -v
replacing identical but unlinked /home/link/.bashrc
removed '/home/link/.bashrc'
'/home/link/.dotfiles/bashrc' -> '/home/link/.bashrc'
[link@remotehost ~]$
```

覆盖现有的 `~/.bashrc`（如果存在）并重启 shell。


就是这些了！指定主机选项 （`-o`） 是对上面这种情况的有用补充。如往常一样，请阅读手册页。它们包含了很多示例命令。




---


via: <https://fedoramagazine.org/managing-dotfiles-rcm/>


作者：[Link Dupont](https://fedoramagazine.org/author/linkdupont/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
A hallmark feature of many GNU/Linux programs is the easy-to-edit configuration file. Nearly all common free software programs store configuration settings inside a plain text file, often in a structured format like JSON, YAML or [“INI-like”](https://en.wikipedia.org/wiki/INI_file). These configuration files are frequently found hidden inside a user’s home directory. However, a basic *ls* won’t reveal them. UNIX standards require that any file or directory name that begins with a period (or “dot”) is considered “hidden” and will not be listed in directory listings unless requested by the user. For example, to list all files using the *ls* program, pass the *-a* command-line option.

Over time, these configuration files become highly customized, and managing them becomes increasingly more challenging as time goes on. Not only that, but keeping them synchronized between multiple computers is a common challenge in large organizations. Finally, many users find a sense of pride in their unique configuration settings and want an easy way to share them with friends. That’s where **rcm** steps in.

**rcm** is a “rc” file management suite (“rc” is another convention for naming configuration files that has been adopted by some GNU/Linux programs like screen or bash). **rcm** provides a suite of commands to manage and list files it tracks. Install **rcm** using **dnf**.

## Getting started

By default, **rcm** uses *~/.dotfiles* for storing all the dotfiles it manages. A managed dotfile is actually stored inside *~/.dotfiles*, and a symlink is placed in the expected file’s location. For example, if *~/.bashrc* is tracked by **rcm**, a long listing would look like this.

[link@localhost ~]$ ls -l ~/.bashrc lrwxrwxrwx. 1 link link 27 Dec 16 05:19 .bashrc -> /home/link/.dotfiles/bashrc [link@localhost ~]$

**rcm** consists of 4 commands:

*mkrc*– convert a file into a dotfile managed by rcm*lsrc*– list files managed by rcm*rcup*– synchronize dotfiles managed by rcm*rcdn*– remove all the symlinks managed by rcm

## Share bashrc across two computers

It is not uncommon today for a user to have shell accounts on more than one computer. Keeping dotfiles synchronized between those computers can be a challenge. This scenario will present one possible solution, using only **rcm** and **git**.

First, convert (or “bless”) a file into a dotfile managed by **rcm** with *mkrc*.

[link@localhost ~]$ mkrc -v ~/.bashrc Moving... '/home/link/.bashrc' -> '/home/link/.dotfiles/bashrc' Linking... '/home/link/.dotfiles/bashrc' -> '/home/link/.bashrc' [link@localhost ~]$

Next, verify the listings are correct with *lsrc*.

[link@localhost ~]$ lsrc /home/link/.bashrc:/home/link/.dotfiles/bashrc [link@localhost ~]$

Now create a git repository inside *~/.dotfiles* and set up an accessible remote repository using your choice of hosted git repositories. Commit the *bashrc* file and push a new branch.

[link@localhost ~]$ cd ~/.dotfiles [link@localhost .dotfiles]$ git init Initialized empty Git repository in /home/link/.dotfiles/.git/ [link@localhost .dotfiles]$ git remote add origin git@github.com:linkdupont/dotfiles.git [link@localhost .dotfiles]$ git add bashrc [link@localhost .dotfiles]$ git commit -m "initial commit" [master (root-commit) b54406b] initial commit 1 file changed, 15 insertions(+) create mode 100644 bashrc [link@localhost .dotfiles]$ git push -u origin master ... [link@localhost .dotfiles]$

On the second machine, clone this repository into *~/.dotfiles*.

[link@remotehost ~]$ git clone git@github.com:linkdupont/dotfiles.git ~/.dotfiles ... [link@remotehost ~]$

Now update the symlinks managed by **rcm** with *rcup*.

[link@remotehost ~]$ rcup -v replacing identical but unlinked /home/link/.bashrc removed '/home/link/.bashrc' '/home/link/.dotfiles/bashrc' -> '/home/link/.bashrc' [link@remotehost ~]$

Overwrite the existing *~/.bashrc *(if it exists) and restart the shell.

That’s it! The host-specific option (*-o*) is a useful addition to the scenario above. And as always, be sure to read the manpages; they contain a wealth of example commands.

## Leslie Satenstein

Very nice write up.

I have a desktop with multiple versions of Fedora (Gnome, KDE) and releases. I use a commen disk to store the configuration files. I go one step further and do likewise for /Documents, /Videos /Templates, /Music and other directories/ files I wish to share.

Is RCM suitable for sharing other than for files?

RCM is now on my Todo list. Thank you.

## Link Dupont

At its core,

rcmis a symlink manager. It’s not a file synchronization tool. It does very well at managing symlinks, but it won’t synchronize those files to other computers. You need to use a separate synchronization tool (rsync, syncthing, etc.) to keep files across computers in sync.## gt

my mc asm me about symlinks

every time when i edit it. how turn off this functions?

## Sascha Peilicke

The only drawback of this solution is that it isn’t self-hosting and depends on a distro package that might not be available everywhere. Instead I recommend using DFM (https://github.com/justone/dfm). It’s a single-file Perl script that works exactly the same as rcm but can be versioned right inside your dotfiles.git.

Example project: https://github.com/saschpe/dotfiles/blob/master/bin/dfm

I just clone that on any new machine and invoke dfm:

$ git clone github.com/saschpe/dotfiles .dotfiles

$ .dotfiles/bin/dfm install

It’s trivial, self-hosting and works on macOS as well.

## Link Dupont

True. I have only used it on Fedora (in full disclosure, I am the Fedora package maintainer), but the upstream project repository has a number of distributions listed. It’s trivial to install and version using GNU stow too; it uses the GNU autotools.

## aairey

You need to know some Perl though to dissect the script in case of issues.

Personally I just use a bash script for this.

## Adolfina

@Author:Thanks for the solution.

BTW It also works nicely for directories …

$ mkrc ~/.config/Clementine

[me@tower .dotfiles]$ lsrc

/home/me/.bashrc:/home/me/.dotfiles/bashrc

/home/me/.config/Clementine/Clementine.conf:/home/me/.dotfiles/config/Clementine/Clementine.conf

/home/me/.config/Clementine/clementine.db:/home/me/.dotfiles/config/Clementine/clementine.db

/home/me/.config/Clementine/clementine.db.bak:/home/me/.dotfiles/config/Clementine/clementine.db.bak

/home/me/.config/Clementine/jamendo.db:/home/me/.dotfiles/config/Clementine/jamendo.db

/home/me/.README.md:/home/me/.dotfiles/README.md

## aairey

Storing an sqlite in a git repository is not exactly “nice”,

## maweki

Is there any major difference to stow?

## Steve

rcm is not nearly as powerful as stow.