Linux tips

9.1

Ignore error for a particular command

set -e

other_command

particular_command || true

other_command

Office

LibreOffice

sudo apt install libreoffice

Modify shell prompt

一种简单的配置

export PS1='\u@[\w]\n\$ '

Show linux version

Show OS info

cat /etc/os-release

Show kernel version

uname -r

ctrl-tab, ctrl-shift-tab for terminal tabs

gsettings set org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/ next-tab '<Primary>Tab'
gsettings set org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/ prev-tab '<Primary><Shift>Tab'

Redirect file descriptor

run > out.log 2>err.log
run > out.log 2>&1
run > out.log 2>/dev/null

How to silence output in a Bash script? - Stack Overflow

Browser in terminal

links

sudo apt-get install links
sudo yum install links

links www.example.com

What to do after a crash

Rethinkdb as an example

Check if you ran out of memory

sudo dmesg | grep oom

Check the log

  • If you started it on a terminal, it will log to rethinkdb_data/log_file by default.
  • If your system uses systemd, try journalctl -u rethinkdb.
  • If your system doesn't use systemd, then it's likely it wil be in the /var/log/rethinkdb.

Audio

  • PulseAudio > Configuration 设置不需要的播放设备为 Off

    Try

    pacmd list-cards
    
    pacmd set-card-profile ...
    

    Choose the card index, 1 for example. Or use the . Then add

    set-default-sink 1
    set-card-profile 1 output:analog-stereo
    set-card-profile 0 off
    

    to the end of /etc/pulse/default.pa

    Comment load-module module-switch-on-port-available

    ### Should be after module-*-restore but before module-*-detect
    #load-module module-switch-on-port-available
    
  • Sound Switcher

cp 保留文件修改时间等信息

cp -p src dest

检测文件变化

sudo apt install inotify-tools
#!/bin/bash

inotifywait -m ./src -e create -e modify -e delete -e delete_self -e move |
  while read path action file; do
    if [[ "$file" =~ .*go$ ]]; then
      echo "go file changed"
    fi
  done

Top

可以查看当前所有命令的 CPU、Memeory 等运行情况

查看特定命令

top -p <pid>
top -p `pidof <process name>`

配置

执行top命令后,按 f 可以进入配置显示信息的页面。完成后按 shift+w 保存。

show folder size

du -sh /path/to/directory

# All files and directories in current directory
du -sh *
du -sh * | sort -h
du -sh * | sort -hr

Customize desktop application

UnityLaunchersAndDesktopFiles

  • ~/.local/share/applications/*.desktop

  • /usr/share/applications/*.desktop

  • icons: /usr/share/pixmaps/*

Install graphic deriver

Launch Software & Updates > Additional Drivers

显卡驱动可能会导致 Android Simulator 无法启动

Install KVM on Ubuntu 18.04

# Install
sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils

# Add user to group
sudo adduser `id -un` libvirt

# Change owner
sudo chown <username>:libvirt /dev/kvm

# Verify
virsh list --all

Run on start

  • rc.local

/etc/rc.local

/path/to/script
exit 0
sudo chmod +x /etc/rc.local

No sound

indicator-sound-switcher

sudo apt-add-repository ppa:yktooo/ppa
sudo apt-get update
sudo apt-get install indicator-sound-switcher

# run then choose the right output
indicator-sound-switcher

Gnome Terminal switch with ctrl-tab

Setting gsettings from the terminal

gsettings set org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/ next-tab '<Primary>Tab'
gsettings set org.gnome.Terminal.Legacy.Keybindings:/org/gnome/terminal/legacy/keybindings/ prev-tab '<Primary><Shift>Tab'

keyboard shortcut gnome-terminal (ctrl-tab) and (ctrl-shift-tab) in 12.04? - Ask Ubuntu

English words

  • CentOS:

    sudo yum install words
    
  • MacOS: 默认安装

对应文件:/usr/share/dict/words

可以统计单词长度分布

awk '{ print length($0); }' /usr/share/dict/words | sort -n | uniq -c

创建文件软链接

软链接,也叫符号链接,可以看成原文件的快捷方式。注意:使用绝对路径。

ln -s source target

-s: Create a symbolic link.

Install / upgrade zsh

安装 zsh 到 /usr/local/bin/zsh

curl -L https://www.zsh.org/pub/zsh-5.8.tar.xz -o zsh-5.8.tar.xz
tar -xf zsh-5.8.tar.xz
cd zsh-5.8
./configure
sudo make && sudo make install

使用新安装的 zsh

echo /usr/local/bin/zsh | sudo tee -a /etc/shells
chsh -s /usr/local/bin/zsh

Ubuntu 可能需要依赖

sudo apt install ncurses-dev

Configure zsh: Oh My Zsh

git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh

cp ~/.zshrc ~/.zshrc.orig
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
chsh -s $(which zsh)

zsh theme: p10k

For Oh My Zsh

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

Set ZSH_THEME="powerlevel10k/powerlevel10k" in ~/.zshrc

zsh 快速切换历史路径

使用d命令

# 列出历史路径
d
# 0   ...
# 1   ...
# 2   ...

# 直接输入数字即可
1

Debug zsh

zsh -lxv

Contraol character

Control character - Wikipedia

at 命令一次性运行任务

at, batch, atq, atrm 都可以在特定的时间点执行任务。

at <time> <command>

at now + 1 minutes tmp.sh

源码编译

ctags 为例,下载源码

    cd ctags-5.8

    ./configure

    make

grep 搜索包含特定文本的文件

    grep -r '127.0.0.1' /etc

    egrep -w 'word1|word2' /path/to/file
  • -i: Ignore case
  • -w: Only words
  • -v: Select non-matching lines
  • -l: Only names of files

find 文件查找

在给定目录树下递归查找文件

find . -name '*.js'

find root_path -name '*.py' -or -name '*.r'

# 使用 {} 在后续命令中获得文件名
find root_path -name '*.ext' wc -l {} \

# 排除特定文件、目录
find . \( -name .git -o -name .config \) -prune -false -o -name '*txt*'

加载新增的硬盘

    # 查看硬盘
    # 找到新增硬盘,例如 /dev/sdb,或 /dev/xvdc
    fdisk -l

    # 新增分区
    # Common fdisk parameters are:
    # n: Create a new partition
    # w: Save the changes and exit the command
    fdisk /dev/sdb

    # 格式化硬盘,注意后面都使用 sdb1
    mkfs.ext4 /dev/sdb1

    # 加载硬盘
    mount /dev/sdb1 /data

    # 如果一直需要加载,可以加一行记录到文件 /etc/fstab
    /dev/sdb1 /data ext4 defaults 0 0

实时系统性能监控

glances

安装(CentOS)

    yum install -y glances

运行

    glances

添加代理

参考

bash 命令

    # http
    export http_proxy=http://your.proxy.server:port/

    # https
    export HTTPS_PROXY=https://your.proxy.server:port/

curl

添加参数

    -x, --proxy [protocol://]host[:port] Use this proxy
    -U, --proxy-user <user:password> Proxy user and password

实时查看文件变化

    tail -f file.log

-- 含义

参考

终端中--一般用来表示命令选项参数结束了,后面的参数就算以-开头,也被命令按位置来传递和使用。

例如,grep命令要搜索-L字符串,默认情况下会被解析成参数。需要先使用--,表示后面的是要搜索的内容

    curl --help | grep -- -L

获取时间

获取日期时间

    DATE=`date '+%Y-%m-%d %H:%M:%S'`

嵌套在其它命令中

    git commit -m "build at $(date '+%Y-%m-%d %H:%M:%S')"

mtime, atime, ctime

  • mtime - modify time

      文件内容改变
    
              ls -l
    
  • atime - access time

      读取或者运行
    
              ls -lu
    
  • ctime - change time

      文件内容、所有权、读写运行权限的改变
    
              ls -lc
    

sed 文本字符处理

文本替换,生成新文件

    sed "s/old/new/g" file1 > file2

Linux

单个文件替换

sed -i "s/old/new/g" file1

new 中如果出现&,表示匹配的内容。如果要替换成字面量&,需使用\&

文件夹批量替换

需要组合findxargs命令,一个例子

find . -name "*.js" |xargs sed -i "s/: \(start.format('YY')\)/: start \&\& \1/g"

MacOS

需要在 -i后使用 ''

可能需要添加-e

sed -i '' "s/old/new/g" file1

find . -type f | xargs sed -i '' -e "s/old/new/g"

查看系统版本

cat /etc/*-release

查找执行历史命令

命令行按下 <ctrl-r>后输入字符,会自动查找匹配以前的命令。确定后按回车。

在两台机器传递文件

scp (Secure copy): 通过 SSH 协议在两台机器间传递文件。传递目录需要-r参数。

Local to remote

scp path/to/local_file username@host:path/to/remote_file
scp path/to/local_file username@host:path/to/remote_dir

Remote to local.

scp username@host:path/to/remote_file path/to/local_dir
scp -r username@host:path/to/remote_dir path/to/local_dir

pscp

pscp (PuTTY Secure Copy)

与 scp 不同,pscp 需要 ppk 格式的秘钥。可以通过下载 puttygen 读取 pem (rsa)秘钥文件生成。

chmod for Permission denied

chmod <flags> <permissions> <filename>

chmod u=rwx,g=r,o=r file
  • r, w, x: read, write, execute
  • g: group
  • o: other
chmod 744 file
  • 4, 2, 1, 0: read, write, execute, no permissions
  • position order: user, group, other

运行脚本报错

./program

报错

-bash: ./program: Permission denied

解决方法

chmod u+x program

修改文件报错

修改文件 /path/to/change/file.txt报错:Permission denied,通常是因为当前用户权限不足。

chmod -R 777 /path/to/change

修改目录权限,允许所有用户操作

📖