Linux常用命令和用法

ls - 列出目录内容

参数:
-l:以详细列表格式显示文件和目录信息
-a:显示所有文件,包括隐藏文件
-h:以人类可读的格式显示文件大小
示例用法:ls -l /path/to/directory
cd - 切换目录

参数:无
示例用法:cd /path/to/directory
pwd - 显示当前工作目录

参数:无
示例用法:pwd
mkdir - 创建目录

参数:无
示例用法:mkdir /path/to/directory
rm - 删除文件或目录

参数:
-r:递归删除目录及其内容
-f:强制删除,不提示确认
示例用法:rm -rf /path/to/file_or_directory
cp - 复制文件或目录

参数:
-r:递归复制目录及其内容
-f:覆盖已存在的目标文件
示例用法:cp -r /path/to/source /path/to/destination
mv - 移动文件或目录

参数:
-f:覆盖已存在的目标文件
示例用法:mv /path/to/source /path/to/destination
touch - 创建空白文件

参数:无
示例用法:touch file.txt
cat - 显示文件内容

参数:无
示例用法:cat file.txt
grep - 搜索文件内容

参数:
-i:忽略大小写
-r:递归搜索目录及其子目录下的文件
示例用法:grep -i "pattern" file.txt
find - 查找文件

参数:
-name:按文件名进行搜索
-type:按文件类型进行搜索
示例用法:find /path/to/directory -name "*.txt"
chmod - 修改文件权限

参数:权限模式(如:u+x、g-w)
示例用法:chmod u+x file.sh
chown - 修改文件所有者

参数:新的所有者用户名
示例用法:chown user file.txt
tar - 打包或解压文件

参数:
-c:创建新的tar文件
-x:解压tar文件
-v:显示详细输出
-f:指定文件名
示例用法:
tar -cvf archive.tar file1 file2
tar -xvf archive.tar
ssh - 远程登录到其他计算机

参数:远程主机的用户名和IP地址/域名
示例用法:ssh user@remotehost
scp - 远程复制文件

参数:
-P:指定SSH端口号
示例用法:
scp file.txt user@remotehost:/path/to/destination
scp -P port file.txt user@remotehost:/path/to/destination
ping - 测试网络连接

参数:远程主机的IP地址/域名
示例用法:ping google.com
ifconfig - 显示或配置网络接口

参数:
-a:显示所有网络接口的详细信息
示例用法:
ifconfig
ifconfig -a


top - 查看系统资源使用情况

参数:
-d:指定刷新时间间隔
-u:只显示指定用户的进程
示例用法:top
ps - 显示进程信息

参数:
-e:显示所有进程
-u:显示指定用户的进程
-f:显示完整的进程信息
示例用法:ps -ef
df - 查看磁盘空间使用情况

参数:
-h:以人类可读的格式显示磁盘空间大小
-T:显示文件系统类型
示例用法:df -h
du - 查看文件或目录的磁盘使用情况

参数:
-h:以人类可读的格式显示磁盘空间大小
-s:只显示总计大小
示例用法:du -sh /path/to/file_or_directory
head - 显示文件的前几行内容

参数:
-n:指定要显示的行数
示例用法:head -n 10 file.txt
tail - 显示文件的后几行内容

参数:
-n:指定要显示的行数
-f:实时追踪文件内容变化
示例用法:tail -n 10 file.txt
less - 分页显示文件内容

参数:无
示例用法:less file.txt
wc - 统计文件的行数、字数和字符数

参数:
-l:只统计行数
-w:只统计字数
-c:只统计字符数
示例用法:wc -l file.txt
diff - 比较两个文件的差异

参数:无
示例用法:diff file1.txt file2.txt
patch - 应用补丁文件

参数:无
示例用法:patch < patchfile
tar -cvf - 打包并压缩文件

参数:
-z:使用gzip进行压缩
-j:使用bzip2进行压缩
示例用法:
tar -czvf archive.tar.gz file1 file2
tar -cjvf archive.tar.bz2 file1 file2
tar -xvf - 解压并解压缩文件

参数:
-z:使用gzip进行解压
-j:使用bzip2进行解压
示例用法:
tar -xzvf archive.tar.gz
tar -xjvf archive.tar.bz2
gzip - 压缩文件

参数:无
示例用法:gzip file.txt
gunzip - 解压缩文件

参数:无
示例用法:gunzip file.txt.gz
bzip2 - 压缩文件

参数:无
示例用法:bzip2 file.txt
bunzip2 - 解压缩文件

参数:无
示例用法:bunzip2 file.txt.bz2
zip - 压缩文件

参数:
-r:递归压缩目录及其内容
-q:静默模式,不显示输出
示例用法:zip -r archive.zip file1 file2
unzip - 解压缩文件

参数:无
示例用法:unzip archive.zip



ssh-keygen - 生成SSH密钥对

参数:
-t:指定密钥类型(如rsa、dsa、ecdsa、ed25519)
-b:指定密钥位数
-f:指定密钥文件名
示例用法:ssh-keygen -t rsa -b 2048 -f mykey
ssh-copy-id - 将SSH公钥复制到远程主机上

参数:
-i:指定公钥文件
-p:指定远程SSH端口号
示例用法:ssh-copy-id -i ~/.ssh/id_rsa.pub user@remotehost
scp -P - 指定SSH端口号复制文件

参数:
-P:指定SSH端口号
示例用法:scp -P 22 file.txt user@remotehost:/path/to/destination
sed - 替换文件中指定的文本

参数:
s/old/new/:将文件中的"old"替换为"new"
示例用法:sed 's/foo/bar/g' file.txt
awk - 处理文本文件并生成报告

参数:
-F:指定字段分隔符
'{pattern action}':根据指定的模式和动作进行处理
示例用法:awk -F, '{print $1}' file.csv
nl - 显示文件的行号

参数:
-b:指定行号显示方式(a为显示非空行,t为显示所有行)
示例用法:nl -b a file.txt
head -n - 显示文件的前N行

参数:
-n:指定要显示的行数
示例用法:head -n 10 file.txt
tail -n - 显示文件的后N行

参数:
-n:指定要显示的行数
示例用法:tail -n 10 file.txt
sort - 排序文件内容

参数:
-k:指定排序字段
-n:按数值进行排序
示例用法:sort -k 2n file.txt
uniq - 去除文件中的重复行

参数:
-c:显示重复行出现的次数
-d:只显示重复行
示例用法:uniq -d file.txt
cut - 从文件中提取指定字段
参数:
-f:指定要提取的字段
-d:指定字段分隔符
示例用法:cut -f 1,3 -d , file.csv
paste - 将文件合并成一列
参数:无
示例用法:paste file1.txt file2.txt
tee - 将输出复制到文件和屏幕上
参数:
-a:追加到文件末尾
示例用法:command | tee -a file.txt
diff -u - 以统一格式显示两个文件的差异
参数:无
示例用法:diff -u file1.txt file2.txt
tar -tvf - 显示打包文件的内容
参数:无
示例用法:tar -tvf archive.tar
tar -xzf - 解压缩压缩文件
参数:无
示例用法:tar -xzf archive.tar.gz
tar -czf - 打包并使用Gzip压缩文件
参数:无
示例用法:tar -czf archive.tar.gz file1 file2
tar -xzf - 解压缩Gzip压缩文件
参数:无
示例用法:tar -xzf archive.tar.gz
tar -cjf - 打包并使用Bzip2压缩文件
参数:无
示例用法:tar -cjf archive.tar.bz2 file1 file2
tar -xjf - 解压缩Bzip2压缩文件
参数:无
示例用法:tar -xjf archive.tar.bz2
nmap - 扫描网络上的主机和服务
参数:
-p:指定要扫描的端口号
-A:执行操作系统和服务版本检测
示例用法:nmap -p 80,443 -A target
tcpdump - 抓取网络数据包
参数:
-i:指定要监听的网络接口
expression:指定过滤表达式
示例用法:tcpdump -i eth0 port 80



ping -c - 发送固定数量的ICMP回显请求

参数:
-c:指定要发送的回显请求数量
示例用法:ping -c 5 google.com
dig - 查询DNS记录

参数:
@server:指定要查询的DNS服务器
type:指定要查询的记录类型(如A、CNAME、MX等)
示例用法:dig @8.8.8.8 google.com
nslookup - 查询DNS记录

参数:无
示例用法:nslookup google.com
host - 显示主机名

参数:无
示例用法:host google.com
whois - 查询域名的注册信息

参数:无
示例用法:whois google.com
ifconfig -a - 显示所有网络接口的详细信息

参数:无
示例用法:ifconfig -a
route - 显示和修改网络路由表

参数:无
示例用法:route
netstat - 显示网络连接信息

参数:
-t:显示TCP连接
-u:显示UDP连接
-n:以数字形式显示IP地址和端口号
示例用法:netstat -tun
lsof - 显示已打开的文件和进程

参数:无
示例用法:lsof
ps -ef - 显示所有进程信息

参数:无
示例用法:ps -ef
kill - 终止进程

参数:
pid:要终止的进程ID
示例用法:kill 1234
top -n - 显示系统资源使用情况(设置更新间隔)
参数:
-n:设置更新间隔(单位为秒)
示例用法:top -n 5
free - 显示系统内存使用情况

参数:无
示例用法:free
uptime - 显示系统运行时间和负载平均值

参数:无
示例用法:uptime
uname -a - 显示系统信息

参数:无
示例用法:uname -a
dmesg - 显示系统启动时的日志信息

参数:无
示例用法:dmesg
last - 显示最近登录用户信息

参数:无
示例用法:last
who - 显示当前登录用户信息

参数:无
示例用法:who
crontab -e - 编辑当前用户的cron任务

参数:无
示例用法:crontab -e
date - 显示或设置系统时间和日期

参数:
MMDDhhmmYYYY:设置时间和日期(月日时分年)
示例用法:date 012019002024


cal - 显示日历

参数:
-y:显示整年的日历
-3:显示本月、上月和下月的日历
示例用法:
cal
cal -y
cal -3
shutdown - 关闭系统

参数:
-h:关闭系统并停止电源
-r:重启系统
+m:在指定分钟数之后关闭系统
示例用法:
shutdown now
shutdown -h now
shutdown -r 10
reboot - 重新启动系统

参数:无
示例用法:reboot
halt - 关闭系统

参数:无
示例用法:halt
init 0 - 关闭系统

参数:无
示例用法:init 0
init 6 - 重新启动系统

参数:无
示例用法:init 6
useradd - 创建新用户账户

参数:
-m:创建用户家目录
示例用法:
useradd username
useradd -m username
usermod - 修改用户账户信息

参数:
-c:修改用户描述信息
-d:修改用户家目录
示例用法:
usermod -c "new description" username
usermod -d /new/home/directory username
passwd - 修改用户密码

参数:无
示例用法:passwd username
groupadd - 创建新用户组

参数:无
示例用法:groupadd groupname
groupmod - 修改用户组信息

参数:无
示例用法:groupmod groupname
chage - 修改用户账户密码过期时间

参数:
-d:设置最近一次修改密码的日期
-E:设置账户的失效日期
示例用法:
chage -d 2024-01-20 username
chage -E 2025-01-20 username
su - 切换到其他用户账户

参数:无
示例用法:su username
sudo - 以超级用户身份执行命令

参数:无
示例用法:sudo command
chroot - 更改根目录

参数:无
示例用法:chroot /new/root/directory command
ulimit - 显示或设置系统资源限制

参数:
-a:显示所有资源限制
-n:设置最大打开文件描述符数
示例用法:
ulimit -a
ulimit -n 1024
man - 显示命令的帮助信息

参数:无
示例用法:man command
info - 显示命令的详细信息

参数:无
示例用法:info command
which - 显示某个命令的完整路径名

参数:无
示例用法:which command
whereis - 搜索某个命令的二进制文件

参数:无
示例用法:whereis command
locate - 快速搜索文件

参数:
-i:忽略大小写
示例用法:
locate filename
locate -i filename
history - 显示最近使用的命令历史记录
参数:
-c:清空历史记录
示例用法:
history
history -c

你可能感兴趣的:(linux,运维,服务器)