Linux常用命令汇总

整理如下,随时补充更新:

Linux系统命令

内存相关

# 查看内存占用最高的进程
ps aux --sort=-%mem | head
ps aux --sort=-%cpu | head

top,按照M按照内存排序,按下P按照CPU排序,按下E转换为KB/MB/GB

文件相关

# 查看当前磁盘占用
df -h

# 查看当前目录大小
du -sh

# 查看当前目录下第一层文件夹的大小,按照由大到小排序
du -sh * | sort -rh (文件过多的话,可以加 | less)

# 同上,但可以指定层级
du -lh --max-depth=1 | sort -rh

# 查看进程打开的文件
lsof | grep xxx (记得加上grep,否则返回的数据会很多)

shell: 创建指定年份的日志目录并修改权限

# 生成日志的目录,并修改所属人为www用户

if [ ! -n "$1" ] ;then
    echo "需要传入参数1:日志路径,例如:/home/logs/project_logs/"
    exit 2
else
    logPath=$1
fi

if [ ! -n "$2" ] ;then
    echo "需要传入参数2:当前年份,例如:2024"
    exit 2
else
    year=$2
fi

#echo $logPath

logPathYear=$logPath$year
if [ -d $logPathYear ]; then
        echo "==== 年份目录已存在 ===="
    else
        mkdir -p $logPathYear
        chmod -R 777 $logPathYear
        chown www:www $logPathYear
        echo "==== 年份目录已创建 ===="
    fi

for (( i=1;i<=12;i=i+1 ))
  do
    if [ $i -lt 10 ]; then
      month="0"$i;
    else
      month=$i;
    fi

    logPathMonth=$logPathYear"/"$month
    echo $logPathMonth
    if [ -d $logPathMonth ]; then
        echo "目录已存在,跳过..."
    else
        mkdir -p $logPathMonth
        chmod -R 777 $logPathMonth
        chown www:www $logPathMonth
        echo "目录创建成功"
    fi
  done

在这里插入图片描述

shell: 创建今天的日志文件,并加入到定时任务

# 日志所在目录:/home/logs/project_logs/2024/03/18
month=`date +%Y/%m`
cd /home/logs/project_logs/${month}

today=`date +%Y%m%d`

user=www #要修改的文件所属用户名
group=www #要修改的文件所属用户组

# 如下定义多个文件名
file1=${today}_api.log
file2=${today}_command.log
file3=${today}_job.log

# 循环处理每个文件名
for var in ${file1} ${file2} ${file3}
do
    touch ${var} 
    chmod 777 ${var}
    chown ${user}:${group} ${var}
done

# 加入定时任务,每天00:00 执行 | crontab -e
# 0 0 * * * sh /root/touch_today_log_file.sh

Git

# 查看全局配置和远程仓库地址
git config --global --list 
git remote -v

# 修改全局配置
git config --global user.name "renxing" 
git config --global user.email "renxing@qq.com"
git config --global alias.s status # 起个别名,之后可以用 git s 代替 git status,下同
git config --global alias.c checkout
git config --global alias.d diff
git config --global alias.p pull

# 或者在 ~/.gitconfig 文件中写入如下内容:
[user]
	name = renxing
	email = renxing@qq.com
[alias]
	s = status
	c = checkout
	d = diff
	p = pull
	b = branch
[core]
	quotepath = false
	autocrlf = input
	whitespace = cr-at-eol

Git常用的shell脚本

# git提交代码,并直接合并到 test 分支 的shell脚本
# 运行方式: sh git-push-merge-test.sh 提交代码的备注信息

#提交本地代码
if [ ! -n "$1" ] ;then
   mark="修改"
else
   mark=$1
fi

git pull
git add .
git commit -m "$mark"
git push

#获取当前分支名称
now_branch=$(git branch --show-current)
echo $now_branch

#切换到test分支,然后合并当前分支,push后,再次回到当前分支
git checkout test
git pull
git merge $now_branch
git push
git checkout $now_branch

快捷操作

  • 快速进入到某个目录,新建shell脚本 cd_folder.sh
# 执行方式: . ./cd_folder.sh, 注意!注意!前面多一个 . 和一个空格
cd /home/www/data
  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮尘笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值