栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

linux运维之Bash自动化-02Bash基础命令

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux运维之Bash自动化-02Bash基础命令

Bash基础命令
    • 1. 前言
    • 2. 基础命令
      • 2.1 ls:列出某个目录下的文件(linux一切皆文件,目录也是文件的一种);
      • 2.2 mkdir: 创建目录
      • 2.3 touch: 创建文件
      • 2.4 cp: 复制文件
      • 2.5 cd : 切换目录
      • 2.6 pwd: 显示当前目录的绝对路径
      • 2.7 rm: 删除文件或者目录
      • 2.8 clear: 清屏
      • 2.9 chmod: 修改文件或者目录权限
      • 2.10 echo:输出信息
      • 2.11 cat: 将文件输出到标准输出(默认是终端)
      • 2.12 grep:从标准输入或者文件中查找
      • 2.12 |:管道竖线
      • 2.13 mv:移动文件或者目录
      • 2.14 head: 查看文件内容或者输入内容的前几行
      • 2.15 tail: 查看文件内容或者输入内容的后几行
      • 2.16 sort: ascii码排列文件内容或者输入内容
      • 2.17 yum: Centos软件管理器
    • 3. 实用小技巧
      • 3.0 !$:上一 个命令最后一个参数
      • 3.1 创建1.txt...100.txt 100个文件
      • 3.2 tab按键提示和补全:
      • 3.3 CTRL+w: 删除前面一个单词
    • 4.结尾

1. 前言

想毕此时大家应该装好虚拟机或者已经准备好学习环境了,基础命令就是一些工作日常经常使用到的命令,我们的目的不是学习这些基础命令,所以这些命令咱们稍微过一些就行。

2. 基础命令 2.1 ls:列出某个目录下的文件(linux一切皆文件,目录也是文件的一种);

-a: 列出所有文件,包括隐藏文件
-l: 列出文件的纤细信息
-d: 列出目录本身,不是目录里面的内容

[root@locahost ~]# ls /
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@locahost ~]# ls ..
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@locahost ~]# ls .
anaconda-ks.cfg
[root@locahost ~]# ls
anaconda-ks.cfg
[root@locahost ~]# ls /tmp/
ks-script-LV1APX                                                         systemd-private-ac18bdaae20d475c973657810ebc212c-chronyd.service-XNCZq1  yum.log
systemd-private-4cda39d2f6cd49dea225fc04b995b64b-chronyd.service-WUSz9a  systemd-private-c489afe0be8f47e89fb43f918f09ccb4-chronyd.service-GUOh9V
[root@locahost ~]# 
2.2 mkdir: 创建目录

-p : 如下所示:mkdir -p dir2/dir3;其父目录dir2不存在,则会先将dir2创建,再创建dir3。

[root@locahost ~]# ls
anaconda-ks.cfg
[root@locahost ~]# mkdir dir1
[root@locahost ~]# ls
anaconda-ks.cfg  dir1
[root@locahost ~]# mkdir -p dir2/dir3
[root@locahost ~]# ls 
anaconda-ks.cfg  dir1  dir2
[root@locahost ~]# ls dir2
dir3
[root@locahost ~]# 
2.3 touch: 创建文件
[root@locahost ~]# touch a.txt
[root@locahost ~]# ls
anaconda-ks.cfg  a.txt  dir1  dir2
[root@locahost ~]# 
2.4 cp: 复制文件

-r : 递归复制目录及其子目录内的所有内容;

[root@locahost ~]# ls
anaconda-ks.cfg  a.txt  dir1  dir2
[root@locahost ~]# cp a.txt dir1
[root@locahost ~]# ls dir1
a.txt
[root@locahost ~]# mkdir dir3
[root@locahost ~]# cp -r dir1 dir3
[root@locahost ~]# ls dir3
dir1
[root@locahost ~]# ls dir3/dir1/
a.txt
[root@locahost ~]# 
2.5 cd : 切换目录

cd -: 返回上次cd前的工作目录;
cd ..: 返回上级目录;
cd .: .代表当前目录

[root@locahost ~]# cd dir3/dir1/
[root@locahost dir1]# cd -
/root
[root@locahost ~]# cd dir3/dir1/
[root@locahost dir1]# cd ..
[root@locahost dir3]# cd .
2.6 pwd: 显示当前目录的绝对路径
[root@locahost dir3]# pwd
/root/dir3
[root@locahost dir3]# 
2.7 rm: 删除文件或者目录

-f: 强制删除
-r: 目录及其子目录内的所有内容;

[root@locahost dir3]# pwd
/root/dir3
[root@locahost dir3]# ls
dir1
[root@locahost dir3]# touch a.txt
[root@locahost dir3]# ls
a.txt  dir1
[root@locahost dir3]# rm a.txt 
rm:是否删除普通空文件 "a.txt"?y
[root@locahost dir3]# ls
dir1
[root@locahost dir3]# touch a.txt
[root@locahost dir3]# ls
a.txt  dir1
[root@locahost dir3]# rm -f a.txt 
[root@locahost dir3]# ls
dir1
[root@locahost dir3]# mkdir dir2
[root@locahost dir3]# rm -r dir2
rm:是否删除目录 "dir2"?y
[root@locahost dir3]# ls
dir1
[root@locahost dir3]# mkdir dir2
[root@locahost dir3]# rm -rf dir2
[root@locahost dir3]# 
2.8 clear: 清屏

当屏幕很乱,就可以清屏了。

2.9 chmod: 修改文件或者目录权限

**-R:**递归修改目录及其子目录内的所有文件或者目录权限

[root@locahost dir3]# ls -l
总用量 0
drwxr-xr-x. 2 root root 19 5月   8 11:56 dir1
[root@locahost dir3]# chmod 0777 dir1/
[root@locahost dir3]# ls -l
总用量 0
drwxrwxrwx. 2 root root 19 5月   8 11:56 dir1
[root@locahost dir3]# 
2.10 echo:输出信息

-e: 使能转义

[root@locahost dir3]# echo "Hello,nWorld"
Hello,nWorld
[root@locahost dir3]# echo -e "Hello,nWorld"
Hello,
World
[root@locahost dir3]# 
2.11 cat: 将文件输出到标准输出(默认是终端)

> : 重定向输出

[root@locahost dir3]# echo "Hello,World" > a.txt
[root@locahost dir3]# cat a.txt 
Hello,World
[root@locahost dir3]# 
2.12 grep:从标准输入或者文件中查找

-n: 显示行号
-i: 不区分大小写
-r: 递归查找

[root@locahost dir3]# ls
a.txt  dir1
[root@locahost dir3]# cat a.txt 
Hello,World
[root@locahost dir3]# echo "two_line" >> a.txt
[root@locahost dir3]# cat a.txt 
Hello,World
two_line
[root@locahost dir3]# grep "two" a.txt
two_line
[root@locahost dir3]# grep "o" a.txt
Hello,World
two_line
[root@locahost dir3]# 
2.12 |:管道竖线

将前面命令的输出作为后面命令的输入

[root@locahost dir3]# cat a.txt  | grep "two"
two_line
[root@locahost dir3]# cat a.txt  | grep "o"
Hello,World
two_line
[root@locahost dir3]# cat a.txt  | grep -i "o"
Hello,World
two_line
[root@locahost dir3]# cat a.txt  | grep -i "O"
Hello,World
two_line
2.13 mv:移动文件或者目录
[root@locahost dir3]# ls
a.txt  dir1
[root@locahost dir3]# mv a.txt b.txt
[root@locahost dir3]# ls
b.txt  dir1
[root@locahost dir3]# 
2.14 head: 查看文件内容或者输入内容的前几行

-n: 指定行数,默认10行

[root@locahost dir3]# ls
b.txt  dir1
[root@locahost dir3]# head b.txt 
Hello,World
two_line
[root@locahost dir3]# head -n1 b.txt 
Hello,World
[root@locahost dir3]# 
2.15 tail: 查看文件内容或者输入内容的后几行

-n: 指定行数,默认10行

[root@locahost dir3]# ls
b.txt  dir1
[root@locahost dir3]# tail b.txt 
Hello,World
two_line
[root@locahost dir3]# tail -n1 b.txt 
two_line
[root@locahost dir3]# 
2.16 sort: ascii码排列文件内容或者输入内容

-r: 反向排列
-u: 去重

[root@locahost dir3]# pwd
/root/dir3
[root@locahost dir3]# ls
b.txt  dir1
[root@locahost dir3]# sort b.txt 
Hello,World
two_line
[root@locahost dir3]# sort -r b.txt 
two_line
Hello,World
[root@locahost dir3]# 
2.17 yum: Centos软件管理器

可以用来安装软件、删除软件。
tree: 树状显示目录结构;

[root@locahost dir3]# tree
-bash: tree: 未找到命令
[root@locahost dir3]# yum install tree -y  # 安装tree
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.bfsu.edu.cn
 * extras: mirrors.bfsu.edu.cn
...................................
...................................
[root@locahost dir3]# tree
.
├── b.txt
└── dir1
    └── a.txt
    
1 directory, 2 files
[root@locahost dir3]# yum remove tree -y
已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 tree.x86_64.0.1.6.0-10.el7 将被 删除
...................................
...................................
[root@locahost dir3]# tree
-bash: /usr/bin/tree: 没有那个文件或目录
3. 实用小技巧 3.0 !$:上一 个命令最后一个参数
[root@locahost dir3]# pwd
/root/dir3
[root@locahost dir3]# mkdir /root/test
[root@locahost dir3]# cd !$
cd /root/test
[root@locahost test]# pwd
/root/test
[root@locahost test]#
3.1 创建1.txt…100.txt 100个文件
[root@locahost test]# touch {1..100}.txt
[root@locahost test]# ls
100.txt  13.txt  17.txt  20.txt  24.txt  28.txt  31.txt  35.txt  39.txt  42.txt  46.txt  4.txt   53.txt  57.txt  60.txt  64.txt  68.txt  71.txt  75.txt  79.txt  82.txt  86.txt  8.txt   93.txt  97.txt
10.txt   14.txt  18.txt  21.txt  25.txt  29.txt  32.txt  36.txt  3.txt   43.txt  47.txt  50.txt  54.txt  58.txt  61.txt  65.txt  69.txt  72.txt  76.txt  7.txt   83.txt  87.txt  90.txt  94.txt  98.txt
11.txt   15.txt  19.txt  22.txt  26.txt  2.txt   33.txt  37.txt  40.txt  44.txt  48.txt  51.txt  55.txt  59.txt  62.txt  66.txt  6.txt   73.txt  77.txt  80.txt  84.txt  88.txt  91.txt  95.txt  99.txt
12.txt   16.txt  1.txt   23.txt  27.txt  30.txt  34.txt  38.txt  41.txt  45.txt  49.txt  52.txt  56.txt  5.txt   63.txt  67.txt  70.txt  74.txt  78.txt  81.txt  85.txt  89.txt  92.txt  96.txt  9.txt
[root@locahost test]# rm -rf {1..100}.txt
[root@locahost test]# ls
[root@locahost test]# 
3.2 tab按键提示和补全:
[root@locahost dir3]# mkd
mkdict    mkdir     mkdumprd
[root@locahost dir3]# ls
b.txt  dir1
[root@locahost dir3]# ls b.txt 	# 输入到b按TAB键
b.txt
[root@locahost dir3]#
3.3 CTRL+w: 删除前面一个单词
[root@locahost dir3]# pwd
/root/dir3
[root@locahost dir3]# thank you very much!	# 此时按下ctrl+w后
[root@locahost dir3]# thank you very # 把光标移动至very前面,按下ctrl+w后
[root@locahost dir3]# thank very 
4.结尾

暂时常用的命令就这些,之后学到新的命令再介绍。

下一章:linux运维之Bash自动化-03vim介绍

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/870352.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号