linux笔记


最后更新
2022.06.29


系统

E

ll -lh 列出目录带有存储大小
ll | wc -l 列出目录中文件多少

df -h 查看内存使用

du -sh ./* 查看当前目录所有文件所占内存,不加./*是看该目录整个内存占用

scp xx/x/x.txt root@192.168.1.1:/xx/x/ 跨服务器cv,需输对方密码

mv 剪切
mv xxx{ ,.bak } 重命名为xxx.bak

cp -r 递归cv

find / -name xxx 在整个 / 里查找文件

ldd xxx 查看可执行文件的依赖

chmod 777 xxx 给文件全部权限
chmod +x xxx 给文件执行权限

top 查看正运行进程 c 详细
ps -ef 查看所有进程 | grep python 和python相关的进程

sudo xxx 以root权限运行,需验证密码,需是sudoers里的用户

yum list | grep python 在yum下所有和python相关的包

touch xx 创建文件

ln -s a.x b.x 软链接,载体b.x 需 not exist,创建 b.x --> a.x

whereis xxx 查找文件

which xxx 查找配置在PATH里的目录

-type f 文件
-type d 文件夹

cat -v xx 能查看到非打印字符

export LANG=zh_CN.gbk SSH显示中文

shift+insert 粘贴

M

grep pi a.py 查看文件中包含pi的行,可用正则

kill -9 `ps -ef | grep xxx | awk ‘{print $2}’ ` 批量删除进程

sh xx.sh >> a.log 输出重定向,>覆盖,>>追加,文件不存在就创建

nohup java x.jar & , nohup python x.py & 不挂断后台运行,log输出到nohup文件里

gzip xx.tar 打包为xx.tar.gz
gunzip xx.tar.gz 从.tar.gz 解压为 .tar
tar -zxvf xx.tar.gz 解压压缩包
tar -jxvf xx.tar.bz2 解压压缩包
tar -xf all.tar 解压.tar
for file in ` ls *.gz ` ; do tar -zxvf $file ;done

PATH 环境变量
LD_LIBRARY_PATH 动态链接库
echo $PATH 查看环境变量
export PATH=xxx:xxx: $PATH 配置添加环境变量

删除30天前的所有log文件
find /opt -mtime +30 -name “*.log” -exec rm -rf {} \ ;
#(-type f -mmin +120)

yum install vixie-cron # 下载定时任务crontab?
yum install crontabs 定时器
crontab -e 进入定时任务vi命令行
20 08 * * * /opt/a.sh 每天8:20定时执行
5,15 8-12 */2 * * python /home/test_scan.py 每隔两天的上午8点到12点的第5和第15分钟执行
crontab -l 查看任务
crontab -r 删除所有任务
cat /var/log/cron 查看定时任务日志
service crond status/restart/… 定时任务进程重启…

H

service iptables防火墙/network网卡/mysqld/xxx status状态/start/stop/restart

chkconfig iptables off/on 设置防火墙开机启不启动

strings /lib64/libc.so.6 | grep GLIBC 查看glibc版本
strings /lib64/libstdc++.so.6 | grep GLIBC 查看gcc内核版本
gcc -v 查看gcc版本
uname -a 查看内核版本
cat /proc/version 查看操作系统版本信息
cat /etc/issue 查看linux版本
cat /etc/sysconfig/i18n 查看系统语言

export LD_PRELOAD=/lib64/libc-2.12.so (libc.so.6) 指定最高优先级动态库的加载,此处为glibc

netstat -an | grep 3306 | wc -l 监控数据库的使用,返回连接数(mysql)
netstat -anp | grep 8877 查看端口占用情况,然后kill -9 进程释放端口,再启动p3 client

tail -f nohup.out 追踪查看日志内容

sh

dos2unix xxx 改程序文件编码适应linux

sh 进入shell解释器
exit 退出

sh记得前面加 #!/bin/sh
有可能还要加 source ~/.bashrc
因为其实系统并不知道你要干啥,还是告知一下

$ {1} 第一个传参,123456
$ {1:0:3} 从第0位取3个字符,123
$ {1:0:3}“_”${1:3:3} 直接拼接,123_456
$ {1:0:5} | cut -c 1-2 拆分,12

` date +%Y%m%d --date=‘-3 day’ `06 获取三天前的日期2019100806
`date -u +%Y%m%d%H` # 2020010709 UTC时间,不加-u就是北京时

sed -n ‘2p’ d.txt 读取文件第二行

echo `seq 1 5` # 1 2 3 4 5,可以拿来循环
echo `expr 5 - 1` # 4,expr实现加减乘除(乘号需要加转义 *)

赋值,为` `号不是’’ (q左上边的)
b=` echo $a | cut -c 3-4 `
i=`expr ${k} + 1`

sh运行不了,用dos2unix转换试试

简单循环
for i in 0 1 2 3 4 5
do 
echo ${i}
done

找到文件夹中所有包的lib添加到PATH
for a in `find xx/x -type d -name lib`
do
....比如添加到$PATH
done
批量重命名
for file in 'ls *' ; do 
echo $file
h=echo $file | cut -c1-3
echo $h
mv $file FFF_ $ h
done
判断
if [ ] ; then 
echo xxx
else
echo zzz
fi
# []中,
# $1==2 
# -d "$1/" 文件夹存在,不传参也通过了
# ! -d "$1/" 文件夹不存在
# -f "zz/fname" 文件存在
# ! -f "zz/$1" 文件不存在
以数据为基础,循环运行程序,加入定时任务
for file in `find REGC/ -type f -name "HRIT*.jpg"`;do
filename=`echo $file `
echo $filename
python xxx.py $filename
rm -f $file
done
ftp传输 (只能传文件,不能传文件夹)
#!/bin/sh
ftp -v -n 192.168.0.24<<EOF
user root 123456
binary
lcd /home/h/PGSimg  # 本地路径
cd /home/h/PGSimg  # 对方服务器路径
prompt
mput *$1*  # 从本地服务器传出去
mget *$1*  # 从对方服务器传过来
bye
EOF
判断文件大小
path='/data/product/'

for f in `ls $path `;do
echo $f
done
echo $f   现在f变量是循环里最后一个文件(夹)名称

f1="SW_CB"$f".dat"
f2="SW_CB"$f".dat"
echo $f1,$f2

cd ${path}${f}/domain1
f1size=`ls -l "$f1" | awk '{print $5}'`
f2size=`ls -l "$f2" | awk '{print $5}'`
echo $f1size,$f2size   两个文件的大小

多判断,||是或,&&是和
-eq等于,-ne不等于,-gt大于,-lt小于,-ge大于等于,-le小于等于
if [ $f1size -eq 37296000 ] && [ $f2size -eq 37296000 ]
then
    echo 1
fi

确保 Shell 脚本只有一个实例运行(如果重启服务器或者强制停止程序后需要把临时文件删除才能继续执行)

file=/tmp/glisten

if [ -e $file ]
then
    echo 'is running'
    exit 0
else
    touch $file
    echo 'touch file'
    python3 a.py
    rm -rf $file
    echo 'rm file'
fi

记录

Permission denied 没有权限

no space left on device 存储空间满了

Killed 内存不足,cpu运行不动了,程序断掉

Resource not find 未找到资源

安装python

mkdir -p /usr/local/python3
tar -zxvf Python-3.7.1.tgz
cd Python-3.7.1
./configure --prefix=/usr/local/python3 --with-ssl
make
make install

vi /etc/profile
PATH=/usr/local/python3/bin/:$PATH
source /etc/profile

检查Python3及pip3是否正常可用:
python3 -V
pip3 -V

(然后安装numpy、tensorflow、matplotlib、psycopg2、postgresql-devel等)

SLL error办法
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
然后重新 make & make install

python --version 2.7
/usr/bin/python的软连接控制默认python版本
ln -s /usr/local/python3/bin/python3 /usr/bin/python即可

samba

服务器挂载共享
yum install samba
mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
修改配置vi smb.conf ,只留第一个[global] ,下一个[data]是要共享的东西
在这里插入图片描述

设置专用用户
useradd smb_root
smbpasswd -a smb_root #然后输入两次密码就可

启动服务 service smb start
从win上资源管理器 \192.168.1.1 就能看到共享出来的文件夹

docker

docker images # 查看所有镜像
docker stop 镜像id # 停止对应dockerID的镜像
docker rm 镜像id # 删除镜像

docker pull centos # 创建镜像
docker commit 镜像id xx:xx # 完成镜像
docker save iid > ./xx.tar # 保存镜像到xx.tar

docker load < ./xx.tar # 导入镜像压缩包
docker run –d -t xx:xx /bin/bash # 启动容器
docker run -d -v linux目录1:docker目录1 -v linux目录2:docker目录2 -t 镜像id # 多目录挂载
(如果你共享的是多级的目录,可能会出现权限不足的提示。这是因为CentOS7中的安全模块selinux把权限禁掉了,我们需要添加参数 --privileged=true 来解决挂载的目录没有权限的问题。在 -v 目录:目录 后加–privileged=true)

docker ps –a # 查看所有己启动的镜像ID
docker exec -it 5108c94080bf /bin/bash # 进入docker镜像

docker version # 查看docker版本
docker inspect did | grep Mounts -A 20 # 查看docker挂载目录

ln -s /home/docker /var/lib/docker # 把docker镜像存储软连接到/home下

各版本docker下载: https://download.docker.com/linux/static/stable/x86_64/


docker修改默认存储路径(防止device存储不足的问题)
1、首先停止docker服务:systemctl stop docker.service
2、将当前docker存储目录复制到目标目录下:cp -r /var/lib/docker ./
3、修改docker.service配置文件,在EXECStart的后面增加 --graph=/home/docker(修改后的存储路径):vi /usr/lib/systemd/system/docker.service
4、执行下列命令然后重启docker:
systemctl daemon-reload
systemctl disable docker.service
systemctl enable docker.service
systemctl start docker.service
5、创建一个存储卷验证来检验修改是否生效
docker volume create vol-test
ll /home/docker/volumes/


git

两种方法上传,一种是git命令方法,一种是右键

右键
在某目录右键Git clone,进入clone的文件夹
把要上传的文件复制过来,TortoiseGit-add
然后右键Git Commit -> “master” , commit and push

命令
到需要上传的目录下,右键,git bash here
'# git clone xxx
git config --global user.name “zzz”
git config --global user.email “1625522381@qq.com”
git init
git remote add origin git@172.18.10.204:ai-team/h-group/h.git
git add .
git commit -m “提示消息”
git push -u origin master

1、
出错:
! [rejected] master -> master (fetch first) error: failed to push some refs to ’ 。。。’

出现这个问题是因为github中的README.md文件不在本地代码目录中,可以通过如下命令进行代码合并

git pull --rebase origin master
先update,再commit

git rm -r -f --cached ./ (删除缓存)

git remote rm origin (del remote)

$ git push -u origin master
The authenticity of host ‘172.18.10.204 (172.18.10.204)’ can’t be established.
ECDSA key fingerprint is SHA256:zjbLWvSIjew9dlkiaimoqBAPDvserW3KVbb7Bho1gak.
Are you sure you want to continue connecting (yes/no)? y
Please type ‘yes’ or ‘no’: yes
Warning: Permanently added ‘172.18.10.204’ (ECDSA) to the list of known hosts.
To 172.18.10.204:zzz/qwe.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘git@172.18.10.204:zzz/qwe.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.

$ git commit -m “end”
On branch master
nothing to commit, working tree clean

git clone 。。。

git add .
git commit -a -m m
git push

2、
错误:
warning: LF will be replaced by CRLF in FY4A/PAR/AGRIX/OCA/thresholds.dat.Terra.

The file will have its original line endings in your working directory
警告:LF将被FY4A/PAR/AGRIX/OCA/threshold . data . terra中的CRLF替换。
该文件将在工作目录中以其原始行结尾

出现上面那个警告的原因是:
  windows中的换行符为CRLF,而Linux下的换行符为LF(使用Git命令行Git Bash,实际上就是相当于linux环境),所以在执行git add xxx.xx操作时,会出现这个错误提示!

解决方法:(注意:会删仓库!)
  <1>删除.git 【rm -rf .git】
  <2>禁用自动转换,即将设置:git config --global core.autocrlf false
再重新初始化,并执行添加add操作

3、
错误:
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘http://172.18.10.204:10080/fy4-process/fy4-python-process.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.
!"[拒绝]主控->主控(先取)
错误:未能将一些参考资料推到“http://172.18.10.204:10080/fy4-process/ fy4-pythonprocess.git”
提示:更新被拒绝,因为远程包含您所做的工作
提示:没有本地。这通常是由另一个存储库推送引起的
提示:指向相同的ref。您可能想首先集成远程更改
提示:(例如,“git pull…”)然后再推。
提示:详情请参阅“git push -help”中的“关于快进的说明”。

git pull
:wq
git push
解决

git无法添加一个空的文件夹

创建新分支 默认分支为master
git branch newbranch
显示分支
git branch
切换分支
git checkout newbranch

4、
git clone 报“The project you were looking for could not be found.”
因为自己的项目不止一个 又有自动保存git密码的功能,当clone第二个项目的时候就报了如下错误

之前一直是找到钥匙串删除,发现有时候并没有效果

发现了一个新的解决办法
在项目前面加用户名

git clone https://rdc.hand-china.com/gitlab/项目.git
git clone https://用户名@rdc.hand-china.com/gitlab/项目.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值