linux命令

简介: 本文整理了Linux系统常用命令,涵盖系统版本、CPU、内存信息查看,及文件操作、文本处理(grep/sed/awk)、压缩解压、进程后台运行等实用技巧,包含别名、通配符、重定向、正则表达式及nohup用法,适合日常运维与自动化脚本编写,提升效率。

总结表格

类别 常用命令 主要用途
系统版本 lsb_release -a 显示发行版信息(推荐)
cat /etc/os-release 查看标准系统信息文件
hostnamectl 查看系统d系统信息(简洁)
uname -a 查看内核版本和架构
CPU lscpu 查看CPU信息(最常用)
cat /proc/cpuinfo 查看详细的CPU原始信息
内存 free -h 查看内存使用情况(最常用)
cat /proc/meminfo 查看详细的内存原始信息
综合工具 lshw 列出详细的硬件配置(需root)
inxi -Fxz 功能全面的系统信息脚本(推荐安装)
dmidecode 查询底层硬件信息(需root)

1

alias unalias

{a..z} {1..100..2}

ctrl+a e u k l

history !! !d !ech

基本正则:^$.[]*

扩展正则:(){}?+|等字符

nohup 2>&1 &用法

nohup python my.py >> /usr/local/python/xxf/my.log 2>&1 &

nohup命令
  1. nohup指不断地运行,是no hang up的缩写,指不间断,不挂断。运行一个进程的时候,不想让其在你退出账号时关闭,即可用nohup。
  2. nohup在不规定的情况下,所以输出内容会到nohup.out中
2>&1
  1. 0 表示stdin标准输入,用户键盘输入的内容
    1 表示stdout标准输出,输出到显示屏的内容
    2 表示stderr标准错误,报错内容
  2. 2>&1是一个整体,>左右不能有空格,即将错误内容重定向输入到标准输出中去。
&

为后台运行

echo

echo -n hello  # 打印后不换行
echo $? # 上次的命令的执行结果,0表示成功 非零值表示失败
echo $JAVA_HOME
echo '$NAME'  # 打印$NAME串
echo "$NAME"  # 这个也会解析NAME变量
echo -e "hello\nworld"

mkdir

mkdir dir1 dir2 
mkdir -p dir/dir1
mkdir -m 700 dir6

cp

cp -v test.txt testdir  # -v打印复制细节
cp -r testdir testdir2  # 复制目录
cp -i test1.txt test2.txt # 覆盖前询问
cp -f test1 test2  # 目标文件已存在,但是无法打开时,删除源文件后重新复制

find

find . -name "*.txt"
find . -iname "*.txt"
find . -type f     
find . -type l 
find . -type d
find . -mmin +1  # 1分钟之前编辑的文件
find . -mmin -1  # 1分钟之内编辑的文件
find . -mtime +1  # 1天之前编辑的文件
find . -mtime -1  # 1天之内编辑的文件
find . -user user1 # user1用户有哪些文件
find . -group root # root组有哪些文件

cat

cat -n test1.txt  # 显示行号
cat -b test1.txt  # 只在非空行前面加行号
cat -s test1.txt  # 多个空行合并为1个空行
cat -E test1.txt  # 对每行末尾增加$表示结尾
cat -T test1.txt  # 打印tab键替换为 ^I
cat -A test1.txt  # 相当于-E和-T一起
cat test1.txt test2.txt # 打开两个文件(内容连接到一起显示)

cat file1 file2 file3 > files
cat > newfile.txt # 编辑文件,回车换行,ctrl+D结束输入
cat >> newfile.txt # 追加
cat file1.txt |wc -l 统计文件有多少行

rm

rm -r testdir1    # 删除文件夹
rm -i -r testdir1 # 删除前询问

wc

wc test1.txt
2 3 18 test1.txt # 表示test1.txt有2行,3个单词,18个字节

wc -l test1.txt # 表示只打印test1.txt的行数
wc -w test1.txt
wc -c test1.txt # 字节数
wc -m test1.txt # 字符数

tar

tar -cf testdir.tar testdir/  # c表示创建压缩文件,f表示指定tar文件名词
tar -cf test.tar test1 test2  # f必须紧跟归档文件名
tar -cf myfiles.tar *.txt     # 
tar -xf myfiles.tar           # 解压tar
tar -zcf test.tar.gz testdir  # 创建时用gzip压缩
tar -zxvf test.tar.gz testdir # 解压

tail

tail testfile1.txt
tail -n 5 test.txt
tail -n +5 test.txt
tail -f test.txt 
tail -F test.txt

grep

grep hello test.txt
grep -i hello test.txt  忽略大小写
grep -w hello test.txt 全词匹配
grep -e hello -e world  test.txt  查找多个
grep -n hello test.txt 打印出行数
grep -v hello test.txt 反向查找(查找不包含的)
grep -r hello testdir/  指定目录下的
grep -lr hello testdir/ 返回查找到的文件名(-l)
grep -E ‘hello|world’ test.txt 正则表达式 (打印出hello或world的)
find / -name aaa |xargs grep bbb
grep "^10" test.txt
grep "11$" test.txt

sed

sed -e '1i\a new line' test1.txt  # 文本第一行增加a new line,其中-e可以省略
sed -ie '1i\a new line' test1.txt # -ie会出现test1.txte备份文件
sed -i '1i\a new line' test1.txt # 不会新增备份文件
sed -e '4a\line' test1.txt
sed -e '4d' test1.txt # 删除
sed -e '1c\line' test1.txt # 修改
sed -e 's/old/new/' test1.txt # 所有行的第一个old都替换成new
sed -e '2s/old/new' test1.txt # 第二行的第一个old替换成new
sed -e '2s/old/new/g' test1.txt # 第二行所有的old都替换成new

sed -n '1p' test1.txt  # 打印第一行
sed -f test.sh test1.txt # 从test.sh中读取参数
相关文章
|
4天前
|
数据采集 人工智能 安全
|
13天前
|
云安全 监控 安全
|
5天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1102 152
|
18天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1780 9
|
10天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
708 152
|
12天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
664 14
|
7天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
467 5