Skip to content

Latest commit

 

History

History
611 lines (456 loc) · 21.3 KB

Linux commands.md

File metadata and controls

611 lines (456 loc) · 21.3 KB
Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 5 column 7
---
title: Linux commands
date: 2021-04-19 04:40:11
updated: 2021-07-28 03:40:11
tags:
    - command
	- linux
	- cheatsheet
categories:
    - computer
---

#command on using Linux.

For the footnotes: [^1] for the usage, info, reference of a command; [^command_name] for the official website of a command.

See also tips & what to do on [[Setup Linux machine]].

Cheat Sheet

#cheatsheet Use lower case on filename ^[@see: 为什么文件名要小写? - 阮一峰的网络日志 ]

Get my IP Address

  • ip addr
  • ifconfig
  • hostname -I

Check kernel version

  • uname -a. Check it, after [[Upgrade Linux kernel]].
  • uname -r
  • cat /proc/version
  • dmesg | grep Linux

Check num of CPU

  • nproc
    • and use it in make: make -j $(nproc) (@see [[Upgrade Linux kernel]])
  • Use the taskset utility, which allows us to control the affinity of a process. 1

Check it with Python 1

# Python 2.6+
try:
    import multiprocessing
    return     multiprocessing.cpu_count()
except (ImportError, NotImplementedError):
    pass
import psutil
psutil.cpu_count()

Check it with Go 2

if runtime.NumCPU() < 10 && os.Getenv("CI") \== "" { // don't bother on slow hardware

t.Skip("this is slow")

}

https://play.golang.org/p/cuaf2ZHLIx

package main

import (  
    "fmt"
    "runtime"
)

func main() {
    fmt.Println(runtime.NumCPU())
}

linux freeze process

linux freeze process is useful when hdd out of space when plotting.

Q: What can I do when hard drive run out of space when half a way during plotting? -> Google: linux freeze process https://superuser.com/questions/485884/can-a-process-be-frozen-temporarily-in-linux/485888

Killing process

Simply kill a process with htop or kill it with kill+<pid>.

  • kill -15 <pid> >> kill -9 <pid>
    • Let process to settle resources with R.I.P. (-> -15 >> -9)
  • pkill -f pattern to kill a process by process pattern
  • for pid in $(ps -ef | awk '/some-search/ {print $2}'); do kill -15 $pid; done

Recover deleted commands

Recover deleted commands in Linux: busybox, coreutils 3 4, moreutils 5

Update & Replacement

#convenient

  • bat >> cat
  • tmux >> GNU screen

Common / General

  • find
  • grep
  • ps -> ps aux
  • uname
  • hostname to see -> hostnamectlto edit
  • alias 6
  • lsblk
  • lsusb
  • lspci
  • lsof to list open file

& more in Coreutils - GNU core utilities

Power-tools

  • powerline 7
  • tmux
  • tldr
  • syncthing
  • ansible for DSC

Progress

  • pv, acronym of pipe viewer, to monitor the progress of data through a pipe 8
  • progress

For fun!

  • cmatrix
  • screenfetch
  • neofetch

Asking help - Read the doc

  • man
  • info
  • tldr 9 10
  • apropos
  • The fuck 11 12
    • install: sudo pip3 install thefuck
  • eg (install: pip install eg) 13

Asking help - from the web, for developer

list

  • ls
  • exa is an improved ls. 16 17 18
  • cat
  • bat is a cat clone & less with syntax highlight. 19

Search

Finding

  • whatis
  • locate
  • whereis
  • which
  • history & !! (bash)
  • search <command> (fish)
  • find
  • fd

Remote access

  • ssh
  • mount remote server file system (in [[FUSE]]) with sshfs 24 25
    1. Create sshfs mount dir: sudo mkdir /mnt/remote_machine
    2. mount it: sshfs <user>@<ip addr> : /home/<user> /mnt/remote_machine
    3. check mount point with df -hT
    4. Finally, unmount it: unmount /mnt/remote_machine

Download

  • wget
  • curl
  • aria2c
  • httpie

Rename

Text processing

Editor

Hex Viewer

  • hexyl 30

Fingerprint

  • md5sum FILE
  • openssl dgst -sha256 FILE

md2 MD2 Digest md5 MD5 Digest mdc2 MDC2 Digest rmd160 RMD-160 Digest sha SHA Digest sha1 SHA-1 Digest sha224 SHA-224 Digest sha256 SHA-256 Digest sha384 SHA-384 Digest sha512 SHA-512 Digest```

Shell

Resources limit / restricting / allocation

  • cpulimit 35 36 to prevent overheat with long-run processes, such as [[Cryptomining]] -> [[Limit CPU usage]]
  • nice value.
  • cputhrottle 37
  • Google: linux gpu limit

Process

  • ps aux
  • pstree
  • pgrep
  • pkill
  • ssh -> mosh Remote terminal application that allows roaming, supports intermittent connectivity, and provides intelligent local echo and line editing of user keystrokes.

Process control system for UNIX

Task management

  • <command> &
  • 使用 nohupdisown 使一個後臺程序持續運行。
  • ctrl-z
  • ctrl-c
  • kill
    • kill -t <pid>
  • jobs
    • fg
    • bg

User Management

  • usermod to change user name 38

Be dangerous

  • Shell bomb / Fork bomb: fork() { fork | fork & } fork -> :(){ :|:& };: [^fork bomb]
  • Delete it forever: rm -f /

System status & Monitoring

  • htop
  • top
  • atop
  • vtop
  • gtop
  • ctop
  • glances
  • bpytop (former: bashtop) (install: pip install bpytop)
  • cockpit Cockpit project by Redhat 39
  • nmon
  • psutil 40
  • Prometheus
  • sar
  • vmstat
  • uptime
  • strace
  • iotop
  • nethogs
  • iostat
  • mpstat
  • free shows memory usage.
  • uptime
  • dmesg shows info of Linux booting.
  • bind shows keyboard key binding.
  • dstat
  • free
  • vmstat

Dashboard

  • sampler 41
  • grafana 42
  • psdash 43 [^pydash]

Dashboard via web

Benchmark (command-line benchmarking)

Backup

Storage

Storage Concepts And Technologies Explained In Detail - OSTechNix

  • rsync >> cp
  • fsck on an unmount partition to fix error 48 49
  • Check bad sector with badblocks when using a failure hard drive 50 -> [[Fix a hard drive]]
    • -> Use info from badblocks with fsck 51
  • diskscan 52
  • samba
  • iometer
  • du to show file & directory usage.
  • dd to backup & restore of block devices.
  • df to show info of disks.
  • smartmontools & smartctl for SSD endurance [^SSD Endurance].
  • 你可以通過 “hdparm -tT /dev/hda” 來測試 “/dev/hda” 硬碟的訪問速度 53

Disk

Network

  • ifconfig
  • ip
  • dig
  • ping,tracert,nslookup
  • mtr 全称 my traceroute,是一个把 ping 和 traceroute 合并到一个程序的网络诊断工具。https://mp.weixin.qq.com/s/_7liuv3_U57VLzVHJstIcg
  • route
  • netstat
  • iptraf & iptraf-ng
  • iftop
  • ntop
  • nload
  • nmap 54
  • netcat

https://github.com/herrbischoff/awesome-command-line-apps#automation

GPU

  • psensor 55

Services & Daemon

  • sshd
  • dhcpd
  • mysqld
  • httpd is Apache
  • rsyncd for rsync
  • lsyncd (Live Syncing Daemon) for sync local and remote directories 56

Scheduler

  • at for one shot.
  • crontab for cron job (regular scheduling). 57

OS Emulation & Translation

  • Wine, a free and open-source compatibility layer that helps us to install and run applications and games developed for Windows on Linux and Unix-like operating systems.
  • Dosbox, to run good-old MS-DOS games and defunct C++ compilers like Turbo C++ in Linux.
  • Anbox, for Android apps. 58
  • Darling is a translation layer that lets you run macOS software on Linux 59

Automation

Browser Automation: selenlum GUI automation: Pyautogui

Video Download

  • youtube-dl (install: python3 -m pip intall youtube-dl)
  • coursera-dl

Execute shell command over SSH

  • Fabric
  • is a tool used for automating routine shell commands over SSH,
  • is a deployment management framework written in Python which makes remotely managing multiple servers incredibly easy
  • ssh <user>@<ip addr> <command to be issued>

Develop

  • cloc 60
  • pygount
  • loc 61

Git

Package Manager

  • pip for Python language
    • python39 -m pip with use of Python3.9
    • python -m with use of a virtual environment activated of Python
  • npm for Javascript & node.js
  • cargo for Rust language

Python

https://awesome-python.com

virtualenv

  • venv is a package shipped with Python 3, which you can run using python3 -m venv It serves the same purpose as virtualenv, but only has a subset of its features. 65
  • python3 -m venv <project-venv> -> source ./project-venv/bin/activate -> pip install <package> -> ``deactivate
  • virtualenv & virtualenvwrapper 65

Manage Python versions

  • pyenv is a bit different to virtual envo -> pyenv is used to isolate Python versions. -> manages different python versions. 65 是 requests 作者 Kenneth Reitz 大神写的一个 python 虚拟环境管理工具, 结合了 pip 和 virtualenv 的功能

MySQL client

  • mycli
  • usql

Ansible

Ansible is a configuration management and orchestration tool. It works as an IT automation engine.

  • [[Manage machine with Ansible]]

Docker

Docker is a docker to provide isolated envo: ensure dev envo & production envo are the same.

https://mp.weixin.qq.com/s/KJZn-y4PBU2rnjE3g7AbIQ Docker 架構 https://mp.weixin.qq.com/s/1uDn4CKitQhU04OTA9F_ZA

Kubernetes

https://mp.weixin.qq.com/s/eSLunx8-4XGlA-FOzyoZrw

Reference:

--EOF

Footnotes

  1. system information - How to find out the number of CPUs using python - Stack Overflow 2

  2. https://github.com/filecoin-project/lotus/search?q=numcpu

  3. How To Restore Or Recover Deleted Commands In Linux - OSTechNix

  4. man cp | grep coreutils -> Coreutils - GNU core utilities

  5. How To Restore Or Recover Deleted Commands In Linux - OSTechNix -> Moreutils - A Collection Of More Useful Unix Utilities - OSTechNix -> https://joeyh.name/code/moreutils/

  6. https://www.cyberciti.biz/tips/bash-aliases-mac-centos-linux-unix.html

  7. https://github.com/powerline/powerline

  8. How To Use Pv Command To Monitor The Progress Of Data In Linux

  9. Good Alternatives To Man Pages Every Linux User Needs To Know

  10. https://github.com/tldr-pages/tldr

  11. Google: awesome command-line -> https://github.com/agarrharr/awesome-cli-apps

  12. https://github.com/nvbn/thefuck

  13. How To Display Linux Commands Cheatsheets Using Eg - OSTechNix

  14. https://github.com/chubin/cheat.sh

  15. https://github.com/cheat/cheat

  16. 25 个 Linux 下的炫酷又强大的命令行神器,你用过其中哪几个呢? - 奇妙的 Linux 世界 2 3 4

  17. 25 个 Linux 下的炫酷又强大的命令行神器,你用过其中哪几个呢? - 奇妙的 Linux 世界 -> https://github.com/ogham/exa

  18. https://mp.weixin.qq.com/s/JORJpCAB3kS2_uiP8oIsQQ

  19. https://github.com/sharkdp/fd -> https://github.com/sharkdp/bat

  20. https://github.com/agarrharr/awesome-cli-apps 2 3

  21. https://github.com/sharkdp/fd

  22. fzf 簡化你的終端機開發生活 | My.APOLLO

  23. https://github.com/BurntSushi/ripgrep/

  24. 2021-04-26 40 Linux Server Hardening Security Tips [2019 edition] - nixCraft -> Redhat Enterprise Linux securely mount remote Linux _ UNIX directory or file system using SSHFS - nixCraft -> https://github.com/libfuse/sshfs

  25. 2021-04-26 Google: linux remote file system -> How to Mount Remote Linux Filesystem or Directory Using SSHFS Over SSH

  26. 6 Methods To Rename Multiple Files At Once In Linux 2 3 4

  27. Google: awesome command-line -> https://github.com/agarrharr/awesome-cli-apps -> -> https://github.com/jhotmann/node-rename-cli

  28. https://github.com/thameera/vimv

  29. https://github.com/learnbyexample/Command-line-text-processing 2

  30. https://github.com/sharkdp/fd -> https://github.com/sharkdp/hexyl

  31. How to use themes on ZSH – Linux Hint

  32. Google: theming zsh -> https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

  33. Google: theming fish -> https://github.com/oh-my-fish/oh-my-fish/blob/master/docs/Themes.md

  34. https://github.com/sharkdp/fd -> https://github.com/sharkdp/pysh

  35. cpulimit attempts to limit the cpu usage of a process (expressed in percentage, not in cpu time). This is useful to control batch jobs, when you don't want them to eat too much cpu. It does not act on the nice value or other scheduling priority stuff, but on the real cpu usage.

  36. https://github.com/opsengine/cpulimit

  37. macOS 如何限制进程 CPU 占用 - 少数派

  38. How Do I Change My Username in Linux? – Linux Hint

  39. https://cockpit-project.org/guide/latest/ [^fork bomb]: https://zh.wikipedia.org/wiki/Fork%E7%82%B8%E5%BC%B9

  40. https://github.com/giampaolo/psutil

  41. https://github.com/topics/command-line -> https://github.com/sqshq/sampler

  42. https://github.com/grafana/grafana

  43. 查看 Linux 系统信息的 Web 面板 psdash - 奇妙的 Linux 世界

  44. https://github.com/sharkdp/fd -> https://github.com/sharkdp/hyperfine

  45. How to Use Sysbench for Linux Performance Testing? – Linux Hint

  46. 推荐一款史上最强跨平台、多端加密同步神器 Restic - 奇妙的 Linux 世界

  47. https://github.com/topics/deduplication -> https://github.com/restic/restic [^SSD Endurance]: https://github.com/Chia-Network/chia-blockchain/wiki/SSD-Endurance

  48. 2021-04-20 Google: linuxmint initramfs -> Linux Mint initramfs prompt at boot, #Linux | How to fix Linux Mint initramfs Prompt at Boot. I run fsck with a live USB on /dev/sda on a notebook computer to fix Linux not boot due to sector corruption on partition .

  49. How To Fix Busybox Initramfs Error On Ubuntu - OSTechNix

  50. Linux 使用 badblocks 指令測試硬碟、隨身碟、記憶卡壞軌與修復教學 - G. T. Wang

  51. https://man.linuxde.net/badblocks

  52. Google: linux hdd low level format -> Low level format of hard drive - Ask Ubuntu -> https://github.com/baruch/diskscan/

  53. https://www.debian.org/doc/manuals/debian-reference/ch09.zh-tw.html#_optimization_of_hard_disk

  54. https://www.cyberciti.biz/networking/nmap-command-examples-tutorials/

  55. Google: rslsync -> 使用 Resilio Sync 进行同步 | Memo -> Ubuntu Memo | Memo

  56. How To Synchronize Local And Remote Directories In Linux - OSTechNix

  57. How to use cron on Linux | Opensource.com

  58. Anbox

  59. Run MacOS Software On Linux Using Darling - OSTechNix

  60. 代码统计利器 Cloc - 奇妙的 Linux 世界

  61. Google: alternative to cloc -> A fast cloc replacement written in rust programming -> https://github.com/cgag/loc

  62. GitUI:Linux CLI 查看 Git Repository 的圖形化工具 – Tsung's Blog

  63. https://github.com/extrawurst/gitui/releases

  64. 用 pre-commit 提升程式碼品質 | My.APOLLO

  65. python - What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc? - Stack Overflow 2 3