这篇博客记录一些我比较有用但是的command, 方便自己查询, 标题加入了一些英语翻译,方便CTRL+F。 欢迎补充
网络(Network related)
curl查询公网出口IP(use curl to query public IP)
curl ifconfig.me
some other useful webstite:
1. ip.cn 2. ipinfo.io 3. cip.cc 4. myip.ipip.net
设置ssh反向tunnel(ssh reverse tunnel)
A机器在内网,B机器在外网,C机器可以连接B但是不能连接A。如果C想通过B链接到A机器
- 从A登录B机器
ssh -fNR 8771:localhost:22 [email protected]_MACHINE_IP
- 这样C机器就可以访问 ssh -p 8771 [email protected]_MACHINE_IP
- 从A登录B机器
列出监听端口(list all listening port)
1
netstat -anlp | grep -w LISTEN
or
1
sudo netstat -tulp
打开关闭网卡(up and down network interface)
1
ip link set eth0 up/down
用ssh执行远程命令(use ssh to execute remote commands)
-t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
1
ssh -t [email protected] "command1 & command2"
For example, if you want to simply pull a docker images on multiple servers, you can write a simple bash script like this:
1
2
3for remote in 192.168.1.1 192.168.1.2 192.168.1.3; do
ssh -t [email protected] "docker pull image:tag"
doneif you want to execute remote command in background, you can try to use nohup or screen:
1
ssh [email protected] screen -d -m ./script
and you can check the result with
1
screen -ls
Show network interface RX-OK/RX-ERROR
1
ip addr show;echo;echo;ip route list table all;echo;echo;netstat -ai
文件查找和字符操作(counting, searching and string operation)
grep查找包含特定文字的文件(use grep to find all files container specific text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20grep -rnw '/path/to/somewhere/' -e 'pattern'
-r or -R is recursive,
-n is line number, and
-w stands for match the whole word.
-l (lower-case L) can be added to just give the file name of matching files.
Along with these, --exclude, --include, --exclude-dir flags could be used for efficient searching:
This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
This will exclude searching all the files ending with .o extension:
grep --exclude=*.o -rnw '/path/to/somewhere/' -e "pattern"
For directories it's possible to exclude a particular directory(ies) through --exclude-dir parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"grep从文件中过滤并统计特定字符数量(use grep to filter string in file and count number)
1
grep -n '2018-03-24' /var/log/xxx/xxx/xxx.log | grep 'stringyouwant' | wc -l
find查找并替换文件中内容(use find to filter string and replace)
stackoverflow1
find . -iname '*.txt' -exec sed -i 's/text-to-search/text-to-replace/g' {} \;
find查找过滤文件中内容(use find to filter string from file)
1
find . -iname '*.txt' | xargs -n 1 grep -nR 'text-to-search'
find recursively find all files in current and subfolders based on wildcard matching
1
find . -name "foo*"
find exclude a directory
- use -prune switch
1
find . -path ./misc -prune -o -name '*.txt' -print
- use -not -pathor
1
find -name "*.js*" -not -path "./directory/*"
1
find -name "*.js*" -not -path "./directory*"
- use -prune switch
find and do operations(like copy)
1
find . -name "*.dae" -not -path "./ignore-directory/*" -exec cp {} ../otherfolder/ \;
better(not tested)
1
find . -name '*.dae' -not -path './ignore-directory/*' -exec cp {} ../otherfolder/ +
查看当前目录下多少文件(Counting Files in the Current Directory
1
2ls -1 | wc -l
ls -l | grep -v ^l | wc -l (exclude symbol file, that's an "L" not a "1" this time)遍历当前目录下所有文件名(iterate over filenames in current directory)
1
2
3
4for filename in echo *; do
echo "Processing $filename"
# do something on $f
done删除字符串后缀(Remove the string prefix)
Shell-Parameter_Expansion1
2
3
4$ x="/foo/fizzbuzz.bar"
$ y=${x%.bar}
$ echo ${y##*/}
fizzbuzzShell
查看当前shell版本(show which shell are you using)
1
echo $0
Debian/Ubuntu System related
如何压缩一个文件名是
--
开头的文件?
如果遇到一个文件名是以--
开头的,那么在linux命令行中会被认为是参数. 例如文件名--test_abc
1
tar cvf test.tar.gz --test_abc
这时候只要用
--
来表示参数终结就可以了1
tar cvf test.tar.gz -- --test.abc
Showing Linux Kernel LOG/Query the system journal
1
sudo dmesg
or use journalctl under debian/ubuntu
all dmesg output in the last 2 hours
1
sudo journalctl -k
all journal since last boot
1
sudo journalctl -b
list boots in the journal
1
journalctl --list-boots
Setting persistent logging for kernel log
by default the log is written non-persistently to
/run/systemd/journal/
(a binary file)Edit the
/etc/systemd/journald.conf
and uncomment#Storage=auto
and changeauto
topersistent
Then you can restart the service
1
sudo systemctl restart system-journald
Make a read-only filesystem writable
remouting it read-write:
1
sudo mount -o remount,rw '/mount-point'
Alternative different version of software
If you have installed multiple versions of gcc, like gcc-6 and gcc-9. your /usr/bin/gcc is probably pointing to gcc-6 by default. You can use the following command to change the version.
1
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 900
open the windows for switching version
1
sudo update-alternatives --config gcc
Emacs
compile .emacs.d directory
1
C-u 0 M-x byte-recompile-directory
will compile all the .el files in the directory and in all subdirectories below.
TheC-u 0
part is to make it not ask about every .el file that does not have a .elc counterpart.From stackoverflow
upgrade emacs packages
1
M-x list-packages
- press
U
to mark all upgradable packages to be upgrade - press
x
to perform the new updates
- press
format code
select the region you want to format
1
M-h
1
M-x (indent-region)
see also : choosing mode
Browser
- open recent closed tab (chrome)
There are 3 ways to do it:
- Get it back by right-clicking in the tab bar and selecting Reopen closed tab from the menu
- By clicking Ctrl+Shift+T.
- You can also find a list of recently closed tabs in the Settings menu. Click on the 3 horizontal line icon top right and choose Recent tabs.
Other commands and tools
screen
1. killing all the detached screens
1 | screen -ls | grep detached | cut -d. -f1 | awk '{print $1}' | xargs kill |
or inside screen
1 | Ctrl-a \ |
Unclutter
Unclutter hides your X mouse cursor when you do not need it, to prevent it from getting in the way. You have only to move the mouse to restore the mouse cursor. Unclutter is very useful in tiling window managers where you do not need the mouse often.
Extract GPS Info from Photos (Python脚本提取照片中的GPS信息)
pip install pillow
run this code under the folder image
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
27from PIL import Image
from PIL.ExifTags import TAGS
import glob
types = ('*.JPG',)
def demo(name):
exifdata = {}
imgfile = Image.open(name)
info = imgfile._getexif()
if info:
for (tag, value) in info.items():
# print(str(tag) + ' : ' + str(value))
decoded = TAGS.get(tag, tag)
# print str(decoded)
exifdata[decoded] = value
exifgps = exifdata.get('GPSInfo', '')
if exifgps:
print('it has founded ; ' + str(name) + " " + str(exifgps))
def main():
for t in types:
filenames = glob.glob(t)
[demo(f) for f in filenames]
if __name__=='__main__':
main()If you have multiple image types, you can change
types
tuple to add more, for exmaple,types = ('\*.JPG', '*.png')