本文记录平时常用的 Linux 相关的命令记录。
一、系统使用
让命令行翻墙,可以直接从 Shadowsocks 软件那里复制具体的命令:
export http_proxy=http://127.0.0.1:1087;export https_proxy=http://127.0.0.1:1087;
离线的 server 装软件,可以先下载好 *.deb
包,然后利用
sudo dpkg -i ./*.deb
找到对应路径下所有包含关键词的命令
find /dir/to/root/ -name "keywor*"
找到某个库装在哪个地方,例如我要找到cuda装在哪儿了
locate cuda | grep cuda$
这里的$
表示结尾,是正则表达式的部分。
更改一个文件夹下所有文件的后缀
替换对应的 oldextension 和 newextension 即可。
find -L . -type f -name "*.oldextension" -print0 | while IFS= read -r -d '' FNAME; do
mv -- "$FNAME" "${FNAME%.oldextension}.newextension"
done
显示特定目录下的文件夹大小
du -sh /dir/to/root/*
一般进到要查的目录下:
cd /dir/to/root
du -sh */
1.1 压缩/解压文件
.gz 后缀
gunzip file.gz
如果解压之后需要保留原压缩包,可用:
gunzip -k file.gz
.bz2 后缀
bzip2 -d file.bz2
如果解压之后需要保留原压缩包,可用:
bzip2 -dk file.bz2
.tar 后缀
文件同步
rsync 可以比较好的实现备份同步,腾讯云有一个比较好的例子。
# 将本地传到远程机器
rsync -av source/ username@remote_host:destination
# 远程到本地
rsync -av username@remote_host:source/ destination
检查还剩多少内存
# Run "free" to see RAM information in KB.
free
# Run "free -m" to see RAM information in MB.
# watch -n 1 free -m
free -m
# Run "free -g" to see RAM information in GB.
free -g
或者采用如下的方式:
# watch -n 1
cat /proc/meminfo
Free Up Unused Memory
Command 1:
sudo sysctl -w vm.drop_caches=3
这命令不能加快系统运行速度或提高系统稳定性,只是清空 cache 就好。
Command 2:
sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
查看硬件设备
If we are not super-user, we can use lspci
, otherwise we can use sudo lshw
.
安装软件
离线安装
平时用 apt-get install 装的软件,可以去 ubuntu packages 上对应找到 deb 包下载下来,然后手动安装:
# -f, --fix-broken
# Fix; attempt to correct a system with broken dependencies in place.
sudo apt-get install -f
sudo dpkg -i /path/to/deb/file
###
二、命令行
uniq
去除重复行
uniq
只能过滤掉相邻的重复行,所以要想搞定全部的重复行,就要先 sort
一下。
cat text.txt | sort | uniq
列出第一列不重复的个数:
awk -F ',' '{print $1}' test1.csv | sort | uniq | wc -l
>
和 >>
重定向
重定向的用一个 greater-than (>),会替换掉之前的同名文件(如果有的话),两个 greater-than (>) 会在重定向指向的文件后面 append 对应的内容(如果存在同名文件的话)。
wc -l uid_stat_test.csv
tail -n 2 uid_stat_test.csv > uid_only_test.csv
head -n -4 uid_stat_test.csv >> uid_stat_test.csv
axel
vs curl
axel
下载某链接的东西时如果出现 too many redirections,可以尝试用 curl
。
awk
awk 是一种处理文本文件的语言,是一个强大的文本分析工具。
记录一些常见的使用
差集
if ((cat ${file1} | wc -l) > 0) then
awk -F ' ' 'NR==FNR{ a[$1]=$1 } NR>FNR{ if(a[$2] == ""){ print $0}}' ${file1} ${file2} | sort -u > ${infile2_notinfile1}
else
cat ${file2} | sort -u > ${infile2_notinfile1}
fi
递归查找目录下特定后缀结尾的文件个数
find ./ -name "*.jpg" | wc -l
将某个文件夹下(包括子目录)特定后缀的所有文件都挪到另一个文件夹中
find ./ -iname '*.jpg' -exec mv '{}' ../JPEGImages/ \;
Shell Programming
list to dict:
dict((el,0) for el in a)
参数
Verbose Mode
A verbose mode is an option available in many computer operating systems, including Microsoft Windows, macOS and Linux that provides additional details as to what the computer is doing and what drivers and software it is loading during startup.
verbose 模式就是能够输出比较详细的运行信息。
第三方库
tmux
tmux new -s session_name
tmux ls
tmux a -t session_name
tmux kill-session -t myname
修改 root 用户访问权限到某个用户下:
sudo chown -R user_name your_directory
File Management
找文件夹下文件中特定的字符串:
grep -iRl "your-text-to-find" ./
Here are the switches: -i - ignore text case -R - recursively search files in subdirectories. -l - show file names instead of file contents portions.
./ - the last parameter is the path to the folder containing files you need to search for your text. In our case, it is the current folder with the file mask. You can change it to the full path of the folder. For example, here is my command
修改 ssh 端口
Your ISP may block connections to port 22 (nothing you or your router can do about it). Just set SSHd to run on a different port, e.g. 2222.
In /etc/ssh/sshd_config, change Port 22 to Port 2222 and then sudo service ssh restart. Port forward 2222 (or whatever), and try again.
Bash
#!/bin/sh
name1="$1"
echo "local files: $name1"
if [ -z "$name1" ];
then
name1=*
else
if [ "$name1"==*/ ];
then
name1=$name1*
fi
fi
name2=$2
echo "remote files:" $name2
if [ -z "$name2" ];
then
name2=""
else
if [ "$name2"==*/ ];
then
name2=$name2
fi
fi
echo scp -r ./$name1 binli@192.168.31.127:/home/binli/repos/cayman/$name2
scp -r ./$name1 binli@192.168.31.127:/home/binli/repos/cayman/$name2
sudo chown username:groupname myfile.txt/folder
# 使同组和其他用户对文件example 有读权限
chmod g+r,o+r example
# 查看挂载情况
lsblk
df