<>基础命令

参照 linux运维之道(丁明一)



文章目录

* 基础命令 <https://blog.csdn.net/liulong1010/article/details/84721239#_1>
* 一、目录以及文件的基本操作
<https://blog.csdn.net/liulong1010/article/details/84721239#_7>
* 1、 pwd <https://blog.csdn.net/liulong1010/article/details/84721239#1_pwd_8>
* 2、cd <https://blog.csdn.net/liulong1010/article/details/84721239#2cd_14>
* 3、ls <https://blog.csdn.net/liulong1010/article/details/84721239#3ls_20>
* 4、touch
<https://blog.csdn.net/liulong1010/article/details/84721239#4touch_36>
* 5、mkdir
<https://blog.csdn.net/liulong1010/article/details/84721239#5mkdir_42>
* 6、cp <https://blog.csdn.net/liulong1010/article/details/84721239#6cp_48>
* 7、rm <https://blog.csdn.net/liulong1010/article/details/84721239#7rm_60>
* 8、mv <https://blog.csdn.net/liulong1010/article/details/84721239#8mv_70>
* 9、find <https://blog.csdn.net/liulong1010/article/details/84721239#9find_76>
* 10、du <https://blog.csdn.net/liulong1010/article/details/84721239#10du_130>
* 二、查看文件内容 <https://blog.csdn.net/liulong1010/article/details/84721239#_142>
* 1、cat <https://blog.csdn.net/liulong1010/article/details/84721239#1cat_144>
* 2、more
<https://blog.csdn.net/liulong1010/article/details/84721239#2more_152>
* 3、less
<https://blog.csdn.net/liulong1010/article/details/84721239#3less_156>
* 4、head
<https://blog.csdn.net/liulong1010/article/details/84721239#4head_160>
* 5、tail
<https://blog.csdn.net/liulong1010/article/details/84721239#5tail_168>
* 6、wc <https://blog.csdn.net/liulong1010/article/details/84721239#6wc_180>
* 7、grep
<https://blog.csdn.net/liulong1010/article/details/84721239#7grep_200>
* 8、echo
<https://blog.csdn.net/liulong1010/article/details/84721239#8echo_224>
* 9、ls <https://blog.csdn.net/liulong1010/article/details/84721239#9ls_268>
* 三、压缩及解压 <https://blog.csdn.net/liulong1010/article/details/84721239#_280>
* 1、gzip
<https://blog.csdn.net/liulong1010/article/details/84721239#1gzip_282>
* 2、bzip2
<https://blog.csdn.net/liulong1010/article/details/84721239#2bzip2_290>
* 3、tar <https://blog.csdn.net/liulong1010/article/details/84721239#3tar_296>


<>一、目录以及文件的基本操作

<>1、 pwd

显示当前工作目录的名称

pwd -P 显示链接的真实路径

<>2、cd

切换当前工作目前

..上级目录,.当前目录,-上次切换的目录

<>3、ls

显示目录与文件信息

*
-a 显示所有的信息,包括隐藏文件和目录

*
-d 显示目录本身的信息,而非目录下资料信息

*
-h人性化显示容量信息

*
-l长格式显示详细信息

*
-c显示文件或目录属性最后修改的时间

*
-u显示文件或目录最后被访问的时间

*
-t以修改时间排序,默认按文件名称排序

<>4、touch

创建或修改文件时间

touch hello.txt 如果hello.txt不存在,则已存在,则更新文件所有的时间为当前系统时间

<>5、mkdir

创建目录。

mkdir -p /tmp/test/jetty 创建多级目录

<>6、cp

复制文件与目录

-r 递归,复制子文件与子目录,一般复制目录时使用

*
cp /etc/hosts /tmp/ 复制文件/etc/hosts至/tmp/目录下

*
cp /etc/hosts /tmp/host 复制文件/etc/hosts至/tmp目录下并改名/host

*
cp -r /var/log/ /tmp/ 复制目录/var/log/ 至/tmp/目录下

<>7、rm

删除文件或目录

*
-f不提示,强制删除

*
-i删除前,提示是否删除

*
-r删除目录以及目录下的所有内容

<>8、mv

移动(重命名)文件或目录。

mv hello.txt hello.doc 讲hello.txt改名为hello.doc

<>9、find

搜索文件或目录

*
-empty 查找空白文件或者目录

*
-group 按组查找

*
-name 按照名称查找

*
-iname按名称查找,不区分大小写

*
-mtime 按修改时间查找

*
-size 按容量大小查找

*
-type 按档案类型查找,文件f、目录d、设备b,c、链接l等

*
-user 按用户查找

*
-exec对找到的稳定执行命令

*
-a 并且

*
-o或者

eg:

*
find -name hello.doc 查找当前目录下名为hello.doc的文档

*
find /root-name "*.log" 查找/root目录下所有名称以.log结尾的档案

*
find -iname "Jacob" 不区分大小写查找档案jacob

*
find / -empty 查找/目录下所有的空白文档

*
find / group tom 查找/目录下所属组tom的文档

*
find / mtime -3 查找/目录下所有3天内被修改过的文档

*
find / mtime +3 查找/目录下所有3天前被修改的文档

*
find / mtime 2 查找计算机中2天前被修改过的文档

*
find ./ -size+10M 查找当前目录下大于10MB的档案

*
find ./ -type f 查找当前目录下的所有普通文件,会把目录下文件也查出来

*
find / -user tom 查找计算机中tom所用有的档案

*
find ./ -size +1M -exec ls - l {} \; 查找大于1MB的文件后列出文件的详细信息,\; 结尾符

*
find / -size +1M -a -type f 查找计算中所有大于1MB的文件

<>10、du

计算文件或者目录的容量

-h人性化显示容量信息

-s 仅显示总容量

*
du /root 查找/root目录以及子目录的容量信息

*
du -sh /root 查看/root所占磁盘空间的总和

<>二、查看文件内容

<>1、cat

查看文件的内容

*
-b 显示行号,空白行不显示行号

*
-n显示行号,包括空白行

<>2、more

分页查看文件内容,通过空格键查看下一页,q键则退出查看

<>3、less

分页查看文件内容,空格下一页、方向键 上下回翻、q键退出查看

<>4、head

查看文件头部内容,默认显示前10行

-c nK 显示文件前nKB的内容

-n 显示文件前n行的内容

<>5、tail

查看文件的尾部内容,默认显示末尾10行

*
-c nK 显示文件末尾nKB的内容

*
-n 显示文件末尾n行的内容

*
-f 动态显示文件内容,安装Ctrl+C组合键退出

tail -100f /va/log/message.log 事实动查看文件末尾100行内容

<>6、wc

显示文件的行、单词与字节统计信息

*
-c 显示文件字节统计信息

*
-l 显示文件行数统计信息

*
-w 显示文件单词统计信息

eg:

*
wc /root/install.log 以此显示文件的行数、单词数、字节数

*
wc -c /root/install.log 显示文件的字节信息

*
wc -l /root/install.log 显示文件的行数

*
wc -w /root/install.log 显示文件单词个数

<>7、grep

查找关键词并打印匹配的行

*
-i忽略大小写

*
-v去反匹配

*
-w匹配单词

*
--color 显示颜色

eg:

*
grep th test.txt 在test.txt文件中过滤出包含th行

*
grep --color th test.txt 对匹配的关键词显示颜色

*
grep -i the test.txt 对过滤包含the的行,不区分大小写

*
grep -w num test.txt 对过滤单词num,不过过滤number

*
grep -v the test.txt 过滤不包含the关键词的行

<>8、echo

显示一行指定的文本

*
-n 不输出行,默认echo输出内容会换行

*
-e支持反斜线开始的转义字符,屏蔽反斜线后面字符的原本含义

如果使用-e选项,则可以识别如下字符序列的特许含义

​ \\ 反斜线

​ \a报警器

​ \b 退格键

​ \c 不生产格式输出,默认echo会自动添加换行

​ \f输入表单格式,换行后保留光标位置

​ \n 换行

​ \t 生成水平Tab

​ \v 生成垂直Tab

eg:

*
echo "Hello The Word" 直接输出指定的字符

*
echo -e "\\" 默认echo无法输出\符号

*
echo -e "\a" 计算机蜂鸣器会响一声

*
echo -e "aa\b22" 回删一个字符

*
echo -e "hello\c" 不换行,等同于-n`选项

*
echo -e "hello\fthe word" 表单格式

*
echo -e "hello\tthe world" 水平Tab键

*
echo -e "hello\vthe\vworld" 垂直Tab键

<>9、ls

创建文件或目录连接,软连接可以跨分区,但源文件不可删除,硬链接不可以跨分区,但可以将源文件删除

*
ln -s /test/hello.txt /tmp/hi.txt 创建文件的软链接

*
ln -s /test/ /var/test 创建目录软连接

*
ln /test/hello.txt /test/hi.txt 创建文件硬链接

​ rm /test/hello.txt 删除源文件后,链接文件仍可以正常使用

<>三、压缩及解压

<>1、gzip

压缩与解压缩 -d解压

*
gzip hello.txt 文件压缩后名为hello.txt.gz

*
gzip -d hello.txt.gz 解压gz文件

<>2、bzip2

*
bzip2 hello.txt 文件压缩后为hello.txt.bz2

*
bzip2 -d hello.txt.bz2 解压bz2文件

<>3、tar

打包与解包文件

用法: tar 模式 [选项][路径]...

模式

*
-c 创建打包文件

*
--delete 从打包文件中删除文件

*
-r 追加文件至打包文档

*
-t 列出打包文档的内容

*
-x 释放打包文件

*
-v 详细地列出处理的文件

选项:

*
-C 指定解压路径

*
-f 指定打包后的文件名称

*
-j 打包后通过bzip2格式压缩

*
--remove-files 打包后删除源文件

*
-z 打包后通过gzip格式压缩

eg:

*
tar -cf etc.tar /etc/ 将/etc/目录打包保存为etc.tar

*
tar -czf boot.tar.gz /boot/ 将/boot 目录打包并压缩为etc.tar.gz

*
tar cjf etc.tar.bz2 /tmp/ 将/tmp/目录打包并压缩为etc.tar.bz2

*
tar --delete etc/hosts -f etc.tar 将打包文档中删除文件hosts

*
tar -f etc.tar -r /root/install.log 追加文件至打包文档`etc.tar中

*
tar -tf boot.tar.gz 查看打包文档中档案的信息

*
tar -tvf etc.tar 查看打包文档中档案详细信息

*
tar -xzf boot.tar.gz 解压gz格式的打包文档至当前目录

*
tar -xzf boot.tar.bz2 解压bz2格式的打包文档至当前目录

*
tar -xvzf boot.tar.gz -C /tmp 指定解压路径为/tmp

*
tar -czvf mess.tar.gz /var/log/messages --removefiles 打包压缩后删除源文件

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信