linux 运维总结 一:基本shell命令
基本命令:
第一部分:目录及文件基本操作
1、 pwd
描述:pwd 命令作用是显示当前工作目录名称。
选项:-P 显示链接的真实路径。
[root@puppet test]# pwd #返回当前目录
[root@puppet test]# pwd -P #返回链接真实路径 (备注目录要能创建软链接)

2、cd
描述:cd 命令作用切换当前工作目录。
[root@puppet ~]# cd /usr/src/ #切换工作目录到 /usr/src/
[root@puppet src]# cd .. #切换工作上当至当前目录上一级目录
[root@puppet usr]# cd - #返回/usr/src目录
[root@puppet src]# cd #返回当前用户家目录

3、ls
描述:命令作用是显示目录与文件信息。
[root@puppet ~]# ls #显示当前目录下了文件与目录名称
[root@puppet ~]# ls /etc #显示/etc 目录下子文件与目录名称

[root@puppet ~]# ls -a #查看以 . 开始隐藏文件与目录
[root@puppet ~]# ls -l #查看文件与目录的详细信息(默认显示时间为文件被修改时间,容量单位是字节)
[root@puppet ~]# ls -ld /root #查看当前root目录自身详细信息
[root@puppet ~]# ls -lh #人性化显示容量信息

[root@puppet ~]# ls -lc /etc/passwd #查看/etc/passwd 文件属性变更时间
[root@puppet ~]# ls -lu /etc/passwd #查看/etc/passwd 最后访问时间。
[root@puppet ~]# ls -lt #查看档案信息并以修改时间排序

4、touch
描述:创建或修改文件时间。

如果hello.txt不存在,则创建,如果存在,则更新文件所有的时间为当前系统时间
5、mkdir
描述:创建目录
选项:-p 创建多级目录
[root@puppet tmp]# mkdir leo
[root@puppet tmp]# mkdir -p /tmp/test/jerry/book/computer

6、cp
描述:复制文件与目录
选项: -r 递归,复制了文件与子目录,一般复制目录时使用。

[root@puppet tmp]# cp /etc/hosts /tmp #复制文件/etc/hosts 至 /tmp目录下
[root@puppet tmp]# cp /etc/hosts /tmp/host #复制 文件/etc/hosts 至 /tmp目录下并改名为host
[root@puppet tmp]# cp -r /var/log/ /tmp/ #复制目录/var/log至/tmp目录下
7、rm
描述:删除文件或目录
选项:-f 不提示,强制删除
-i删除前,提示是否删除
-r 删除目录以及目录下的所有内容

[root@puppet tmp]# rm readme.txt #删除文件readme.txt
rm: remove regular empty file `readme.txt'? y
[root@puppet tmp]# rm -rf Jacob/ #删除目录不提示
8、mv
描述:移动(重命名)文件或目录

[root@puppet tmp]# mv hello.txt hello.doc #将hello.txt改名为hello.doc
[root@puppet tmp]# mv hello.doc /root/ #hello.doc 移至/root 目录下
9、find
描述:搜索文件或目录
[root@puppet ~]# find -name hello.doc #查找当前目录下名为hello.doc的档案(备注:档案指是文件或目录)
[root@puppet ~]# find /root -name "*.log" #查找/root目录所有名称以 .log 结尾档案
[root@puppet ~]# find -iname "Jacob" #不区分大小写查打档案jacob
[root@puppet ~]# find / -empty #查找计算机中所有空文档
[root@puppet ~]# find / -group tom #查找计算机中所属组为tom的档案
[root@puppet ~]# find / -mtime -3 #查找计算机中所有3天内被修改过的档案
[root@puppet ~]# find / -mtime +4 #查找计算机中所有4天前被修改过的档案。
[root@puppet ~]# find / -mtime 2 #查计算机中2天前的被修改过档案
[root@puppet ~]# find . / -size +10M #查找当前目录下大于10MB档案 备注. 点是指当前目录, /是指根目录
[root@puppet ~]# find . / -type f #查找当前目录下所有普通文件
[root@puppet ~]# find / -user tom #查找计算机中tom 拥有所有档案
[root@puppet ~]# find . / -size 1M -exec ls -l {} \; #查找大于1MB文件后列出 文件的详细信息
[root@puppet ~]# find / -size +1M -a -type f #查找计算机中所有大于1MB文件
10、 du
描述:计算文件或目录的容量
选项:-h 人性化显示容量信息。
-s 公显示总容量
[root@puppet etc]# du /root #查看/root目录及了目录的容量信息

[root@puppet etc]# du -sh /root #查看 /root所占磁盘空间总和

第二部分 查看文件内容
1、cat
描述:查看文件内容
选项:-b 显示行号,空白行不显示行号
-n 显示行号,包括空白行
[root@puppet etc]# cat /root/install.log
[root@puppet etc]# cat -n /etc/passwd
[root@puppet etc]# cat -b /etc/passwd
2、more
描述:分页查看文件内容,通过空格键查看下一页,q键退出查看
[root@puppet etc]# more /root/install.log
3、less
描述:分页查看文件内容,空格(下一页),方向键(上下回翻), q 键(退出查看)
[root@puppet etc]# less /root/install.log
4、head
描述:查看文件头部内容,默认显示前10行
选项:-c nK 显示文件前nKB的内容
-n 显示文件前n行内容
5、tail
描述:查看文件的尾部内容,默认显示未尾10行。
选项:-c nK 显示文件末尾nKB的内容
-n 显示文件末尾n行内容
-f 动态显示文件内容 按ctrl +c 组合
[root@puppet etc]# tail -c 2K /root/install.log #查看文件末尾2KB内容。
[root@puppet etc]# tail -20 /root/install.log #查看文件末尾20行内容
[root@puppet etc]# tail -f /var/log/messages #实时动态查看文件内容
6、wc
描述:显示文件的行、单词与字节统计信息
选项:-c 显示文件字节
-l 显示文件行数
-w 显示文件单词
[root@puppet etc]# wc /root/install.log #依显示文件行数,单词数,字节数
[root@puppet etc]# wc -c /root/install.log #显示文件字节信息
[root@puppet etc]# wc -l /root/install.log #显示文件行数
[root@puppet etc]# wc -w /root/install.log #显示文件单词数
7、 grep
描述:查找关键词并打印匹配行
选项:-i 忽略大小写
-v 取反匹配
-w 匹配单词
--color 显示颜色
[root@puppet etc]# grep th test.txt #在test.txt文件中过滤出包含th的行
[root@puppet etc]# grep --color th test.txt #对匹配关键词显示颜色
[root@puppet etc]# grep -i the test.txt #过滤包含the 的行(不区分大小写)
[root@puppet etc]# greo -w num test.txt #过滤单词num (不过滤number)
[root@puppet etc]# grep -v the test.txt #过滤不包含the 关键词的行
8、echo
描述:显示一行指定文本。
选项:-n 不输出换行,默认echo 输出内容后会换行
-e 支持反斜线开始的转义字符,屏蔽反斜线后面字符原本含义
[root@puppet etc]# echo "Hello The Wrod" #直接输出指定字串
[root@puppet etc]# echo -e "\\" #默认echo 无法输入\ 符号

第三部分 链接文件
linux 中链接文件不同于windows 快捷方式,linux链接文件分硬链接和软链接,软链接可以跨分区,但源文件不可删除,硬链接不可以跨分区,但可以将源文件删除。
软链接
[root@puppet etc]# ln -s /test/hello.txt /tmp/hi.txt #创建文件软链接
[root@puppet etc]# ln -s /test/ /var/test #创建目录软链接
硬链接
[root@puppet etc]# ln /test/hello.txt /test/hi.txt
[root@puppet etc]# rm /test/hello.txt #删除源文件后,链接文件仍可以正常使用。
第四部分 压缩及解压
1、gzip
描述:压缩与解压缩
选项:-d 解压
[root@puppet etc]# gzip hello.txt # 文件压缩名为hello.txt.gz
[root@puppet etc]# gzip -d hello.txt.gz #解压gz文件。
2、bzip2
描述:压缩与解压
[root@puppet tmp]# bzip2 hello.txt #文件压缩为hello.txt.gz2
[root@puppet tmp]# bzip2 -d hello.txt.bz2 # 解压gz文件
备注:gzip 和 bzip 工具不可以对目录打包压缩
3、tar
描述:打包与解包
模式: -c 创建打包文件
--delete 从打包文件中删除文件
-r 追加文件到打包文档
-t 列出 打包文档内容。
-x 释放打包文件
选项:-C 指定解压路径
-f 指定打包后文件名
-j 打包后通过bzip2格式压缩
--remove-files 打包后删除源文件
-z 打包通过gzip格式压缩


浙公网安备 33010602011771号