Shell

Quick notes

System information

  • neofetch, system info
  • du -sh <file>/<directory>, check the size of file or directory
  • df -h, display free disk space
    • -h, 1k = 1024 byte
    • -H, 1k = 1000 byte
  • uname -s, print kernel name
    • Linux
    • Darwin
  • uname -r, linux release number
  • last -n 50, check last logged in users
  • lastb -n 50,
  • pstree -p, process tree
  • w, show who is logged on and what they are doing

Auto delete files in tmp (CentOS 7)

  • tmpwatch - removes files which haven’t been accessed for a period of time
    • install: yum install -y tmpwatch
    • delete files not accessed in 7 days: tmpwatch 7d /tmp
  • cron 定期执行指定程序
    • crond
    • crontab -e
      • 0 0 * * * tmpwatch 7d /tmp

sed

Sed is the ultimate stream editor.

Some examples:

  • sed s/spaceship/honukai/g <old.file >new.file
  • change = “
    • sed -i.bak '/ZSH_THEME/s/".*"/"honukai"/'
      • backup old file to .bak
      • ZSH_THEME="robbyrussell" => ZSH_THEME="honukai"
  • change =
    • sed -i.bak 's/^key\s*=.*/key = value/'
    • ^, start with
    • \s, space, tag, …

timezone

apt-get install tzdata
ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

file exist

file=data
if [ -f ${file} ]
then
    echo "$file found"
else
    echo "$file not found"
fi
  • -d exists and is a directory
  • -e exists
  • -f exists and is a regular file
  • -h exists and is a symbolic link

ln -sf <target> <symlink>

Example for glibc:

ln -sf libstdc++.so.6.0.21 libstdc++.so.6
strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX

Open port in CentOS 7

firewall-cmd --permanent --add-port=8888/tcp
firewall-cmd --reload
firewall-cmd --list-ports

Create and delete user

Create user:

useradd <username> -b <base_dir> -e <expire_date>
echo <passwd> | passwd --stdin <username>
  • -b, --base-dir, HOME_DIR=${BASE_DIR}/${USERNAME}
  • -d, --home-dir
  • -e, example: 2018-01-30

Delete user:

userdel -r <username>
  • -f, force remove, even if the user is still logged in. Dangerous!
  • -r, remove user’s home directory

The art of command line

https://github.com/jlevy/the-art-of-command-line

AMAZING!

  • set -o vi, vi style command line
  • !!, last command
  • jq, JSON processor

man bash

  • bash -c <string>, Docker use

TODO

List all versions

Yum.

yum --showduplicates list <package>
yum install <package>-<version>

Brew.

brew info <package>
brew search <package>@
brew install <package>@<version>
brew link <package>@<version> --force --overwrite
for i in {0..255}; do
    printf "\x1b[38;5;${i}mcolour${i}\x1b[0m\n"
done