The most commonly used commands for Linux (Ubuntu)

First put a mind map that contains Linux core commands:

Insert picture description here
The following article will separately explain the key commands and the commands not listed in the above figure.

1. File operation commands

The following are the main file directories in Linux:

/root:系统管理员的主文件夹
/home:用户主目录

/usr/local:用户自行安装的软件
/opt:放置第三方的软件

/etc:配置文件
/dev:任何设备与接口都以文件形式存放在此目录

/bin:单用户维护模式下还能够被操作的命令
/boot:开机所需文件——内核,开机菜单及所需配置文件等

/tmp:任何人均可使用的“共享”临时目录
/var:主要存放经常变化的文件,如日志

1.1 File read and write operations

1.1.1 读(cat/more/less/head/tail)

pwd // 当前位置
ls (-a/-l) // 当前目录下所有文件(夹)

cat (-n) a.txt | more  // 只读,-n行号,more分页

more a.txt // 分页读,一次性加载完
less a.txt // 分页度,逐页加载,适合读大文件

head (-n 5) a.txt // 首读,默认10
tail(-n 5) (-f) a.log // 尾读,默认 10,-f实时更新

1.1.2 写(vim/echo/>/>>)

vim:
yy / 5yy:复制5行
p:粘贴
dd:删除
G/gg:首/尾
a:撤销
/~~:查找~
set nu:行号

echo // 打印到控制台

> // 重定向:覆盖,可配合所有读操作(ls /a > a.txt)
>> // 重定向:追加

1.1.3 Execution

./ a.sh  // 前台执行

nohup command>out.file 2>&1 & // 后台执行
// nohup: no hang up 不挂起
// 2>&1: 标准错误与标准输出合并,合并到out.file
// &: 后台运行

ps -ef|grep 关键字|awk '{print %2}'|xargs kill -9 // 查找,进程号,并关闭

1.2 CRUD of files

1.2.1 增(mkdir/touch)

mkdir (-p) a/b // 创建目录,-p多级目录
touch a.txt // 创建空文件

1.2.2 Delete (rm)

rm (-r) (-f) a.txt // 删除,-r目录,-f强制删除

1.2.3 Change (mv/cp/unzip)

mv a.txt b.txt // 重命名
mv a.txt /root // 移动到指定目录

cp (-r) a.txt /root // 拷贝文件到指定目录,-r目录

tar -zcvf a.tar.gz  a.txt b.txt // 打包成tar.gz,将当前目录下的a.txt,b.txt打包成a.tar.gz
tar -zxvf a.tar.gz // 解压tar.gz

zip a.txt // 打包成a.zip
zip -r a.zip /a // 将a目录打包成a.zip
unzip (-d /a) a.zip // 解压zip,-d指定解压目录

gzip a.txt // 打包成a.gzip
gunzip a.gzip // 解药a.gzip

1.2.3 查(find/locate/grep/history)

find /a -name a.txt(*.txt) // 在/a目录下按名查找
find /a -user a // 在/a目录下按user查找
find /a -size +20M // 在/a目录下按文件大小查找

updatedb // 创建locate库
locate a.txt // 寻找指定文件,快

grep // 管道连接符

histroy (5) // 查看历史指令,5只显示近5个
!178 // 执行178号历史了命令

2. Application operation commands

2.1 Installation and uninstallation (apt-get/apt-cache)

apt-get is equivalent to the software steward of Ubuntu, and the software steward of CentOS is yum

apt-get update // 更新源
apt-get install (--reinstall) a // 安装,--reinstall重新安装
apt-get remove (--purage) a // 卸载,--purage包括配置文件
apt-get pgrade a // 更新已安装
apt-get source a // 下载源码包

apt-cache search a // 搜索安装包
apt-cache show a // 获取包相关信息

export JAVA_HOME=/root/jdk-XXX_linux-x64 
export PATH=$JAVA_HOME/bin:$PATH // 配置环境变量,只在当前会话有效,即退出命令行后就失效
source .bashrc // 可以通过编辑.bashrc实现永久配置

2.2 Operation management (systemctl/ufw)

systemctl status ~ // 查看指定程序状态
systemctl enable/disable ~ // 自启
systemctl start/restart/stop ~ // 开始/停止/重启

ufw stauts // 防火墙状态
ufw enable/disable //防火墙开关
ufw allow 22 (from ip) // 开放端口,from指定ip
ufw deny smtp // 禁止的服务

2.3 View (ps/kill/pstree/netstat)

ps 
-a:显示当前终端所有进程信息
-u:以用户格式显示
-x:显示后台运行参数
-e:所有进程
-f 全格式

kill (-9) a// 杀死指定进程,一般要指定杀死级别(默认15)
killall // 全部杀死

pstree

netstat -tlpn // 查看网络状态

2.4 System operation

shutdown (-h now/1) // 关机,现在/1min
halt // 立即关机
reboot // 重启
sync // 数据同步
设置运行级别
磁盘操作

3. Permission related commands

3.1 Command (useradd / pwd… / userdel / su / sudo)

useradd a
-m:同时创建用户目录
-d /home/a  a: 指定用户目录名
-g aa a:加入指定群组

passwd a // 设置密码

userdel (-r) a // 删除用户,-r删掉用户目录

id uid // 查看指定用户

su -xf a // 用户切换,exit退出,高(root)->低(普通用户)不用密码
sudo // 以root身份执行

Note: The created user information is in the /etc/passwd file

3.2 组(groupadd/groupdel/usermod)

groupadd
groupdel
usermod -g 用户组 用户名
usermod -d 目录 用户名 // 改变用户登录目录

3.3 File (chown/chgrp/chmod)

chown (-r) 用户名 文件名 // 更改所有者

chgrp (-r) 组名 文件名 // 更改组

chmod u=rwx g=rx o=r // 更改权限 u=所有者,g=同组,o=other
chmod 7 5 1 // r=4,w=2,x=1

Guess you like

Origin blog.csdn.net/weixin_43935927/article/details/108578143