Shell 教程

Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁。Shell 既是一种命令语言,又是一种程序设计语言。

Shell 是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务。

Ken Thompson 的 sh 是第一种 Unix Shell,Windows Explorer 是一个典型的图形界面 Shell。

Shell 在线工具


Shell 脚本

Shell 脚本(shell script),是一种为 shell 编写的脚本程序。

业界所说的 shell 通常都是指 shell 脚本,但读者朋友要知道,shell 和 shell script 是两个不同的概念。

由于习惯的原因,简洁起见,本文出现的 "shell编程" 都是指 shell 脚本编程,不是指开发 shell 自身。


Shell 环境

Shell 编程跟 java、php 编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。

Linux 的 Shell 种类众多,常见的有:

  • Bourne Shell(/usr/bin/sh或/bin/sh)
  • Bourne Again Shell(/bin/bash)
  • C Shell(/usr/bin/csh)
  • K Shell(/usr/bin/ksh)
  • Shell for Root(/sbin/sh)
  • ……

Shell 基本命令

快捷键

1.tab键    用于自动补全命令/文件名/目录名
2.ctrl + l  清理终端显示
3.clear/cls 清理终端显示
4.ctrl + c 终止当前操作
echo命令

普通用户跟root切换 su:

# 切换到普通用户:su 用户名
# 切换到root用户:su
[root@localhost test]# su lrj
[lrj@localhost /]$ su
Password:
[root@localhost /]#

创建目录:

[root@localhost /]# mkdir qiyue
[root@localhost /]# ls
bin boot dev etc home lib lib64 media mnt oldboy opt proc qiyue root run sbin srv sys tmp usr var
# 在qiyue目录下递归创建目录 a/b c/d /子目录

 [root@localhost /]# cd qiyue/
 [root@localhost qiyue]# mkdir a/b c/d
 [root@localhost qiyue]# mkdir -p a/b c/d
 [root@localhost qiyue]# ls
 a c
 [root@localhost qiyue]# cd a  # 改变当前的目录/位置
 [root@localhost a]# ls
 b
 [root@localhost a]# cd ../c
 [root@localhost c]# ls
 d

#递归创建test/a,b,c,d四个目录 {}同级

 [root@localhost qiyue]# mkdir -p test/{a,b,c,d}
 [root@localhost qiyue]# ls  # 查看当前目录下的所有文件
 a c test
 [root@localhost qiyue]# ls test #显示/test下的内容(查看指定目录下的所有内容)

 a b c d

[root@localhost qiyue]# pwd # 打印当前工作目录
/qiyue
 


文本编辑:
vi # 记事本打开
vim # notepad++ 打开
使用vi打开oldboy.py,默认是命令模式,需要输入a/i进入编辑模式,然后输入文本"Life is short,i use python"
按下esc键,回到命令模式
输入 :wq! 强制保存退出
w write 写入
q quit 退出
! 强制
或者 :x 保存退出
------
:q 不保存退出
:q! 不保存强制退出

查看纯文本内容: cat

[root@localhost qiyue]# cat liu.txt
今天学习linux中的Shell命令.
[root@localhost qiyue]#

more命令

1.more命令用于查看内容较多的文本,例如要看一个很长的配置文件,cat查看内容屏幕会快速翻滚到结尾。

2.more命令查看文本会以百分比形式告知已经看到了多少,使用回车键向下读取内容

more /etc/passwd
按下空格space是翻页
按下b键是上一页
回车键向下读取内容

移动, 改名:mv

将liu.txt 移动到test目录下, 将test目录下的a改成aa

[root@localhost qiyue]# ls
a c liu.txt test
[root@localhost qiyue]# mv liu.txt test
[root@localhost qiyue]# ls
a c test
[root@localhost qiyue]# ls test
a b c d liu.txt
[root@localhost qiyue]# cd test/
[root@localhost test]# mv a aa
[root@localhost test]# ls
aa b c d liu.txt
[root@localhost test]#

删除命令rm:

[root@localhost test]# rm liu.txt # #默认有提示删除,需要输入y, 加上参数-f 不提醒
rm: remove regular file ‘liu.txt’? y
[root@localhost test]# ls
aa b c d
[root@localhost test]# rm -rf b #rm默认无法删除目录,需要跟上参数-r
[root@localhost test]# ls
aa c d rm -rf /* # 跑路跑路跑路跑路跑路跑路跑路跑路跑路跑路

echo命令

echo命令用于在终端输出字符串或变量提取后的值,格式是“echo 【字符串|$变量】”

#默认吧内容显示到终端上
echo "七月"
#把“七月”写入到文件里!
echo "七月" > /tmp/qiyue.txt
echo $PATH #取出打印PATH的值

查找

#Linux里如何找到需要的文件 例如 oldboy.py
find 在哪里(目录) 什么类型(文件类型) 叫什么名字(文件名)
参数 -name 按照文件名查找文件
-type 查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
s - socket文件 find /tmp/ -type f -name "oldboy.py" #找出/tmp所有以 .txt 结尾的文件
find /tmp/ -type f -name "*.txt" #找到/etc下所有名字以host开头的文件
find /etc -name 'host*' #找到/opt上一个名为settings.py
find /opt -name 'settings.py'

管道命令: |

Linux提供的管道符“|”讲两条命令隔开,管道符左边命令的输出会作为管道符右边命令的输入。
常见用法:
#检查python程序是否启动
ps -ef|grep "python" #找到/tmp目录下所有txt文件
ls /tmp|grep '.txt' #检查nginx的端口是否存活
netstat -tunlp |grep nginx

grep

语法:
grep [参数] [--color=auto] [字符串] filename
参数详解:
-i : 忽略大小写
-n : 输出行号
-v : 反向选择
--color = auto : 给关键词部分添加颜色 grep "我要找什么" /tmp/oldboy.txt
#排除 -v,排除我要找的东西
grep -v "我要找什么 /tmp/oldboy.txt

head、tail命令

head显示文件前几行,默认前10行
tail显示文件后几行,默认后10行
#查看前两行
head -2 /tmp/oldboy.txt
#查看后两行
tail -2 /tmp/oldboy.txt
#持续刷新显示
tail -f xx.log #显示文件10-30行
head -30 /tmp/oldboy.txt |tail -21

Shell基础命令(一)的更多相关文章

  1. 【CDN+】 Hbase入门 以及Hbase shell基础命令

    前言 大数据的基础离不开Hbase, 本文就hbase的基础概念,特点,以及框架进行简介, 实际操作种需要注意hbase shell的使用. Hbase  基础 官网:https://hbase.ap ...

  2. 运维 04 Shell基础命令(二)

    Shell基础命令(二)   查看Linux的发行版 cat /etc/redhat-release cat /etc/os-release 查看系统用户的id信息 id 用户名 id root id ...

  3. 运维02 Shell基础命令(一)

    Shell基础命令(一)   Shell 教程 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应 ...

  4. 第二篇:shell基础命令(部分)

    目录 一.shell命令规则 二.基础命令详解(部分) ls :列出目录内容 mkdir : 创建目录 rmdir :删除目录 touch:新建文件 mv:修改文件(目录)名.移动路径 cp:复制文件 ...

  5. Linux从入门到进阶全集——【第十四集:Shell基础命令】

    1,Shell就是命令行执行器 2,作用:将外层引用程序的例如ls ll等命令进行解释成01表示的二进制代码给内核,从而让硬件执行:硬件的执行结果返回给shell,shell解释成我们能看得懂的代码返 ...

  6. shell基础命令

    什么是脚本? 脚本简单地说就是一条条的文字命令(一些指令的堆积),这些文字命令是可以看到的(如可以用记事本打开查看.编辑). 常见的脚本: JavaScript(JS,前端),VBScript, AS ...

  7. linux shell基础命令

    du -h  #查询磁盘文件大小和列表 df  -h   # 查询服务器磁盘使用情况 top/free   # 查询服务器内存,cpu等资源使用情况 iptables    # 防火墙相关的命令 vi ...

  8. Shell基础命令(二)

    查看Linux的发行版 cat /etc/redhat-release cat /etc/os-release 查看系统用户的id信息 id 用户名 id root id 创建系统用户的命令 user ...

  9. Linux安全基础:shell及一些基础命令

    1.什么是shell?Shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其中缺省使用的是Bash. 2.shell的分类(1)bash bash shell 是 Bourn ...

随机推荐

  1. aspnet core2中使用csp内容安全策略

    aspnet core2中使用csp内容安全策略 问题:aspnet core2如何使用csp防止xss的攻击 方法: public void ConfigureServices( IServiceC ...

  2. Android APK 瘦身 - JOOX Music项目实战

    导语 JOOX Music是腾讯海外布局的一个音乐产品,2014年发布以来已经成为5个国家和地区排名第一的音乐App.东南亚是JOOX Music的主要发行地区,由于JOOX Music所面对的市场存 ...

  3. 深入理解JVM(七)——性能监控工具

    前言 工欲善其事必先利其器,性能优化和故障排查在我们大都数人眼里是件比较棘手的事情,一是需要具备一定的原理知识作为基础,二是需要掌握排查问题和解决问题的流程.方法.本文就将介绍利用性能监控工具,帮助开 ...

  4. 微信小程序没有返回按钮怎么办?微信小程序左上角返回按钮怎么调出来?

    如果你发现自己的小程序页面没有返回按钮,请检查是不是用的wx.redirectTo(OBJECT)进行的跳转,如果是那就把它改成wx.navigateTo(OBJECT)就可以了. wx.naviga ...

  5. [Swift]LeetCode39. 组合总和 | Combination Sum

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  6. [Swift]LeetCode389. 找不同 | Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  7. [Swift]LeetCode447. 回旋镖的数量 | Number of Boomerangs

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  8. [Swift]LeetCode965. 单值二叉树 | Univalued Binary Tree

    A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...

  9. Storm学习笔记 - 消息容错机制

    Storm学习笔记 - 消息容错机制 文章来自「随笔」 http://jsynk.cn/blog/articles/153.html 1. Storm消息容错机制概念 一个提供了可靠的处理机制的spo ...

  10. spring boot -spring data-redis

    //添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...