some cute linux command

在linux下面用命令打开网页进行浏览,也就是在linux的命令模式下面进行浏览网页:
用lynx可以浏览用links也行,先打开终端,或者你本来就在文字界面下,输入:links或者lynx
如果没安装的话他会提示你用 sudo apt-get install links 或者 sudo apt-get install lynx
进行安装,然后就可以用了,links http://www.baidu.com 就可以用了。

$ python -m webbrowser -t "http://www.python.org"

查看文件的大小

du -h filepath 直接得出人好识别的文件大小
wc -c filename 参数-c表示统计字符, 因为一个字符一个字节, 所以这样得到字节数
stat ~/Downloads/jdk-8u60-linux-x64.tar.gz
ls -l filepath 第五列为文件字节数
ls -h filepath h表示human, 加-h参数得到人好读的文件大小

# 在输出或打印中,替换字符串。并不改变原文件内容
sed '作用范围s/替换查找目标/替换成为/替换目标option' 文件名


# 替换字符串,并更改原文件内容
# 在sed后面加 -i,即编辑文档“edit files in place”选项
sed -i '作用范围s/替换查找目标/替换成为/替换目标option' 文件名

$ sed ‘s/cat/dog/g’ pet.txt
在全局范围(s前的作用范围无内容)查找cat并替换为dog,作用范围内每一行出现的不仅第一个目标,而是所有目标都会被替换(g)。


# 作用范围在第1行
sed '1s/cat/dog/g' pet.txt

# 作用范围在第6行到第10行
sed '6,10s/cat/dog/g' pet.txt

# 作用范围在第6行到最后一行
sed '6,$s/cat/dog/g' pet.txt

# 作用范围在指定行到其后2行,用加号(减号不可用)
sed '1,+2s/cat/dog/g’ pet.txt

# 输出test.txt的第5-7行
sed -n ‘5,7p’ test.txt

# 输出test.txt的第99行, 打印完之后在100行退出。
sed -n '99p;100q’ test.txt

# 打印test.txt中第一列为This的行:
awk '$1=="This” test.txt


# 打印表头和test.txt中第一列为This的行:
awk '$1=="This" || NR == 1’ test.txt


# 格式化输出:
awk '$1=="This" || NR==1 {printf "%-20s %-20s\n",$4,$5}' test.txt

# 如果字符串中有“/”,就会发生混乱,在这个时候可以用#当做分隔符,就变成
sed -i "s#abc#cde#g" file
# 这时如果abc中包含“/”就可以随便替换了

> pip show cvxopt
Name: cvxopt
Version: 1.2.0
...
Location: /usr/local/lib/python2.7/site-packages


$ python3 -m pip show graphviz
Name: graphviz
Version: 0.17
Summary: Simple Python interface for Graphviz
Home-page: https://github.com/xflr6/graphviz
Author: Sebastian Bank
Author-email: sebastian.bank@uni-leipzig.de
License: MIT
Location: /usr/local/lib/python3.9/site-packages
Requires:
Required-by:

brew info curl
brew upgrade curl


$ sudo cat /dev/zero > zero.fill; sudo sync; sleep 1; sudo sync; sudo rm -f zero.fill; sudo shutdown -h now
$ sudo reboot
export PATH=$PATH:$HOME/pyretic:$HOME/pox   
export PYTHONPATH=$HOME/pyretic:$HOME/mininet:$HOME/pox

// where the path files .pth are
/usr/local/lib/python2.7/dist-packages

// keep pip up to date
pip install --upgrade setuptools pip

// show all the installed packages and their versions
pip freeze 

// uninstall 
apt-get purge packagename or apt-get remove --purge packagename

// show what terminal you are using
echo $0 

// 清理 apt-get
sudo apt-get install deborphan -y


Ubuntu 里的.bashrc文档路径: /etc/bash.bashrc

python3 -m http.server 3333

Pythonistas

find的使用实例:

  $ find . -name 'my*'
搜索当前目录(含子目录,以下同)中,所有文件名以my开头的文件。
  $ find . -name 'my*' -ls
搜索当前目录中,所有文件名以my开头的文件,并显示它们的详细信息。
  $ find . -type f -mmin -10
搜索当前目录中,所有过去10分钟中更新过的普通文件。如果不加-type f参数,则搜索普通文件+特殊文件+目录
    $ find . -name '*.cnf' -ls


$ find . -name pro\*
列出以“pro”开头的当前目录中的所有文件。
注意:find命令默认为区分大小写。如果要搜索单词或短语不区分大小写,请使用-inamefind命令中的选项。这是-name命令的不区分大小写的版本。

查找文件并移动到某文件夹:

find . -name '2019*' -exec mv {} /Users/qianruzhou/Documents/code/my/AIforecast/dataset/weather-forcast/cepri_historic_2019010112_2020123112_JSGF011_JSGF011/2019 \;


这里:
 => -exec mv {} /mnt/mp3 \; - 运行mv命令。
 => {} - 字符 '{}' 代表find到的所有内容。
 =>../表示当前用户目录的上一级目录
 => \; - 结束 /bin/mv 命令。
find和grep的使用权限是所有用户
find命令的作用是在目录中根据文件名搜索文件
find 列出当前目录及其子目录的所有文件和文件夹的完整路径。
find -name Help.java 在当前目录及其子目录中搜索文件名为Help.java的文件。
find . -name Help.java 在当前目录及其子目录中搜索文件名为Help.java的文件(同上)。
find / -name Help.java 在整个硬盘中搜索文件名为Help.java的文件。
find -perm 755 在当前目录及其子目录中查找指定权限的文件
find -type b 在当前目录及其子目录下查找块设备文件。
find -type d 在当前目录及其子目录下查文件夹。不能按名字搜索文件夹,只能搜索所有的文件夹。
find -type c 在当前目录及其子目录下查找字符设备文件。
find -type p 在当前目录及其子目录下查找管道文件。
find -type l 在当前目录及其子目录下查找符号链接文件。
find -type f 在当前目录及其子目录下查找普通文件。
find -type d -exec ls -l {} \; 查找当前目录及其子目录下的文件夹,并将查找结果以ls -l的方式展现。
find -type d -ok rm -rf {} \;查找当前目录及其子目录下的文件夹,并将查找结果依次执行rm -rf命令,但是在执行命令前会有确认提示。

grep命令的作用是在目录中根据文件内容搜索文件
grep Clock * 查找当前目录下的所有文件中包含Clock字符串的文件,不查找子目录
grep -r Clock * 查找当前目录下的所有文件中包含Clock字符串的文件,查找子目录
grep -nr Clock * 查找当前目录下的所有文件中包含Clock字符串的文件,查找子目录,并显示行号

For example, the patterns fo*' and F??' match the file names Foo', FOO', foo', fOo', etc. In these patterns, unlike filename expansion by the shell, an initial '.' can be matched
by *'. That is, find -name *bar will match the file .foobar'. Please note that you should quote patterns as a matter of course, otherwise the shell will expand any wildcard characters in them

grep 多个关键字,grep查找2020年十月和十一月的日志,把它输出到新的文本日志中

cat nginx.log|grep -E "Oct/2020|Nov/2020”> grep.log

grep多条件过滤,查看2020年十月的日志,并且是index.html的日志

cat nginx.log|grep "Oct/2020” | grep "index.html">grep.log

例1:查找tomcat7文件夹所在的位置

	find / -name 'tomcat7' -type d 

例2:查找server.xml文件的位置

find / -name 'server.xml' -print

ag:比grep、ack更快的递归搜索文件内容
tig:字符模式下交互查看git项目,可以替代git命令
mycli:mysql客户端,支持语法高亮和命令补全,效果类似ipython,可以替代mysql命令。
jq: json文件处理以及格式化显示,支持高亮,可以替换python -m json.tool。
shellcheck:shell脚本静态检查工具,能够识别语法错误以及不规范的写法。
yapf:Google开发的python代码格式规范化工具,支持pep8以及Google代码风格。

mosh:基于UDP的终端连接,可以替代ssh,连接更稳定,即使IP变了,也能自动重连。

fzf:命令行下模糊搜索工具,能够交互式智能搜索并选取文件或者内容,配合终端ctrl-r历史命令搜索简直完美。

PathPicker(fpp):在命令行输出中自动识别目录和文件,支持交互式,配合git非常有用。

运行以下命令:

git diff HEAD~8 --stat | fpp

htop: 提供更美观、更方便的进程监控工具,替代top命令。
axel:多线程下载工具,下载文件时可以替代curl、wget。

axel -n 20 http://centos.ustc.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso

sz/rz:交互式文件传输,在多重跳板机下传输文件非常好用,不用一级一级传输。
cloc:代码统计工具,能够统计代码的空行数、注释行、编程语言。

ccache:高速C/C++编译缓存工具,反复编译内核非常有用。使用起来也非常方便:
gcc foo.c

改成:

ccache gcc foo.c
tmux:终端复用工具,替代screen、nohup。
neovim: 替代vim。

script/scriptreplay: 终端会话录制。

script -t 2>time.txt session.typescript # 录制开始

your commands

exit # 录制结束

回放:

scriptreplay -t time.txt session.typescript
you-get: 非常强大的媒体下载工具,支持youtube、google+、优酷、芒果TV、腾讯视频、秒拍等视频下载。

还有mac专有的pbcopy/pbpaste:

把命令行输出拷贝到系统粘贴板:

cat test.sh| pbcopy

把系统粘贴板内容拷到终端:

pbpaste

mac下的say命令支持多种语言(英语、普通话、粤语)文本朗读,支持各种销魂恐怖的语气。跑这个命令感受下,小心被吓着了:

for i in `say -v '?' | cut -d ' ' -f 1`; do echo $i && say -v "$i" 'Hello World';done

TCPDUMP uses Libpcap (a c/c++ library that's used for packet capturing.)
捕捉时,Wireshark使用libpcap(WinPcap)捕捉库(支持纳秒精度)
pox, ryu 也用 libpcap 来捕捉包

python tools for libpcap:
pypcapfile is a pure Python library for handling libpcap save files. https://pypi.python.org/pypi/pypcapfile

pypcap: https://github.com/dugsong/pypcap
python-libpcap: Python module for the libpcap packet capture library, based on the original python libpcap module by Aaron Rhodes. https://sourceforge.net/projects/pylibpcap/

Pcapy: a Python extension module that interfaces with the libpcap packet capture library. https://www.coresecurity.com/corelabs-research/open-source-tools/pcapy

pypcap就已经基本淘汰了pcapy了

传文件

scp my_app_nodeDownDetect.py centos@137.195.53.231:/home/centos/

传本地的my_app_nodeDownDetect.py 到远程centos@137.195.53.231的

比较两个文件的不同:
diff 1.txt 2.txt
man diff

$ pdfcrop input.pdf output.pdf
如果没有安装,则
 sudo apt-get install texlive-extra-utils
What Does The Linux More Command Do
The more command allows you to display output in the terminal one page at a time. This is especially useful when running a command which causes a lot of scrolling such as the ls command or the du command.
sysctl: read/write system parameters
 e.g.: 
mininet-wifi> h2 sysctl net.ipv4
net.ipv4.conf.all.accept_local = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.all.arp_accept = 0
net.ipv4.conf.all.arp_announce = 0
net.ipv4.conf.all.arp_filter = 0
net.ipv4.conf.all.arp_ignore = 0
net.ipv4.conf.all.arp_notify = 0
net.ipv4.conf.all.bootp_relay = 0
net.ipv4.conf.all.disable_policy = 0
net.ipv4.conf.all.disable_xfrm = 0
net.ipv4.conf.all.force_igmp_version = 0
net.ipv4.conf.all.forwarding = 1
net.ipv4.conf.all.igmpv2_unsolicited_report_interval = 10000
net.ipv4.conf.all.igmpv3_unsolicited_report_interval = 1000
net.ipv4.conf.all.log_martians = 0
net.ipv4.conf.all.mc_forwarding = 0
net.ipv4.conf.all.medium_id = 0
net.ipv4.conf.all.promote_secondaries = 0
net.ipv4.conf.all.proxy_arp = 0
net.ipv4.conf.all.proxy_arp_pvlan = 0
net.ipv4.conf.all.route_localnet = 0
net.ipv4.conf.all.rp_filter = 1
In order to "zip" a directory, the correct command would be
tar -zcvf archive.tar.gz directory/

查看 kernel 的信息   $ uname
change root's password: $ passwd

du 统计文件大小相加 
df  统计数据块使用情况 

du -h --max-depth 1 bin/Mdroid 

表示列出bin/Mdroid这个目下的文件夹的大小以及当前文件夹的大小。

df -h

参数 -h  表示使用[Human-readable]的输出, 在这里也就是是用GB,MB的容易识别的格式描述档案大小

sudo du -h -d 1

disk usage and search by directory level 1 in human readable format. Which tells you how much space is being used by each directory

du -sh /*
看一下每个文件占用的空间综合。

pip 升级:

pip install --upgrade pip

pip 的 SSL 问题是因为OpenSSL 太旧了,用 brew update 就可以了

HEX to binary without trimmed zeros:

my_hexdata = "1a"
scale = 16 ## equals to hexadecimal
num_of_bits = 8
bin(int(my_hexdata, scale))[2:].zfill(num_of_bits)
or
>>> int = 0x2b
>>> int
43
>>> format(int, '0>42b')
'000000000000000000000000000000000000101011'

# change string to binary
>>> st = "hello world"
>>> ' '.join(format(ord(x), 'b') for x in st)
'1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'

# using `bytearray`
>>> ' '.join(format(x, 'b') for x in bytearray(st))
'1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100'

生成有 24 个字母的随机字符串:
    salt = ''.join(random.sample(string.ascii_letters + string.digits, 24 ))
将彼字符串转换成二进制,一个字母有 7 bits 
    b = ''.join(format(ord(x), ‘0>7b') for x in salt)
或者 for full bytes:
    b = ' '.join('{0:08b}'.format(ord(x), 'b') for x in salt)

按行读取文件:

with open(‘log’, ‘r') as f:
    for line in f.readlines():
        print line

把二进制字符串变成二进制数:

>>> s1 = '010101'
>>> bin(int(s1,2))
'0b10101'
>>> int(s1,2)
21
>>> s1='111'
>>> int(s1,2)
7
>>> format(int(s1,2),'0>9b')
'000000111'

遇到‘httperror_seek_wrapper: HTTP Error 403: request disallowed by robots.txt’

br = mechanize.Browser()
br.set_handle_robots(False)

可以终止 com.alipay.DispatcherService 的命令
sudo launchctl unload /Library/LaunchDaemons/com.alipay.DispatcherService.plist
---
extract from .tar.gz file
tar -xvzf community_images.tar.gz
use man tar for more information

ubuntu apt-get 错误 Temporary failure resolving 'us.archive.ubuntu.com’ 解决

原因是dns没有配置,解决办法 加入dns服务器地址:

vi /etc/resolv.conf

添加

nameserver 202.96.134.133
nameserver 8.8.8.8
如果提示只读,用sudo运行, 运行完重启系统解决

Mac 下安装 homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

# 用法1: awk '{[pattern] action}' {filenames}   # 行匹配语句 awk '' 只能用单引号
# 每行按空格或TAB分割,输出文本中的1、4项
 $ awk '{print $1,$4}' log.txt
 ---------------------------------------------
 2 a
 3 like
 This's
 10 orange,apple,mongo
 # 格式化输出
 $ awk '{printf "%-8s %-10s\n",$1,$4}' log.txt
 ---------------------------------------------
 2        a
 3        like
 This's
 10       orange,apple,mongo


# 用法2: awk -F  #-F相当于内置变量FS, 指定分割字符
# 使用","分割
 $  awk -F, '{print $1,$2}'   log.txt
 ---------------------------------------------
 2 this is a test
 3 Are you like awk
 This's a test
 10 There are orange apple
 # 或者使用内建变量
 $ awk 'BEGIN{FS=","} {print $1,$2}'     log.txt
 ---------------------------------------------
 2 this is a test
 3 Are you like awk
 This's a test
 10 There are orange apple
 # 使用多个分隔符.先使用空格分割,然后对分割结果再使用","分割
 $ awk -F '[ ,]'  '{print $1,$2,$5}’   log.txt


# 用法3: awk -v  # 设置变量
$ awk -va=1 '{print $1,$1+a}' log.txt
 ---------------------------------------------
 2 3
 3 4
 This's 1
 10 11
 $ awk -va=1 -vb=s '{print $1,$1+a,$1b}' log.txt
 ---------------------------------------------
 2 3 2s
 3 4 3s
 This's 1 This'ss
 10 11 10s


# 用法4: awk -f {awk脚本} {文件名}
$ awk -f cal.awk log.txt

$ awk 'BEGIN{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","FILENAME","ARGC","FNR","FS","NF","NR","OFS","ORS","RS";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS,RS}'  log.txt
FILENAME ARGC  FNR   FS   NF   NR  OFS  ORS   RS
---------------------------------------------
log.txt    2    1         5    1
log.txt    2    2         5    2
log.txt    2    3         3    3
log.txt    2    4         4    4
$ awk -F\' 'BEGIN{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","FILENAME","ARGC","FNR","FS","NF","NR","OFS","ORS","RS";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS,RS}’  log.txt
FILENAME ARGC  FNR   FS   NF   NR  OFS  ORS   RS
---------------------------------------------
log.txt    2    1    '    1    1
log.txt    2    2    '    1    2
log.txt    2    3    '    2    3
log.txt    2    4    '    1    4
# 输出顺序号 NR, 匹配文本行号
$ awk '{print NR,FNR,$1,$2,$3}' log.txt
---------------------------------------------
1 1 2 this is
2 2 3 Are you
3 3 This's a test
4 4 10 There are
# 指定输出分割符
$  awk '{print $1,$2,$5}' OFS=" $ "  log.txt
——————————————————————
2 $ this $ test
3 $ Are $ awk
This's $ a $
10 $ There $

# 输出第二列包含 "th",并打印第二列与第四列,~ 表示模式开始。// 中是模式。
$ awk '$2 ~ /th/ {print $2,$4}' log.txt
---------------------------------------------
this a


# 输出包含"re" 的行
$ awk '/re/ ' log.txt
---------------------------------------------
3 Are you like awk
10 There are orange,apple,mongo

# 忽略大小写
$ awk 'BEGIN{IGNORECASE=1} /this/' log.txt
---------------------------------------------
2 this is a test
This's a test


# 模式取反
$ awk '$2 !~ /th/ {print $2,$4}' log.txt
---------------------------------------------
Are like
a
There orange,apple,mongo
$ awk '!/th/ {print $2,$4}' log.txt
---------------------------------------------
Are like
a
There orange,apple,mongo
posted @   周倩如  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示