Linux指令整理(从入门到高级)+Shell语法

一、Linux简介与安装

1.1 计算机的相关概念

1. 什么是计算机
	 能够接收使用者输入的指令与数据,经由中央处理器的算术与逻辑单元运算处理后,以产生或存储有用的新数据。比如计算器,手机,汽车导航系统,提款机,桌面电脑,手提电脑等。
2. 计算机的硬件组成
	计算单元和控制单元(CPU)
	输入单元(键盘,鼠标)
	输出单元(显示器,打印机)
	存储单元(硬盘,u盘,内存条)
3. 操作系统
	用来管理计算机硬件和其他软件的计算机程序,没有操作系统的计算机,就是一堆废铁。
	常见的PC端操作系统:windows,linux,mac
	常见的移动端操作系统:鸿蒙系统(大力支持),ios,Android等
4. 文件系统
    用来管理文件(数据)的软件程序。
    通常其组织结构都是树形结构

1.2 Linux的简介

1. 简介
	- 是一款免费的,开源的类Unix操作系统,支持多用户,多任务,多线程,多cpu
	- 支持32位和64位
	- Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。
	- Linux也继承了Unix的文件设计思想,一切皆文件(文件夹也是文件)
	- Linux之父是“托瓦斯林纳斯”
2. 版本说明:
    Linux由于是开源的,所以有很多公司在Linux内核程序基础上开发了自己的有特别功能的程序(工具),然后,再命令为一个新的版本,因此Linux有众多的版本型号。
    总结:严格来讲,Linux操作系统指的是“linux内核+各种软件”.
    
    常见的Linux版本:
    	 Red Hat系列:
    	 		其中就有RedHat,CentOS
    	 Debin系列
    	        其中就有Debin,Ubuntu
3.  CentOS社区版介绍
     CentOS官网:http://www.centos.org/
     CentOS搜狐镜像:http://mirrors.sohu.com/centos/
     CentOS网易镜像:http://mirrors.163.com/centos/
     CentOS阿里镜像:https://mirrors.aliyun.com/centos/
     
     CentOS 是RedHat的社区版本,功能和RedHat基本一样
4. 用途:
   Linux企业服务器  
   嵌入式系统:Linux可安装在各种计算机硬件设备中,比如手机、平板电脑、路由器、智能家电等嵌入式系统。
   电影娱乐行业:电影后期特效处理。

1.3 VMware的安装

1. VMware的简介
	是一款虚拟化工具。可以帮助用户来模拟计算机硬件,比如模拟计算机的内存条,磁盘,cpu,输入和输出设备。然后这些硬件模拟出来后,用户就可以在这个基础上安装OS。
2. vmware的安装
  
   安装vmware16. 双击,点击下一步即可。
   
   注意:如果安装成功,那么在物理机的网络适配器里,应该会多出两个VMware NetWork Adapter。
   一个是VMnet1,
   一个是VMnet8.
 
   破解:百度上搜VMware16激活码。
 
 注意:如果想要在vmware中安装OS,那么物理机的BIOS必须开启虚拟化支持。

1.4 Linux的安装

1. 第一大步:使用vmware先虚拟化一台硬件机器
   文件-->新建虚拟机-->典型-->稍后安装操作系统
   -->选择Linux,并选择CentOS 7 64位
   -->  虚拟机名称: test01
        位置:最好改成其他盘符
   --> 磁盘大小: 50G,    hadoop之后一定要50G以上
       单个文件和多个文件,哪一个都行。
   -->自定义硬件
          内存:2G
          处理器: 1个cpu,2个核
          新CD/DVD:  指定要安装的OS的映像文件
          删除声卡和打印机, 确定即可     最终完成
          
        ---------------------到此为止,硬件模拟完成----------------------  
2. 安装OS
    开启此虚拟机-->选择install centos......
    -->简体中文
    -->设置分区:自动或者自定义
         自定义:  +键
             /boot     300M
             /swap(磁盘交换空间)  1024M
             /          剩下的全部给根
             
             接受更改
    --> 安装
    --> 安装期间设置root的密码或者是新用户
           超级管理员:root
           密码:123123  
    -->  重新加载
    -->  成功启动了,开心的玩吧。    

二、Linux的基础命令(重要)

2.1 命令格式的说明

1. 进入命令行界面的提示符解析
   [root@localhost ~]#
   root位置:  登录用户名
   @: 连接符号
   localhost位置:  本机的主机名 
   ~位置:   当前的所在位置
   #位置:  表示是超级管理员还是普通用户
           超级管理员则使用#
           普通用户则使用$
    
   eg: 如果登录名为scott,主机名为qianfeng01, 当前位置为/home/soft/tencent/wechat/
       那么提示符的样子:
       [scott@qianfeng01 wechat]$ 
2. 命令格式的说明(格式标明时的中括号一般情况下都表示可有可无)
	 
    格式如下:      
     [scott@qianfeng01 wechat]$   command [-option][...] [参数]
	eg: ls  -la  /usr

	说明:	
		- 大部分命令遵从该格式
		- 多个选项时,可以一起写   
			eg: ls –l –a  ls –la
		- 简化选项与完整选项(注:并非所有选项都可使用完整选项) 	
			eg: ls –all   ls –a

2.2 三个最常用的指令

1. pwd :  print current work directory三个单词的简写
          作用就是以绝对路径的形式显示当前的位置所在
          eg:
          [root@localhost network-scripts]# pwd
		  /etc/sysconfig/network-scripts
		  [root@localhost ~]# pwd
		  /root
2. ls :   list directory contents的简写
          作用,就是列出指定目录下的内容(文件,子目录等)
          eg:  ls   默认列出当前工作空间里的内容
          
          常用的选项:  
          	-l :  列出每个子文件的属性详情     
            -a :  显示所有的内容,包括隐藏的
            -S :  以大小进行降序排序显示, 尽量与-h,-l一起使用
            -h:   以方便人类可读的显示效果显示大小的单位,比如k,MB,G
         eg:  ls -l 显示属性详情,  可以简化写: ll
         eg:  ls -a 显示所有包括隐藏的内容
         eg:  ls -l -a 显示所有包括隐藏的内容的属性详情   ,简化写:ll -a  或者ls -la
         eg:  ls -lhS  显示当前目录下的所有内容,并降序排序
         eg:  ls -lhS /etc  显示指定目录/etc下的所有内容,并降序排序
         
         
3. cd :  change directory的简写,  切换工作空间。
         注意:是一个特殊指令,特殊在是一个shell内置指令。
         
         cd  [target directory]
         eg:  cd       回家
         eg:   cd ~     回家
         eg:   cd -    回到上一次的位置
         eg:   cd /etc   切换到/etc下
         eg:   cd ..   回到上一级目录(父目录)
               cd .  表示不动

扩展1:属性详情

【-rw-------】 【1】  【root】 【root】 【1259】 【11月 29 19:03】 【anaconda-ks.cfg】
文件类型+权限    硬链接数  owner  group   文件大小  最后一次访问时间   文件名

扩展2:绝对和相对路径

在计算机中,路径有两种写法,分别是绝对路径和相对路径

-- 绝对路径:  从根开始书写的路径。  Linux的根的写法,就是一个斜杠  /  
             eg:  /home/scott/app/tencent/qq/bin/qq.sh
-- 相对路径:  从当前目录下开始书写的路径。
          
          .   :   当前目录的表示方式    注意: 书写相对路径时,当前目录可以省略不写
          ..  :   上一级目录的表示方式
          
          eg:  假如工作空间位于:/home/scott/app/tencent/qq/bin/目录下
               
               问题1:  切换到app目录下
                       绝对路径的写法: cd /home/scott/app/
                       相对路径的写法: cd ./../../../
                                      cd ../../../
                                      
               问题2:  切换到/home/michael/app/目录下
                       绝对路径的写法: cd /home/michael/app
                       相对路径的写法: cd ../../../../../michael/app/


         小贴士:  如果不涉及移动操作,那么绝对路径书写起来简单。
                  如果涉及到项目移动,项目部署,则应该使用相对路径

2.3 帮助指令

man指令:

作用:查看指定命令的帮助文档
语法: man  指令
eg:   man ls
      man pwd
      man cd

help指令

作用:查看指定命令的主题信息
语法:  help  指令

注意: 不是所有的指令都有主题信息

info指令

作用:用来查看指令命令的详细信息
语法: info 指令

2.4 文件处理指令

2.4.1 touch

作用:用于创建一个空文件
语法: touch  filename.....
eg:  
[root@localhost etc]# touch file1 file2 file3
[root@localhost etc]# pwd
/etc
[root@localhost etc]# touch ~/file4
[root@localhost etc]# touch ~/{file5,file6}
[root@localhost etc]# ls ~
anaconda-ks.cfg  file1  file2  file3  file4  file5  file6

2.4.2 mkdir

作用:用于创一个目录
语法:mkdir [-p]  dirname.....

[root@localhost ~]# mkdir dir1
[root@localhost ~]# mkdir ./dir2 ./dir3
[root@localhost ~]# cd /etc
[root@localhost etc]# mkdir ~/{dir4,dir5}
[root@localhost etc]# mkdir ~/dir6/dir66       #会报错,因为dir6不存在,所以不能创建dir66
[root@localhost etc]# mkdir -p ~/dir6/dir66    #表示多层级创建目录
[root@localhost etc]# ls -l ~
总用量 4
-rw-------. 1 root root 1259 1129 19:03 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 1129 22:32 dir1
drwxr-xr-x. 2 root root    6 1129 22:33 dir2
drwxr-xr-x. 2 root root    6 1129 22:33 dir3
drwxr-xr-x. 2 root root    6 1129 22:33 dir4
drwxr-xr-x. 2 root root    6 1129 22:33 dir5
drwxr-xr-x. 3 root root   19 1129 22:34 dir6
-rw-r--r--. 1 root root    0 1129 22:28 file1
-rw-r--r--. 1 root root    0 1129 22:28 file2
-rw-r--r--. 1 root root    0 1129 22:28 file3
-rw-r--r--. 1 root root    0 1129 22:29 file4
-rw-r--r--. 1 root root    0 1129 22:30 file5
-rw-r--r--. 1 root root    0 1129 22:30 file6
[root@localhost etc]# ls ~/dir6
dir66

2.4.3 rm

作用:删除文件或者是目录
语法:rm [-rf]  filename .....
eg:
  
[root@localhost ~]# ls
anaconda-ks.cfg  dir1  dir2  dir3  dir4  dir5  dir6  file1  file2  file3  file4  file5  file6
[root@localhost ~]# rm file1
rm:是否删除普通空文件 "file1"?y
[root@localhost ~]# ls
anaconda-ks.cfg  dir1  dir2  dir3  dir4  dir5  dir6  file2  file3  file4  file5  file6
[root@localhost ~]# rm dir1
rm: 无法删除"dir1": 是一个目录
[root@localhost ~]# rm -f file2
[root@localhost ~]# ls
anaconda-ks.cfg  dir1  dir2  dir3  dir4  dir5  dir6  file3  file4  file5  file6
[root@localhost ~]# rm -r dir1
rm:是否删除目录 "dir1"?y
[root@localhost ~]# ls
anaconda-ks.cfg  dir2  dir3  dir4  dir5  dir6  file3  file4  file5  file6
[root@localhost ~]#  


总结:  默认情况下,是有询问的删除文件,输入y表示删除,输入n表示不删除
       如果想要强制删除文件,添加-f, 但是要慎用。
       
       如果想要删除目录,必须添加-r,表示递归删除。

2.4.4 mv

作用:移动文件或者目录,有更名作用
语法: mv [OPTION]... SOURCE... DIRECTORY

eg: 
[root@localhost ~]# ls
anaconda-ks.cfg  dir2  dir3  dir4  dir5  dir6  file3  file4  file5  file6
[root@localhost ~]# mv file3 file4 dir2 dir3    # 将file3 file4 dir2 移动到dir3里
[root@localhost ~]# ls
anaconda-ks.cfg  dir3  dir4  dir5  dir6  file5  file6
[root@localhost ~]# ls dir3
dir2  file3  file4
[root@localhost ~]# mv file6 dir3/file7     #将file6移动到dir3里并更名为file7
[root@localhost ~]# ls dir3
dir2  file3  file4  file7
[root@localhost ~]#

2.4.5 cp

作用:拷贝文件或者是目录
语法:cp [-r] source....directory

eg:
[root@localhost ~]# rm -rf ./*    删除当前目录下的所有非隐藏文件
[root@localhost ~]# touch file1 file2 file3
[root@localhost ~]# mkdir dir1 dir2 dir3
[root@localhost ~]# ll
总用量 0
drwxr-xr-x. 2 root root 6 1129 23:04 dir1
drwxr-xr-x. 2 root root 6 1129 23:04 dir2
drwxr-xr-x. 2 root root 6 1129 23:04 dir3
-rw-r--r--. 1 root root 0 1129 23:04 file1
-rw-r--r--. 1 root root 0 1129 23:04 file2
-rw-r--r--. 1 root root 0 1129 23:04 file3
[root@localhost ~]# cp file1 dir1   # 拷贝file1到dir1里
[root@localhost ~]# ll
总用量 0
drwxr-xr-x. 2 root root 19 1129 23:04 dir1
drwxr-xr-x. 2 root root  6 1129 23:04 dir2
drwxr-xr-x. 2 root root  6 1129 23:04 dir3
-rw-r--r--. 1 root root  0 1129 23:04 file1   #源文件依然存在
-rw-r--r--. 1 root root  0 1129 23:04 file2
-rw-r--r--. 1 root root  0 1129 23:04 file3
[root@localhost ~]# ls dir1   # 查看dir1里的内容
file1
[root@localhost ~]# echo "helloworld" > file2   #写一些字符串到file2文件里
[root@localhost ~]# ll
总用量 4
drwxr-xr-x. 2 root root 19 1129 23:04 dir1
drwxr-xr-x. 2 root root  6 1129 23:04 dir2
drwxr-xr-x. 2 root root  6 1129 23:04 dir3
-rw-r--r--. 1 root root  0 1129 23:04 file1
-rw-r--r--. 1 root root 11 1129 23:05 file2
-rw-r--r--. 1 root root  0 1129 23:04 file3
[root@localhost ~]# cp file2 dir1/file22   # 复制file2到dir1里同时更名为file22
[root@localhost ~]# ll dir1
总用量 4
-rw-r--r--. 1 root root  0 1129 23:04 file1
-rw-r--r--. 1 root root 11 1129 23:05 file22
[root@localhost ~]# cp file3 file4     #拷贝并更名
[root@localhost ~]# ll
总用量 4
drwxr-xr-x. 2 root root 33 1129 23:05 dir1
drwxr-xr-x. 2 root root  6 1129 23:04 dir2
drwxr-xr-x. 2 root root  6 1129 23:04 dir3
-rw-r--r--. 1 root root  0 1129 23:04 file1
-rw-r--r--. 1 root root 11 1129 23:05 file2
-rw-r--r--. 1 root root  0 1129 23:04 file3
-rw-r--r--. 1 root root  0 1129 23:07 file4
[root@localhost ~]# cp dir2 dir3    # 拷贝目录时, 不带参数-r,会忽略目录
cp: 略过目录"dir2"
[root@localhost ~]# ll dir3
总用量 0
[root@localhost ~]# cp dir1 dir3
cp: 略过目录"dir1"
[root@localhost ~]# ll dir3
总用量 0
[root@localhost ~]# cp -r dir1 dir3   # 如果想要拷贝目录,应该带上-r参数,表示递归拷贝
[root@localhost ~]# ll dir3
总用量 0
drwxr-xr-x. 2 root root 33 1129 23:08 dir1

2.4.6 ln

作用:用于创建链接文件
语法: ln [-s] filename newfilename
解析:
    linux的链接文件分为两类,
    一类是软链接文件:  软链接文件相当于windows的快捷方式
                     文件和目录都可以有软链接
                     创建语法:  ln -s filename newfilename
                     
    一类是硬链接文件:  文件可以有硬链接,目录不能有硬链接
    				 创建语法: ln filename newfilename
    
    
    总结: 软链接,新开辟了一个文件块和inode
          硬链接,就是源文件名的别名

在这里插入图片描述

2.4.7 echo

作用: 用于展示一行文件信息
语法:  echo  字符串|环境变量名

[root@localhost ~]# echo you are best     # 打印一串字符,到控制台
you are best
[root@localhost ~]# echo "you are best"
you are best
[root@localhost ~]# echo $HOME   #打印HOME变量的值
/root
[root@localhost ~]# echo $PATH   #打印PATH变量的值
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# echo $HOSTNAME  #打印HOSTNAME变量的值
localhost.localdomain

扩展:重定向符号

> : 重定向到某一个位置,会覆盖原有的内容
>>: 重定向到某一个文职,追加到原有内容的后面


[root@localhost ~]# echo "hello world"
hello world

# 将一串字符输入到content.txt文件里, 注意,如果该文件不存在,会直接创建。
[root@localhost ~]# echo "hello world" > content.txt  
[root@localhost ~]# ll
总用量 16
-rw-r--r--. 1 root root 12 1129 23:43 content.txt
[root@localhost ~]# echo "hello" > content.txt   #覆盖
[root@localhost ~]# ll
总用量 16
-rw-r--r--. 1 root root  6 1129 23:43 content.txt     #变成6个字节
[root@localhost ~]# echo "world" >> content.txt  #追加
[root@localhost ~]# ll
总用量 16
-rw-r--r--. 1 root root 12 1129 23:44 content.txt     #变成12个字节

2.5 文件查看指令

2.5.1 cat

作用: 查看整个文件的内容
语法:  cat [-An] filename
解析: -A  显示隐藏的字符
      -n  显示行号

2.5.2 more/less

作用: 用于分页查看文件内容
语法: more filename
解析:  默认查看第一页内容
       空格键/f键    查看下一页
       enter键:  一行一行的滚动
       b :往回翻页
       q|Q: 退出
       
       less和more用法一样

2.5.3 head

作用: 查看文件的头部信息,默认查看10行
语法: head [-number] filename
解析: 如果想要查看指定行数,添加-数字

2.5.4 tail

作用:查看文件的末尾信息,默认查看10行
语法: tail [-number] filename
解析: 如果想要查看指定行数,添加-数字

2.6 文件查找指令

find

作用:是可以根据指定类型参数,来查找文件系统中的文件或者是目录的
语法: find 搜索位置 条件
eg:
 
[root@localhost ~]# find /etc -name 'init*'    按照名字查找init开头的文件或目录
/etc/inittab
/etc/sysconfig/init
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/init.d
/etc/rc.d/init.d
/etc/selinux/targeted/active/modules/100/init
/etc/selinux/targeted/contexts/initrc_context
[root@localhost ~]# find /etc -name 'init'    按照名字为init的文件或目录
/etc/sysconfig/init
/etc/selinux/targeted/active/modules/100/init
[root@localhost ~]# find /etc -name 'in??'    按照名字查找in开头并且长度为4的文件或目录
/etc/sysconfig/init
/etc/selinux/targeted/active/modules/100/init
[root@localhost ~]# find /etc -name '?i*'   按照名字查找第二个字符是i的文件或目录


find /etc -type d    查看/etc下的所有目录,包括子目录
                d:目录   l:软链接, f:普通文件
find /etc -size -1024 
find /etc -size +2K  -size -3k       单位: k,M,G等
          
          
          注意: 默认单位为一个block   一个block相当于512byte
                如果想要查询小于100MB的文件, 100*1024KB  100*1024*2block
                所以:find /etc -size -204800 
                     find /etc -size +1024    查看大于512KB的文件
                     

grep

作用:用于过滤查询文件内容
语法:grep [-cinv] '搜寻字符串' filename

-c :输出匹配行的个数(是以行为单位,不是以出现次数为单位)
-i :忽略大小写,所以大小写视为相同
-n :显示匹配行及行号
-v :反向选择,显示不包含匹配文本的所有行。

eg:
  [root@localhost ~]# grep -i HOST ./profile   忽略大小写的查找host所在的行信息
  [root@localhost ~]# grep -ci HOST ./profile   忽略大小写的查找host所在的行的数量
  [root@localhost ~]# grep -in HOST ./profile   忽略大小写的查找host所在的行信息以及行号
  [root@localhost ~]# grep -v HOST ./profile  查找除了HOST所在的行的其他行的信息

扩展:管道

管道:  |
作用: 将前一个命令的结果通过管道交给后一个命令,继续操作

[root@localhost ~]# grep -i HOST ./profile
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# 前一个查询的结果交给下一个grep继续过滤
[root@localhost ~]# grep -i HOST ./profile  | grep USER 
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

which

作用:用来在path变量的值中查找命令的位置
语法: which  命令

whereis

作用:用于查看命令的位置和帮助文件的位置
语法:  whereis  命令

2.7 文件(解)压缩指令

gzip/gunzip

作用:压缩指令,将每一个文件进行压缩。一个文件对应一个压缩文件
语法:  gzip  filename.....
特点:  
		- 只能压缩文件,
		- 并且源文件会消失
        - 压缩文件名的后缀.gz
gzip -d 表示解压缩,相当于gunzip的作用

bzip2/bunzip2(需要安装)

作用:压缩指令,将每一个文件进行压缩。一个文件对应一个压缩文件
语法:  bzip2  filename.....
特点:  
		- 只能压缩文件,
		- 默认情况下,源文件会消失,  除非带上-k参数,会保留源文件
        - 压缩文件名的后缀.bz2
bzip2 -d 表示解压缩,相当于bunzip2的作用

zip

作用:压缩指令,可以将多个文件或者目录压缩到一个压缩文件中。
语法: zip -r  compressfilename.zip  file1 file2.... dir1 dir2..
  • 8
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值