💡
学习完这门课程后,我也有了自己的 dotfile repo:‣
知道的前提是知道自己不知道什么。
- Shell
- Tools
- Job Control
- Aliases
- Dotfiles
- Portability
- SSH Configuration
- Key based authentication
- Copying files over SSH
- ZSH
- Editors (Vim)
- Keyboard remapping
- Daemon & Automation
- FUSE
- Backups
- Filesystem Hierarchy Standard
Shell
- Connecting programs
< file
and> file
rewire the input and output streams of a program to a file.>>
to append to a file.|
chain programs by the output of one is the input of another.
sysfs
exposes a number of kernel parameters as files.- Conditionally execute commands,
&&
and||
- Separate commands within the same line
;
- Get the output of a command as a variable.
$( CMD )
, directly replace the output in the command.<( CMD )
, executeCMD
and place the output in a temporary file and substitute the<()
with that file’s name.
- Shell globbing
?
and*
to match one or any amount of characters.{}
expand a series of commands- e.g.
convert image.{png,jpg}
- e.g.
Tools
xargs
man
replacement:TLDR
- Finding files
fd
- Finding code
grep
ack
ag
rg
- Shell finding commands
fzf
- Directory Navigation
fasd
autojump
frecency
- View directory structure
tree
broot
nnn
ranger
- Stream editor
sed
, a useful programming languageawk
(人生苦短,我用Python rsync
Job Control
kill
- Stop a process
Ctrl-Z
- Continue the paused job in the foreground or in the background using
fg
orbg
jobs
tmux
Aliases
Note that there is no space around the equal sign =
, because alias
is a shell command that takes a single argument.
Dotfiles
Use Git to manage secret content via creating bash functions to encrypt / decrypt secret content into / from Git.
Portability
if [[ "$(uname)" == "Linux" ]]; then {do_something}; fi
# Check before using shell-specific features
if [[ "$SHELL" == "zsh" ]]; then {do_something}; fi
# You can also make it machine-specific
if [[ "$(hostname)" == "myServer" ]]; then {do_something}; fi
# Test if ~/.aliases exists and source it
if [ -f ~/.aliases ]; then
source ~/.aliases
fi
gitconfig supports including configurations from local files.
[include]
path = ~/.gitconfig_local
SSH Configuration
- generate a key pair →
ssh-keygen
- manage passphrase →
ssh-agent
gpg-agent
~/.ssh/config
Host vm
User foobar
HostName 172.16.174.141
Port 2222
IdentityFile ~/.ssh/id_ed25519
LocalForward 9999 localhost:8888
# Configs can also take wildcards
Host *.mit.edu
User foobaz
Key based authentication
cat .ssh/id_ed25519.pub | ssh foobar@remote 'cat >> ~/.ssh/authorized_keys'
# Or
ssh-copy-id -i .ssh/id_ed25519 foobar@remote
Copying files over SSH
rsync
ZSH
为什么不使用fish?
因为我对shell不够了解,fish不支持Posix标准,我觉得有很多我不知道的坑。
Editors (Vim)
Know keyboard shortcuts about your most recently used editor!d
Keyboard remapping
Daemon & Automation
systemd
# /etc/systemd/system/myapp.service
[Unit]
Description=My Custom App
After=network.target
[Service]
User=foo
Group=foo
WorkingDirectory=/home/foo/projects/mydaemon
ExecStart=/usr/bin/local/python3.7 app.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
cron
FUSE
filesystem in user space,对文件系统的中间抽象,用来加上远程、分布式、加密等功能。
- sshfs - Open locally remote files/folder through an SSH connection.
- rclone - Mount cloud storage services like Dropbox, GDrive, Amazon S3 or Google Cloud Storage and open data locally.
- gocryptfs - Encrypted overlay system. Files are stored encrypted but once the FS is mounted they appear as plaintext in the mountpoint.
- kbfs - Distributed filesystem with end-to-end encryption. You can have private, shared and public folders.
- borgbackup - Mount your deduplicated, compressed and encrypted backups for ease of browsing.
Backups
Filesystem Hierarchy Standard
/bin
- Essential command binaries/sbin
- Essential system binaries, usually to be run by root/dev
- Device files, special files that often are interfaces to hardware devices/etc
- Host-specific system-wide configuration files/home
- Home directories for users in the system/lib
- Common libraries for system programs/opt
- Optional application software/sys
- Contains information and configuration for the system/tmp
- Temporary files (also/var/tmp
). Usually deleted between reboots./usr/
- Read only user data/usr/bin
- Non-essential command binaries/usr/sbin
- Non-essential system binaries, usually to be run by root/usr/local/bin
- Binaries for user compiled programs
/var
- Variable files like logs or caches