Linux command of the day

This article lists a Linux cheatsheet including lots of tools’ commands.

I. Processes and system management

Linux background execute command

If we want to proceed our work when we’re executing a Linux command that may spend a lot of time, I find two ways that may solve this paradox.

  1. Keep this terminal and open another one.
  2. Append a ‘&’ to the time-spending command and it will be excuted in the background.

count all processes and threads running

1
2
ps aux | wc -l # count all processes
ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }' # count all threads

linux count all processes and threads

For Windows, just type Ctrl+Alt+Delete to open Task Manager and choose the performance tab and see:
windows count all processes and threads

User management

add user

1
adduser 用户名

add user to group

1
usermod -a -G 组名 用户名

II. Text manipulation

sed

删除 .bashrc 的最后一行:sed -i '$d' ~/.bashrc

IV. IO

mount

Linux mount command is used for mounting filesystems to directory and /etc/fstab file the default mounting config file at every booting time. Surely you can alse use sudo mount -a to remount all filesystems in /etc/fstab

USB FAT32 filesystem

It’s the easiest one:

1
2
3
sudo mkdir /mnt/usb
lsblk
sudo mount /dev/sdb1 /mnt/usb

NAS NFS filesystem

https://wiki.archlinux.org/index.php/NFS

NAS CIFS filesystem

1
2
3
4
lsmod | grep cifs
# if not, `sudo modprobe cifs` or add `cifs` into /etc/modules-load.d/cifs.conf
smbclient -L nas -U%
sudo mount -t cifs //nas/wangshengdian /mnt/lab-nas-wangshengdian -o username=[your username],password=[your password],workgroup=workgroup,iocharset=utf8,uid=shane,gid=shane

noauto

Mount at boot-up

/etc/fstab

V. Network

Web Downloader

Downloading from the web has many choices on Linux.

aria2

For chrome extension, I use Camtd. Go to https://github.com/jae-jae/Camtd/releases and download the latest .crx file.
Then upload it to https://crxextractor.com/ for getting the .zip file and download it.
Then unzip -d dir Camtd.zip. And open chrome://extensions to load the unpacked directory.

But sometimes you don’t need a browser to download file. You can directly download from your command line. Here are they:

wget

If you want to download a file on Github, it’s easy to use wget. Let’s say

1
wget https://raw.githubusercontent.com/ethereum/web3.js/1.x/dist/web3.min.js

curl

Campus proxy

Some campus net environments have ip restrictions with a maximum number of two. In order to support the network connection for more devices(including virtual machines) as well as to surpass the firewall, additional efforts seem neccessary to deploy a personal proxy server. Here I use v2ray to acomplish this goal.

Sample configuration

On VPS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
"inbounds": [{
"port": 监听端口号,
"protocol": "vmess",
"settings": {
"clients": [
{
"id": "协商的id号",
"level": 1,
"alterId": 64
}
]
}
}],
"outbounds": [{
"protocol": "freedom",
"settings": {}
},{
"protocol": "blackhole",
"settings": {},
"tag": "blocked"
}],
"routing": {
"rules": [
{
"type": "field",
"ip": ["geoip:private"],
"outboundTag": "blocked"
}
]
}
}

On the desktop with an external IP address serving as a campus proxy server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
"log": {
"loglevel": "warning"
},
"inbounds": [{
"port": 用于监听的IP端口,

"listen": "0.0.0.0",

"protocol": "socks",

"settings": {
"auth": "noauth",
"udp": false,
"ip": "0.0.0.0"
},

"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
}
}],
"outbounds": [{
"protocol": "vmess",
"settings": {
"vnext": [{
"address":"Oerseas VPS的IP地址",
"port": 它的端口号,
"users": [{
"id": "协商的id号,必须与VPS inbounds 中设置的一致",
"security": "auto",
"level": 1,
"alterId": 64
}]
}]
}
},{
"protocol": "freedom",
"settings": {},
"tag": "direct"
},{
"protocol": "blackhole",
"settings": {},
"tag": "blocked"
}],

"routing": {
"domainStrategy": "IPOnDemand",
"rules":[
{
"type": "field",
"ip": ["geoip:private"],
"outboundTag": "direct"
},
{
"type": "field",
"domain": ["geosite:cn"],
"outboundTag": "direct"
},
{
"type": "field",
"domain": ["geosite:category-ads"],
"outboundTag": "blocked"
}
]
},
"dns": {
"hosts": {
"domain:v2ray.com": "www.vicemc.net",
"domain:github.io": "pages.github.com",
"domain:wikipedia.org": "www.wikimedia.org",
"domain:shadowsocks.org": "electronicsrealm.com"
},
"servers": [
{
"address": "114.114.114.114",
"port": 53,
"domains": [
"geosite:cn"
]
},
"8.8.8.8",
"localhost"
]
},
"policy": {
"levels": {
"0": {
"uplinkOnly": 0,
"downlinkOnly": 0
}
},
"system": {
"statsInboundUplink": false,
"statsInboundDownlink": false
}
},
"other": {}
}

On the laptop client with only internal ip address

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{
"log": {
"loglevel": "warning"
},
"inbounds": [{
"port": 1080,

"listen": "127.0.0.1",

"protocol": "socks",

"settings": {
"auth": "noauth",
"udp": false,
"ip": "127.0.0.1"
},

"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
}
}],
"outbounds": [{
"protocol": "socks",
"settings": {
"servers": [{
"address":"能连外网的主机IP地址",
"port": 它的端口号
}]
}
}],

"routing": {
"domainStrategy": "IPOnDemand",
"rules":[
]
},

"dns": {
"hosts": {
"domain:v2ray.com": "www.vicemc.net",
"domain:github.io": "pages.github.com",
"domain:wikipedia.org": "www.wikimedia.org",
"domain:shadowsocks.org": "electronicsrealm.com"
},
"servers": [
{
"address": "114.114.114.114",
"port": 53,
"domains": [
"geosite:cn"
]
},
"8.8.8.8",
"202.204.60.10",
"202.204.48.8",
"localhost"
]
},

"policy": {
"levels": {
"0": {
"uplinkOnly": 0,
"downlinkOnly": 0
}
},
"system": {
"statsInboundUplink": false,
"statsInboundDownlink": false
}
},
"other": {}
}

use v2ray -test -config 你的配置文件 to ensure the correctness of the configuration format.

Test and run

Step 1: Login with the campus network account on the desktop to get an external ip address and do not login on the laptop. Ensure the connection availability from your laptop to the desktop, and from the desktop to VPS server.
Step 2: start v2ray

1
sudo v2ray --config=你的配置文件

or run in the backgroud

1
sudo systemctl start v2ray # or sudo service start v2ray

How to Keep user proxy settings when ‘sudo’

Add ‘/etc/sudoers’

1
Defaults env_keep += "all_proxy ftp_proxy http_proxy https_proxy no_proxy"

III. Applications

xdg-open (Launch program in terminal)

1
xdg-open your_file

Some useful

1
2
xdg-mime query filetype your_file
xdg-mime default feh.desktop image/jpeg

~/.config/mimeapps.list

1
2
3
4
5
6
7
8
9
[Default Applications]
text/html=google-chrome.desktop
x-scheme-handler/http=google-chrome.desktop
x-scheme-handler/https=google-chrome.desktop
x-scheme-handler/about=google-chrome.desktop
x-scheme-handler/unknown=google-chrome.desktop
application/pdf=FoxitReader.desktop
application/ppdf=FoxitReader.desktop
x-scheme-handler/postman=Postman.desktop

Calculator

bc

支持运算:

1
2
3
4
5
6
+ 加
- 减
* 乘
/ 除
^ 幂
% 取余

例:

1
echo "1 + 1" | bc

默认仅为整数域运算(参考C语言的四则运算),若要支持小数需要设置scale参数,使用分号分割。例:

1
2
echo "scale=3;(0.12+0.17)*6.0/5.0" | bc
echo "(0.12+0.17)*6.0/5.0" | bc

bc也支持其他进制运算,使用ibase设置输入进制,obase设置输出进制,默认为10进制。例:

1
echo "obase=2;255" | bc

bc 也支持文件内容进行运算,新建文件 calc.txt

1
2
3
4
3+2
4+5
8*2
10/4

运行

1
bc calc.txt # 或 cat calc.txt | bc

References

https://blog.csdn.net/liuyanfeier/article/details/62422742

https://wiki.archlinux.org/index.php/Samba
https://wiki.archlinux.org/index.php/Kernel_module

Configuration on Windows WSL How to install MATALB 2017a on Windows 10

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×