您的位置:首页 > 运维架构 > Linux

linux常用命令总结-第一次考试

2017-01-16 12:53 495 查看
1.1 mkdir创建目录
语法:cat [选项] [文件名]
选项:-p 多层目录创建
-v 每次创建新目录都显示信息
范例:1.创建多层嵌套目录
[root@ns2 tmp]# mkdir -pv /tmp/test/file/new
mkdir: 已创建目录 "/tmp/test"
mkdir: 已创建目录 "/tmp/test/file"
mkdir: 已创建目录 "/tmp/test/file/new"
2.创建多个平行目录
[root@ns2 tmp]# mkdir trat{1..9} 创建trat1到trat9
[root@ns2 tmp]# ls
trat1 trat2 trat3 trat4 trat5 trat6 trat7 trat8 trat9
注:{0..9} 表示 0-9 也可以是{a..z}或{A..Z}
1.2 ls:列出目录内容,包括文件和子目录的名称
语法:ls [选项] [文件或目录名称]
选项:-a 显示所有子目录与文件信息,包括以.和..开头的隐藏文件
-d 显示目录本身的属性
-l 使用较长格式列出详细信息
-R 递归处理,将指定目录下的所有文件及子目录一并处理。
选项可合并
范例:[root@oldboy34 ~]# ls -R /etc
[root@oldboy34 ~]# ls -l /bin
[root@oldboy34 ~]# ls -d /root
1.3 cd 切换目录
语法:cd [目的目录]
cd .. 返回上一级目录
cd ../.. 返回上两级目录
cd 或 cd~ 进入个人的主目录
cd - 返回上次所在的目录
[root@oldboy34 ~]# cd ..
[root@oldboy34 /]# cd /etc/sysconfig/network-scripts/
[root@oldboy34 network-scripts]# cd ../..
[root@oldboy34 etc]# cd -
/etc/sysconfig/network-scripts
[root@oldboy34 network-scripts]# cd
[root@oldboy34 ~]#
1.4 pwd 显示当前工作目录
语法:pwd
[root@oldboy34 ~]# pwd
/root
1.5 touch 创建一个新文件
语法:touch [文件名称]
[root@oldboy34 ~]# touch /data/oldboy.txt
[root@oldboy34 ~]# ls /data/
oldboy.txt
[root@ns2 tmp]# touch dir{0..9} 创建多个文件
[root@ns2 tmp]# ls
dir0 dir1 dir2 dir3 dir4 dir5 dir6 dir7 dir8 dir9
1.6 echo将输入的字符串送往标准输出
语法:echo [选项] [输出内容] [输出文件]
[root@oldboy34 ~]# echo 'I am studying linux' > /data/oldboy.txt
[root@oldboy34 tmp]# echo '111 插入多行内容
> 222
> 333
> 444' > oldboy.txt
1.7 重定向符号
">" 重定向 会清除文件以前的数据,增加新内容
">>"追加重定向,文件结尾加入内容,不会删除已有文件内容
"<" 输入重定向
"<<" 追加输入重定向
箭头方向就是数据流向
1.8 xargs 分组显示
选项:-n 多行显示
[root@oldbay34 ~]# xargs -n5 <tao.txt
1 2 3 4 5
6 7 8 9 10
1.9 man手册 查看命令帮助
语法:man [命令]
[root@oldbay34 ~] man find
[root@oldbay34 ~]# ls --help 查询帮助
1.10 cat 查看文件内容
语法:cat [选项] [文件名]
选项:-n 对所有输出的行数编号
[root@oldboy34 data]# cat oldboy.txt
I am studying linux
[root@oldbay34 tmp]# cat -n oldboy.txt 对所有输出的行数编号
1 111
2 222
3 333
特殊用法:写入文件内容
cat>>/tmp/test.txt<<stop
> I like linux 输入内容
> stop 结束语 (可以为任意字符) 但要成对出现
1.11 cp 复制文件
语法:cp [选项] [源对象] [目标对象]
选项: -a 复制所有源对象
-p 复制的文件属性信息不变
-r 递归复制目录
[root@oldboy34 data]# cp oldboy.txt /tmp
[root@oldboy34 data]# ls /tmp
oldboy.txt
[root@oldbay34 tmp]# cp -r /etc/ /tmp 递归复制/etc目录到/tmp
备份:原地复制一个文件加上一个后面加一个.bak
[root@oldbay34 ~]# cp test.txt test.txt.bak
多用于备份
1.12 mv 将源文件移动至指定目录
语法: mv [选项] [源对象] [目标对象]
选项:-b 若需覆盖文件,则覆盖前先行备份
-f 覆盖前不询问
-i 覆盖前询问
-n 不覆盖已存在文件
[root@oldboy34 ~]# mv /data/ /root
[root@oldboy34 ~]# ls
anaconda-ks.cfg data install.log install.log.syslog
/data ===> /data及/data下面的所以内容
/data/ ===> /data 下面的所有内容,不包括/data目录本身
1.13 rm 删除指定文件或目录
语法:rm [选项] [文件或目录]
选项: -f 强制删除 ==> 很危险
-r 递归删除
注意:操作前要备份,防止误删。离开电脑一定要锁屏!
[root@oldboy34 ~]# cd /root/data/
[root@oldboy34 data]# rm oldboy.txt
rm: remove regular file `oldboy.txt'? y
[root@oldbay34 ~]# rm -rf data/ 强制删除目录及目录下的所有内容
1.14 grep 查找文件中符合条件的字符串的行
语法:grep [选项] [查找条件] [目标文件]
选项:-A 除了显示符合条件的那一行之外,并显示该行之后n行的内容。
-B 除了显示符合条件的那一行之外,并显示该行之前n行的内容。
-c 计算符合范本样式的列数。
-C 除了显示符合条件的那一列之外,并显示该列之前后n行的内容
-n 显示关键词所在行的行号
-v 反向查找 显示不包括关键词的行
补充说明: “^......”表示以 ……开头 “......$”表示以……结尾 “^$” 表示空行
[root@oldbay34 ~]# grep -v "^#" /etc/yum.conf | grep -v "^$" 过滤/etc/yum.conf 中的注释行和空行
[root@oldbay34 oldboy]# grep 20 -A 10 test.txt 先输出第20行 再输出后10行
[root@oldbay34 oldboy]# grep 30 -B 10 test.txt 先输出第30行 再输出前10行
[root@oldbay34 oldboy]# grep 25 -C 5 test.txt 先输出第25行 再输出前5行 后5行
1.15 head 查看文件的头部(默认前10行)
语法:head [选项] [文件]
选项:-<数字> 指定显示的行数
[root@oldbay34 ~]# head -5 man.txt
1.16 tail 显示文件的后几行 (默认10行)
语法:tail [选项] [文件]
选项:-[数字] 指定显示的行数
-f 动态跟踪文件内容 经常用来查看日志文件的最新内容
[root@ns2 tmp]# tail -5 man.txt
[root@ns2 tmp]# tail -f /var/log/messages 动态查看日志文件最新内容
1.17 which 查找命令的绝对路径
语法:which [命令]
[root@oldbay34 ~]# which cp
alias cp='cp -i'
/bin/cp
[root@oldbay34 ~]# which find
/bin/find
1.18 find 查找文件或目录,任何位于参数之前的字符串都将被视为欲查找的目录
语法:find [查找范围] [查找条件]
选项:-type[文件类型] 只寻找符合指定的文件类型的文件
{ f 普通文件 ,d 目录,l 符号连接,b 块设备,c 字符设备,p 管道文件,s 套接字 }
-name[范本样式] 指定字符串作为寻找文件或目录的条件
[root@oldbay34 ~]# find . 列出当前目录及子目录下所有文件和文件夹
[root@oldboy34 ~]# find /root/oldboy/ -type f -name "*.sh"
[root@oldbay34 ~]# find /home -name "*.txt" 在/home目录下查找以.txt结尾的文件名
注意:通配符 “ * ” 代表零个或多个字符 匹配所有
1.19 sed 用来自动编辑一个或多个文件;简化对文件的反复操作
语法:sed [选项] [编辑指令] [文件1…文件2…]
选项:-e 指定要执行的命令 只有一个编辑指令时可省略
-n 只输出处理后的行 不显示默认输出
-i 直接编辑文件,而不输出结果
编辑指令的格式:[操作范围] [操作] [参数]
操作范围:可以是数字,正则表达式 ,没有地址代表是所有行
d 删除,删除选择的行。
s 替换指定字符
g 表示行内全部替换
p 打印模板块的行
[root@oldbay34 ~]# sed -n 'p' test.txt 输出所有内容,等同于 cat test.txt
[root@oldbay34 ~]# sed -n '3p' test.txt 输出第 3 行
[root@oldbay34 ~]# sed -n '3,5p' test.txt 输出第 3~5 行
[root@oldbay34 ~]# sed -n '/apple/p' test.txt 输出包含apple的行
[root@oldbay34 ~]# sed '3d' test.txt 删除第 3 行
[root@oldbay34 ~]# sed '3,5d' test.txt 删除3~5行
[root@oldbay34 ~]# sed '/apple/d' test.txt 删除包含apple的行
[root@oldbay34 ~]# sed 's/猫/狮子/' test.txt 将每行中的第一个猫替换为狮子
替换操作中分隔符/可改为#等其他字符
[root@oldbay34 ~]# sed 's/猫/狮子/g' test.txt 将文件中所有的猫替换为狮子

1.20 alias 查看,修改别名
作用:1.给危险命令添加保护,防止误操作
2.把复杂的字符串或命令变为简单的字符串或命令。简化命令
unalias 取消别名
root@oldbay34 ~] alias net='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
alias 小名=命令 设置别名

将修改写入配置文件/etc/profile让其永久生效 vim /etc/profile
让配置生效 source /etc/profile
配置rm/mv/cp命令的别名需修改/root/.bashrc
vim /root/.bashrc
#把rm 别名注释即可

1.21 awk linux 三剑客老大 擅长取列
选项:NR 表示行号
[root@www ~]# awk 'NR==20,NR==30' ett.txt NR 表示行号
[root@www ~]# awk 'NR>=20&&NR<=30' ett.txt && 并且同时成立

第2章 网络管理
2.1 ifconfig
功能:配置和显示Linux内核中网络接口的网络参数
语法:ifconfig [参数]
[root@oldbay34 ~]# ifconfig 查看网卡
eth0 Link encap:Ethernet HWaddr 00:0C:29:A0:41:FD
inet addr:192.168.49.128 Bcast:192.168.49.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fea0:41fd/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:164996 errors:0 dropped:0 overruns:0 frame:0
TX packets:45364 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:84545164 (80.6 MiB) TX bytes:4199739 (4.0 MiB)

[root@oldbay34 ~]# ifconfig eth0 192.168.10.44/24 临时修改eth0的IP地址

2.2 ifup 启动网卡
语法:ifup [网卡]
[root@oldbay34 ~]# ifup eth0 启动eth0网卡
2.3 ifdown 关闭网卡
语法:ifdown [网卡]
[root@oldbay34 ~]# ifdown eth0 关闭eth0网卡
[root@oldbay34 ~]# ifdown eth0 && ifup eth0 #重启网卡
2.4 ping 检查网络连通性
语法:ping [参数] [IP地址或域名]
[root@oldbay34 ~]# ping www.baidu.com ping 域名
PING www.a.shifen.com (111.206.223.205) 56(84) bytes of data.
64 bytes from 111.206.223.205: icmp_seq=1 ttl=128 time=5.79 ms
64 bytes from 111.206.223.205: icmp_seq=2 ttl=128 time=7.24 ms
[root@oldbay34 ~]# ping 192.168.49.128 ping IP
PING 192.168.49.128 (192.168.49.128) 56(84) bytes of data.
64 bytes from 192.168.49.128: icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from 192.168.49.128: icmp_seq=2 ttl=64 time=0.076 msv

2.5 netstat 查看网络状态
语法:netstat [选项]
选项:-a 显示当前主机中所有活动的网络连接信息
-n 以数字形式显示
-t 查看TCP协议的相关信息
-u查看UDP协议的相关信息
-p 显示与网络连接相关联的进程号
-l 显示监听端口
通常使用 -lanpt 组合选项
[root@oldbay34 ~]# netstat -lanpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1311/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1392/master
tcp 0 0 192.168.49.128:22 192.168.49.1:55060 ESTABLISHED 33945/ssh
查看ssh 进程
[root@oldbay34 oldboy]# netstat -lntup|grep sshd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1311/sshd
查看端口
[root@oldbay34 ~]# netstat -lanpt | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1311/sshd
tcp 0 64 192.168.49.128:22 192.168.49.1:55060 ESTABLISHED 33945/sshd
tcp 0 0 :::22 :::* LISTEN 1311/sshd

2.6 telnet用于登录远程主机,对远程主机进行管理
语法:telnet [选项]
[e:\~]$ telnet 192.168.49.128 无密码登陆主机 安全性差
第3章 系统管理
3.1 uname 查看系统信息
语法:uname [选项]
选项:-a 显示系统全部的信息
-m 显示机器的处理器架构
-r 显示正在使用的内核版本
[root@oldbay34 ~]# uname -r
2.6.32-642.el6.x86_64
[root@oldbay34 ~]# uname -a
Linux oldbay34 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@oldbay34 ~]# uname -m
x86_64

3.2 date 显示系统日期
[root@oldbay34 ~]# date
Mon Dec 26 04:59:53 CST 2016

3.3 usersdd创建普通用户
语法: useradd [选项] [用户名]
[root@oldboy34-moban ~]# useradd oldboy 创建用户

3.4 passwd 设置密码
语法:passwd [用户]
[root@oldboy34-moban ~]# passwd oldboy 交互式设置密码
Changing password for user oldboy.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.

3.5 su 切换用户
[root@oldbay34 ~]# su - oldboy #由root管理员,切换到oldboy
[oldboy@oldbay34 ~]$ whoami #查看当前用户
oldboy
[oldboy@oldbay34 ~]$ su - root 切回root管理员
Password:
3.6 source 使配置文件生效
语法:source [配置文件]
[root@oldbay34 ~]# source /etc/profile

3.7 whoami 我是谁 (显示当前用户名)
[root@oldbay34 ~]# whoami
root

3.8 init 切换运行级别
语法:init [运行级别]
运行级别:
cat /etc/inittab
# Default runlevel. The runlevels used are:
# 0 - halt (Do NOT set initdefault to this)----关机,不要设置默认的级别为0
# 1 - Single user mode-------单用户模式
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)---Multiuser(多用户),如果不需要网络,且不需要NFS
NFS(网络文件系统(Network File System)),它同运行级别3。
# 3 - Full multiuser mode---(Full multiuser mode纯文本模式)
# 4 - unused ====还没使用的。
# 5 - X11 ======图形化模式
# 6 - reboot (Do NOT set initdefault to this)----重启状态。
修改运行级别
[root@oldbay34 ~]# init 3 命令行模式
[root@oldbay34 ~]# init 6 重启

3.9 runlevel查看当前运行级别
[root@oldbay34 ~]# runlevel
N 3

3.10 chkconfig查看系统开机启动服务
[root@oldbay34 ~]# chkconfig
abrt-ccpp 0:off 1:off 2:off 3:on 4:off 5:on 6:off
abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off
cpuspeed 0:off 1:on 2:on 3:on 4:on 5:on 6:off
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
haldaemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off
ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
只查看iptables
[root@oldbay34 ~]# chkconfig | grep iptables
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
关闭iptables开机自起
[root@oldboy34-moban data]# chkconfig iptables off
[root@oldboy34-moban data]# chkconfig |grep iptables
iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 信息